Skip to content

Commit d064f64

Browse files
add lstm wiki + minor fixes
1 parent a0da868 commit d064f64

File tree

8 files changed

+227
-48
lines changed

8 files changed

+227
-48
lines changed

ann_class/batch_donut.py

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ def test_donut():
8181
LL = [] # keep track of likelihoods
8282
learning_rate = 0.0001
8383
regularization = 0.1
84-
last_error_rate = None
8584

8685

8786
# batch version

hmm_class/edgar_allan_poe.txt

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
LO! Death hath rear'd himself a throne
22
In a strange city, all alone,
3-
Far down within the dim west
3+
Far down within the dim west
44
Where the good, and the bad, and the worst, and the best,
55
Have gone to their eternal rest.
66

77
There shrines, and palaces, and towers
8-
Are not like any thing of ours
8+
Are not like any thing of ours
99
Oh no! O no! ours never loom
1010
To heaven with that ungodly gloom!
1111
Time-eaten towers that tremble not!
@@ -17,23 +17,23 @@ The melancholy waters lie.
1717
No holy rays from heaven come down
1818
On the long night-time of that town,
1919
But light from out the lurid sea
20-
Streams up the turrets silently
21-
Up thrones up long-forgotten bowers
22-
Of scultur'd ivy and stone flowers
23-
Up domes up spires up kingly halls
24-
Up fanes up Babylon-like walls
20+
Streams up the turrets silently
21+
Up thrones up long-forgotten bowers
22+
Of scultur'd ivy and stone flowers
23+
Up domes up spires up kingly halls
24+
Up fanes up Babylon-like walls
2525
Up many a melancholy shrine
2626
Whose entablatures intertwine
27-
The mask the viol and the vine.
27+
The mask the viol and the vine.
2828

29-
There open temples open graves
30-
Are on a level with the waves
29+
There open temples open graves
30+
Are on a level with the waves
3131
But not the riches there that lie
3232
In each idol's diamond eye,
3333
Not the gaily-jewell'd dead
3434
Tempt the waters from their bed:
3535
For no ripples curl, alas!
36-
Along that wilderness of glass
36+
Along that wilderness of glass
3737
No swellings hint that winds may be
3838
Upon a far-off happier sea:
3939
So blend the turrets and shadows there
@@ -42,13 +42,13 @@ While from the high towers of the town
4242
Death looks gigantically down.
4343

4444
But lo! a stir is in the air!
45-
The wave there is a ripple there!
45+
The wave there is a ripple there!
4646
As if the towers had thrown aside,
47-
In slightly sinking, the dull tide
47+
In slightly sinking, the dull tide
4848
As if the turret-tops had given
4949
A vacuum in the filmy heaven.
50-
The waves have now a redder glow
51-
The very hours are breathing low
50+
The waves have now a redder glow
51+
The very hours are breathing low
5252
And when, amid no earthly moans,
5353
Down, down, that town shall settle hence,
5454
All Hades, from a thousand thrones,
@@ -209,7 +209,7 @@ And love- a simple duty.
209209

210210
When from your gems of thought I turn
211211
To those pure orbs, your heart to learn,
212-
I scarce know which to prize most high
212+
I scarce know which to prize most high
213213
The bright i-dea, or the bright dear-eye.
214214

215215
Of all who hail thy presence as the morning-
@@ -290,23 +290,23 @@ Darkly my Present and my Past,
290290
Let my Future radiant shine
291291
With sweet hopes of thee and thine!
292292

293-
The bells! ah, the bells!
293+
The bells! ah, the bells!
294294
The little silver bells!
295295
How fairy-like a melody there floats
296-
From their throats.
297-
From their merry little throats
296+
From their throats.
297+
From their merry little throats
298298
From the silver, tinkling throats
299-
Of the bells, bells, bells
299+
Of the bells, bells, bells
300300
Of the bells!
301301

