-
Notifications
You must be signed in to change notification settings - Fork 25
/
cnn_train.py
235 lines (189 loc) · 9.13 KB
/
cnn_train.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
#!/usr/bin/env python
import os.path
import random
import shutil
import chainer
import chainer.functions as F
import chainer.links as L
import numpy as np
import six
from chainer import training
from chainer.datasets import tuple_dataset
from chainer.training import extensions
from tqdm import tqdm
import cnn_model
import xml_cnn_model
from MyEvaluator import MyEvaluator
from MyUpdater import MyUpdater
USE_CUDNN = 'never' ## always, auto, or never
# Extraction of enurons whose threshold values are larger than 0.5
# =========================================================
def select_function(scores):
scores = chainer.cuda.to_cpu(scores)
np_predicts = np.zeros(scores.shape,dtype=np.int8)
for i in tqdm(range(len(scores)),desc="select labels based on threshold loop"):
np_predicts[i] = (scores[i] >= 0.5)
return np_predicts
# The Setting of the seed value for random number generation
# =========================================================
def set_seed_random(seed):
random.seed(seed)
np.random.seed(seed)
if chainer.cuda.available:
chainer.cuda.cupy.random.seed(seed)
# Main process of CNN learning
# =========================================================
def main(params):
print("")
print('# gpu: {}'.format(params["gpu"]))
print('# unit: {}'.format(params["unit"]))
print('# batch-size: {}'.format(params["batchsize"]))
print('# epoch: {}'.format(params["epoch"]))
print('# number of category: {}'.format(params["output_dimensions"]))
print('# embedding dimension: {}'.format(params["embedding_dimensions"]))
print('# current layer: {}'.format(params["current_depth"]))
print('# model-type: {}'.format(params["model_type"]))
print('')
f = open('./CNN/LOG/configuration_' + params["current_depth"] + '.txt', 'w')
f.write('# gpu: {}'.format(params["gpu"])+"\n")
f.write('# unit: {}'.format(params["unit"])+"\n")
f.write('# batch-size: {}'.format(params["batchsize"])+"\n")
f.write('# epoch: {}'.format(params["epoch"])+"\n")
f.write('# number of category: {}'.format(params["output_dimensions"])+"\n")
f.write('# embedding dimension: {}'.format(params["embedding_dimensions"])+"\n")
f.write('# current layer: {}'.format(params["current_depth"])+"\n")
f.write('# model-type: {}'.format(params["model_type"])+"\n")
f.write("\n")
f.close()
embedding_weight = params["embedding_weight"]
embedding_dimensions = params["embedding_dimensions"]
input_data = params["input_data"]
x_train = input_data['x_trn']
x_val = input_data['x_val']
y_train = input_data['y_trn']
y_val = input_data['y_val']
cnn_params = {"cudnn":USE_CUDNN,
"out_channels":params["out_channels"],
"row_dim":embedding_dimensions,
"batch_size":params["batchsize"],
"hidden_dim":params["unit"],
"n_classes":params["output_dimensions"],
"embedding_weight":embedding_weight,
}
if params["fine_tuning"] == 0:
cnn_params['mode'] = 'scratch'
elif params["fine_tuning"] == 1:
cnn_params['mode'] = 'fine-tuning'
cnn_params['load_param_node_name'] = params['upper_depth']
if params["model_type"] == "XML-CNN":
model = xml_cnn_model.CNN(**cnn_params)
else:
model = cnn_model.CNN(**cnn_params)
if params["gpu"] >= 0:
chainer.cuda.get_device_from_id(params["gpu"]).use()
model.to_gpu()
# Learning CNN by training and validation data
# =========================================================
optimizer = chainer.optimizers.Adam()
optimizer.setup(model)
train = tuple_dataset.TupleDataset(x_train, y_train)
val = tuple_dataset.TupleDataset(x_val, y_val)
train_iter = chainer.iterators.SerialIterator(train, params["batchsize"], repeat=True, shuffle=False)
val_iter = chainer.iterators.SerialIterator(val, params["batchsize"], repeat = False, shuffle=False)
# The setting of Early stopping validation refers to a loss value (validation/main/loss) obtained by validation data
# =========================================================
stop_trigger = training.triggers.EarlyStoppingTrigger(
monitor='validation/main/loss',
max_trigger=(params["epoch"], 'epoch'))
updater = MyUpdater(train_iter, optimizer, params["output_dimensions"], device=params["gpu"])
trainer = training.Trainer(updater, stop_trigger, out='./CNN/')
trainer.extend(MyEvaluator(val_iter, model, class_dim=params["output_dimensions"], device=params["gpu"]))
trainer.extend(extensions.dump_graph('main/loss'))
trainer.extend(extensions.snapshot_object(model, 'parameters_for_multi_label_model_' + params["current_depth"] + '.npz'),trigger=training.triggers.MinValueTrigger('validation/main/loss',trigger=(1,'epoch')))
trainer.extend(extensions.LogReport(log_name='LOG/log_' + params["current_depth"] + ".txt", trigger=(1, 'epoch')))
trainer.extend(extensions.PrintReport(
['epoch', 'main/loss', 'validation/main/loss',
'elapsed_time']))
trainer.extend(extensions.ProgressBar())
trainer.extend(
extensions.PlotReport(['main/loss', 'validation/main/loss'],
'epoch', file_name='LOG/loss_' + params["current_depth"] + '.png'))
trainer.run()
filename = 'parameters_for_multi_label_model_' + params["current_depth"] + '.npz'
src = './CNN/'
dst = './CNN/PARAMS'
shutil.move(os.path.join(src, filename), os.path.join(dst, filename))
# Prediction process for test data.
# =========================================================
print ("-"*50)
print ("Testing...")
x_tst = input_data['x_tst']
y_tst = input_data['y_tst']
n_eval = len(x_tst)
cnn_params['mode'] = 'test-predict'
cnn_params['load_param_node_name'] = params["current_depth"]
if params["model_type"] == "XML-CNN":
model = xml_cnn_model.CNN(**cnn_params)
else:
model = cnn_model.CNN(**cnn_params)
model.to_gpu()
output = np.zeros([n_eval,params["output_dimensions"]],dtype=np.int8)
output_probability_file_name = "CNN/RESULT/probability_" + params["current_depth"] + ".csv"
with open(output_probability_file_name, 'w') as f:
f.write(','.join(params["learning_categories"])+"\n")
test_batch_size = params["batchsize"]
with chainer.using_config('train', False), chainer.no_backprop_mode():
for i in tqdm(six.moves.range(0, n_eval, test_batch_size),desc="Predict Test loop"):
x = chainer.Variable(chainer.cuda.to_gpu(x_tst[i:i + test_batch_size]))
t = y_tst[i:i + test_batch_size]
net_output = F.sigmoid(model(x))
output[i: i + test_batch_size] = select_function(net_output.data)
with open(output_probability_file_name , 'a') as f:
tmp = chainer.cuda.to_cpu(net_output.data)
low_values_flags = tmp < 0.001
tmp[low_values_flags] = 0
np.savetxt(f,tmp,fmt='%.4g',delimiter=",")
return output
# Categorization of the top level of a hierarchy by using WoFt and HFT models
# =========================================================
def load_top_level_weights(params):
print ("-"*50)
print ("Testing...")
embedding_weight = params["embedding_weight"]
embedding_dimensions = params["embedding_dimensions"]
input_data = params["input_data"]
cnn_params = {"cudnn":USE_CUDNN,
"out_channels":params["out_channels"],
"row_dim":embedding_dimensions,
"batch_size":params["batchsize"],
"hidden_dim":params["unit"],
"n_classes":params["output_dimensions"],
"embedding_weight":embedding_weight,
}
x_tst = input_data['x_tst']
y_tst = input_data['y_tst']
n_eval = len(x_tst)
cnn_params['mode'] = 'test-predict'
cnn_params['load_param_node_name'] = params["current_depth"]
if params["model_type"] == "XML-CNN":
model = xml_cnn_model.CNN(**cnn_params)
else:
model = cnn_model.CNN(**cnn_params)
model.to_gpu()
output = np.zeros([n_eval,params["output_dimensions"]],dtype=np.int8)
output_probability_file_name = "CNN/RESULT/probability_" + params["current_depth"] + ".csv"
with open(output_probability_file_name, 'w') as f:
f.write(','.join(params["learning_categories"])+"\n")
test_batch_size = params["batchsize"]
with chainer.using_config('train', False), chainer.no_backprop_mode():
for i in tqdm(six.moves.range(0, n_eval, test_batch_size),desc="Predict Test loop"):
x = chainer.Variable(chainer.cuda.to_gpu(X_tst[i:i + params["batchsize"]]))
t = Y_tst[i:i + test_batch_size]
net_output = F.sigmoid(model(x))
output[i: i + test_batch_size] = select_function(net_output.data)
with open(output_probability_file_name , 'a') as f:
tmp = chainer.cuda.to_cpu(net_output.data)
low_values_flags = tmp < 0.001
tmp[low_values_flags] = 0
np.savetxt(f,tmp,fmt='%.4g',delimiter=",")
return output