def test_update_usage_vector(self): graph = tf.Graph() with graph.as_default(): with tf.Session(graph=graph) as session: mem = Memory(4, 5, 2, 2) free_gates = np.random.uniform(0, 1, (2, 2)).astype(np.float32) init_read_weightings = random_softmax((2, 4, 2), axis=1) init_write_weightings = random_softmax((2, 4), axis=1) init_usage = np.random.uniform(0, 1, (2, 4)).astype(np.float32) psi = np.product( 1 - init_read_weightings * free_gates[:, np.newaxis, :], axis=2) predicted_usage = (init_usage + init_write_weightings - init_usage * init_write_weightings) * psi mem.read_weightings = tf.convert_to_tensor( init_read_weightings) mem.write_weighting = tf.convert_to_tensor( init_write_weightings) mem.usage_vector = tf.convert_to_tensor(init_usage) op = mem.update_usage_vector(free_gates) u = session.run(op) #updated_usage = session.run(mem.usage_vector.value()) self.assertEqual(u.shape, (2, 4)) self.assertTrue(np.array_equal(u, predicted_usage))
def test_get_directional_weightings(self): graph = tf.Graph() with graph.as_default(): with tf.Session(graph=graph) as session: mem = Memory(4, 5, 2, 2) _link_matrix = np.random.uniform(0, 1, (2, 4, 4)).astype(np.float32) _read_weightings = random_softmax((2, 4, 2), axis=1) predicted_forward = np.matmul(_link_matrix, _read_weightings) predicted_backward = np.matmul( np.transpose(_link_matrix, [0, 2, 1]), _read_weightings) mem.read_weightings = tf.convert_to_tensor(_read_weightings) fop, bop = mem.get_directional_weightings(_link_matrix) forward_weighting, backward_weighting = session.run([fop, bop]) self.assertTrue( np.allclose(forward_weighting, predicted_forward)) self.assertTrue( np.allclose(backward_weighting, predicted_backward))