-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriiid-sakt-model-inference.py
204 lines (163 loc) · 8.49 KB
/
riiid-sakt-model-inference.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import numpy as np
import gc
import random
from tqdm.notebook import tqdm
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
import seaborn as sns
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.utils.rnn as rnn_utils
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
from pathlib import Path
import psutil
import pickle
import riiideducation
MODEL_PATH = '../input/riiid-sakt-model-training-with-gpu/SAKT_model.pt'
MAX_SEQ = 200
ACCEPTED_USER_CONTENT_SIZE = 4
EMBED_SIZE = 256
BATCH_SIZE = 64
DROPOUT = 0.1
def load_obj(name):
with open('../input/riiid-sakt-model-training-with-gpu/' + name + '.pkl',mode = 'rb') as f:
return pickle.load(f)
n_skill = 13523
group = load_obj('group')
class FFN(nn.Module):
def __init__(self, state_size = 200, forward_expansion = 1, bn_size=MAX_SEQ - 1, dropout=0.2):
super(FFN, self).__init__()
self.state_size = state_size
self.lr1 = nn.Linear(state_size, forward_expansion * state_size)
self.relu = nn.ReLU()
self.bn = nn.BatchNorm1d(bn_size)
self.lr2 = nn.Linear(forward_expansion * state_size, state_size)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.relu(self.lr1(x))
x = self.bn(x)
x = self.lr2(x)
return self.dropout(x)
def future_mask(seq_length):
future_mask = (np.triu(np.ones([seq_length, seq_length]), k = 1)).astype('bool')
return torch.from_numpy(future_mask)
class TransformerBlock(nn.Module):
def __init__(self, embed_dim, heads = 8, dropout = DROPOUT, forward_expansion = 1):
super(TransformerBlock, self).__init__()
self.multi_att = nn.MultiheadAttention(embed_dim=embed_dim, num_heads=heads, dropout=dropout)
self.dropout = nn.Dropout(dropout)
self.layer_normal = nn.LayerNorm(embed_dim)
self.ffn = FFN(embed_dim, forward_expansion = forward_expansion, dropout=dropout)
self.layer_normal_2 = nn.LayerNorm(embed_dim)
def forward(self, value, key, query, att_mask):
att_output, att_weight = self.multi_att(value, key, query, attn_mask=att_mask)
att_output = self.dropout(self.layer_normal(att_output + value))
att_output = att_output.permute(1, 0, 2) # att_output: [s_len, bs, embed] => [bs, s_len, embed]
x = self.ffn(att_output)
x = self.dropout(self.layer_normal_2(x + att_output))
return x.squeeze(-1), att_weight
class Encoder(nn.Module):
def __init__(self, n_skill, max_seq=100, embed_dim=128, dropout = DROPOUT, forward_expansion = 1, num_layers=1, heads = 8):
super(Encoder, self).__init__()
self.n_skill, self.embed_dim = n_skill, embed_dim
self.embedding = nn.Embedding(2 * n_skill + 1, embed_dim)
self.pos_embedding = nn.Embedding(max_seq - 1, embed_dim)
self.e_embedding = nn.Embedding(n_skill+1, embed_dim)
self.layers = nn.ModuleList([TransformerBlock(embed_dim, forward_expansion = forward_expansion) for _ in range(num_layers)])
self.dropout = nn.Dropout(dropout)
def forward(self, x, question_ids):
device = x.device
x = self.embedding(x)
pos_id = torch.arange(x.size(1)).unsqueeze(0).to(device)
pos_x = self.pos_embedding(pos_id)
x = self.dropout(x + pos_x)
x = x.permute(1, 0, 2) # x: [bs, s_len, embed] => [s_len, bs, embed]
e = self.e_embedding(question_ids)
e = e.permute(1, 0, 2)
for layer in self.layers:
att_mask = future_mask(e.size(0)).to(device)
x, att_weight = layer(e, x, x, att_mask=att_mask)
x = x.permute(1, 0, 2)
x = x.permute(1, 0, 2)
return x, att_weight
class SAKTModel(nn.Module):
def __init__(self, n_skill, max_seq=100, embed_dim=128, dropout = DROPOUT, forward_expansion = 1, enc_layers=1, heads = 8):
super(SAKTModel, self).__init__()
self.encoder = Encoder(n_skill, max_seq, embed_dim, dropout, forward_expansion, num_layers=enc_layers)
self.pred = nn.Linear(embed_dim, 1)
def forward(self, x, question_ids):
x, att_weight = self.encoder(x, question_ids)
x = self.pred(x)
return x.squeeze(-1), att_weight
def create_model():
return SAKTModel(n_skill, max_seq=MAX_SEQ, embed_dim = EMBED_SIZE, forward_expansion=1, enc_layers=1, heads=8, dropout=0.1)
class TestDataset(Dataset):
def __init__(self, samples, test_df, n_skill, max_seq=100):
super(TestDataset, self).__init__()
self.samples, self.user_ids, self.test_df = samples, [x for x in test_df['user_id'].unique()], test_df
self.n_skill, self.max_seq = n_skill, max_seq
def __len__(self):
return self.test_df.shape[0]
def __getitem__(self, index):
test_info = self.test_df.iloc[index]
user_id = test_info['user_id']
target_id = test_info['content_id']
content_id_seq = np.zeros(self.max_seq, dtype=int)
answered_correctly_seq = np.zeros(self.max_seq, dtype=int)
if user_id in self.samples.index:
content_id, answered_correctly = self.samples[user_id]
seq_len = len(content_id)
if seq_len >= self.max_seq:
content_id_seq = content_id[-self.max_seq:]
answered_correctly_seq = answered_correctly[-self.max_seq:]
else:
content_id_seq[-seq_len:] = content_id
answered_correctly_seq[-seq_len:] = answered_correctly
x = content_id_seq[1:].copy()
x += (answered_correctly_seq[1:] == 1) * self.n_skill
questions = np.append(content_id_seq[2:], [target_id])
return x, questions
if __name__ == '__main__':
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = create_model()
model.load_state_dict(torch.load(MODEL_PATH))
model.to(device)
env = riiideducation.make_env()
iter_test = env.iter_test()
model.eval()
prev_test_df = None
for (test_df, sample_prediction_df) in tqdm(iter_test):
if (prev_test_df is not None) & (psutil.virtual_memory().percent<90):
print(psutil.virtual_memory().percent)
prev_test_df['answered_correctly'] = eval(test_df['prior_group_answers_correct'].iloc[0])
prev_test_df = prev_test_df[prev_test_df.content_type_id == False]
prev_group = prev_test_df[['user_id', 'content_id', 'answered_correctly']].groupby('user_id').apply(lambda r: (
r['content_id'].values,
r['answered_correctly'].values))
for prev_user_id in prev_group.index:
prev_group_content = prev_group[prev_user_id][0]
prev_group_answered_correctly = prev_group[prev_user_id][1]
if prev_user_id in group.index:
group[prev_user_id] = (np.append(group[prev_user_id][0], prev_group_content),
np.append(group[prev_user_id][1], prev_group_answered_correctly))
else:
group[prev_user_id] = (prev_group_content, prev_group_answered_correctly)
if len(group[prev_user_id][0]) > MAX_SEQ:
new_group_content = group[prev_user_id][0][-MAX_SEQ:]
new_group_answered_correctly = group[prev_user_id][1][-MAX_SEQ:]
group[prev_user_id] = (new_group_content, new_group_answered_correctly)
prev_test_df = test_df.copy()
test_df = test_df[test_df.content_type_id == False]
test_dataset = TestDataset(group, test_df, n_skill, max_seq=MAX_SEQ)
test_dataloader = DataLoader(test_dataset, batch_size=len(test_df), shuffle=False)
item = next(iter(test_dataloader))
x = item[0].to(device).long()
target_id = item[1].to(device).long()
with torch.no_grad():
output, _ = model(x, target_id)
output = torch.sigmoid(output)
output = output[:, -1]
test_df['answered_correctly'] = output.cpu().numpy()
env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']])