-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcharscnn.py
233 lines (171 loc) · 6.65 KB
/
charscnn.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
#!/usr/local/bin python
#! -*- coding: utf-8 -*-
from __future__ import print_function
import numpy as np
import theano
import theano.tensor as T
from sklearn.cross_validation import train_test_split
from theano.tensor.nnet import conv
from theano.tensor.signal import pool as downsample
from layers import ConvolutionalLayer
from layers import EmbedIDLayer
from layers import FullyConnectedLayer
from layers import MaxPoolingLayer
from optimizers import *
from utils import *
class CharSCNN(object):
def __init__(
self,
rng,
batchsize=100,
activation=relu
):
import char_load
(num_sent, char_cnt, word_cnt, max_word_len, max_sen_len,\
k_chr, k_wrd, x_chr, x_wrd, y) = char_load.read("tweets_clean.txt")
dim_word = 30
dim_char = 5
cl_word = 300
cl_char = 50
k_word = k_wrd
k_char = k_chr
data_train_word,\
data_test_word,\
data_train_char,\
data_test_char,\
target_train,\
target_test\
= train_test_split(x_wrd, x_chr, y, random_state=1234, test_size=0.1)
x_train_word = theano.shared(np.asarray(data_train_word, dtype='int16'), borrow=True)
x_train_char = theano.shared(np.asarray(data_train_char, dtype='int16'), borrow=True)
y_train = theano.shared(np.asarray(target_train, dtype='int8'), borrow=True)
x_test_word = theano.shared(np.asarray(data_test_word, dtype='int16'), borrow=True)
x_test_char = theano.shared(np.asarray(data_test_char, dtype='int16'), borrow=True)
y_test = theano.shared(np.asarray(target_test, dtype='int8'), borrow=True)
self.n_train_batches = x_train_word.get_value(borrow=True).shape[0] / batchsize
self.n_test_batches = x_test_word.get_value(borrow=True).shape[0] / batchsize
"""symbol definition"""
index = T.iscalar()
x_wrd = T.wmatrix('x_wrd')
x_chr = T.wtensor3('x_chr')
y = T.bvector('y')
train = T.iscalar('train')
"""network definition"""
layer_char_embed_input = x_chr#.reshape((batchsize, max_sen_len, max_word_len))
layer_char_embed = EmbedIDLayer(
rng,
layer_char_embed_input,
n_input=char_cnt,
n_output=dim_char
)
layer1_input = layer_char_embed.output.reshape(
(batchsize*max_sen_len, 1, max_word_len, dim_char)
)
layer1 = ConvolutionalLayer(
rng,
layer1_input,
filter_shape=(cl_char, 1, k_char, dim_char),# cl_charフィルタ数
image_shape=(batchsize*max_sen_len, 1, max_word_len, dim_char)
)
layer2 = MaxPoolingLayer(
layer1.output,
poolsize=(max_word_len-k_char+1, 1)
)
layer_word_embed_input = x_wrd #.reshape((batchsize, max_sen_len))
layer_word_embed = EmbedIDLayer(
rng,
layer_word_embed_input,
n_input=word_cnt,
n_output=dim_word
)
layer3_word_input = layer_word_embed.output.reshape((batchsize, 1, max_sen_len, dim_word))
layer3_char_input = layer2.output.reshape((batchsize, 1, max_sen_len, cl_char))
layer3_input = T.concatenate(
[layer3_word_input,
layer3_char_input],
axis=3
)#.reshape((batchsize, 1, max_sen_len, dim_word+cl_char))
layer3 = ConvolutionalLayer(
rng,
layer3_input,
filter_shape=(cl_word, 1, k_word, dim_word + cl_char),#1は入力チャネル数
image_shape=(batchsize, 1, max_sen_len, dim_word + cl_char),
activation=activation
)
layer4 = MaxPoolingLayer(
layer3.output,
poolsize=(max_sen_len-k_word+1, 1)
)
layer5_input = layer4.output.reshape((batchsize, cl_word))
layer5 = FullyConnectedLayer(
rng,
dropout(rng, layer5_input, train),
n_input=cl_word,
n_output=50,
activation=activation
)
layer6_input = layer5.output
layer6 = FullyConnectedLayer(
rng,
dropout(rng, layer6_input, train, p=0.1),
n_input=50,
n_output=2,
activation=None
)
result = Result(layer6.output, y)
loss = result.negative_log_likelihood()
accuracy = result.accuracy()
params = layer6.params\
+layer5.params\
+layer3.params\
+layer_word_embed.params\
+layer1.params\
+layer_char_embed.params
updates = RMSprop(learning_rate=0.001, params=params).updates(loss)
self.train_model = theano.function(
inputs=[index],
outputs=[loss, accuracy],
updates=updates,
givens={
x_wrd: x_train_word[index*batchsize: (index+1)*batchsize],
x_chr: x_train_char[index*batchsize: (index+1)*batchsize],
y: y_train[index*batchsize: (index+1)*batchsize],
train: np.cast['int32'](1)
}
)
self.test_model = theano.function(
inputs=[index],
outputs=[loss, accuracy],
givens={
x_wrd: x_test_word[index*batchsize: (index+1)*batchsize],
x_chr: x_test_char[index*batchsize: (index+1)*batchsize],
y: y_test[index*batchsize: (index+1)*batchsize],
train: np.cast['int32'](0)
}
)
def train_and_test(self, n_epoch=100):
epoch = 0
accuracies = []
while epoch < n_epoch:
epoch += 1
sum_loss = 0
sum_accuracy = 0
for batch_index in xrange(self.n_train_batches):
batch_loss, batch_accuracy = self.train_model(batch_index)
sum_loss = 0
sum_accuracy = 0
for batch_index in xrange(self.n_test_batches):
batch_loss, batch_accuracy = self.test_model(batch_index)
sum_loss += batch_loss
sum_accuracy += batch_accuracy
loss = sum_loss / self.n_test_batches
accuracy = sum_accuracy / self.n_test_batches
accuracies.append(accuracy)
print('epoch: {}, test mean loss={}, test accuracy={}'.format(epoch, loss, accuracy))
print('')
return accuracies
if __name__ == '__main__':
random_state = 1234
rng = np.random.RandomState(random_state)
charscnn = CharSCNN(rng, batchsize=10, activation=relu)
charscnn.train_and_test(n_epoch=3)