Пример #1
0
    def testMapWithPathsCompatibleStructures(self, s1, s2, check_types,
                                             expected):
        def format_sum(path, *values):
            return (path, sum(values))

        result = nest.map_structure_with_paths(format_sum,
                                               s1,
                                               s2,
                                               check_types=check_types)
        self.assertEqual(expected, result)
Пример #2
0
def to_tf_example(game):
    if game.start.is_teams:
        return None

    player_ports = [
        i for i, player in enumerate(game.start.players) if player is not None
    ]
    game_buffer = nest.map_structure(lambda _: [], game_paths)

    for frame in game.frames[:2]:
        for i, p in enumerate(player_ports):
            data = frame.ports[p].leader
            append_data(data, game_buffer[i])

    _to_feature = lambda t, l: TYPE_FEATURE_MAP[t](l)

    game_features = nest.map_structure_up_to(game_paths, _to_feature,
                                             game_paths, game_buffer)
    flat_features = {}
    nest.map_structure_with_paths(write_flat, game_features, d=flat_features)

    return tf.train.Example(features=tf.train.Features(feature=flat_features))
Пример #3
0
    def testNestMapStructureWithPaths(self,
                                      structure,
                                      expected,
                                      expand_composites=True):
        def func1(path, x):
            return '%s:%s' % (path, x)

        result = nest.map_structure_with_paths(
            func1, structure, expand_composites=expand_composites)
        self.assertEqual(result, expected)

        # Use the same test cases for map_structure_with_tuple_paths.
        def func2(tuple_path, x):
            return '%s:%s' % ('/'.join(str(v) for v in tuple_path), x)

        result = nest.map_structure_with_tuple_paths(
            func2, structure, expand_composites=expand_composites)
        self.assertEqual(result, expected)
  def testNestMapStructureWithPaths(self):
    structure = [[TestCompositeTensor(1, 2, 3)], 100, {
        'y': TestCompositeTensor(TestCompositeTensor(4, 5), 6)
    }]

    def func(path, x):
      return '%s:%s' % (path, x)

    result = nest.map_structure_with_paths(
        func, structure, expand_composites=True)
    expected = [[TestCompositeTensor('0/0/0:1', '0/0/1:2', '0/0/2:3')], '1:100',
                {
                    'y':
                        TestCompositeTensor(
                            TestCompositeTensor('2/y/0/0:4', '2/y/0/1:5'),
                            '2/y/1:6')
                }]
    self.assertEqual(result, expected)
  def testNestMapStructureWithPaths(self,
                                    structure,
                                    expected,
                                    expand_composites=True):

    def func1(path, x):
      return '%s:%s' % (path, x)

    result = nest.map_structure_with_paths(
        func1, structure, expand_composites=expand_composites)
    self.assertEqual(result, expected)

    # Use the same test cases for map_structure_with_tuple_paths.
    def func2(tuple_path, x):
      return '%s:%s' % ('/'.join(str(v) for v in tuple_path), x)

    result = nest.map_structure_with_tuple_paths(
        func2, structure, expand_composites=expand_composites)
    self.assertEqual(result, expected)
    def testNestMapStructureWithPaths(self):
        structure = [[TestCompositeTensor(1, 2, 3)], 100, {
            'y': TestCompositeTensor(TestCompositeTensor(4, 5), 6)
        }]

        def func(path, x):
            return '%s:%s' % (path, x)

        result = nest.map_structure_with_paths(func,
                                               structure,
                                               expand_composites=True)
        expected = [[TestCompositeTensor('0/0/0:1', '0/0/1:2', '0/0/2:3')],
                    '1:100', {
                        'y':
                        TestCompositeTensor(
                            TestCompositeTensor('2/y/0/0:4', '2/y/0/1:5'),
                            '2/y/1:6')
                    }]
        self.assertEqual(result, expected)
Пример #7
0
 def testMapWithPathsIncompatibleStructures(self, s1, s2, error_type):
     with self.assertRaises(error_type):
         nest.map_structure_with_paths(lambda path, *s: 0, s1, s2)
Пример #8
0
 def testMapWithPathsIncompatibleStructures(self, s1, s2, error_type):
   with self.assertRaises(error_type):
     nest.map_structure_with_paths(lambda path, *s: 0, s1, s2)
Пример #9
0
 def testMapWithPathsCompatibleStructures(self, s1, s2, check_types, expected):
   def format_sum(path, *values):
     return (path, sum(values))
   result = nest.map_structure_with_paths(format_sum, s1, s2,
                                          check_types=check_types)
   self.assertEqual(expected, result)