forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpos_baseline.py
227 lines (193 loc) · 7.05 KB
/
pos_baseline.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# 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
# You can get the data from this URL: https://www.clips.uantwerpen.be/conll2000/chunking/
# If above URL does not work, try this:
# https://drive.google.com/file/d/0BxGV7C-8DTe5QmF2MTFwN3JjWGc/view?usp=sharing
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 os, sys
import numpy as np
import theano
import theano.tensor as T
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.metrics import f1_score
from sklearn.tree import DecisionTreeClassifier
class LogisticRegression:
def __init__(self):
pass
def fit(self, X, Y, V=None, K=None, D=50, lr=1e-1, mu=0.99, batch_sz=100, epochs=6):
if V is None:
V = len(set(X))
if K is None:
K = len(set(Y))
N = len(X)
W = np.random.randn(V, K) / np.sqrt(V + K)
b = np.zeros(K)
self.W = theano.shared(W)
self.b = theano.shared(b)
self.params = [self.W, self.b]
thX = T.ivector('X')
thY = T.ivector('Y')
py_x = T.nnet.softmax(self.W[thX] + self.b)
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]
self.cost_predict_op = theano.function(
inputs=[thX, thY],
outputs=[cost, prediction],
allow_input_downcast=True,
)
updates = [
(p, p + mu*dp - lr*g) for p, dp, g in zip(self.params, dparams, grads)
] + [
(dp, mu*dp - lr*g) for dp, g in zip(dparams, grads)
]
train_op = theano.function(
inputs=[thX, thY],
outputs=[cost, prediction],
updates=updates,
allow_input_downcast=True
)
costs = []
n_batches = N // batch_sz
for i in range(epochs):
X, Y = shuffle(X, Y)
print("epoch:", i)
for j in range(n_batches):
Xbatch = X[j*batch_sz:(j*batch_sz + batch_sz)]
Ybatch = Y[j*batch_sz:(j*batch_sz + batch_sz)]
c, p = train_op(Xbatch, Ybatch)
costs.append(c)
if j % 200 == 0:
print(
"i:", i, "j:", j,
"n_batches:", n_batches,
"cost:", c,
"error:", np.mean(p != Ybatch)
)
plt.plot(costs)
plt.show()
def score(self, X, Y):
_, p = self.cost_predict_op(X, Y)
return np.mean(p == Y)
def f1_score(self, X, Y):
_, p = self.cost_predict_op(X, Y)
return f1_score(Y, p, average=None).mean()
def get_data(split_sequences=False):
if not os.path.exists('chunking'):
print("Please create a folder in your local directory called 'chunking'")
print("train.txt and test.txt should be stored in there.")
print("Please check the comments to get the download link.")
exit()
elif not os.path.exists('chunking/train.txt'):
print("train.txt is not in chunking/train.txt")
print("Please check the comments to get the download link.")
exit()
elif not os.path.exists('chunking/test.txt'):
print("test.txt is not in chunking/test.txt")
print("Please check the comments to get the download link.")
exit()
word2idx = {}
tag2idx = {}
word_idx = 0
tag_idx = 0
Xtrain = []
Ytrain = []
currentX = []
currentY = []
for line in open('chunking/train.txt'):
line = line.rstrip()
if line:
r = line.split()
word, tag, _ = r
# for a word index dict
if word not in word2idx:
word2idx[word] = word_idx
word_idx += 1
currentX.append(word2idx[word])
# index the tags too (categories)
if tag not in tag2idx:
tag2idx[tag] = tag_idx
tag_idx += 1
currentY.append(tag2idx[tag])
elif split_sequences:
Xtrain.append(currentX)
Ytrain.append(currentY)
currentX = []
currentY = []
if not split_sequences:
Xtrain = currentX
Ytrain = currentY
# load and score test data
Xtest = []
Ytest = []
currentX = [] # a growing list of word indexes
currentY = []
for line in open('chunking/test.txt'):
line = line.rstrip()
if line:
r = line.split()
word, tag, _ = r
if word in word2idx:
currentX.append(word2idx[word])
else:
currentX.append(word_idx) # use this as unknown
currentY.append(tag2idx[tag])
# in between sequences is an empty line, so it will go here
elif split_sequences:
Xtest.append(currentX)
Ytest.append(currentY)
currentX = []
currentY = []
if not split_sequences:
Xtest = currentX
Ytest = currentY
return Xtrain, Ytrain, Xtest, Ytest, word2idx
def main():
Xtrain, Ytrain, Xtest, Ytest, word2idx = get_data(split_sequences=False)
# convert to numpy arrays
Xtrain = np.array(Xtrain)
Ytrain = np.array(Ytrain)
# convert Xtrain to indicator matrix
N = len(Xtrain)
V = len(word2idx) + 1
print("vocabulary size:", V)
# Xtrain_indicator = np.zeros((N, V))
# Xtrain_indicator[np.arange(N), Xtrain] = 1
# decision tree
dt = DecisionTreeClassifier()
# without indicator
dt.fit(Xtrain.reshape(N, 1), Ytrain)
print("dt train score:", dt.score(Xtrain.reshape(N, 1), Ytrain))
p = dt.predict(Xtrain.reshape(N, 1))
print("dt train f1:", f1_score(Ytrain, p, average=None).mean())
# with indicator -- too slow!!
# dt.fit(Xtrain_indicator, Ytrain)
# print("dt score:", dt.score(Xtrain_indicator, Ytrain))
# train and score
model = LogisticRegression()
model.fit(Xtrain, Ytrain, V=V)
print("training complete")
print("lr train score:", model.score(Xtrain, Ytrain))
print("lr train f1:", model.f1_score(Xtrain, Ytrain))
Ntest = len(Xtest)
Xtest = np.array(Xtest)
Ytest = np.array(Ytest)
# convert Xtest to indicator
# Xtest_indicator = np.zeros((Ntest, V))
# Xtest_indicator[np.arange(Ntest), Xtest] = 1
# decision tree test score
print("dt test score:", dt.score(Xtest.reshape(Ntest, 1), Ytest))
p = dt.predict(Xtest.reshape(Ntest, 1))
print("dt test f1:", f1_score(Ytest, p, average=None).mean())
# print("dt test score:", dt.score(Xtest_indicator, Ytest)) # too slow!
# logistic test score -- too slow!!
print("lr test score:", model.score(Xtest, Ytest))
print("lr test f1:", model.f1_score(Xtest, Ytest))
if __name__ == '__main__':
main()