-
Notifications
You must be signed in to change notification settings - Fork 718
/
nest_utils.py
1285 lines (1063 loc) · 42.7 KB
/
nest_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
# Copyright 2020 The TF-Agents Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for handling nested tensors."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import numbers
from typing import Optional, Text
from absl import logging
import numpy as np
from six.moves import zip
import tensorflow as tf
from tf_agents.typing import types
from tf_agents.utils import composite
import wrapt
# TODO(b/128613858): Update to a public facing API.
from tensorflow.python.util import nest # pylint:disable=g-direct-tensorflow-import # TF internal
try:
# Python 3.3 and above.
collections_abc = collections.abc
except AttributeError:
collections_abc = collections
flatten_up_to = nest.flatten_up_to
flatten_with_tuple_paths = nest.flatten_with_tuple_paths
map_structure_up_to = nest.map_structure_up_to
map_structure_with_paths = nest.map_structure_with_paths
class _Dot(object):
"""An object whose representation is a simple '.'."""
def __repr__(self):
return '.'
def __str__(self):
return '.'
_DOT = _Dot()
def assert_same_structure(
nest1,
nest2,
check_types: bool = True,
expand_composites: bool = False,
allow_shallow_nest1: bool = False,
message: Optional[Text] = None,
) -> None:
"""Same as tf.nest.assert_same_structure but with cleaner error messages.
Args:
nest1: an arbitrarily nested structure.
nest2: an arbitrarily nested structure.
check_types: if `True` (default) types of sequences are checked as well,
including the keys of dictionaries. If set to `False`, for example a list
and a tuple of objects will look the same if they have the same size. Note
that namedtuples with identical name and fields are always considered to
have the same shallow structure. Two types will also be considered the
same if they are both list subtypes (which allows "list" and
"_ListWrapper" from trackable dependency tracking to compare equal).
expand_composites: If true, then composite tensors such as `tf.SparseTensor`
and `tf.RaggedTensor` are expanded into their component tensors.
allow_shallow_nest1: If `True`, `nest1` is allowed to be more shallow than
`nest2`.
message: Optional error message to provide in case of failure.
Raises:
ValueError: If the two structures do not have the same number of elements
or if the two structures are not nested in the same way.
TypeError: If the two structures differ in the type of sequence in any
of their substructures. Only possible if `check_types is True`.
"""
if not isinstance(check_types, bool):
raise TypeError(
"check_types must be a bool but saw: '{}'".format(check_types)
)
if not isinstance(expand_composites, bool):
raise TypeError(
"expand_composites must be a bool but saw: '{}'".format(
expand_composites
)
)
message = message or 'The two structures do not match'
exception = None
if allow_shallow_nest1:
check_fn = nest.assert_shallow_structure
else:
check_fn = tf.nest.assert_same_structure
try:
check_fn(
nest1,
nest2,
check_types=check_types,
expand_composites=expand_composites,
)
except (TypeError, ValueError) as e:
exception = type(e)
if exception:
str1 = tf.nest.map_structure(
lambda _: _DOT, nest1, expand_composites=expand_composites
)
str2 = tf.nest.map_structure(
lambda _: _DOT, nest2, expand_composites=expand_composites
)
raise exception(
'{}:\n {}\nvs.\n {}\nValues:\n {}\nvs.\n {}.'.format(
message, str1, str2, nest1, nest2
)
)
def flatten_with_joined_paths(structure, expand_composites=False):
flattened = flatten_with_tuple_paths(
structure, expand_composites=expand_composites
)
def stringify_and_join(path_elements):
return '/'.join(str(path_element) for path_element in path_elements)
return [(stringify_and_join(path), value) for (path, value) in flattened]
def fast_map_structure_flatten(func, structure, *flat_structure, **kwargs):
expand_composites = kwargs.get('expand_composites', False)
entries = zip(*flat_structure)
return tf.nest.pack_sequence_as(
structure,
[func(*x) for x in entries],
expand_composites=expand_composites,
)
def fast_map_structure(func, *structure, **kwargs):
expand_composites = kwargs.get('expand_composites', False)
flat_structure = [
tf.nest.flatten(s, expand_composites=expand_composites) for s in structure
]
entries = zip(*flat_structure)
return tf.nest.pack_sequence_as(
structure[0],
[func(*x) for x in entries],
expand_composites=expand_composites,
)
def has_tensors(*x):
return np.any(
[tf.is_tensor(t) for t in tf.nest.flatten(x, expand_composites=True)]
)
def _is_namedtuple(x):
return isinstance(x, tuple) and isinstance(
getattr(x, '_fields', None), collections_abc.Sequence
)
def _is_attrs(x):
return getattr(type(x), '__attrs_attrs__', None) is not None
def _attr_items(x):
attrs = getattr(type(x), '__attrs_attrs__')
attr_names = [a.name for a in attrs]
return [(attr_name, getattr(x, attr_name)) for attr_name in attr_names]
def prune_extra_keys(narrow, wide):
"""Recursively prunes keys from `wide` if they don't appear in `narrow`.
Often used as preprocessing prior to calling `tf.nest.flatten`
or `tf.nest.map_structure`.
This function is more forgiving than the ones in `nest`; if two substructures'
types or structures don't agree, we consider it invalid and `prune_extra_keys`
will return the `wide` substructure as is. Typically, additional checking is
needed: you will also want to use
`nest.assert_same_structure(narrow, prune_extra_keys(narrow, wide))`
to ensure the result of pruning is still a correct structure.
Examples:
```python
wide = [{"a": "a", "b": "b"}]
# Narrows 'wide'
assert prune_extra_keys([{"a": 1}], wide) == [{"a": "a"}]
# 'wide' lacks "c", is considered invalid.
assert prune_extra_keys([{"c": 1}], wide) == wide
# 'wide' contains a different type from 'narrow', is considered invalid
assert prune_extra_keys("scalar", wide) == wide
# 'wide' substructure for key "d" does not match the one in 'narrow' and
# therefore is returned unmodified.
assert (prune_extra_keys({"a": {"b": 1}, "d": None},
{"a": {"b": "b", "c": "c"}, "d": [1, 2]})
== {"a": {"b": "b"}, "d": [1, 2]})
# assert prune_extra_keys((), wide) == ()
# assert prune_extra_keys({"a": ()}, wide) == {"a": ()}
```
Args:
narrow: A nested structure.
wide: A nested structure that may contain dicts with more fields than
`narrow`.
Returns:
A structure with the same nested substructures as `wide`, but with
dicts whose entries are limited to the keys found in the associated
substructures of `narrow`.
In case of substructure or size mismatches, the returned substructures
will be returned as is. Note that ObjectProxy-wrapped objects are
considered equivalent to their non-ObjectProxy types.
"""
# If `narrow` is `()`, then `()` is returned. That is, we narrow any
# object w.r.t. an empty tuple to to an empty tuple. We use `id()`
# here because the emtpy tuple is a singleton in cpython and
# because using "x is ()" or "x == ()" gives syntax warnings for
# numpy arrays.
narrow_raw = (
narrow.__wrapped__ if isinstance(narrow, wrapt.ObjectProxy) else narrow
)
if id(narrow_raw) == id(()):
return narrow
if isinstance(wide, wrapt.ObjectProxy):
return type(wide)(prune_extra_keys(narrow, wide.__wrapped__))
wide_raw = wide.__wrapped__ if isinstance(wide, wrapt.ObjectProxy) else wide
if (
(type(narrow_raw) != type(wide_raw)) # pylint: disable=unidiomatic-typecheck
and not (isinstance(narrow_raw, list) and isinstance(wide_raw, list))
and not (
isinstance(narrow_raw, collections_abc.Mapping)
and isinstance(wide_raw, collections_abc.Mapping)
)
):
# We return early if the types are different; but we make some exceptions:
# list subtypes are considered the same (e.g. ListWrapper and list())
# Mapping subtypes are considered the same (e.g. DictWrapper and dict())
# (TupleWrapper subtypes are handled by unwrapping ObjectProxy above).
return wide
if isinstance(narrow, collections_abc.Mapping):
if len(narrow) > len(wide):
# wide lacks a required key from narrow; return early.
return wide
narrow_keys = set(narrow.keys())
wide_keys = set(wide.keys())
if not wide_keys.issuperset(narrow_keys):
# wide lacks a required key from narrow; return early.
return wide
ordered_items = [
(k, prune_extra_keys(v, wide[k])) for k, v in narrow.items()
]
if isinstance(wide, collections.defaultdict):
subset = type(wide)(wide.default_factory, ordered_items)
else:
subset = type(wide)(ordered_items)
return subset
if nest.is_nested(narrow):
if _is_attrs(wide):
items = [
prune_extra_keys(n, w)
for n, w in zip(_attr_items(narrow), _attr_items(wide))
]
return type(wide)(*items)
# Not an attrs, so can treat as lists or tuples from here on.
if len(narrow) != len(wide):
# wide's size is different than narrow; return early.
return wide
items = [prune_extra_keys(n, w) for n, w in zip(narrow, wide)]
if _is_namedtuple(wide):
return type(wide)(*items)
elif _is_attrs(wide):
return type(wide)
return type(wide)(items)
# narrow is a leaf, just return wide
return wide
def assert_tensors_matching_dtypes_and_shapes(
tensors_1, tensors_2, caller, tensors_1_name, tensors_2_name
):
"""Checks if tensors have matching dtypes and shapes.
Args:
tensors_1: A nest of tensor objects.
tensors_2: A nest of tensor objects.
caller: The object calling `assert...`.
tensors_1_name: (str) Name to use for tensors_1 in case of an error.
tensors_2_name: (str) Name to use for tensors_2 in case of an error.
Raises:
ValueError: If the tensors do not match dtypes or shapes.
"""
assert_same_structure(
tensors_1,
tensors_2,
message=(
'{}: {} and {} do not have matching structures'.format(
caller, tensors_1_name, tensors_2_name
)
),
)
def convert_to_tensor(t):
return tf.convert_to_tensor(t) if not tf.is_tensor(t) else t
flat_t1 = tf.nest.map_structure(convert_to_tensor, tf.nest.flatten(tensors_1))
flat_t2 = tf.nest.map_structure(convert_to_tensor, tf.nest.flatten(tensors_2))
t1_shapes = [t.shape for t in flat_t1]
t1_dtypes = [t.dtype for t in flat_t1]
t2_shapes = [t.shape for t in flat_t2]
t2_dtypes = [t.dtype for t in flat_t2]
compatible = True
if any(
t1_dtype != t2_dtype for t1_dtype, t2_dtype in zip(t1_dtypes, t2_dtypes)
):
compatible = False
else:
for t1_shape, t2_shape in zip(t1_shapes, t2_shapes):
if t1_shape.ndims != t2_shape.ndims:
compatible = False
break
if not compatible:
get_dtypes = lambda v: tf.nest.map_structure(lambda x: x.dtype, v)
get_shapes = lambda v: tf.nest.map_structure(lambda x: x.shape, v)
raise ValueError(
'{}: Inconsistent dtypes or shapes between {} and {}.\n'
'dtypes:\n{}\nvs.\n{}.\n'
'shapes:\n{}\nvs.\n{}.'.format(
caller,
tensors_1_name,
tensors_2_name,
get_dtypes(tensors_1),
get_dtypes(tensors_2),
get_shapes(tensors_1),
get_shapes(tensors_2),
)
)
def assert_matching_dtypes_and_inner_shapes(
tensors_or_specs,
specs,
caller,
tensors_name,
specs_name,
allow_extra_fields=False,
):
"""Returns `True` if tensors and specs have matching dtypes and inner shapes.
Args:
tensors_or_specs: A nest of `Tensor` like or `tf.TypeSpec` objects.
specs: A nest of `tf.TypeSpec` objects.
caller: The object calling `assert...`.
tensors_name: (str) Name to use for the tensors in case of an error.
specs_name: (str) Name to use for the specs in case of an error.
allow_extra_fields: If `True`, then `tensors` may contain more keys or list
fields than strictly required by `specs`.
Raises:
ValueError: If the tensors do not match the specs' dtypes or their inner
shapes do not match the specs' shapes.
"""
if allow_extra_fields:
tensors_or_specs = prune_extra_keys(specs, tensors_or_specs)
assert_same_structure(
tensors_or_specs,
specs,
message=(
'{}: {} and {} do not have matching structures'.format(
caller, tensors_name, specs_name
)
),
)
flat_tensors = nest.flatten(tensors_or_specs)
flat_specs = tf.nest.flatten(specs)
def _convert(t, s):
if not isinstance(t, tf.TypeSpec) and not tf.is_tensor(t):
t = tf.convert_to_tensor(t, dtype_hint=s.dtype)
return t
flat_tensors = [_convert(t, s) for (t, s) in zip(flat_tensors, flat_specs)]
tensor_shapes = [t.shape for t in flat_tensors]
tensor_dtypes = [t.dtype for t in flat_tensors]
spec_shapes = [spec_shape(s) for s in flat_specs]
spec_dtypes = [t.dtype for t in flat_specs]
compatible = True
if any(
s_dtype != t_dtype for s_dtype, t_dtype in zip(spec_dtypes, tensor_dtypes)
):
compatible = False
else:
for s_shape, t_shape in zip(spec_shapes, tensor_shapes):
if s_shape.ndims in (0, None) or t_shape.ndims is None:
continue
if s_shape.ndims > t_shape.ndims:
compatible = False
break
if not s_shape.is_compatible_with(t_shape[-s_shape.ndims :]):
compatible = False
break
if not compatible:
get_dtypes = lambda v: tf.nest.map_structure(lambda x: x.dtype, v)
get_shapes = lambda v: tf.nest.map_structure(spec_shape, v)
raise ValueError(
'{}: Inconsistent dtypes or shapes between {} and {}.\n'
'dtypes:\n{}\nvs.\n{}.\n'
'shapes:\n{}\nvs.\n{}.'.format(
caller,
tensors_name,
specs_name,
get_dtypes(tensors_or_specs),
get_dtypes(specs),
get_shapes(tensors_or_specs),
get_shapes(specs),
)
)
def is_batched_nested_tensors(
tensors,
specs,
num_outer_dims=1,
allow_extra_fields=False,
check_dtypes=True,
):
"""Compares tensors to specs to determine if all tensors are batched or not.
For each tensor, it checks the dimensions and dtypes with respect to specs.
Returns `True` if all tensors are batched and `False` if all tensors are
unbatched.
Raises a `ValueError` if the shapes are incompatible or a mix of batched and
unbatched tensors are provided.
Raises a `TypeError` if tensors' dtypes do not match specs.
Args:
tensors: Nested list/tuple/dict of Tensors.
specs: Nested list/tuple/dict of Tensors or CompositeTensors describing the
shape of unbatched tensors.
num_outer_dims: The integer number of dimensions that are considered batch
dimensions. Default 1.
allow_extra_fields: If `True`, then `tensors` may have extra subfields which
are not in specs. In this case, the extra subfields will not be checked.
For example: ```python tensors = {"a": tf.zeros((3, 4),
dtype=tf.float32), "b": tf.zeros((5, 6), dtype=tf.float32)} specs = {"a":
tf.TensorSpec(shape=(4,), dtype=tf.float32)} assert
is_batched_nested_tensors(tensors, specs, allow_extra_fields=True) ``` The
above example would raise a ValueError if `allow_extra_fields` was False.
check_dtypes: If `True` will validate that tensors and specs have the same
dtypes.
Returns:
True if all Tensors are batched and False if all Tensors are unbatched.
Raises:
ValueError: If
1. Any of the tensors or specs have shapes with ndims == None, or
2. The shape of Tensors are not compatible with specs, or
3. A mix of batched and unbatched tensors are provided.
4. The tensors are batched but have an incorrect number of outer dims.
TypeError: If `dtypes` between tensors and specs are not compatible.
"""
if allow_extra_fields:
tensors = prune_extra_keys(specs, tensors)
assert_same_structure(
tensors,
specs,
message='Tensors and specs do not have matching structures',
)
flat_tensors = nest.flatten(tensors)
flat_specs = tf.nest.flatten(specs)
tensor_shapes = [t.shape for t in flat_tensors]
tensor_dtypes = [t.dtype for t in flat_tensors]
spec_shapes = [spec_shape(s) for s in flat_specs]
spec_dtypes = [t.dtype for t in flat_specs]
if any(s_shape.rank is None for s_shape in spec_shapes):
raise ValueError(
'All specs should have ndims defined. Saw shapes: %s'
% (tf.nest.pack_sequence_as(specs, spec_shapes),)
)
if any(t_shape.rank is None for t_shape in tensor_shapes):
raise ValueError(
'All tensors should have ndims defined. Saw shapes: %s'
% (tf.nest.pack_sequence_as(specs, tensor_shapes),)
)
if check_dtypes and any(
s_dtype != t_dtype for s_dtype, t_dtype in zip(spec_dtypes, tensor_dtypes)
):
packed_tensor_dtypes = tf.nest.pack_sequence_as(specs, tensor_dtypes)
packed_spec_dtypes = tf.nest.pack_sequence_as(specs, spec_dtypes)
mismatched = tf.nest.map_structure(
lambda t, s: t != s, packed_tensor_dtypes, packed_spec_dtypes
)
num_mismatched = tf.math.count_nonzero(
tf.nest.flatten(mismatched), dtype=tf.int32
)
raise TypeError(
'Tensor dtypes do not match spec dtypes:\n{}\nvs.\n{}\nNumber of'
' mismatched entries: {}\nMismatch:\n{}'.format(
tf.nest.pack_sequence_as(specs, tensor_dtypes),
tf.nest.pack_sequence_as(specs, spec_dtypes),
num_mismatched,
mismatched,
)
)
is_unbatched = [
s_shape.is_compatible_with(t_shape)
for s_shape, t_shape in zip(spec_shapes, tensor_shapes)
]
if all(is_unbatched):
return False
tensor_ndims_discrepancy = [
t_shape.rank - s_shape.rank
for s_shape, t_shape in zip(spec_shapes, tensor_shapes)
]
tensor_matches_spec = [
s_shape.is_compatible_with(t_shape[discrepancy:])
for discrepancy, s_shape, t_shape in zip(
tensor_ndims_discrepancy, spec_shapes, tensor_shapes
)
]
# Check if all tensors match and have correct number of outer_dims.
is_batched = all(
discrepancy == num_outer_dims for discrepancy in tensor_ndims_discrepancy
) and all(tensor_matches_spec)
if is_batched:
return True
# Check if tensors match but have incorrect number of batch dimensions.
if all(
discrepancy == tensor_ndims_discrepancy[0]
for discrepancy in tensor_ndims_discrepancy
) and all(tensor_matches_spec):
return False
raise ValueError(
'Received a mix of batched and unbatched Tensors, or Tensors'
' are not compatible with Specs. num_outer_dims: %d.\n'
'Saw tensor_shapes:\n %s\n'
'And spec_shapes:\n %s'
% (
num_outer_dims,
tf.nest.pack_sequence_as(specs, tensor_shapes),
tf.nest.pack_sequence_as(specs, spec_shapes),
)
)
def spec_shape(t):
if isinstance(t, tf.SparseTensor):
rank = tf.compat.dimension_value(t.dense_shape.shape[0])
return tf.TensorShape([None] * rank)
else:
return t.shape
def batch_nested_tensors(tensors, specs=None):
"""Add batch dimension if needed to nested tensors while checking their specs.
If specs is None, a batch dimension is added to each tensor.
If specs are provided, each tensor is compared to the corresponding spec,
and a batch dimension is added only if the tensor doesn't already have it.
For each tensor, it checks the dimensions with respect to specs, and adds an
extra batch dimension if it doesn't already have it.
Args:
tensors: Nested list/tuple or dict of Tensors.
specs: Nested list/tuple or dict of TensorSpecs, describing the shape of the
non-batched Tensors.
Returns:
A nested batched version of each tensor.
Raises:
ValueError: if the tensors and specs have incompatible dimensions or shapes.
"""
if specs is None:
return tf.nest.map_structure(lambda x: composite.expand_dims(x, 0), tensors)
assert_same_structure(
tensors,
specs,
message='Tensors and specs do not have matching structures',
)
flat_tensors = tf.nest.flatten(tensors)
flat_shapes = [spec_shape(s) for s in tf.nest.flatten(specs)]
batched_tensors = []
tensor_rank = lambda tensor: tensor.shape.rank
for tensor, shape in zip(flat_tensors, flat_shapes):
if tensor_rank(tensor) == shape.rank:
tensor.shape.assert_is_compatible_with(shape)
tensor = composite.expand_dims(tensor, 0)
elif tensor_rank(tensor) == shape.rank + 1:
tensor.shape[1:].assert_is_compatible_with(shape)
else:
raise ValueError(
'Tensor does not have the correct dimensions. '
'tensor.shape {} expected shape {}'.format(tensor.shape, shape)
)
batched_tensors.append(tensor)
return tf.nest.pack_sequence_as(tensors, batched_tensors)
def _flatten_and_check_shape_nested_tensors(tensors, specs, num_outer_dims=1):
"""Flatten nested tensors and check their shape for use in other functions."""
assert_same_structure(
tensors,
specs,
message='Tensors and specs do not have matching structures',
)
flat_tensors = tf.nest.flatten(tensors)
flat_shapes = [spec_shape(s) for s in tf.nest.flatten(specs)]
for tensor, shape in zip(flat_tensors, flat_shapes):
if tensor.shape.rank == shape.rank:
tensor.shape.assert_is_compatible_with(shape)
elif tensor.shape.rank == shape.rank + num_outer_dims:
tensor.shape[num_outer_dims:].assert_is_compatible_with(shape)
else:
raise ValueError(
'Tensor does not have the correct dimensions. '
'tensor.shape {} expected shape {}'.format(
tensor.shape, [None] + shape.as_list()
)
)
return flat_tensors, flat_shapes
def flatten_and_check_shape_nested_specs(specs, reference_specs):
"""Flatten nested specs and check their shape for use in other functions."""
try:
flat_specs, flat_shapes = _flatten_and_check_shape_nested_tensors(
specs, reference_specs, num_outer_dims=0
)
except ValueError as exc:
raise ValueError(
'specs must be compatible with reference_specs'
'; instead got specs=%s, reference_specs=%s' % (specs, reference_specs)
) from exc
return flat_specs, flat_shapes
def unbatch_nested_tensors(tensors, specs=None):
"""Remove the batch dimension if needed from nested tensors using their specs.
If specs is None, the first dimension of each tensor will be removed.
If specs are provided, each tensor is compared to the corresponding spec,
and the first dimension is removed only if the tensor was batched.
Args:
tensors: Nested list/tuple or dict of batched Tensors.
specs: Nested list/tuple or dict of TensorSpecs, describing the shape of the
non-batched Tensors.
Returns:
A nested non-batched version of each tensor.
Raises:
ValueError: if the tensors and specs have incompatible dimensions or shapes.
"""
if specs is None:
return tf.nest.map_structure(lambda x: composite.squeeze(x, 0), tensors)
unbatched_tensors = []
flat_tensors, flat_shapes = _flatten_and_check_shape_nested_tensors(
tensors, specs
)
for tensor, shape in zip(flat_tensors, flat_shapes):
if tensor.shape.rank == shape.rank + 1:
tensor = composite.squeeze(tensor, 0)
unbatched_tensors.append(tensor)
return tf.nest.pack_sequence_as(tensors, unbatched_tensors)
def split_nested_tensors(tensors, specs, num_or_size_splits):
"""Split batched nested tensors, on batch dim (outer dim), into a list.
Args:
tensors: Nested list/tuple or dict of batched Tensors.
specs: Nested list/tuple or dict of TensorSpecs, describing the shape of the
non-batched Tensors.
num_or_size_splits: Same as argument for tf.split. Either a python integer
indicating the number of splits along batch_dim or a list of integer
Tensors containing the sizes of each output tensor along batch_dim. If a
scalar then it must evenly divide value.shape[axis]; otherwise the sum of
sizes along the split dimension must match that of the value. For
`SparseTensor` inputs, `num_or_size_splits` must be the scalar `num_split`
(see documentation of `tf.sparse.split` for more details).
Returns:
A list of nested non-batched version of each tensor, where each list item
corresponds to one batch item.
Raises:
ValueError: if the tensors and specs have incompatible dimensions or shapes.
ValueError: if a non-scalar is passed and there are SparseTensors in the
structure.
"""
split_tensor_lists = []
flat_tensors, flat_shapes = _flatten_and_check_shape_nested_tensors(
tensors, specs
)
for tensor, shape in zip(flat_tensors, flat_shapes):
if tensor.shape.rank == shape.rank:
raise ValueError('Can only split tensors with a batch dimension.')
if tensor.shape.rank == shape.rank + 1:
if isinstance(tensor, tf.SparseTensor):
if not isinstance(num_or_size_splits, numbers.Number):
raise ValueError(
'Saw a SparseTensor, for which num_or_size_splits must be a '
'scalar. But it is not: {}'.format(num_or_size_splits)
)
split_tensors = tf.sparse.split(
sp_input=tensor, num_split=num_or_size_splits, axis=0
)
else:
split_tensors = tf.split(tensor, num_or_size_splits)
split_tensor_lists.append(split_tensors)
split_tensors_zipped = zip(*split_tensor_lists)
return [
tf.nest.pack_sequence_as(tensors, zipped)
for zipped in split_tensors_zipped
]
def unstack_nested_tensors(tensors, specs):
"""Make list of unstacked nested tensors.
Args:
tensors: Nested tensors whose first dimension is to be unstacked.
specs: Tensor specs for tensors.
Returns:
A list of the unstacked nested tensors.
Raises:
ValueError: if the tensors and specs have incompatible dimensions or shapes.
"""
unstacked_tensor_lists = []
flat_tensors, flat_shapes = _flatten_and_check_shape_nested_tensors(
tensors, specs
)
for tensor, shape in zip(flat_tensors, flat_shapes):
if tensor.shape.rank == shape.rank:
raise ValueError('Can only unstack tensors with a batch dimension.')
if tensor.shape.rank == shape.rank + 1:
unstacked_tensors = tf.unstack(tensor)
unstacked_tensor_lists.append(unstacked_tensors)
unstacked_tensors_zipped = zip(*unstacked_tensor_lists)
return [
tf.nest.pack_sequence_as(tensors, zipped)
for zipped in unstacked_tensors_zipped
]
def stack_nested_tensors(tensors, axis=0):
"""Stacks a list of nested tensors along the dimension specified.
Args:
tensors: A list of nested tensors to be stacked.
axis: the axis along which the stack operation is applied.
Returns:
A stacked nested tensor.
"""
return tf.nest.map_structure(
lambda *tensors: tf.stack(tensors, axis=axis), *tensors
)
def flatten_multi_batched_nested_tensors(tensors, specs):
"""Reshape tensors to contain only one batch dimension.
For each tensor, it checks the number of extra dimensions beyond those in
the spec, and reshapes tensor to have only one batch dimension.
NOTE: Each tensor's batch dimensions must be the same.
Args:
tensors: Nested list/tuple or dict of batched Tensors or SparseTensors.
specs: Nested list/tuple or dict of TensorSpecs, describing the shape of the
non-batched Tensors.
Returns:
A nested version of each tensor with a single batch dimension.
A list of the batch dimensions which were flattened.
Raises:
ValueError: if the tensors and specs have incompatible dimensions or shapes.
"""
assert_same_structure(
tensors,
specs,
message='Tensors and specs do not have matching structures',
)
flat_tensors = tf.nest.flatten(tensors)
flat_spec_shapes = [spec_shape(s) for s in tf.nest.flatten(specs)]
out_tensors = []
batch_dims = []
for i, (tensor, sp_shape) in enumerate(zip(flat_tensors, flat_spec_shapes)):
if i == 0: # Set batch_dims based on first tensor.
batch_dims = tensor.shape[: tensor.shape.rank - sp_shape.rank]
if batch_dims.is_fully_defined():
batch_dims = batch_dims.as_list()
batch_prod = np.prod(batch_dims)
batch_dims = tf.constant(batch_dims, dtype=tf.int64)
else:
batch_dims = tf.shape(tensor)[: tensor.shape.rank - sp_shape.rank]
batch_prod = tf.reduce_prod(batch_dims)
if not sp_shape.is_fully_defined():
# When shape of spec is not fully defined, we do not rely on it to
# reshape the tensor but retain the original non-batch dims of tensors.
non_batch_dims = tf.shape(tensor)[tensor.shape.rank - sp_shape.rank :]
reshaped_dims = tf.concat([[batch_prod], non_batch_dims], 0)
else:
reshaped_dims = [batch_prod] + sp_shape.as_list()
out_tensors.append(composite.reshape(tensor, reshaped_dims))
return tf.nest.pack_sequence_as(tensors, out_tensors), batch_dims
def get_outer_shape(nested_tensor, spec):
"""Runtime batch dims of tensor's batch dimension `dim`.
Args:
nested_tensor: Nest of tensors.
spec: The nested spec.
Returns:
A `Tensor` containing the outer shape.
Raises:
ValueError: If `nested_tensor` and `spec` have different structures.
TypeError: If `nested_tensor` and `spec` structures have differing types.
"""
assert_same_structure(
nested_tensor,
spec,
message='Tensors and specs do not have matching structures',
)
first_tensor = tf.nest.flatten(nested_tensor)[0]
first_spec = tf.nest.flatten(spec)[0]
# Check tensors have same batch shape.
num_outer_dims = len(first_tensor.shape) - len(first_spec.shape)
if not is_batched_nested_tensors(
nested_tensor, spec, num_outer_dims=num_outer_dims, check_dtypes=False
):
return tf.constant([], dtype=tf.int32)
return tf.shape(input=first_tensor)[:num_outer_dims]
def get_outer_rank(tensors, specs):
"""Compares tensors to specs to determine the number of batch dimensions.
For each tensor, it checks the dimensions with respect to specs and
returns the number of batch dimensions if all nested tensors and
specs agree with each other.
Args:
tensors: Nested list/tuple/dict of Tensors or SparseTensors.
specs: Nested list/tuple/dict of TensorSpecs, describing the shape of
unbatched tensors.
Returns:
The number of outer dimensions for all Tensors (zero if all are
unbatched or empty).
Raises:
ValueError: If
1. Any of the tensors or specs have shapes with ndims == None, or
2. The shape of Tensors are not compatible with specs, or
3. A mix of batched and unbatched tensors are provided.
4. The tensors are batched but have an incorrect number of outer dims.
"""
assert_same_structure(
tensors,
specs,
message='Tensors and specs do not have matching structures',
)
tensor_shapes = [t.shape for t in tf.nest.flatten(tensors)]
spec_shapes = [spec_shape(s) for s in tf.nest.flatten(specs)]
if any(s_shape.rank is None for s_shape in spec_shapes):
raise ValueError(
'All specs should have ndims defined. Saw shapes: %s' % spec_shapes
)
if any(t_shape.rank is None for t_shape in tensor_shapes):
raise ValueError(
'All tensors should have ndims defined. Saw shapes: %s' % tensor_shapes
)
is_unbatched = [
s_shape.is_compatible_with(t_shape)
for s_shape, t_shape in zip(spec_shapes, tensor_shapes)
]
if all(is_unbatched):
return 0
tensor_ndims_discrepancy = [
t_shape.rank - s_shape.rank
for s_shape, t_shape in zip(spec_shapes, tensor_shapes)
]
tensor_matches_spec = [
s_shape.is_compatible_with(t_shape[discrepancy:])
for discrepancy, s_shape, t_shape in zip(
tensor_ndims_discrepancy, spec_shapes, tensor_shapes
)
]
# At this point we are guaranteed to have at least one tensor/spec.
num_outer_dims = tensor_ndims_discrepancy[0]
# Check if all tensors match and have correct number of batch dimensions.
is_batched = all(
discrepancy == num_outer_dims for discrepancy in tensor_ndims_discrepancy
) and all(tensor_matches_spec)
if is_batched:
return num_outer_dims
# Check if tensors match but have incorrect number of batch dimensions.
incorrect_batch_dims = (
tensor_ndims_discrepancy
and all(
discrepancy == tensor_ndims_discrepancy[0] and discrepancy >= 0
for discrepancy in tensor_ndims_discrepancy
)
and all(tensor_matches_spec)
)
if incorrect_batch_dims:
raise ValueError(
'Received tensors with %d outer dimensions. Expected %d.'
% (tensor_ndims_discrepancy[0], num_outer_dims)
)
raise ValueError(
'Received a mix of batched and unbatched Tensors, or Tensors'
' are not compatible with Specs. num_outer_dims: %d.\n'
'Saw tensor_shapes:\n %s\n'