| 23 | |
| 24 | |
| 25 | class RecursiveNN: |
| 26 | def __init__(self, V, D, K, activation=tf.tanh): |
| 27 | self.V = V |
| 28 | self.D = D |
| 29 | self.K = K |
| 30 | self.f = activation |
| 31 | |
| 32 | def fit(self, trees, test_trees, reg=1e-3, epochs=8, train_inner_nodes=False): |
| 33 | D = self.D |
| 34 | V = self.V |
| 35 | K = self.K |
| 36 | N = len(trees) |
| 37 | |
| 38 | We = init_weight(V, D) |
| 39 | W11 = np.random.randn(D, D, D) / np.sqrt(3*D) |
| 40 | W22 = np.random.randn(D, D, D) / np.sqrt(3*D) |
| 41 | W12 = np.random.randn(D, D, D) / np.sqrt(3*D) |
| 42 | W1 = init_weight(D, D) |
| 43 | W2 = init_weight(D, D) |
| 44 | bh = np.zeros(D) |
| 45 | Wo = init_weight(D, K) |
| 46 | bo = np.zeros(K) |
| 47 | |
| 48 | self.We = tf.Variable(We.astype(np.float32)) |
| 49 | self.W11 = tf.Variable(W11.astype(np.float32)) |
| 50 | self.W22 = tf.Variable(W22.astype(np.float32)) |
| 51 | self.W12 = tf.Variable(W12.astype(np.float32)) |
| 52 | self.W1 = tf.Variable(W1.astype(np.float32)) |
| 53 | self.W2 = tf.Variable(W2.astype(np.float32)) |
| 54 | self.bh = tf.Variable(bh.astype(np.float32)) |
| 55 | self.Wo = tf.Variable(Wo.astype(np.float32)) |
| 56 | self.bo = tf.Variable(bo.astype(np.float32)) |
| 57 | self.weights = [self.We, self.W11, self.W22, self.W12, self.W1, self.W2, self.Wo] |
| 58 | |
| 59 | |
| 60 | words = tf.compat.v1.placeholder(tf.int32, shape=(None,), name='words') |
| 61 | left_children = tf.compat.v1.placeholder(tf.int32, shape=(None,), name='left_children') |
| 62 | right_children = tf.compat.v1.placeholder(tf.int32, shape=(None,), name='right_children') |
| 63 | labels = tf.compat.v1.placeholder(tf.int32, shape=(None,), name='labels') |
| 64 | |
| 65 | # save for later |
| 66 | self.words = words |
| 67 | self.left = left_children |
| 68 | self.right = right_children |
| 69 | self.labels = labels |
| 70 | |
| 71 | def dot1(a, B): |
| 72 | return tf.tensordot(a, B, axes=[[0], [1]]) |
| 73 | |
| 74 | def dot2(B, a): |
| 75 | return tf.tensordot(B, a, axes=[[1], [0]]) |
| 76 | |
| 77 | def recursive_net_transform(hiddens, n): |
| 78 | h_left = hiddens.read(left_children[n]) |
| 79 | h_right = hiddens.read(right_children[n]) |
| 80 | return self.f( |
| 81 | dot1(h_left, dot2(self.W11, h_left)) + |
| 82 | dot1(h_right, dot2(self.W22, h_right)) + |