-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
260 lines (212 loc) · 12.9 KB
/
training.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
from preprocessing import data_preparation
import os
import torch
import dataloader.dataloader as dataloader
from torch.utils.data import DataLoader
from model import CNN_OD, CNN_LSTM_ATTENTION, HAHNN
from trainer import loss, loss_len
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def kim_training(epochs_rep=5):
print(device)
path = os.path.abspath('')
dataset, pretrained_matrix = data_preparation.data_preparation(path=path, min_freq=5, remove_stop_words=True)
X_train, Y_train = dataset[0]
X_val, Y_val = dataset[1]
X_test, Y_test = dataset[2]
batch_size = 32
epochs = 25
print("Classification")
for _ in range(epochs_rep):
data_training = DataLoader(dataloader.DataSet_1(X_train, Y_train[:, 0]),
batch_size=batch_size,
collate_fn=dataloader.collate_batch, shuffle=True)
data_val = DataLoader(dataloader.DataSet_1(X_val, Y_val[:, 0]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
tester = DataLoader(dataloader.DataSet_1(X_test, Y_test[:, 0]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=True)
kim = kim.to(device)
load_best = loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs)
data_training = DataLoader(dataloader.DataSet_1(X_train, Y_train[:, 0]),
batch_size=batch_size//2,
collate_fn=dataloader.collate_batch, shuffle=True)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=True)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition(16)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs//2, lr=1e-5)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=True)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition(16, dim=1)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs//2, lr=1e-5)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=True)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition(16, orthogonal=False)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs//2, lr=1e-5)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=True)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition_fusion(16)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs//2, lr=1e-4)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=True)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition_fusion(16, dim=1)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs//2, lr=1e-4)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=True)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition_fusion(16, orthogonal=False)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs//2, lr=1e-4)
print("Regression")
for _ in range(epochs_rep):
data_training = DataLoader(dataloader.DataSet_1(X_train, Y_train[:, 1]),
batch_size=batch_size,
collate_fn=dataloader.collate_batch, shuffle=True)
data_val = DataLoader(dataloader.DataSet_1(X_val, Y_val[:, 1]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
tester = DataLoader(dataloader.DataSet_1(X_test, Y_test[:, 1]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=False)
kim = kim.to(device)
load_best = loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs)
data_training = DataLoader(dataloader.DataSet_1(X_train, Y_train[:, 1]),
batch_size=batch_size//2,
collate_fn=dataloader.collate_batch, shuffle=True)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=False)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition(16)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs//2, lr=1e-5)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=False)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition(16, dim=1)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs//2, lr=1e-5)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=False)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition(16, orthogonal=False)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs//2, lr=1e-5)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=False)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition_fusion(16)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs//2, lr=1e-4)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=False)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition_fusion(16, orthogonal=False)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs//2, lr=1e-4)
kim = CNN_OD.CnnKim(pretrained_matrix=pretrained_matrix, classification=False)
kim.load_state_dict(torch.load(load_best))
kim.apply_cp_decomposition_fusion(16, dim=1)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs//2, lr=1e-4)
def main():
print("The type of device is:{0}".format(device))
kim_training(10)
#CNN_LSTM(5)
#Hahnn(5, len_sentence=15)
if __name__ == "__main__":
main()
"""
def CNN_LSTM(epochs_rep=5):
print(device)
path = os.path.abspath('')
dataset, pretrained_matrix = data_preparation.data_preparation(path=path, min_freq=5, remove_stop_words=True)
X_train, Y_train = dataset[0]
X_val, Y_val = dataset[1]
X_test, Y_test = dataset[2]
batch_size = 32
epochs = 22
print("Classification")
for _ in range(epochs_rep):
data_training = DataLoader(dataloader.DataSet_1(X_train, Y_train[:, 0]),
batch_size=batch_size,
collate_fn=dataloader.collate_batch, shuffle=True)
data_val = DataLoader(dataloader.DataSet_1(X_val, Y_val[:, 0]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
tester = DataLoader(dataloader.DataSet_1(X_test, Y_test[:, 0]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
kim = CNN_LSTM_ATTENTION.CnnLstmAttention(pretrained_matrix=pretrained_matrix, classification=True)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs)
print("Regression")
for _ in range(epochs_rep):
data_training = DataLoader(dataloader.DataSet_1(X_train, Y_train[:, 1]),
batch_size=batch_size,
collate_fn=dataloader.collate_batch, shuffle=True)
data_val = DataLoader(dataloader.DataSet_1(X_val, Y_val[:, 1]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
tester = DataLoader(dataloader.DataSet_1(X_test, Y_test[:, 1]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
kim = CNN_LSTM_ATTENTION.CnnLstmAttention(pretrained_matrix=pretrained_matrix, classification=False)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs)
def CNN_LSTM(epochs_rep=5):
print(device)
path = os.path.abspath('')
dataset, pretrained_matrix = data_preparation.data_preparation(path=path, min_freq=5, remove_stop_words=True)
X_train, Y_train = dataset[0]
X_val, Y_val = dataset[1]
X_test, Y_test = dataset[2]
batch_size = 32
epochs = 22
print("Classification")
for _ in range(epochs_rep):
data_training = DataLoader(dataloader.DataSet_1(X_train, Y_train[:, 0]),
batch_size=batch_size,
collate_fn=dataloader.collate_batch, shuffle=True)
data_val = DataLoader(dataloader.DataSet_1(X_val, Y_val[:, 0]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
tester = DataLoader(dataloader.DataSet_1(X_test, Y_test[:, 0]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
kim = CNN_LSTM_ATTENTION.CnnLstmAttention(pretrained_matrix=pretrained_matrix, classification=True)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 0],
loss_function=loss.loss_classification, classification=True, epochs=epochs)
print("Regression")
for _ in range(epochs_rep):
data_training = DataLoader(dataloader.DataSet_1(X_train, Y_train[:, 1]),
batch_size=batch_size,
collate_fn=dataloader.collate_batch, shuffle=True)
data_val = DataLoader(dataloader.DataSet_1(X_val, Y_val[:, 1]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
tester = DataLoader(dataloader.DataSet_1(X_test, Y_test[:, 1]),
batch_size=1,
collate_fn=dataloader.collate_batch, shuffle=False)
kim = CNN_LSTM_ATTENTION.CnnLstmAttention(pretrained_matrix=pretrained_matrix, classification=False)
kim = kim.to(device)
loss.train_callback(kim ,list(data_training), list(data_val), tester=iter(tester),Y_test=Y_test[:, 1],
loss_function=loss.loss_regression, classification=False, epochs=epochs)
"""