|
| 1 | +import theano |
| 2 | +import theano.tensor as T |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import json |
| 6 | + |
| 7 | +from sklearn.utils import shuffle |
| 8 | +from util import init_weight, get_wikipedia_data |
| 9 | + |
| 10 | + |
| 11 | +class GRU: |
| 12 | + def __init__(self, Mi, Mo, activation): |
| 13 | + self.Mi = Mi |
| 14 | + self.Mo = Mo |
| 15 | + self.f = activation |
| 16 | + |
| 17 | + # numpy init |
| 18 | + Wxr = init_weight(Mi, Mo) |
| 19 | + Whr = init_weight(Mo, Mo) |
| 20 | + br = np.zeros(Mo) |
| 21 | + Wxz = init_weight(Mi, Mo) |
| 22 | + Whz = init_weight(Mo, Mo) |
| 23 | + bz = np.zeros(Mo) |
| 24 | + Wxh = init_weight(Mi, Mo) |
| 25 | + Whh = init_weight(Mo, Mo) |
| 26 | + bh = np.zeros(Mo) |
| 27 | + h0 = np.zeros(Mo) |
| 28 | + |
| 29 | + # theano vars |
| 30 | + self.Wxr = theano.shared(Wxr) |
| 31 | + self.Whr = theano.shared(Whr) |
| 32 | + self.br = theano.shared(br) |
| 33 | + self.Wxz = theano.shared(Wxz) |
| 34 | + self.Whz = theano.shared(Whz) |
| 35 | + self.bz = theano.shared(bz) |
| 36 | + self.Wxh = theano.shared(Wxh) |
| 37 | + self.Whh = theano.shared(Whh) |
| 38 | + self.bh = theano.shared(bh) |
| 39 | + self.h0 = theano.shared(h0) |
| 40 | + self.params = [self.Wxr, self.Whr, self.br, self.Wxz, self.Whz, self.bz, self.Wxh, self.Whh, self.bh, self.h0] |
| 41 | + |
| 42 | + def recurrence(self, x_t, h_t1): |
| 43 | + r = T.nnet.sigmoid(x_t.dot(self.Wxr) + h_t1.dot(self.Whr) + self.br) |
| 44 | + z = T.nnet.sigmoid(x_t.dot(self.Wxz) + h_t1.dot(self.Whz) + self.bz) |
| 45 | + hhat = self.f(x_t.dot(self.Wxh) + (r * h_t1).dot(self.Whh) + self.bh) |
| 46 | + h = (1 - z) * h_t1 + z * hhat |
| 47 | + return h |
| 48 | + |
| 49 | + def output(self, x): |
| 50 | + # input X should be a matrix (2-D) |
| 51 | + # rows index time |
| 52 | + h, _ = theano.scan( |
| 53 | + fn=self.recurrence, |
| 54 | + sequences=x, |
| 55 | + outputs_info=[self.h0], |
| 56 | + n_steps=x.shape[0], |
| 57 | + ) |
| 58 | + return h |
| 59 | + |
| 60 | +class RNN: |
| 61 | + def __init__(self, D, hidden_layer_sizes, V): |
| 62 | + self.hidden_layer_sizes = hidden_layer_sizes |
| 63 | + self.D = D |
| 64 | + self.V = V |
| 65 | + |
| 66 | + def fit(self, X, learning_rate=10e-5, mu=0.99, epochs=10, show_fig=True, activation=T.nnet.relu, RecurrentUnit=GRU): |
| 67 | + D = self.D |
| 68 | + V = self.V |
| 69 | + N = len(X) |
| 70 | + |
| 71 | + We = init_weight(V, D) |
| 72 | + self.hidden_layers = [] |
| 73 | + Mi = D |
| 74 | + for Mo in self.hidden_layer_sizes: |
| 75 | + ru = RecurrentUnit(Mi, Mo, activation) |
| 76 | + self.hidden_layers.append(ru) |
| 77 | + Mi = Mo |
| 78 | + |
| 79 | + Wo = init_weight(Mi, V) |
| 80 | + bo = np.zeros(V) |
| 81 | + |
| 82 | + self.We = theano.shared(We) |
| 83 | + self.Wo = theano.shared(Wo) |
| 84 | + self.bo = theano.shared(bo) |
| 85 | + self.params = [self.Wo, self.bo] |
| 86 | + for ru in self.hidden_layers: |
| 87 | + self.params += ru.params |
| 88 | + |
| 89 | + thX = T.ivector('X') |
| 90 | + thY = T.ivector('Y') |
| 91 | + |
| 92 | + Z = self.We[thX] |
| 93 | + for ru in self.hidden_layers: |
| 94 | + Z = ru.output(Z) |
| 95 | + py_x = T.nnet.softmax(Z.dot(self.Wo) + self.bo) |
| 96 | + |
| 97 | + prediction = T.argmax(py_x, axis=1) |
| 98 | + # let's return py_x too so we can draw a sample instead |
| 99 | + self.predict_op = theano.function( |
| 100 | + inputs=[thX], |
| 101 | + outputs=[py_x, prediction], |
| 102 | + allow_input_downcast=True, |
| 103 | + ) |
| 104 | + |
| 105 | + cost = -T.mean(T.log(py_x[T.arange(thY.shape[0]), thY])) |
| 106 | + grads = T.grad(cost, self.params) |
| 107 | + dparams = [theano.shared(p.get_value()*0) for p in self.params] |
| 108 | + |
| 109 | + dWe = theano.shared(self.We.get_value()*0) |
| 110 | + gWe = T.grad(cost, self.We) |
| 111 | + dWe_update = mu*dWe - learning_rate*gWe |
| 112 | + We_update = self.We + dWe_update |
| 113 | + We_update /= We_update.sum(axis=1).dimshuffle(0, 'x') |
| 114 | + |
| 115 | + updates = [ |
| 116 | + (p, p + mu*dp - learning_rate*g) for p, dp, g in zip(self.params, dparams, grads) |
| 117 | + ] + [ |
| 118 | + (dp, mu*dp - learning_rate*g) for dp, g in zip(dparams, grads) |
| 119 | + ] + [ |
| 120 | + (self.We, We_update), (dWe, dWe_update) |
| 121 | + ] |
| 122 | + |
| 123 | + self.train_op = theano.function( |
| 124 | + inputs=[thX, thY], |
| 125 | + outputs=[cost, prediction], |
| 126 | + updates=updates |
| 127 | + ) |
| 128 | + |
| 129 | + costs = [] |
| 130 | + for i in xrange(epochs): |
| 131 | + X = shuffle(X) |
| 132 | + n_correct = 0 |
| 133 | + n_total = 0 |
| 134 | + cost = 0 |
| 135 | + for j in xrange(N): |
| 136 | + if np.random.random() < 0.01 or len(X[j]) <= 1: |
| 137 | + input_sequence = [0] + X[j] |
| 138 | + output_sequence = X[j] + [1] |
| 139 | + else: |
| 140 | + input_sequence = [0] + X[j][:-1] |
| 141 | + output_sequence = X[j] |
| 142 | + n_total += len(output_sequence) |
| 143 | + |
| 144 | + # test: |
| 145 | + |
| 146 | + try: |
| 147 | + # we set 0 to start and 1 to end |
| 148 | + c, p = self.train_op(input_sequence, output_sequence) |
| 149 | + except Exception as e: |
| 150 | + PYX, pred = self.predict_op(input_sequence) |
| 151 | + print "input_sequence len:", len(input_sequence) |
| 152 | + print "PYX.shape:",PYX.shape |
| 153 | + print "pred.shape:", pred.shape |
| 154 | + raise e |
| 155 | + # print "p:", p |
| 156 | + cost += c |
| 157 | + # print "j:", j, "c:", c/len(X[j]+1) |
| 158 | + for pj, xj in zip(p, output_sequence): |
| 159 | + if pj == xj: |
| 160 | + n_correct += 1 |
| 161 | + 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) |
| 164 | + costs.append(cost) |
| 165 | + |
| 166 | + if show_fig: |
| 167 | + plt.plot(costs) |
| 168 | + plt.show() |
| 169 | + |
| 170 | + |
| 171 | +def train_wikipedia(): |
| 172 | + # there are 32 files |
| 173 | + sentences, word2idx = get_wikipedia_data(n_files=32, n_vocab=2000) |
| 174 | + print "finished retrieving data" |
| 175 | + 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) |
| 178 | + |
| 179 | + np.save('word_embeddings.npy', rnn.We.get_value()) |
| 180 | + with open('wikipedia_word2idx.json', 'w') as f: |
| 181 | + json.dump(word2idx, f) |
| 182 | + |
| 183 | +def generate_wikipedia(): |
| 184 | + pass |
| 185 | + |
| 186 | +def find_analogies(w1, w2, w3): |
| 187 | + We = np.load('word_embeddings.npy') |
| 188 | + with open('wikipedia_word2idx.json') as f: |
| 189 | + word2idx = json.load(f) |
| 190 | + |
| 191 | + king = We[word2idx[w1]] |
| 192 | + man = We[word2idx[w2]] |
| 193 | + woman = We[word2idx[w3]] |
| 194 | + v0 = king - man + woman |
| 195 | + |
| 196 | + def dist1(a, b): |
| 197 | + return np.linalg.norm(a - b) |
| 198 | + def dist2(a, b): |
| 199 | + return 1 - a.dot(b) / (np.linalg.norm(a) * np.linalg.norm(b)) |
| 200 | + |
| 201 | + for dist, name in [(dist1, 'Euclidean'), (dist2, 'cosine')]: |
| 202 | + min_dist = float('inf') |
| 203 | + best_word = ''; |
| 204 | + for word, idx in word2idx.iteritems(): |
| 205 | + if word not in (w1, w2, w3): |
| 206 | + v1 = We[idx] |
| 207 | + d = dist(v0, v1) |
| 208 | + if d < min_dist: |
| 209 | + min_dist = d |
| 210 | + best_word = word |
| 211 | + print "closest match by", name, "distance:", best_word |
| 212 | + print w1, "-", w2, "=", best_word, "-", w3 |
| 213 | + |
| 214 | +if __name__ == '__main__': |
| 215 | + train_wikipedia() |
| 216 | + find_analogies('king', 'man', 'woman') |
| 217 | + find_analogies('france', 'paris', 'london') |
| 218 | + find_analogies('france', 'paris', 'rome') |
| 219 | + find_analogies('paris', 'france', 'italy') |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
0 commit comments