Пример #1
0
 def test_one_of_mixed_input_type(self):
   block = (tdb.Identity(), tdb.Scalar('int32')) >> tdb.OneOf(
       key_fn=tdb.GetItem(0),
       case_blocks=(tdb.Function(tf.square), tdb.Function(tf.negative)),
       pre_block=tdb.GetItem(1))
   self.assertBuilds(4, block, (0, 2))
   self.assertBuilds(-2, block, (1, 2))
Пример #2
0
 def test_one_of_pre(self):
   block = tdb.OneOf(lambda x: x['key'],
                     {'a': tdb.GetItem('bar') >> tdb.Scalar(),
                      'b': tdb.GetItem('baz') >> tdb.Scalar()},
                     tdb.GetItem('val'))
   self.assertBuildsConst(42., block,
                          {'key': 'a', 'val': {'bar': 42, 'baz': 0}})
   self.assertBuildsConst(0., block,
                          {'key': 'b', 'val': {'bar': 42, 'baz': 0}})
Пример #3
0
 def test_init_raises(self):
     six.assertRaisesRegex(self, TypeError,
                           'root must have at least one output',
                           tdc.Compiler.create, tdb.Record([]))
     six.assertRaisesRegex(self, TypeError,
                           'root outputs must all be tensors',
                           tdc.Compiler.create, tdb.GetItem('foo'))
     six.assertRaisesRegex(self, TypeError,
                           'root output may not contain sequences',
                           tdc.Compiler.create, tdb.Map(tdb.Scalar()))
Пример #4
0
  def test_hierarchical_rnn(self):
    char_cell = tdl.ScopedLayer(
        tf.contrib.rnn.BasicLSTMCell(num_units=16), 'char_cell')
    word_cell = tdl.ScopedLayer(
        tf.contrib.rnn.BasicLSTMCell(num_units=32), 'word_cell')

    char_lstm = (tdb.InputTransform(lambda s: [ord(c) for c in s]) >>
                 tdb.Map(tdb.Scalar('int32') >>
                         tdb.Function(tdl.Embedding(128, 8))) >>
                 tdb.RNN(char_cell))
    word_lstm = (tdb.Map(char_lstm >> tdb.GetItem(1) >> tdb.Concat()) >>
                 tdb.RNN(word_cell))

    with self.test_session():
      word_lstm.eval(['the', 'cat', 'sat', 'on', 'a', 'mat'])
Пример #5
0
  def test_forward_declarations(self):
    # Define a simple expression data structure
    nlit = lambda x: {'op': 'lit', 'val': x}
    nadd = lambda x, y: {'op': 'add', 'left': x, 'right': y}
    nexpr = nadd(nadd(nlit(3.0), nlit(5.0)), nlit(2.0))

    # Define a recursive block using forward declarations
    expr_fwd = tdb.ForwardDeclaration(tdt.PyObjectType(),
                                      tdt.TensorType((), 'float32'))
    lit_case = tdb.GetItem('val') >> tdb.Scalar()
    add_case = (tdb.Record({'left': expr_fwd(), 'right': expr_fwd()})
                >> tdb.Function(tf.add))
    expr = tdb.OneOf(lambda x: x['op'], {'lit': lit_case, 'add': add_case})
    expr_fwd.resolve_to(expr)

    self.assertBuilds(10.0, expr, nexpr, max_depth=2)
Пример #6
0
 def test_get_item_tuple(self):
   block = (tdb.Scalar(), tdb.Scalar()) >> tdb.GetItem(-1)
   self.assertBuildsConst(2., block, (1, 2))
Пример #7
0
 def test_get_item_sequence(self):
   block = tdb.Map(tdb.Scalar()) >> tdb.GetItem(-1)
   self.assertBuildsConst(9., block, range(10))
Пример #8
0
 def test_get_item_pyobject(self):
   self.assertBuildsConst(2., tdb.GetItem(1) >> tdb.Scalar(), [1, 2, 3])
Пример #9
0
 def test_composition_no_output_void_type(self):
   b = tdb.AllOf(tdb.Void(), tdb.Scalar()) >> tdb.GetItem(1)
   self.assertBuildsConst(42., b, 42)
