In TensorFlow 2.0, iterating over a TensorShape instance returns values. This enables the new behavior. Concretely, `tensor_shape[i]` returned a Dimension instance in V1, but it V2 it returns either an integer, or None. Examples: ``` ####################### # If you had this in V1:
()
| 34 | |
| 35 | @tf_export(v1=["enable_v2_tensorshape"]) |
| 36 | def enable_v2_tensorshape(): |
| 37 | """In TensorFlow 2.0, iterating over a TensorShape instance returns values. |
| 38 | |
| 39 | This enables the new behavior. |
| 40 | |
| 41 | Concretely, `tensor_shape[i]` returned a Dimension instance in V1, but |
| 42 | it V2 it returns either an integer, or None. |
| 43 | |
| 44 | Examples: |
| 45 | |
| 46 | ``` |
| 47 | ####################### |
| 48 | # If you had this in V1: |
| 49 | value = tensor_shape[i].value |
| 50 | |
| 51 | # Do this in V2 instead: |
| 52 | value = tensor_shape[i] |
| 53 | |
| 54 | ####################### |
| 55 | # If you had this in V1: |
| 56 | for dim in tensor_shape: |
| 57 | value = dim.value |
| 58 | print(value) |
| 59 | |
| 60 | # Do this in V2 instead: |
| 61 | for value in tensor_shape: |
| 62 | print(value) |
| 63 | |
| 64 | ####################### |
| 65 | # If you had this in V1: |
| 66 | dim = tensor_shape[i] |
| 67 | dim.assert_is_compatible_with(other_shape) # or using any other shape method |
| 68 | |
| 69 | # Do this in V2 instead: |
| 70 | if tensor_shape.rank is None: |
| 71 | dim = Dimension(None) |
| 72 | else: |
| 73 | dim = tensor_shape.dims[i] |
| 74 | dim.assert_is_compatible_with(other_shape) # or using any other shape method |
| 75 | |
| 76 | # The V2 suggestion above is more explicit, which will save you from |
| 77 | # the following trap (present in V1): |
| 78 | # you might do in-place modifications to `dim` and expect them to be reflected |
| 79 | # in `tensor_shape[i]`, but they would not be. |
| 80 | ``` |
| 81 | """ |
| 82 | global _TENSORSHAPE_V2_OVERRIDE # pylint: disable=invalid-name |
| 83 | _TENSORSHAPE_V2_OVERRIDE = True |
| 84 | _api_usage_gauge.get_cell().set(True) |
| 85 | |
| 86 | |
| 87 | @tf_export(v1=["disable_v2_tensorshape"]) |