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
|
- import torch
- from torch import nn, optim
- from torch.nn import functional as F
- import numpy as np
- from tqdm import tqdm
- import torch.utils.data as data_utils
- from features import downsample
- import pickle as pkl
- import models
-
- class neural_net_sklearn():
-
- """
- sklearn wrapper for training a neural net
- """
-
- def __init__(self, D_in=40, H=40, p=17, epochs=1000, batch_size=100,
- lr=0.001,
- track_name='X_same_length_normalized', arch='fcnn', torch_seed=2):
-
- """
- Parameters:
- ==========================================================
- D_in, H, p: int
- same as input to FCNN
-
- epochs: int
- number of epochs
-
- batch_size: int
- batch size
-
- track_name: str
- column name of track (the tracks should be of the same length)
- """
-
- torch.manual_seed(torch_seed)
- self.D_in = D_in
- self.H = H
- self.p = p
- self.epochs = epochs
- self.batch_size = batch_size
- self.track_name = track_name
- self.torch_seed = torch_seed
- self.lr = lr
- self.arch = arch
-
- torch.manual_seed(self.torch_seed)
- if self.arch == 'fcnn':
- self.model = models.FCNN(self.D_in, self.H, self.p)
- elif 'lstm' in self.arch:
- self.model = models.LSTMNet(self.D_in, self.H, self.p)
- elif 'cnn' in self.arch:
- self.model = models.CNN(self.D_in, self.H, self.p)
- elif 'attention' in self.arch:
- self.model = models.AttentionNet(self.D_in, self.H, self.p)
- elif 'video' in self.arch:
- self.model = models.VideoNet()
- def fit(self, X, y, verbose=False, checkpoint_fname=None, device='cpu'):
-
- """
- Train model
-
- Parameters:
- ==========================================================
- X: pd.DataFrame
- input data, should contain tracks and additional covariates
-
- y: np.array
- input response
- """
- print('fit', X.shape, X.columns)
- torch.manual_seed(self.torch_seed)
- if self.arch == 'fcnn':
- self.model = models.FCNN(self.D_in, self.H, self.p)
- elif 'lstm' in self.arch:
- self.model = models.LSTMNet(self.D_in, self.H, self.p)
- elif 'cnn' in self.arch:
- self.model = models.CNN(self.D_in, self.H, self.p)
- elif 'attention' in self.arch:
- self.model = models.AttentionNet(self.D_in, self.H, self.p)
- elif 'video' in self.arch:
- self.model = models.VideoNet()
-
- # convert input dataframe to tensors
- X_track = X[self.track_name] # track
- X_track = torch.tensor(np.array(list(X_track.values)), dtype=torch.float)
-
- if len(X.columns) > 1: # covariates
- X_covariates = X[[c for c in X.columns if c != self.track_name]]
- X_covariates = torch.tensor(np.array(X_covariates).astype(float), dtype=torch.float)
- else:
- X_covariates = None
-
- # response
- y = torch.tensor(y.reshape(-1, 1), dtype=torch.float)
-
- # initialize optimizer
- optimizer = optim.Adam(self.model.parameters(), lr=self.lr)
-
- # initialize dataloader
- if X_covariates is not None:
- dataset = torch.utils.data.TensorDataset(X_track, X_covariates, y)
- else:
- dataset = torch.utils.data.TensorDataset(X_track, y)
- train_loader = torch.utils.data.DataLoader(dataset,
- batch_size=self.batch_size,
- shuffle=True)
- #train_loader = [(X1, X2, y)]
-
- # train fcnn
- print('fitting dnn...')
- self.model = self.model.to(device)
- for epoch in tqdm(range(self.epochs)):
- train_loss = 0
- for batch_idx, data in enumerate(train_loader):
- optimizer.zero_grad()
- # print('shapes input', data[0].shape, data[1].shape)
- if X_covariates is not None:
- preds = self.model(data[0].to(device), data[1].to(device))
- y = data[2].to(device)
- else:
- preds = self.model(data[0].to(device))
- y = data[1].to(device)
- loss_fn = torch.nn.MSELoss()
- loss = loss_fn(preds, y)
- loss.backward()
- train_loss += loss.item()
- optimizer.step()
- if verbose:
- print(f'Epoch: {epoch}, Average loss: {train_loss/len(X_track):.4e}')
- elif epoch % (self.epochs // 10) == 99:
- print(f'Epoch: {epoch}, Average loss: {train_loss/len(X_track):.4e}')
- if checkpoint_fname is not None:
- pkl.dump({'model_state_dict': self.model.state_dict()},
- open(checkpoint_fname, 'wb'))
-
- def predict(self, X_new):
-
- """
- make predictions with new data
-
- Parameters:
- ==========================================================
- X_new: pd.DataFrame
- input new data, should contain tracks and additional covariates
- """
- print('predict', X_new.columns, X_new.shape, self.track_name)
- self.model.eval()
- with torch.no_grad():
- # convert input dataframe to tensors
- X_new_track = X_new[self.track_name]
- X_new_track = torch.tensor(np.array(list(X_new_track.values)), dtype=torch.float)
-
- if len(X_new.columns) > 1:
- X_new_covariates = X_new[[c for c in X_new.columns if c != self.track_name]]
- X_new_covariates = torch.tensor(np.array(X_new_covariates).astype(float), dtype=torch.float)
- preds = self.model(X_new_track, X_new_covariates)
- else:
- preds = self.model(X_new_track)
- return preds.data.numpy().reshape(1, -1)[0]
-
-
|