forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpos_rnn.py
170 lines (143 loc) · 5.09 KB
/
pos_rnn.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
# Course URL:
# https://deeplearningcourses.com/c/natural-language-processing-with-deep-learning-in-python
# https://udemy.com/natural-language-processing-with-deep-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
import theano
import theano.tensor as T
import os
import sys
sys.path.append(os.path.abspath('..'))
from rnn_class.gru import GRU
from rnn_class.lstm import LSTM
from pos_baseline import get_data
from sklearn.utils import shuffle
from util import init_weight
from datetime import datetime
from sklearn.metrics import f1_score
class RNN:
def __init__(self, D, hidden_layer_sizes, V, K):
self.hidden_layer_sizes = hidden_layer_sizes
self.D = D
self.V = V
self.K = K
def fit(self, X, Y, learning_rate=1e-4, mu=0.99, epochs=30, show_fig=True, activation=T.nnet.relu, RecurrentUnit=LSTM, normalize=False):
D = self.D # dimensions
V = self.V # vocabulary
N = len(X) # number of items
We = init_weight(V, D)
self.hidden_layers = []
Mi = D
for Mo in self.hidden_layer_sizes:
ru = RecurrentUnit(Mi, Mo, activation)
self.hidden_layers.append(ru)
Mi = Mo
Wo = init_weight(Mi, self.K)
bo = np.zeros(self.K)
self.We = theano.shared(We)
self.Wo = theano.shared(Wo)
self.bo = theano.shared(bo)
self.params = [self.Wo, self.bo]
for ru in self.hidden_layers:
self.params += ru.params
thX = T.ivector('X')
thY = T.ivector('Y')
Z = self.We[thX]
for ru in self.hidden_layers:
Z = ru.output(Z)
py_x = T.nnet.softmax(Z.dot(self.Wo) + self.bo)
testf = theano.function(
inputs=[thX],
outputs=py_x,
)
testout = testf(X[0])
print("py_x.shape:", testout.shape)
prediction = T.argmax(py_x, axis=1)
cost = -T.mean(T.log(py_x[T.arange(thY.shape[0]), thY]))
grads = T.grad(cost, self.params)
dparams = [theano.shared(p.get_value()*0) for p in self.params]
dWe = theano.shared(self.We.get_value()*0)
gWe = T.grad(cost, self.We)
dWe_update = mu*dWe - learning_rate*gWe
We_update = self.We + dWe_update
if normalize:
We_update /= We_update.norm(2)
updates = [
(p, p + mu*dp - learning_rate*g) for p, dp, g in zip(self.params, dparams, grads)
] + [
(dp, mu*dp - learning_rate*g) for dp, g in zip(dparams, grads)
] + [
(self.We, We_update), (dWe, dWe_update)
]
self.cost_predict_op = theano.function(
inputs=[thX, thY],
outputs=[cost, prediction],
allow_input_downcast=True,
)
self.train_op = theano.function(
inputs=[thX, thY],
outputs=[cost, prediction],
updates=updates
)
costs = []
sequence_indexes = range(N)
n_total = sum(len(y) for y in Y)
for i in range(epochs):
t0 = datetime.now()
sequence_indexes = shuffle(sequence_indexes)
n_correct = 0
cost = 0
it = 0
for j in sequence_indexes:
c, p = self.train_op(X[j], Y[j])
cost += c
n_correct += np.sum(p == Y[j])
it += 1
if it % 200 == 0:
sys.stdout.write(
"j/N: %d/%d correct rate so far: %f, cost so far: %f\r" %
(it, N, float(n_correct)/n_total, cost)
)
sys.stdout.flush()
print(
"i:", i, "cost:", cost,
"correct rate:", (float(n_correct)/n_total),
"time for epoch:", (datetime.now() - t0)
)
costs.append(cost)
if show_fig:
plt.plot(costs)
plt.show()
def score(self, X, Y):
n_total = sum(len(y) for y in Y)
n_correct = 0
for x, y in zip(X, Y):
_, p = self.cost_predict_op(x, y)
n_correct += np.sum(p == y)
return float(n_correct) / n_total
def f1_score(self, X, Y):
P = []
for x, y in zip(X, Y):
_, p = self.cost_predict_op(x, y)
P.append(p)
Y = np.concatenate(Y)
P = np.concatenate(P)
return f1_score(Y, P, average=None).mean()
def flatten(l):
return [item for sublist in l for item in sublist]
def main():
Xtrain, Ytrain, Xtest, Ytest, word2idx = get_data(split_sequences=True)
V = len(word2idx) + 1
K = len(set(flatten(Ytrain)) | set(flatten(Ytest)))
rnn = RNN(10, [10], V, K)
rnn.fit(Xtrain, Ytrain)
print("train score:", rnn.score(Xtrain, Ytrain))
print("test score:", rnn.score(Xtest, Ytest))
print("train f1:", rnn.f1_score(Xtrain, Ytrain))
print("test f1:", rnn.f1_score(Xtest, Ytest))
if __name__ == '__main__':
main()