forked from lvyilin/BaikeNRE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_cnn.py
130 lines (109 loc) · 4.69 KB
/
train_cnn.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
import os
import time
import numpy as np
import mxnet as mx
from mxnet import gluon, init, autograd, nd
from mxnet.gluon import loss as gloss, nn
from sklearn.metrics import precision_recall_fscore_support, classification_report
CWD = os.getcwd()
SAVE_MODEL_PATH = os.path.join(CWD, "net_params", "cnn", "net_cnn_epoch%d_12610.params")
SENTENCE_DIMENSION = 100
POS_DIMENSION = 5
DIMENSION = SENTENCE_DIMENSION + 2 * POS_DIMENSION
FIXED_WORD_LENGTH = 60
ADAPTIVE_LEARNING_RATE = False
CTX = mx.cpu(0)
ctx = [CTX]
input_train = np.load('data_train_12610.npy')
input_test = np.load('data_test_12610.npy')
x_train = input_train[:, 3:].reshape((input_train.shape[0], FIXED_WORD_LENGTH, DIMENSION))
x_train = np.expand_dims(x_train, axis=1)
y_train = input_train[:, 0]
print(x_train.shape)
print(y_train.shape)
x_test = input_test[:, 3:].reshape((input_test.shape[0], FIXED_WORD_LENGTH, DIMENSION))
x_test = np.expand_dims(x_test, axis=1)
y_test = input_test[:, 0]
print(x_test.shape)
print(y_test.shape)
x_train = x_train.astype(np.float32)
y_train = y_train.astype(np.float32)
x_test = x_test.astype(np.float32)
y_test = y_test.astype(np.float32)
print(x_train.shape, x_test.shape)
x_train = nd.array(x_train, ctx=CTX)
y_train = nd.array(y_train, ctx=CTX)
x_test = nd.array(x_test, ctx=CTX)
y_test = nd.array(y_test, ctx=CTX)
net = nn.Sequential()
with net.name_scope():
# net.add(nn.Conv2D(256, kernel_size=(5, DIMENSION), padding=(1, 0), activation='relu'))
net.add(nn.Conv2D(256, kernel_size=(3, DIMENSION), padding=(1, 0), activation='relu'))
# net.add(nn.MaxPool2D(pool_size=(FIXED_WORD_LENGTH - 2, 1)))
net.add(nn.MaxPool2D(pool_size=(FIXED_WORD_LENGTH, 1)))
net.add(nn.Dense(256, activation='relu'))
net.add(nn.Dropout(0.5))
net.add(nn.Dense(7))
net.collect_params().initialize(init=init.Xavier(), ctx=ctx)
print(net)
batch_size = 128
num_epochs = 100
decay_rate = 0.1
gap = 25
loss = gloss.SoftmaxCrossEntropyLoss()
# trainer = gluon.Trainer(net.collect_params(), 'AdaDelta', {'rho': 0.95, 'epsilon': 1e-6, 'wd': 0.01})
# trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': 0.0001})
# trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .01})
if ADAPTIVE_LEARNING_RATE:
trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': 0.01})
else:
trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': 0.0001})
train_data = gluon.data.DataLoader(gluon.data.ArrayDataset(x_train, y_train), batch_size, shuffle=True)
test_data = gluon.data.DataLoader(gluon.data.ArrayDataset(x_test, y_test), batch_size, shuffle=False)
def accuracy(y_hat, y):
return (y_hat.argmax(axis=1) == y.astype('float32')).mean().asscalar()
def evaluate_accuracy(net, data_iter):
acc = 0
for X, y in data_iter:
y = y.copyto(CTX)
acc += accuracy(net(X), y)
return acc / len(data_iter)
def train(net, train_iter, test_iter, loss, num_epochs, batch_size, trainer):
highest_epoch = -1
highest_acc = -1
for epoch in range(1, num_epochs + 1):
train_loss_sum = 0
train_acc_sum = 0
if ADAPTIVE_LEARNING_RATE and epoch % gap == 0:
trainer.set_learning_rate(trainer.learning_rate * decay_rate)
print("learning_rate decay: %f" % trainer.learning_rate)
start = time.time()
for X, y in train_iter:
y = y.copyto(CTX)
with autograd.record():
y_hat = net(X)
lss = loss(y_hat, y)
lss.backward()
trainer.step(batch_size)
train_loss_sum += lss.mean().asscalar()
train_acc_sum += accuracy(y_hat, y)
test_acc = evaluate_accuracy(test_iter, net)
if test_acc > highest_acc:
highest_acc = test_acc
highest_epoch = epoch
print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f time %.1f sec'
% (epoch, train_loss_sum / len(train_iter),
train_acc_sum / len(train_iter), test_acc, time.time() - start))
net.save_params(SAVE_MODEL_PATH % epoch)
print("highest epoch & acc: %d, %f" % (highest_epoch, highest_acc))
evaluate_model(net, highest_epoch)
def evaluate_model(net, epoch):
net.load_params(SAVE_MODEL_PATH % epoch, ctx=CTX)
y_hat = net(x_test)
result = nd.concat(y_test.expand_dims(axis=1), y_hat, dim=1)
np.save("result_crcnn.npy", result.asnumpy())
predict_list = y_hat.argmax(axis=1).asnumpy().astype(np.int).tolist()
label_list = y_test.astype(np.int).asnumpy().tolist()
print(precision_recall_fscore_support(label_list, predict_list, average='weighted'))
print(classification_report(label_list, predict_list))
train(net, train_data, test_data, loss, num_epochs, batch_size, trainer)