Beispiel #1
0
 def test_loom_type_shapes_2_distinct_tags(self):
     shape1 = loom.TypeShape('float32', (20, ), 'alice')
     shape2 = loom.TypeShape('float32', (20, ), 'bob')
     ops = {
         'add': BinaryLoomOp(shape1, tf.add),
         'mul': BinaryLoomOp(shape2, tf.multiply)
     }
     the_loom = loom.Loom(named_ops=ops)
     self.assertEqual([shape1, shape2], the_loom.type_shapes)
Beispiel #2
0
 def test_loom_type_shapes2(self):
     shape1 = loom.TypeShape('float32', (20, ))
     shape2 = loom.TypeShape('float32', (30, ))
     ops = {
         'add': BinaryLoomOp(shape1, tf.add),
         'mul': BinaryLoomOp(shape2, tf.multiply)
     }
     the_loom = loom.Loom(named_ops=ops)
     self.assertEqual([shape1, shape2], the_loom.type_shapes)
Beispiel #3
0
 def test_loom_type_shapes_output_shapes(self):
     shape1 = loom.TypeShape('float32', (20, ))
     shape2 = loom.TypeShape('float32', (30, ))
     ops = {
         'add': BinaryLoomOp(shape1, tf.add),
         'mul': BinaryLoomOp(shape2, tf.multiply)
     }
     the_loom = loom.Loom(named_ops=ops)
     self.assertEqual([None, 20],
                      the_loom.output_tensor(shape1).get_shape().as_list())
     self.assertEqual([None, 30],
                      the_loom.output_tensor(shape2).get_shape().as_list())
Beispiel #4
0
 def test_loom_type_shapes2_get_type_shape(self):
     shape1 = loom.TypeShape('float32', (20, ))
     shape2 = loom.TypeShape('float32', (30, ))
     ops = {
         'add': BinaryLoomOp(shape1, tf.add),
         'mul': BinaryLoomOp(shape2, tf.multiply)
     }
     the_loom = loom.Loom(named_ops=ops)
     weaver = the_loom.make_weaver()
     self.assertEqual(
         weaver.get_type_shape(weaver(np.zeros((20, ), 'float32'))), shape1)
     self.assertEqual(
         weaver.get_type_shape(weaver(np.zeros((30, ), 'float32'))), shape2)
Beispiel #5
0
 def test_loom_build_graph(self):
     shape = loom.TypeShape('int64', (500, ))
     ops = {
         'add': BinaryLoomOp(shape, tf.add),
         'mul': BinaryLoomOp(shape, tf.multiply)
     }
     _ = loom.Loom(named_ops=ops)
Beispiel #6
0
    def test_gradient_with_direct_feed_dict(self):
        x_var = tf.Variable(tf.zeros([3], dtype='float64'), name='x')
        shape = loom.TypeShape('float64', (3, ))
        ops = {
            'add': BinaryLoomOp(shape, tf.add),
            'mul': BinaryLoomOp(shape, tf.multiply)
        }
        the_loom = loom.Loom(named_tensors={'x': x_var},
                             named_ops=ops,
                             direct_feed_dict=True)

        output_tensor = the_loom.output_tensor(shape)
        output = tf.reduce_sum(output_tensor)
        gradient = tf.gradients(output, [x_var])[0]
        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())

            weaver = the_loom.make_weaver()
            m = weaver(np.array([1, 2, 3], dtype='float64'))
            b = weaver(np.array([47, 9, -1], dtype='float64'))
            mx = weaver.mul(m, weaver.x)
            mx_plus_b = weaver.add(mx, b)
            result = gradient.eval(
                feed_dict=weaver.build_feed_dict([mx_plus_b]))
        self.assertTrue((result == np.array([1.0, 2.0, 3.0],
                                            dtype='float64')).all())
Beispiel #7
0
 def test_path_through_loom_op(self):
     shape = loom.TypeShape('int64', (3, ))
     op = loom.PassThroughLoomOp(shape)
     self.assertEqual([shape], op.input_type_shapes)
     self.assertEqual([shape], op.output_type_shapes)
     test_obj = tf.constant([1, 2, 3], dtype=shape.dtype)
     self.assertEqual([test_obj], op.instantiate_batch([test_obj]))
