(self, trees, lr=1e-2, mu=0.9, reg=1e-1, epochs=5)
| 75 | self.params = [self.We, self.W11, self.W22, self.W12, self.W1, self.W2, self.Wo] |
| 76 | |
| 77 | def fit(self, trees, lr=1e-2, mu=0.9, reg=1e-1, epochs=5): |
| 78 | train_ops = [] |
| 79 | costs = [] |
| 80 | predictions = [] |
| 81 | all_labels = [] |
| 82 | i = 0 |
| 83 | N = len(trees) |
| 84 | print("Compiling ops") |
| 85 | for t in trees: |
| 86 | i += 1 |
| 87 | sys.stdout.write("%d/%d\r" % (i, N)) |
| 88 | sys.stdout.flush() |
| 89 | logits = self.get_output(t) |
| 90 | labels = get_labels(t) |
| 91 | all_labels.append(labels) |
| 92 | |
| 93 | cost = self.get_cost(logits, labels, reg) |
| 94 | costs.append(cost) |
| 95 | |
| 96 | prediction = tf.argmax(logits, 1) |
| 97 | predictions.append(prediction) |
| 98 | |
| 99 | train_op = tf.train.MomentumOptimizer(lr, mu).minimize(cost) |
| 100 | train_ops.append(train_op) |
| 101 | |
| 102 | # save for later so we don't have to recompile if we call score |
| 103 | self.predictions = predictions |
| 104 | self.all_labels = all_labels |
| 105 | |
| 106 | self.saver = tf.train.Saver() |
| 107 | init = tf.initialize_all_variables() |
| 108 | actual_costs = [] |
| 109 | per_epoch_costs = [] |
| 110 | correct_rates = [] |
| 111 | with tf.Session() as session: |
| 112 | session.run(init) |
| 113 | |
| 114 | for i in range(epochs): |
| 115 | train_ops, costs, predictions, all_labels = shuffle(train_ops, costs, predictions, all_labels) |
| 116 | epoch_cost = 0 |
| 117 | n_correct = 0 |
| 118 | n_total = 0 |
| 119 | j = 0 |
| 120 | N = len(train_ops) |
| 121 | for train_op, cost, prediction, labels in zip(train_ops, costs, predictions, all_labels): |
| 122 | _, c, p = session.run([train_op, cost, prediction]) |
| 123 | epoch_cost += c |
| 124 | actual_costs.append(c) |
| 125 | n_correct += np.sum(p == labels) |
| 126 | n_total += len(labels) |
| 127 | |
| 128 | j += 1 |
| 129 | if j % 10 == 0: |
| 130 | sys.stdout.write("j: %d, N: %d, c: %f\r" % (j, N, c)) |
| 131 | sys.stdout.flush() |
| 132 | |
| 133 | if np.isnan(c): |
| 134 | exit() |
no test coverage detected