Пример #10
0
  def test_repr(self):
    goldens = {
        tdb.Tensor([]): '<td.Tensor dtype=\'float32\' shape=()>',
        tdb.Tensor([1, 2], 'int32', name='foo'):
        '<td.Tensor \'foo\' dtype=\'int32\' shape=(1, 2)>',

        tdb.Scalar('int64'): '<td.Scalar dtype=\'int64\'>',

        tdb.Vector(42): '<td.Vector dtype=\'float32\' size=42>',

        tdb.FromTensor(tf.zeros(3)): '<td.FromTensor \'zeros:0\'>',

        tdb.Function(tf.negative,
                     name='foo'): '<td.Function \'foo\' tf_fn=\'negative\'>',

        tdb.Identity(): '<td.Identity>',
        tdb.Identity('foo'): '<td.Identity \'foo\'>',

        tdb.InputTransform(ord): '<td.InputTransform py_fn=\'ord\'>',

        tdb.SerializedMessageToTree('foo'):
        '<td.SerializedMessageToTree \'foo\' '
        'py_fn=\'serialized_message_to_tree\'>',

        tdb.GetItem(3, 'mu'): '<td.GetItem \'mu\' key=3>',

        tdb.Length(): '<td.Length dtype=\'float32\'>',

        tdb.Slice(stop=2): '<td.Slice key=slice(None, 2, None)>',
        tdb.Slice(stop=2, name='x'):
        '<td.Slice \'x\' key=slice(None, 2, None)>',

        tdb.ForwardDeclaration(name='foo')():
        '<td.ForwardDeclaration() \'foo\'>',

        tdb.Composition(name='x').input: '<td.Composition.input \'x\'>',
        tdb.Composition(name='x').output: '<td.Composition.output \'x\'>',
        tdb.Composition(name='x'): '<td.Composition \'x\'>',

        tdb.Pipe(): '<td.Pipe>',
        tdb.Pipe(tdb.Scalar(), tdb.Identity()): '<td.Pipe>',

        tdb.Record({}, name='x'): '<td.Record \'x\' ordered=False>',
        tdb.Record((), name='x'): '<td.Record \'x\' ordered=True>',

        tdb.AllOf(): '<td.AllOf>',
        tdb.AllOf(tdb.Identity()): '<td.AllOf>',
        tdb.AllOf(tdb.Identity(), tdb.Identity()): '<td.AllOf>',

        tdb.AllOf(name='x'): '<td.AllOf \'x\'>',
        tdb.AllOf(tdb.Identity(), name='x'): '<td.AllOf \'x\'>',
        tdb.AllOf(tdb.Identity(), tdb.Identity(), name='x'): '<td.AllOf \'x\'>',

        tdb.Map(tdb.Scalar(), name='x'):
        '<td.Map \'x\' element_block=<td.Scalar dtype=\'float32\'>>',

        tdb.Fold(tdb.Function(tf.add), tf.ones([]), name='x'):
        '<td.Fold \'x\' combine_block=<td.Function tf_fn=\'add\'> '
        'start_block=<td.FromTensor \'ones:0\'>>',

        tdb.RNN(tdl.ScopedLayer(tf.contrib.rnn.GRUCell(num_units=8))):
        '<td.RNN>',
        tdb.RNN(tdl.ScopedLayer(tf.contrib.rnn.GRUCell(num_units=8)), name='x'):
        '<td.RNN \'x\'>',
        tdb.RNN(tdl.ScopedLayer(tf.contrib.rnn.GRUCell(num_units=8)),
                initial_state=tf.ones(8)):
        '<td.RNN>',
        tdb.RNN(tdl.ScopedLayer(tf.contrib.rnn.GRUCell(num_units=8)),
                initial_state=tf.ones(8), name='x'):
        '<td.RNN \'x\'>',

        tdb.Reduce(tdb.Function(tf.add), name='x'):
        '<td.Reduce \'x\' combine_block=<td.Function tf_fn=\'add\'>>',

        tdb.Sum(name='foo'):
        '<td.Sum \'foo\' combine_block=<td.Function tf_fn=\'add\'>>',

        tdb.Min(name='foo'):
        '<td.Min \'foo\' combine_block=<td.Function tf_fn=\'minimum\'>>',

        tdb.Max(name='foo'):
        '<td.Max \'foo\' combine_block=<td.Function tf_fn=\'maximum\'>>',

        tdb.Mean(name='foo'): '<td.Mean \'foo\'>',

        tdb.OneOf(ord, (tdb.Scalar(), tdb.Scalar()), name='x'):
        '<td.OneOf \'x\'>',

        tdb.Optional(tdb.Scalar(), name='foo'):
        '<td.Optional \'foo\' some_case_block=<td.Scalar dtype=\'float32\'>>',

        tdb.Concat(1, True, 'x'):
        '<td.Concat \'x\' concat_dim=1 flatten=True>',

        tdb.Broadcast(name='x'): '<td.Broadcast \'x\'>',

        tdb.Zip(name='x'): '<td.Zip \'x\'>',

        tdb.NGrams(n=42, name='x'): '<td.NGrams \'x\' n=42>',

        tdb.OneHot(2, 3, name='x'):
        '<td.OneHot \'x\' dtype=\'float32\' start=2 stop=3>',
        tdb.OneHot(3): '<td.OneHot dtype=\'float32\' start=0 stop=3>',

        tdb.OneHotFromList(['a', 'b']): '<td.OneHotFromList>',
        tdb.OneHotFromList(['a', 'b'], name='foo'):
        '<td.OneHotFromList \'foo\'>',

        tdb.Nth(name='x'): '<td.Nth \'x\'>',

        tdb.Zeros([], 'x'): '<td.Zeros \'x\'>',

        tdb.Void(): '<td.Void>',
        tdb.Void('foo'): '<td.Void \'foo\'>',

        tdm.Metric('foo'): '<td.Metric \'foo\'>'}
    for block, expected_repr in sorted(six.iteritems(goldens),
                                       key=lambda kv: kv[1]):
      self.assertEqual(repr(block), expected_repr)