Beispiel #8
0
    def test_two_ops_network_with_merge(self):
        shape = loom.TypeShape('int64', (3, ))
        named_tensors = {'c1': tf.constant([1, 2, 3], dtype='int64')}
        ops = {
            'add': BinaryLoomOp(shape, tf.add),
            'mul': BinaryLoomOp(shape, tf.multiply)
        }
        the_loom = loom.Loom(named_tensors=named_tensors, named_ops=ops)
        output_tensor = the_loom.output_tensor(shape)
        with self.test_session():
            weaver1 = the_loom.make_weaver()
            c1 = weaver1.c1
            c2 = weaver1(np.array([2, 4, 6], dtype='int64'))
            c3 = weaver1(np.array([3, 6, 9], dtype='int64'))
            sum_2_3 = weaver1.add(c2, c3)
            sum_12_13 = weaver1.mul(c1, sum_2_3)
            weaver1.add_output(sum_12_13)

            weaver2 = the_loom.make_weaver()
            c1 = weaver2.c1
            c2 = weaver2(np.array([2, 4, 6], dtype='int64'))
            c3 = weaver2(np.array([3, 6, 9], dtype='int64'))
            sum_1_3 = weaver2.add(c1, c3)
            sum_21_23 = weaver2.mul(c2, sum_1_3)
            weaver2.add_output(sum_21_23)

            weaver_merged = [weaver1.serialize(), weaver2.serialize()]

            result = output_tensor.eval(
                feed_dict={the_loom.input_tensor: weaver_merged})
        self.assertTrue((result == np.array([[5, 20, 45], [8, 32, 72]],
                                            dtype='int64')).all())
Beispiel #9
0
 def test_type_shape(self):
     self.assertEqual(loom.TypeShape('float32', ()).dtype, 'float32')
     self.assertRaises(TypeError, loom.TypeShape, np.float32, ())
     self.assertRaises(TypeError, loom.TypeShape, object, ())
     self.assertRaises(TypeError, loom.TypeShape, 'foobar', ())
     self.assertRaises(TypeError, loom.TypeShape, 'float32', (3.2, ))
     self.assertRaises(TypeError, loom.TypeShape, 'float32', (-4, ))
Beispiel #10
0
 def test_loom_type_shapes(self):
     shape = loom.TypeShape('int64', (500, ))
     ops = {
         'add': BinaryLoomOp(shape, tf.add),
         'mul': BinaryLoomOp(shape, tf.multiply)
     }
     the_loom = loom.Loom(named_ops=ops)
     self.assertEqual([shape], the_loom.type_shapes)
Beispiel #11
0
 def test_add_output_method(self):
     shape = loom.TypeShape('int64', (3, ))
     value = np.array([1, 2, 3], dtype='int64')
     ops = {'add': BinaryLoomOp(shape, tf.add)}
     the_loom = loom.Loom(named_ops=ops)
     output_tensor = the_loom.output_tensor(shape)
     with self.test_session():
         weaver = the_loom.make_weaver()
         weaver.add_output(weaver(value))
         result = output_tensor.eval(feed_dict=weaver.build_feed_dict())
     self.assertTrue((result[0] == value).all())
Beispiel #12
0
 def __init__(self, left_type_shape, right_type_shape):
     if left_type_shape.dtype != right_type_shape.dtype:
         raise TypeError('CatLoomOp needs TypeShapes with the same dtype.')
     if left_type_shape.shape[1:] != right_type_shape.shape[1:]:
         raise TypeError(
             'CatLoomOp needs TypeShapes with shapes agreeing on '
             'all but the first dimension.')
     dim0 = left_type_shape.shape[0] + right_type_shape.shape[0]
     cat_type_shape = loom.TypeShape(left_type_shape.dtype,
                                     (dim0, ) + left_type_shape.shape[1:])
     super(CatLoomOp, self).__init__([left_type_shape, right_type_shape],
                                     [cat_type_shape])
Beispiel #13
0
 def test_constant_network_with_tags(self):
     shape1 = loom.TypeShape('int64', (3, ), 'alpha')
     shape2 = loom.TypeShape('int64', (3, ), 'beta')
     value1 = np.array([1, 2, 3], dtype='int64')
     value2 = np.array([4, 5, 6], dtype='int64')
     ops = {
         'add1': BinaryLoomOp(shape1, tf.add),
         'add2': BinaryLoomOp(shape2, tf.add)
     }
     the_loom = loom.Loom(named_ops=ops)
     output_tensor1 = the_loom.output_tensor(shape1)
     output_tensor2 = the_loom.output_tensor(shape2)
     with self.test_session():
         weaver = the_loom.make_weaver()
         c1 = weaver(value1, tag='alpha')
         c2 = weaver(value2, tag='beta')
         result1 = output_tensor1.eval(
             feed_dict=weaver.build_feed_dict([c2, c1]))
         result2 = output_tensor2.eval(
             feed_dict=weaver.build_feed_dict([c2, c1]))
     self.assertTrue((result1[0] == value1).all())
     self.assertTrue((result2[0] == value2).all())
