-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathprocessing.py
167 lines (141 loc) · 6.9 KB
/
processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
import pandas as pd
import os
import torch
import torch.nn.functional as F
from torch.utils.data import Dataset, TensorDataset, DataLoader
from torch.nn.utils.rnn import pad_sequence
from utils import build_dense_graph
# Graph-based Knowledge Tracing: Modeling Student Proficiency Using Graph Neural Network.
# For more information, please refer to https://dl.acm.org/doi/10.1145/3350546.3352513
# Author: jhljx
# Email: [email protected]
class KTDataset(Dataset):
def __init__(self, features, questions, answers):
super(KTDataset, self).__init__()
self.features = features
self.questions = questions
self.answers = answers
def __getitem__(self, index):
return self.features[index], self.questions[index], self.answers[index]
def __len__(self):
return len(self.features)
def pad_collate(batch):
(features, questions, answers) = zip(*batch)
features = [torch.LongTensor(feat) for feat in features]
questions = [torch.LongTensor(qt) for qt in questions]
answers = [torch.LongTensor(ans) for ans in answers]
feature_pad = pad_sequence(features, batch_first=True, padding_value=-1)
question_pad = pad_sequence(questions, batch_first=True, padding_value=-1)
answer_pad = pad_sequence(answers, batch_first=True, padding_value=-1)
return feature_pad, question_pad, answer_pad
def load_dataset(file_path, batch_size, graph_type, dkt_graph_path=None, train_ratio=0.7, val_ratio=0.2, shuffle=True, model_type='GKT', use_binary=True, res_len=2, use_cuda=True):
r"""
Parameters:
file_path: input file path of knowledge tracing data
batch_size: the size of a student batch
graph_type: the type of the concept graph
shuffle: whether to shuffle the dataset or not
use_cuda: whether to use GPU to accelerate training speed
Return:
concept_num: the number of all concepts(or questions)
graph: the static graph is graph type is in ['Dense', 'Transition', 'DKT'], otherwise graph is None
train_data_loader: data loader of the training dataset
valid_data_loader: data loader of the validation dataset
test_data_loader: data loader of the test dataset
NOTE: stole some code from https://github.com/lccasagrande/Deep-Knowledge-Tracing/blob/master/deepkt/data_util.py
"""
df = pd.read_csv(file_path)
if "skill_id" not in df.columns:
raise KeyError(f"The column 'skill_id' was not found on {file_path}")
if "correct" not in df.columns:
raise KeyError(f"The column 'correct' was not found on {file_path}")
if "user_id" not in df.columns:
raise KeyError(f"The column 'user_id' was not found on {file_path}")
# if not (df['correct'].isin([0, 1])).all():
# raise KeyError(f"The values of the column 'correct' must be 0 or 1.")
# Step 1.1 - Remove questions without skill
df.dropna(subset=['skill_id'], inplace=True)
# Step 1.2 - Remove users with a single answer
df = df.groupby('user_id').filter(lambda q: len(q) > 1).copy()
# Step 2 - Enumerate skill id
df['skill'], _ = pd.factorize(df['skill_id'], sort=True) # we can also use problem_id to represent exercises
# Step 3 - Cross skill id with answer to form a synthetic feature
# use_binary: (0,1); !use_binary: (1,2,3,4,5,6,7,8,9,10,11,12). Either way, the correct result index is guaranteed to be 1
if use_binary:
df['skill_with_answer'] = df['skill'] * 2 + df['correct']
else:
df['skill_with_answer'] = df['skill'] * res_len + df['correct'] - 1
# Step 4 - Convert to a sequence per user id and shift features 1 timestep
feature_list = []
question_list = []
answer_list = []
seq_len_list = []
def get_data(series):
feature_list.append(series['skill_with_answer'].tolist())
question_list.append(series['skill'].tolist())
answer_list.append(series['correct'].eq(1).astype('int').tolist())
seq_len_list.append(series['correct'].shape[0])
df.groupby('user_id').apply(get_data)
max_seq_len = np.max(seq_len_list)
print('max seq_len: ', max_seq_len)
student_num = len(seq_len_list)
print('student num: ', student_num)
feature_dim = int(df['skill_with_answer'].max() + 1)
print('feature_dim: ', feature_dim)
question_dim = int(df['skill'].max() + 1)
print('question_dim: ', question_dim)
concept_num = question_dim
# print('feature_dim:', feature_dim, 'res_len*question_dim:', res_len*question_dim)
# assert feature_dim == res_len * question_dim
kt_dataset = KTDataset(feature_list, question_list, answer_list)
train_size = int(train_ratio * student_num)
val_size = int(val_ratio * student_num)
test_size = student_num - train_size - val_size
train_dataset, val_dataset, test_dataset = torch.utils.data.random_split(kt_dataset, [train_size, val_size, test_size])
print('train_size: ', train_size, 'val_size: ', val_size, 'test_size: ', test_size)
train_data_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=shuffle, collate_fn=pad_collate)
valid_data_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=shuffle, collate_fn=pad_collate)
test_data_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=shuffle, collate_fn=pad_collate)
graph = None
if model_type == 'GKT':
if graph_type == 'Dense':
graph = build_dense_graph(concept_num)
elif graph_type == 'Transition':
graph = build_transition_graph(question_list, seq_len_list, train_dataset.indices, student_num, concept_num)
elif graph_type == 'DKT':
graph = build_dkt_graph(dkt_graph_path, concept_num)
if use_cuda and graph_type in ['Dense', 'Transition', 'DKT']:
graph = graph.cuda()
return concept_num, graph, train_data_loader, valid_data_loader, test_data_loader
def build_transition_graph(question_list, seq_len_list, indices, student_num, concept_num):
graph = np.zeros((concept_num, concept_num))
student_dict = dict(zip(indices, np.arange(student_num)))
for i in range(student_num):
if i not in student_dict:
continue
questions = question_list[i]
seq_len = seq_len_list[i]
for j in range(seq_len - 1):
pre = questions[j]
next = questions[j + 1]
graph[pre, next] += 1
np.fill_diagonal(graph, 0)
# row normalization
rowsum = np.array(graph.sum(1))
def inv(x):
if x == 0:
return x
return 1. / x
inv_func = np.vectorize(inv)
r_inv = inv_func(rowsum).flatten()
r_mat_inv = np.diag(r_inv)
graph = r_mat_inv.dot(graph)
# covert to tensor
graph = torch.from_numpy(graph).float()
return graph
def build_dkt_graph(file_path, concept_num):
graph = np.loadtxt(file_path)
assert graph.shape[0] == concept_num and graph.shape[1] == concept_num
graph = torch.from_numpy(graph).float()
return graph