17
17
18
18
19
19
class RNN :
20
- def __init__ (self , D , hidden_layer_sizes , V ):
20
+ def __init__ (self , D , hidden_layer_sizes , V , K ):
21
21
self .hidden_layer_sizes = hidden_layer_sizes
22
22
self .D = D
23
23
self .V = V
24
+ self .K = K
24
25
25
26
def fit (self , X , Y , learning_rate = 1e-4 , mu = 0.99 , epochs = 30 , show_fig = True , activation = T .nnet .relu , RecurrentUnit = GRU , normalize = False ):
26
27
D = self .D
@@ -35,8 +36,8 @@ def fit(self, X, Y, learning_rate=1e-4, mu=0.99, epochs=30, show_fig=True, activ
35
36
self .hidden_layers .append (ru )
36
37
Mi = Mo
37
38
38
- Wo = init_weight (Mi , V )
39
- bo = np .zeros (V )
39
+ Wo = init_weight (Mi , self . K )
40
+ bo = np .zeros (self . K )
40
41
41
42
self .We = theano .shared (We )
42
43
self .Wo = theano .shared (Wo )
@@ -53,6 +54,13 @@ def fit(self, X, Y, learning_rate=1e-4, mu=0.99, epochs=30, show_fig=True, activ
53
54
Z = ru .output (Z )
54
55
py_x = T .nnet .softmax (Z .dot (self .Wo ) + self .bo )
55
56
57
+ testf = theano .function (
58
+ inputs = [thX ],
59
+ outputs = py_x ,
60
+ )
61
+ testout = testf (X [0 ])
62
+ print "py_x.shape:" , testout .shape
63
+
56
64
prediction = T .argmax (py_x , axis = 1 )
57
65
58
66
cost = - T .mean (T .log (py_x [T .arange (thY .shape [0 ]), thY ]))
@@ -127,10 +135,16 @@ def f1_score(self, X, Y):
127
135
P = np .concatenate (P )
128
136
return f1_score (Y , P , average = None ).mean ()
129
137
138
+
139
+ def flatten (l ):
140
+ return [item for sublist in l for item in sublist ]
141
+
142
+
130
143
def main ():
131
144
Xtrain , Ytrain , Xtest , Ytest , word2idx = get_data (split_sequences = True )
132
145
V = len (word2idx ) + 1
133
- rnn = RNN (10 , [10 ], V )
146
+ K = len (set (flatten (Ytrain )) | set (flatten (Ytest )))
147
+ rnn = RNN (10 , [10 ], V , K )
134
148
rnn .fit (Xtrain , Ytrain )
135
149
print "train score:" , rnn .score (Xtrain , Ytrain )
136
150
print "test score:" , rnn .score (Xtest , Ytest )
0 commit comments