302-
The bells! ah, the bells!
302+
The bells! ah, the bells!
303303
The heavy iron bells!
304304
How horrible a monody there floats
305-
From their throats
306-
From their deep-toned throats
305+
From their throats
306+
From their deep-toned throats
307307
From their melancholy throats!
308308
How I shudder at the notes
309-
Of the bells, bells, bells
309+
Of the bells, bells, bells
310310
Of the bells!
311311

312312
How often we forget all time, when lone
@@ -554,7 +554,7 @@ The Elfin from the green grass, and from me
554554
The summer dream beneath the tamarind tree?
555555

556556
I'll tell you a plan for gaining wealth,
557-
Better than banking, trade or leases
557+
Better than banking, trade or leases
558558
Take a bank note and fold it up,
559559
And then you will find your money in creases!
560560
This wonderful plan, without danger or loss,

linear_regression_class/lr_1d.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@
4545
d1 = Y - Yhat
4646
d2 = Y - Y.mean()
4747
r2 = 1 - d1.dot(d1) / d2.dot(d2)
48-
print "the r-squared is:", r2
48+
print("the r-squared is:", r2)

linear_regression_class/lr_poly.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@
4949
d1 = Y - Yhat
5050
d2 = Y - Y.mean()
5151
r2 = 1 - d1.dot(d1) / d2.dot(d2)
52-
print "the r-squared is:", r2
52+
print("the r-squared is:", r2)

nlp_class/lsa.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ def tokens_to_vector(tokens):
7171
X[:,i] = tokens_to_vector(tokens)
7272
i += 1
7373

74-
svd = TruncatedSVD()
75-
Z = svd.fit_transform(X)
76-
plt.scatter(Z[:,0], Z[:,1])
77-
for i in xrange(D):
78-
plt.annotate(s=index_word_map[i], xy=(Z[i,0], Z[i,1]))
79-
plt.show()
74+
def main():
75+
svd = TruncatedSVD()
76+
Z = svd.fit_transform(X)
77+
plt.scatter(Z[:,0], Z[:,1])
78+
for i in xrange(D):
79+
plt.annotate(s=index_word_map[i], xy=(Z[i,0], Z[i,1]))
80+
plt.show()
81+
82+
if __name__ == '__main__':
83+
main()
8084

rnn_class/gru_wiki.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import sys
12
import theano
23
import theano.tensor as T
34
import numpy as np
45
import matplotlib.pyplot as plt
56
import json
67

8+
from datetime import datetime
79
from sklearn.utils import shuffle
810
from util import init_weight, get_wikipedia_data
911

@@ -63,7 +65,7 @@ def __init__(self, D, hidden_layer_sizes, V):
6365
self.D = D
6466
self.V = V
6567

