-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscnn.py
197 lines (140 loc) · 5.2 KB
/
scnn.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
#!/usr/local/bin python
#! -*- coding: utf-8 -*-
from __future__ import print_function
import theano
import theano.tensor as T
from theano.tensor.nnet import conv
from theano.tensor.signal import pool as downsample
import numpy as np
from sklearn.cross_validation import train_test_split
from layers import EmbedIDLayer
from layers import FullyConnectedLayer
from layers import ConvolutionalLayer
from layers import MaxPoolingLayer
from utils import *
from optimizers import *
# 31566 100
class SCNN(object):
def __init__(
self,
rng,
batchsize=100,
activation=tanh
):
import load
(num_sent, word_cnt, max_sen_len, k_wrd, x_wrd, y) \
= load.read("tweets_clean.txt")
dim_word = 100
cl_word = 300
k_wrd = 5
vocab_size = word_cnt
n_hidden = 300
data_train,\
data_test,\
target_train,\
target_test\
= train_test_split(x_wrd, y, random_state=1234, test_size=0.1)
x_train = theano.shared(np.asarray(data_train, dtype='int16'), borrow=True)
y_train = theano.shared(np.asarray(target_train, dtype='int32'), borrow=True)
x_test = theano.shared(np.asarray(data_test, dtype='int16'), borrow=True)
y_test = theano.shared(np.asarray(target_test, dtype='int32'), borrow=True)
self.n_train_batches = x_train.get_value(borrow=True).shape[0] / batchsize
self.n_test_batches = x_test.get_value(borrow=True).shape[0] / batchsize
"""symbol definition"""
index = T.iscalar()
x = T.wmatrix('x')
y = T.ivector('y')
train = T.iscalar('train')
layer_embed_input = x#.reshape((batchsize, max_sen_len))
layer_embed = EmbedIDLayer(
rng,
layer_embed_input,
n_input=vocab_size,
n_output=dim_word,
)
layer1_input = layer_embed.output.reshape((batchsize, 1, max_sen_len, dim_word))
layer1 = ConvolutionalLayer(
rng,
layer1_input,
filter_shape=(cl_word, 1, k_wrd, dim_word),#1は入力チャネル数
image_shape=(batchsize, 1, max_sen_len, dim_word),
activation=activation
)
layer2 = MaxPoolingLayer(
layer1.output,
poolsize=(max_sen_len-k_wrd+1, 1)
)
layer3_input = layer2.output.reshape((batchsize, cl_word))
layer3 = FullyConnectedLayer(
rng,
dropout(rng, layer3_input, train),
n_input=cl_word,
n_output=n_hidden,
activation=activation
)
layer4 = FullyConnectedLayer(
rng,
dropout(rng, layer3.output, train),
n_input=n_hidden,
n_output=2,
activation=None
)
result = Result(layer4.output, y)
# loss = result.negative_log_likelihood()
loss = result.cross_entropy()
accuracy = result.accuracy()
params = layer4.params + layer3.params + layer1.params + layer_embed.params
# updates = AdaDelta(params=params).updates(loss)
updates = RMSprop(learning_rate=0.001, params=params).updates(loss)
self.train_model = theano.function(
inputs=[index],
outputs=[loss, accuracy],
updates=updates,
givens={
x: x_train[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: x_test[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__':
tmp = []
seed = 10
for _ in xrange(seed):
rng = np.random.RandomState(_)
scnn = SCNN(rng, batchsize=100, activation=relu)
accuracies = scnn.train_and_test(n_epoch=3)
tmp.append(accuracies)
tmp = np.array(tmp)
mean = [np.mean(tmp[:,i]) for i in range(len(tmp[0]))]
mean = np.array(mean)
np.savetxt('scnn.csv', mean, delimiter=',')