Beispiel #14
0
 def test_simple_sum_network_with_max_depth(self):
     shape = loom.TypeShape('int64', (3, ))
     ops = {'add': BinaryLoomOp(shape, tf.add)}
     the_loom = loom.Loom(max_depth=2, named_ops=ops)
     output_tensor = the_loom.output_tensor(shape)
     with self.test_session():
         weaver = the_loom.make_weaver()
         c1 = weaver(np.array([1, 1, 8], dtype='int64'))
         c2 = weaver(np.array([2, 3, 9], dtype='int64'))
         sum_result = weaver.add(c1, c2)
         result = output_tensor.eval(
             feed_dict=weaver.build_feed_dict([sum_result]))
     self.assertTrue((result == np.array([[3, 4, 17]],
                                         dtype='int64')).all())
Beispiel #15
0
    def test_two_shapes_network(self):
        shape3 = loom.TypeShape('int64', (3, ))
        shape6 = loom.TypeShape('int64', (6, ))
        cat_op = CatLoomOp(shape3, shape3)
        self.assertEqual(cat_op.output_type_shapes[0], shape6)
        ops = {
            'add3': BinaryLoomOp(shape3, tf.add),
            'add6': BinaryLoomOp(shape6, tf.add),
            'cat': cat_op
        }

        the_loom = loom.Loom(named_ops=ops)
        output_tensor = the_loom.output_tensor(shape6)
        with self.test_session():
            weaver = the_loom.make_weaver()
            c1 = weaver(np.array([1, 2, 3], dtype='int64'))
            c2 = weaver(np.array([2, 4, 6], dtype='int64'))
            c3 = weaver(np.array([3, 6, 9], dtype='int64'))
            c4 = weaver(np.array([4, 8, 12], dtype='int64'))
            self.assertEqual(0, weaver.deepest)
            self.assertEqual(0, weaver.depth(c1))
            v_3 = weaver.add3(c1, c2)
            v_7 = weaver.add3(c3, c4)
            self.assertEqual(1, weaver.deepest)
            self.assertEqual(1, weaver.depth(v_7))
            v_3_v_7 = weaver.cat(v_3, v_7)
            self.assertEqual(2, weaver.deepest)
            self.assertEqual(2, weaver.depth(v_3_v_7))
            v_5 = weaver.add3(c1, c4)
            v_5_v_2 = weaver.cat(v_5, c2)  # Level skip.
            v_8_v_9 = weaver.add6(v_3_v_7, v_5_v_2)
            self.assertEqual(3, weaver.deepest)
            self.assertEqual(3, weaver.depth(v_8_v_9))
            result = output_tensor.eval(
                feed_dict=weaver.build_feed_dict([v_8_v_9]))
        self.assertTrue((result == np.array([[8, 16, 24, 9, 18, 27]],
                                            dtype='int64')).all())
Beispiel #16
0
 def test_simple_sum_network_with_batch_inputs(self):
     batch_vectors = tf.placeholder('int64')
     shape = loom.TypeShape('int64', (3, ))
     ops = {'add': BinaryLoomOp(shape, tf.add)}
     the_loom = loom.Loom(named_ops=ops,
                          batch_inputs={shape: batch_vectors})
     output_tensor = the_loom.output_tensor(shape)
     with self.test_session():
         weaver = the_loom.make_weaver()
         sum_result = weaver.add(weaver.batch_input(shape, 0),
                                 weaver.batch_input(shape, 1))
         weaver.add_output(sum_result)
         fd = {
             the_loom.input_tensor: weaver.serialize(),
             batch_vectors: np.array([[1, 2, 3], [4, 5, 6]], dtype='int64')
         }
         result = output_tensor.eval(feed_dict=fd)
     self.assertTrue((result == np.array([[5, 7, 9]], dtype='int64')).all())
