def test_lookup_weighting(self): graph = tf.Graph() with graph.as_default(): with tf.Session(graph=graph) as session: mem = Memory(4, 5, 2, 2) initial_mem = np.random.uniform(0, 1, (2, 4, 5)).astype(np.float32) keys = np.random.uniform(0, 1, (2, 5, 2)).astype(np.float32) strengths = np.random.uniform(0, 1, (2, 2)).astype(np.float32) norm_mem = initial_mem / np.sqrt( np.sum(initial_mem**2, axis=2, keepdims=True)) norm_keys = keys / np.sqrt( np.sum(keys**2, axis=1, keepdims=True)) sim = np.matmul(norm_mem, norm_keys) sim = sim * strengths[:, np.newaxis, :] predicted_wieghts = np.exp(sim) / np.sum( np.exp(sim), axis=1, keepdims=True) mem.memory_matrix = tf.convert_to_tensor(initial_mem) op = mem.get_lookup_weighting(keys, strengths) c = session.run(op) self.assertEqual(c.shape, (2, 4, 2)) self.assertTrue(np.allclose(c, predicted_wieghts))
def test_update_memory(self): graph = tf.Graph() with graph.as_default(): with tf.Session(graph=graph) as session: mem = Memory(4, 5, 2, 2) write_weighting = random_softmax((2, 4), axis=1) write_vector = np.random.uniform(0, 1, (2, 5)).astype(np.float32) erase_vector = np.random.uniform(0, 1, (2, 5)).astype(np.float32) memory_matrix = np.random.uniform(-1, 1, (2, 4, 5)).astype(np.float32) ww = write_weighting[:, :, np.newaxis] v, e = write_vector[:, np.newaxis, :], erase_vector[:, np.newaxis, :] predicted = memory_matrix * (1 - np.matmul(ww, e)) + np.matmul( ww, v) mem.memory_matrix = tf.convert_to_tensor(memory_matrix) op = mem.update_memory(write_weighting, write_vector, erase_vector) M = session.run(op) #updated_memory = session.run(mem.memory_matrix.value()) self.assertEqual(M.shape, (2, 4, 5)) self.assertTrue(np.allclose(M, predicted))