-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq2seqTrain.py
273 lines (240 loc) · 8.12 KB
/
seq2seqTrain.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import torch
import pandas as pd
import numpy as np
from torch import nn
from sklearn.model_selection import train_test_split
# from gensim.models import Word2Vec
from torch.utils.tensorboard import SummaryWriter
from torch.nn.utils.rnn import pack_padded_sequence
from pathlib import Path
import argparse
# import tensorflow as tf
data = pd.read_csv("events.csv")
parser = argparse.ArgumentParser()
parser.add_argument('-bs', type=str, required=True, help='Please provide a batch size')
parser.add_argument('-lr', type=str, required=True, help='Please provide a learning rate')
parser.add_argument('-e', type=str, required=True, help='Please provide number of epochs')
parser.add_argument('-ev', type=str, required=True, help='Please provide number of evaluation epochs')
args = parser.parse_args()
device='cuda:0' if torch.cuda.is_available() else 'cpu'
batch_size = int(args.bs)
lr = float(args.lr)
bidi = False
n_layers = 2
n_emb = 50
dropout = 0.2
bias = True
eval_iters = int(args.ev)
iters = int(args.e)
clip_value = 1.0
#Mappings : time | event_type | side | is_goal | assist_method | fast_break
eventM = {
0: 'Announcement',
1: 'Attempt',
2: 'Corner',
3: 'Foul',
4: 'Yellow Card',
5: 'Second Yellow Card',
6: 'Red Card',
7: 'Substitution',
8: 'Free kick won',
9: 'Offside',
10: 'Hand ball',
11: 'Penalty conceded'
}
sideM = {
1: 'Home',
2: 'Away'
}
goalM = {
1 : 'Goal!',
0: 'Not a Goal'
}
assistM = {
0 : 'None',
1 : 'Pass',
2 : 'Cross',
3 : 'Headed pass',
4 : 'Through ball'
}
fastM = {
1 : 'fast break',
0 : 'not applicable'
}
data1 = data.drop(columns=['id_odsp','id_event','sort_order'])
data2 = data1.drop(columns=['player2','player_in','player_out','event_type2','shot_place','shot_outcome','bodypart','situation','location'])
data2 = data2.iloc[:20000,:]
data2['event_type'] = data2['event_type'].replace(eventM)
data2['side'] = data2['side'].replace(sideM)
data2['is_goal'] = data2['is_goal'].replace(goalM)
data2['assist_method'] = data2['assist_method'].replace(assistM)
data2['fast_break'] = data2['fast_break'].replace(fastM)
# data2['time'] = data2['time'].astype(str)
data2.fillna('None' , inplace=True)
for i in range(len(data2)):
data2.iloc[i,0] = str(data2.iloc[i,0])
dataX = data2.drop(columns=['text'])
dataY = data2['text']
for i,y in enumerate(dataY):
dataY[i] = '<Start> ' + y + ' <End>'
def getMax(Y):
max = 0
for i in Y:
if max < len(i):
max = len(i)
return max
def padSeq(L, l, c):
L1 = L[:]
diff = l - len(L)
if diff != 0:
for i in range(diff):
L1.append(c)
return L1
y_trainT = []
for i in dataY.values:
for j in i.split():
y_trainT.append(j)
X_T = []
for i in dataX.values:
for j in i:
X_T.append(j)
d_T = X_T + y_trainT
d_T.append(' ')
d_T.append('/n')
d_T = sorted(set(d_T))
vocab_size = len(d_T)
hiddenDim = vocab_size
# wordVec = Word2Vec(d_T,vector_size=100,window=5,min_count=1)
str2int = { ch:i for i,ch in enumerate(d_T)}
int2str = { i:ch for i,ch in enumerate(d_T)}
enc = lambda x : [str2int[i] for i in x]
dec = lambda x : [int2str[i] for i in x]
encDataX = [enc(i) for i in dataX.values]
encDataY = [enc(i.split()) for i in dataY.values]
X_train, X_test, y_train, y_test = train_test_split(encDataX, encDataY, test_size=0.2, random_state=42)
#convert list to tensor
#pad y to max length present in the batch and convert to tensor , padd using the <End> token
def get_batch(X_train,y_train, X_test,y_test, batch_size,split=True):
if split:
dataX,dataY = X_train,y_train
else:
dataX, dataY = X_test,y_test
ix = torch.randint(0,len(dataX),(batch_size,))
x = torch.tensor([dataX[i] for i in ix]).to(device)
y = [dataY[i] for i in ix]
padL = getMax(y)
y = torch.tensor([padSeq(i,getMax(y),str2int['<End>']) for i in y], dtype=torch.long, device=device)
# y = torch.stack([data[i + 1: i + blk_size + 1] for i in ix]).to(device)
return x, y
class Encoder(nn.Module):
def __init__(self):
super().__init__()
self.embedT = nn.Embedding(vocab_size,n_emb)
self.rnn = nn.GRU(n_emb,hiddenDim,n_layers,bias=bias,dropout=dropout,bidirectional=bidi)
def forward(self,x):
x = self.embedT(x)
B,T,C = x.shape
h0 = self.init_state(T,x)
logits, h0 = self.rnn(x,h0)
return logits, h0
def init_state(self,T,x):
return torch.zeros(n_layers,T,hiddenDim, device=device, dtype=x.dtype)
class Decoder(nn.Module):
def __init__(self, encoder):
super().__init__()
self.enc = encoder
self.embedT = nn.Embedding(vocab_size,n_emb)
self.dec = nn.GRU(n_emb,hiddenDim,n_layers,bias=bias, dropout=dropout,bidirectional=bidi)
self.ff = nn.Sequential(nn.Linear(hiddenDim, n_layers*hiddenDim),nn.ReLU(),nn.Linear(n_layers*hiddenDim,hiddenDim),nn.Dropout(dropout))
self.softmax = nn.Softmax(dim=-1)
self.apply(self._init_weights)
def _init_weights(self,module):
if isinstance(module, torch.nn.Linear):
torch.nn.init.normal_(module.weight,mean = 0.0 , std=0.02)
if module.bias is not None:
torch.nn.init.zeros_(module.bias)
elif isinstance(module, torch.nn.Embedding):
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
def forward(self,x,y=None):
op, hn = self.enc(x)
y1 = self.embedT(x) if y is None else self.embedT(y)
ph0 = torch.zeros(hn.shape[0],y1.shape[1] , hn.shape[2], device=device, dtype=y1.dtype)
ph0[:, :hn.shape[1], :] = hn
logits, hn = self.dec(y1,ph0)
logits = self.ff(logits)
# print(f'y1 dt: {y1.dtype} | ph0 dt: {ph0.dtype} | h dt:{hn.dtype}')
# logits, hn = torch.utils.checkpoint.checkpoint(self.dec, y1, ph0)
# logits = self.softmax(logits)
if y is None:
loss = None
else:
B,T,H = logits.shape
# print(f'Logits : {logits.shape} | op : {y.shape}')
logits = logits.view(B*T,H)
# print(f'Logits : {logits.shape} | op : {y.shape}')
op = y.view(B*T)
loss = nn.functional.cross_entropy(logits, op)
return logits, loss
def genTokens(self, iters, ip):
for _ in range(iters):
# ip_crop = ip[:,-]
logits, loss = self.forward(ip)
logits = logits[:,-1,:]
probs = torch.nn.functional.softmax(logits,dim=-1)
nxt = torch.multinomial(logits, num_samples=1)
ip = torch.cat((ip,nxt),dim=1)
return ip
def check_nan_gradients(model):
"""
Checks if any of the gradients in the model are NaN.
"""
for param in model.parameters():
if torch.any(torch.isnan(param.grad)):
print("NaN gradients detected!")
return True
return False
encoder = Encoder().to(device)
model = Decoder(encoder).to(device)
# model = model.half()
# lencoder = Encoder()
# lmodel = Decoder(lencoder)
model.load_state_dict(torch.load(f='seq2seq.pt', map_location=torch.device(device)))
MODEL_PATH = Path('/content/drive/MyDrive/Pytorch/LSTM/models')
MODEL_PATH.mkdir(parents=True, exist_ok=True)
MODEL_NAME = 'seq2seq.pt'
MODEL_SAVE_PATH = MODEL_NAME
# torch.save(obj=model.state_dict(),f=MODEL_SAVE_PATH)
from torch.utils.tensorboard import SummaryWriter
# Define a SummaryWriter for logging
writer = SummaryWriter()
opt = torch.optim.AdamW(model.parameters(), lr=lr)
lossT = torch.zeros(eval_iters).to(device)
lossV = torch.zeros(eval_iters).to(device)
for i in range(iters):
model.train()
x,y = get_batch(X_train, y_train, X_test, y_test, batch_size)
# print(x.shape, y.shape)
logits, loss = model.forward(x,y)
# if i % eval_iters == 0:
opt.zero_grad()
# else:
# loss /= eval_iters
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), clip_value)
# if (i + 1) % eval_iters == 0:
opt.step()
check_nan_gradients(model)
lossT[i % eval_iters] = loss.item()
model.eval()
with torch.inference_mode():
x,y = get_batch(X_train, y_train, X_test, y_test, batch_size,split=False)
logits, loss = model.forward(x,y)
lossV[i % eval_iters] = loss.item()
if i % eval_iters == 0:
print(f'Epochs: {i} | Train Loss : {lossT.mean()} | Val Loss : {lossV.mean()}')
lossT = torch.zeros(eval_iters)
lossV = torch.zeros(eval_iters)
torch.save(obj=model.state_dict(),f=MODEL_SAVE_PATH)
writer.add_scalar('/Loss/train', lossT.mean(), i)
writer.add_scalar('/Loss/val', lossV.mean(), i)
writer.close()