66-
def fit(self, X, learning_rate=10e-5, mu=0.99, epochs=10, show_fig=True, activation=T.nnet.relu, RecurrentUnit=GRU):
68+
def fit(self, X, learning_rate=10e-5, mu=0.99, epochs=10, show_fig=True, activation=T.nnet.relu, RecurrentUnit=GRU, normalize=True):
6769
D = self.D
6870
V = self.V
6971
N = len(X)
@@ -110,7 +112,8 @@ def fit(self, X, learning_rate=10e-5, mu=0.99, epochs=10, show_fig=True, activat
110112
gWe = T.grad(cost, self.We)
111113
dWe_update = mu*dWe - learning_rate*gWe
112114
We_update = self.We + dWe_update
113-
We_update /= We_update.sum(axis=1).dimshuffle(0, 'x')
115+
if normalize:
116+
We_update /= We_update.sum(axis=1).dimshuffle(0, 'x')
114117

115118
updates = [
116119
(p, p + mu*dp - learning_rate*g) for p, dp, g in zip(self.params, dparams, grads)
@@ -128,6 +131,7 @@ def fit(self, X, learning_rate=10e-5, mu=0.99, epochs=10, show_fig=True, activat
128131

129132
costs = []
130133
for i in xrange(epochs):
134+
t0 = datetime.now()
131135
X = shuffle(X)
132136
n_correct = 0
133137
n_total = 0
@@ -159,33 +163,34 @@ def fit(self, X, learning_rate=10e-5, mu=0.99, epochs=10, show_fig=True, activat
159163
if pj == xj:
160164
n_correct += 1
161165
if j % 200 == 0:
162-
print "j:", j, "correct rate so far:", (float(n_correct)/n_total)
163-
print "i:", i, "cost:", cost, "correct rate:", (float(n_correct)/n_total)
166+
sys.stdout.write("j/N: %d/%d correct rate so far: %f\r" % (j, N, float(n_correct)/n_total))
167+
sys.stdout.flush()
168+
print "i:", i, "cost:", cost, "correct rate:", (float(n_correct)/n_total), "time for epoch:", (datetime.now() - t0)
164169
costs.append(cost)
165170

166171
if show_fig:
167172
plt.plot(costs)
168173
plt.show()
169174

170175

171-
def train_wikipedia():
176+
def train_wikipedia(we_file='word_embeddings.npy', w2i_file='wikipedia_word2idx.json'):
172177
# there are 32 files
173178
sentences, word2idx = get_wikipedia_data(n_files=32, n_vocab=2000)
174179
print "finished retrieving data"
175180
print "vocab size:", len(word2idx), "number of sentences:", len(sentences)
176-
rnn = RNN(20, [20], len(word2idx))
177-
rnn.fit(sentences, learning_rate=10e-5, epochs=10, show_fig=True, activation=T.nnet.relu)
181+
rnn = RNN(30, [30], len(word2idx))
182+
rnn.fit(sentences, learning_rate=10e-6, epochs=10, show_fig=True, activation=T.nnet.relu)
178183

179-
np.save('word_embeddings.npy', rnn.We.get_value())
180-
with open('wikipedia_word2idx.json', 'w') as f:
184+
np.save(we_file, rnn.We.get_value())
185+
with open(w2i_file, 'w') as f:
181186
json.dump(word2idx, f)
182187

183188
def generate_wikipedia():
184189
pass
185190

186-
def find_analogies(w1, w2, w3):
187-
We = np.load('word_embeddings.npy')
188-
with open('wikipedia_word2idx.json') as f:
191+
def find_analogies(w1, w2, w3, we_file='word_embeddings.npy', w2i_file='wikipedia_word2idx.json'):
192+
We = np.load(we_file)
193+
with open(w2i_file) as f:
189194
word2idx = json.load(f)
190195

191196
king = We[word2idx[w1]]

rnn_class/lstm_wiki.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import json
2+
import numpy as np
3+
import theano
4+
import theano.tensor as T
5+
6+
from util import init_weight, get_wikipedia_data
7+
from gru_wiki import RNN, find_analogies
8+
9+
class LSTM:
10+
def __init__(self, Mi, Mo, activation):
11+
self.Mi = Mi
12+
self.Mo = Mo
13+
self.f = activation
14+
15+
# numpy init
16+
Wxi = init_weight(Mi, Mo)
17+
Whi = init_weight(Mo, Mo)
18+
Wci = init_weight(Mo, Mo)
19+
bi = np.zeros(Mo)
20+
Wxf = init_weight(Mi, Mo)
21+
Whf = init_weight(Mo, Mo)
22+
Wcf = init_weight(Mo, Mo)
23+
bf = np.zeros(Mo)
24+
Wxc = init_weight(Mi, Mo)
25+
Whc = init_weight(Mo, Mo)
26+
bc = np.zeros(Mo)
27+
Wxo = init_weight(Mi, Mo)
28+
Who = init_weight(Mo, Mo)
29+
Wco = init_weight(Mo, Mo)
30+
bo = np.zeros(Mo)
31+
c0 = np.zeros(Mo)
32+
h0 = np.zeros(Mo)
33+
34+
# theano vars
35+
self.Wxi = theano.shared(Wxi)
36+
self.Whi = theano.shared(Whi)
37+
self.Wci = theano.shared(Wci)
38+
self.bi = theano.shared(bi)
39+
self.Wxf = theano.shared(Wxf)
40+
self.Whf = theano.shared(Whf)
41+
self.Wcf = theano.shared(Wcf)
42+
self.bf = theano.shared(bf)
43+
self.Wxc = theano.shared(Wxc)
44+
self.Whc = theano.shared(Whc)
45+
self.bc = theano.shared(bc)
46+
self.Wxo = theano.shared(Wxo)
47+
self.Who = theano.shared(Who)
48+
self.Wco = theano.shared(Wco)
49+
self.bo = theano.shared(bo)
50+
self.c0 = theano.shared(c0)
51+
self.h0 = theano.shared(h0)
52+
self.params = [
53+
self.Wxi,
54+
self.Whi,
55+
self.Wci,
56+
self.bi,
57+
self.Wxf,
58+
self.Whf,
59+
self.Wcf,
60+
self.bf,
61+
self.Wxc,
62+
self.Whc,
63+
self.bc,
64+
self.Wxo,
65+
self.Who,
66+
self.Wco,
67+
self.bo,
68+
self.c0,
69+
self.h0,
70+
]
71+
72+
def recurrence(self, x_t, h_t1, c_t1):
73+
i_t = T.nnet.sigmoid(x_t.dot(self.Wxi) + h_t1.dot(self.Whi) + c_t1.dot(self.Wci) + self.bi)
74+
f_t = T.nnet.sigmoid(x_t.dot(self.Wxf) + h_t1.dot(self.Whf) + c_t1.dot(self.Wcf) + self.bf)
75+
c_t = f_t * c_t1 + i_t * T.tanh(x_t.dot(self.Wxc) + h_t1.dot(self.Whc) + self.bc)
76+
o_t = T.nnet.sigmoid(x_t.dot(self.Wxo) + h_t1.dot(self.Who) + c_t.dot(self.Wco) + self.bo)
77+
h_t = o_t * T.tanh(c_t)
78+
return h_t, c_t
79+
80+
def output(self, x):
81+
# input X should be a matrix (2-D)
82+
# rows index time
83+
[h, c], _ = theano.scan(
84+
fn=self.recurrence,
85+
sequences=x,
86+
outputs_info=[self.h0, self.c0],
87+
n_steps=x.shape[0],
88+
)
89+
return h
90+
91+
92+
def train_wikipedia(we_file='lstm_word_embeddings.npy', w2i_file='lstm_wikipedia_word2idx.json'):
93+
# there are 32 files
94+
sentences, word2idx = get_wikipedia_data(n_files=100, n_vocab=2000)
95+
print "finished retrieving data"
96+
print "vocab size:", len(word2idx), "number of sentences:", len(sentences)
97+
rnn = RNN(50, [50], len(word2idx))
98+
# todo: next try increas LR
99+
rnn.fit(sentences, learning_rate=10e-6, epochs=10, show_fig=True, activation=T.nnet.relu, RecurrentUnit=LSTM, normalize=False)
100+
101+
np.save(we_file, rnn.We.get_value())
102+
with open(w2i_file, 'w') as f:
103+
json.dump(word2idx, f)
104+
105+
106+
if __name__ == '__main__':
107+
train_wikipedia()
108+
find_analogies('king', 'man', 'woman', 'lstm_word_embeddings.npy', 'lstm_wikipedia_word2idx.json')
109+
find_analogies('france', 'paris', 'london', 'lstm_word_embeddings.npy', 'lstm_wikipedia_word2idx.json')
110+
find_analogies('france', 'paris', 'rome', 'lstm_word_embeddings.npy', 'lstm_wikipedia_word2idx.json')
111+
find_analogies('paris', 'france', 'italy', 'lstm_word_embeddings.npy', 'lstm_wikipedia_word2idx.json')
112+

0 commit comments

Comments
 (0)