def testMapSingleCollection(self): ab_tuple = collections.namedtuple("ab_tuple", "a, b") nt = ab_tuple(a=("something", "something_else"), b="yet another thing") rev_nt = nest.map(lambda x: x[::-1], nt) # Check the output is the correct structure, and all strings are reversed. nest.assert_same_structure(nt, rev_nt) self.assertEqual(nt.a[0][::-1], rev_nt.a[0]) self.assertEqual(nt.a[1][::-1], rev_nt.a[1]) self.assertEqual(nt.b[::-1], rev_nt.b)
def testMapSingleCollection(self): ab_tuple = collections.namedtuple("ab_tuple", "a, b") nt = ab_tuple(a=("something", "something_else"), b="yet another thing") rev_nt = nest.map(lambda x: x[::-1], nt) # Check the output is the correct structure, and all strings are reversed. nest.assert_same_structure(nt, rev_nt) self.assertEqual(nt.a[0][::-1], rev_nt.a[0]) self.assertEqual(nt.a[1][::-1], rev_nt.a[1]) self.assertEqual(nt.b[::-1], rev_nt.b)
def testMapOverTwoTuples(self): inp_a = (tf.placeholder(tf.float32, shape=[3, 4]), tf.placeholder(tf.float32, shape=[3, 7])) inp_b = (tf.placeholder(tf.float32, shape=[3, 4]), tf.placeholder(tf.float32, shape=[3, 7])) output = nest.map(lambda x1, x2: x1 + x2, inp_a, inp_b) nest.assert_same_structure(output, inp_a) self.assertShapeEqual(np.zeros((3, 4)), output[0]) self.assertShapeEqual(np.zeros((3, 7)), output[1]) feed_dict = { inp_a: (np.random.randn(3, 4), np.random.randn(3, 7)), inp_b: (np.random.randn(3, 4), np.random.randn(3, 7)) } with self.test_session() as sess: output_np = sess.run(output, feed_dict=feed_dict) self.assertAllClose(output_np[0], feed_dict[inp_a][0] + feed_dict[inp_b][0]) self.assertAllClose(output_np[1], feed_dict[inp_a][1] + feed_dict[inp_b][1])
def testMapOverTwoTuples(self): inp_a = (tf.placeholder(tf.float32, shape=[3, 4]), tf.placeholder(tf.float32, shape=[3, 7])) inp_b = (tf.placeholder(tf.float32, shape=[3, 4]), tf.placeholder(tf.float32, shape=[3, 7])) output = nest.map(lambda x1, x2: x1 + x2, inp_a, inp_b) nest.assert_same_structure(output, inp_a) self.assertShapeEqual(np.zeros((3, 4)), output[0]) self.assertShapeEqual(np.zeros((3, 7)), output[1]) feed_dict = { inp_a: (np.random.randn(3, 4), np.random.randn(3, 7)), inp_b: (np.random.randn(3, 4), np.random.randn(3, 7)) } with self.test_session() as sess: output_np = sess.run(output, feed_dict=feed_dict) self.assertAllClose(output_np[0], feed_dict[inp_a][0] + feed_dict[inp_b][0]) self.assertAllClose(output_np[1], feed_dict[inp_a][1] + feed_dict[inp_b][1])