-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitness.py
189 lines (167 loc) · 5.34 KB
/
fitness.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
import functools
import operator
import torch
from sklearn.metrics import (
classification_report,
f1_score,
precision_recall_fscore_support,
)
from torch import optim as optim, nn as nn
from models import Model
import time
""" def fit(
TEXT,
train_dl,
valid_dl,
config,
conv_depth,
dense_depth,
hidden_dim=100,
lr=1e-3,
kernel_size=3,
pool_size=2,
similarity="dot",
loss="BCELoss",
validate_each_epoch=True,
trainable=False,
):
model = Model(
TEXT,
hidden_dim=hidden_dim,
conv_depth=conv_depth,
dense_depth=dense_depth,
similarity=similarity,
max_len=20,
kernel_size=kernel_size,
pool_size=pool_size,
trainable=trainable,
)
opt = optim.Adam(model.parameters(), lr=lr)
loss_func = getattr(nn, loss)()
model.train()
if validate_each_epoch:
y_true = [v[2] for v in test_dl]
y_true = functools.reduce(operator.iconcat, y_true, [])
print("Start training")
for epoch in range(1, config["epochs"] + 1):
running_loss = 0.0
t0 = time.time()
for left, right, y in train_dl:
opt.zero_grad()
preds = model([left, right])
loss = loss_func(preds, torch.unsqueeze(y, 1))
loss.backward()
opt.step()
running_loss += loss.data.item()
epoch_loss = running_loss / len(train_dl)
print(
"Epoch: {}, Elapsed: {:.2f}s, Training Loss: {:.4f}".format(
epoch, time.time() - t0, epoch_loss
)
)
if validate_each_epoch:
# calculate the validation loss for this epoch
predictions = []
val_loss = 0.0
model.eval() # turn on evaluation mode
for left, right, y in valid_dl:
preds = model([left, right])
loss = loss_func(preds, torch.unsqueeze(y, 1))
val_loss += loss.data.item()
val_loss /= len(valid_dl)
print("Validate epoch: {}, Val Loss: {:.4f}".format(epoch, val_loss))
return model
def evaluate(model, test_dl, print_results=True):
y_true = [v[2] for v in test_dl]
y_true = functools.reduce(operator.iconcat, y_true, [])
predictions = []
model.eval() # turn on evaluation mode
for left, right, y in test_dl:
preds = model([left, right])
predictions.extend(preds.data > 0.5)
if print_results:
print(classification_report(y_true, predictions))
return f1_score(y_true, predictions, average="weighted") + 1e-10 """
def fit(
TEXT,
train_dl,
valid_dl,
config,
hidden_dim,
conv_depth,
kernel_size,
dense_depth1,
dense_depth2,
lr=1e-3,
pool_size=2,
similarity="dot",
loss="CrossEntropyLoss",
validate_each_epoch=True,
trainable=False,
):
model = Model(
TEXT,
hidden_dim=hidden_dim,
conv_depth=conv_depth,
kernel_size=kernel_size,
pool_size=pool_size,
dense_depth1=dense_depth1,
dense_depth2=dense_depth2,
max_len=20,
similarity=similarity,
trainable=trainable,
)
opt = optim.Adam(model.parameters(), lr=lr)
loss_func = getattr(nn, loss)()
model.train()
if validate_each_epoch:
y_true = [v[2] for v in valid_dl]
y_true = functools.reduce(operator.iconcat, y_true, [])
print("Start training")
for epoch in range(1, config["epochs"] + 1):
running_loss = 0.0
t0 = time.time()
for left, right, y in train_dl:
opt.zero_grad()
preds = model([left, right])
loss = loss_func(preds, y.long())
loss.backward()
opt.step()
running_loss += loss.data.item()
epoch_loss = running_loss / len(train_dl)
print(
"Epoch: {}, Elapsed: {:.2f}s, Training Loss: {:.4f}".format(
epoch, time.time() - t0, epoch_loss
)
)
if validate_each_epoch:
# calculate the validation loss for this epoch
predictions = []
val_loss = 0.0
model.eval() # turn on evaluation mode
for left, right, y in valid_dl:
preds = model([left, right])
loss = loss_func(preds, y.long())
val_loss += loss.data.item()
predictions.extend(torch.argmax(torch.log_softmax(preds, dim=1), dim=1))
val_loss /= len(valid_dl)
prec, rec, f1, _ = precision_recall_fscore_support(
y_true, predictions, labels=[0, 1], average="weighted"
)
print(
"Validate epoch: {}, Val Loss: {:.4f}, Prec: {:.4f}, Rec: {:.4f}, F1: {:.4f}".format(
epoch, val_loss, prec, rec, f1
)
)
return model
def evaluate(model, test_dl, print_results=True):
y_true = [v[2] for v in test_dl]
y_true = functools.reduce(operator.iconcat, y_true, [])
predictions = []
model.eval() # turn on evaluation mode
for left, right, y in test_dl:
preds = model([left, right])
predictions.extend(torch.argmax(torch.log_softmax(preds, dim=1), dim=1))
if print_results:
print(classification_report(y_true, predictions))
return f1_score(y_true, predictions, average="weighted") + 1e-10