(self, X, Y, batch_sz=20, learning_rate=0.1, mu=0.9, activation=tf.nn.sigmoid, epochs=100, show_fig=False)
| 43 | |
| 44 | |
| 45 | def fit(self, X, Y, batch_sz=20, learning_rate=0.1, mu=0.9, activation=tf.nn.sigmoid, epochs=100, show_fig=False): |
| 46 | N, T, D = X.shape # X is of size N x T(n) x D |
| 47 | K = len(set(Y.flatten())) |
| 48 | M = self.M |
| 49 | self.f = activation |
| 50 | |
| 51 | # initial weights |
| 52 | # note: Wx, Wh, bh are all part of the RNN unit and will be created |
| 53 | # by BasicRNNCell |
| 54 | Wo = init_weight(M, K).astype(np.float32) |
| 55 | bo = np.zeros(K, dtype=np.float32) |
| 56 | |
| 57 | # make them tf variables |
| 58 | self.Wo = tf.Variable(Wo) |
| 59 | self.bo = tf.Variable(bo) |
| 60 | |
| 61 | # tf Graph input |
| 62 | tfX = tf.placeholder(tf.float32, shape=(batch_sz, T, D), name='inputs') |
| 63 | tfY = tf.placeholder(tf.int64, shape=(batch_sz, T), name='targets') |
| 64 | |
| 65 | # turn tfX into a sequence, e.g. T tensors all of size (batch_sz, D) |
| 66 | sequenceX = x2sequence(tfX, T, D, batch_sz) |
| 67 | |
| 68 | # create the simple rnn unit |
| 69 | rnn_unit = BasicRNNCell(num_units=self.M, activation=self.f) |
| 70 | |
| 71 | # Get rnn cell output |
| 72 | # outputs, states = rnn_module.rnn(rnn_unit, sequenceX, dtype=tf.float32) |
| 73 | outputs, states = get_rnn_output(rnn_unit, sequenceX, dtype=tf.float32) |
| 74 | |
| 75 | # outputs are now of size (T, batch_sz, M) |
| 76 | # so make it (batch_sz, T, M) |
| 77 | outputs = tf.transpose(outputs, (1, 0, 2)) |
| 78 | outputs = tf.reshape(outputs, (T*batch_sz, M)) |
| 79 | |
| 80 | # Linear activation, using rnn inner loop last output |
| 81 | logits = tf.matmul(outputs, self.Wo) + self.bo |
| 82 | predict_op = tf.argmax(logits, 1) |
| 83 | targets = tf.reshape(tfY, (T*batch_sz,)) |
| 84 | |
| 85 | cost_op = tf.reduce_mean( |
| 86 | tf.nn.sparse_softmax_cross_entropy_with_logits( |
| 87 | logits=logits, |
| 88 | labels=targets |
| 89 | ) |
| 90 | ) |
| 91 | train_op = tf.train.MomentumOptimizer(learning_rate, momentum=mu).minimize(cost_op) |
| 92 | |
| 93 | costs = [] |
| 94 | n_batches = N // batch_sz |
| 95 | |
| 96 | init = tf.global_variables_initializer() |
| 97 | with tf.Session() as session: |
| 98 | session.run(init) |
| 99 | for i in range(epochs): |
| 100 | X, Y = shuffle(X, Y) |
| 101 | n_correct = 0 |
| 102 | cost = 0 |
no test coverage detected