Beispiel #17
0
 def test_two_layer_sum_network(self):
     shape = loom.TypeShape('int64', (3, ))
     ops = {'add': BinaryLoomOp(shape, tf.add)}
     the_loom = loom.Loom(named_ops=ops)
     output_tensor = the_loom.output_tensor(shape)
     with self.test_session():
         weaver = the_loom.make_weaver()
         c1 = weaver(np.array([1, 2, 3], dtype='int64'))
         c2 = weaver(np.array([2, 4, 6], dtype='int64'))
         c3 = weaver(np.array([3, 6, 9], dtype='int64'))
         c4 = weaver(np.array([4, 8, 12], dtype='int64'))
         sum_1_2 = weaver.add(c1, c2)
         sum_3_4 = weaver.add(c3, c4)
         sum_1_2_3_4 = weaver.add(sum_1_2, sum_3_4)
         result = output_tensor.eval(
             feed_dict=weaver.build_feed_dict([sum_1_2_3_4]))
     self.assertTrue((result == np.array([[10, 20, 30]],
                                         dtype='int64')).all())
Beispiel #18
0
 def test_two_ops_network(self):
     shape = loom.TypeShape('int64', (3, ))
     ops = {
         'add': BinaryLoomOp(shape, tf.add),
         'mul': BinaryLoomOp(shape, tf.multiply)
     }
     the_loom = loom.Loom(named_ops=ops)
     output_tensor = the_loom.output_tensor(shape)
     with self.test_session():
         weaver = the_loom.make_weaver()
         c1 = weaver(np.array([1, 2, 3], dtype='int64'))
         c2 = weaver(np.array([2, 4, 6], dtype='int64'))
         c3 = weaver(np.array([3, 6, 9], dtype='int64'))
         sum_2_3 = weaver.add(c2, c3)
         sum_12_13 = weaver.mul(c1, sum_2_3)
         result = output_tensor.eval(
             feed_dict=weaver.build_feed_dict([sum_12_13]))
     self.assertTrue((result == np.array([[5, 20, 45]],
                                         dtype='int64')).all())
Beispiel #19
0
    def test_three_layer_sum_network(self):
        shape = loom.TypeShape('int64', (3, ))
        ops = {'add': BinaryLoomOp(shape, tf.add)}
        the_loom = loom.Loom(named_ops=ops)
        output_tensor = the_loom.output_tensor(shape)

        with self.test_session():
            weaver = the_loom.make_weaver()
            vals = [
                weaver(np.array([0, 1, 1 << k], dtype='int64'))
                for k in range(8)
            ]
            for _ in xrange(3):
                vals = [weaver.add(*args) for args in group_values(vals, 2)]
            big_sum = vals[0]
            result = output_tensor.eval(
                feed_dict=weaver.build_feed_dict([big_sum]))
        self.assertTrue((result == np.array([[0, 8, 255]],
                                            dtype='int64')).all())
Beispiel #20
0
 def test_two_ops_network_tagged_named_tensorx(self):
     shape = loom.TypeShape('int64', (3, ), tag='x')
     ops = {
         'add': BinaryLoomOp(shape, tf.add),
         'mul': BinaryLoomOp(shape, tf.multiply)
     }
     named_tensors = {
         'c1': (tf.constant(np.array([1, 2, 3], dtype='int64')), 'x'),
         'c2': (tf.constant(np.array([2, 4, 6], dtype='int64')), 'x'),
         'c3': (tf.constant(np.array([3, 6, 9], dtype='int64')), 'x')
     }
     the_loom = loom.Loom(named_ops=ops, named_tensors=named_tensors)
     output_tensor = the_loom.output_tensor(shape)
     with self.test_session():
         weaver = the_loom.make_weaver()
         sum_2_3 = weaver.add(weaver.c2, weaver.c3)
         sum_12_13 = weaver.mul(weaver.c1, sum_2_3)
         result = output_tensor.eval(
             feed_dict=weaver.build_feed_dict([sum_12_13]))
     self.assertTrue((result == np.array([[5, 20, 45]],
                                         dtype='int64')).all())
Beispiel #21
0
def index_type():
    return loom.TypeShape('int32', ())
Beispiel #22
0
 def test_good_constant(self):
     shape = loom.TypeShape('int64', (3, ))
     ops = {'add': BinaryLoomOp(shape, tf.add)}
     the_loom = loom.Loom(named_ops=ops)
     weaver = the_loom.make_weaver()
     self.assertTrue(weaver(np.array([1, 2, 3], dtype='int64')) is not None)
Beispiel #23
0
def vector_type():
    return loom.TypeShape('float32', (FLAGS.vector_size, ))