MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / fit

Method fit

nlp_class2/pos_rnn.py:32–132  ·  view source on GitHub ↗
(self, X, Y, learning_rate=1e-4, mu=0.99, epochs=30, show_fig=True, activation=T.nnet.relu, RecurrentUnit=GRU, normalize=False)

Source from the content-addressed store, hash-verified

30 self.K = K
31
32 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):
33 D = self.D
34 V = self.V
35 N = len(X)
36
37 We = init_weight(V, D)
38 self.hidden_layers = []
39 Mi = D
40 for Mo in self.hidden_layer_sizes:
41 ru = RecurrentUnit(Mi, Mo, activation)
42 self.hidden_layers.append(ru)
43 Mi = Mo
44
45 Wo = init_weight(Mi, self.K)
46 bo = np.zeros(self.K)
47
48 self.We = theano.shared(We)
49 self.Wo = theano.shared(Wo)
50 self.bo = theano.shared(bo)
51 self.params = [self.Wo, self.bo]
52 for ru in self.hidden_layers:
53 self.params += ru.params
54
55 thX = T.ivector('X')
56 thY = T.ivector('Y')
57
58 Z = self.We[thX]
59 for ru in self.hidden_layers:
60 Z = ru.output(Z)
61 py_x = T.nnet.softmax(Z.dot(self.Wo) + self.bo)
62
63 testf = theano.function(
64 inputs=[thX],
65 outputs=py_x,
66 )
67 testout = testf(X[0])
68 print("py_x.shape:", testout.shape)
69
70 prediction = T.argmax(py_x, axis=1)
71
72 cost = -T.mean(T.log(py_x[T.arange(thY.shape[0]), thY]))
73 grads = T.grad(cost, self.params)
74 dparams = [theano.shared(p.get_value()*0) for p in self.params]
75
76 dWe = theano.shared(self.We.get_value()*0)
77 gWe = T.grad(cost, self.We)
78 dWe_update = mu*dWe - learning_rate*gWe
79 We_update = self.We + dWe_update
80 if normalize:
81 We_update /= We_update.norm(2)
82
83 updates = [
84 (p, p + mu*dp - learning_rate*g) for p, dp, g in zip(self.params, dparams, grads)
85 ] + [
86 (dp, mu*dp - learning_rate*g) for dp, g in zip(dparams, grads)
87 ] + [
88 (self.We, We_update), (dWe, dWe_update)
89 ]

Callers 15

mainFunction · 0.95
mainFunction · 0.95
pos_ner_keras.pyFile · 0.45
bilstm_mnist.pyFile · 0.45
cnn_toxic.pyFile · 0.45
wseq2seq.pyFile · 0.45
attention.pyFile · 0.45
poetry.pyFile · 0.45
memory_network.pyFile · 0.45
lstm_toxic.pyFile · 0.45

Calls 3

init_weightFunction · 0.90
outputMethod · 0.45
gradMethod · 0.45

Tested by

no test coverage detected