当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.compat.v1.enable_v2_tensorshape用法及代码示例


在 TensorFlow 2.0 中,迭代 TensorShape 实例会返回值。

用法

tf.compat.v1.enable_v2_tensorshape()

这启用了新的行为。

具体来说,tensor_shape[i] 在 V1 中返回了一个 Dimension 实例,但在 V2 中它返回一个整数或 None。

例子:

#######################
# If you had this in V1:
value = tensor_shape[i].value

# Do this in V2 instead:
value = tensor_shape[i]

#######################
# If you had this in V1:
for dim in tensor_shape:
  value = dim.value
  print(value)

# Do this in V2 instead:
for value in tensor_shape:
  print(value)

#######################
# If you had this in V1:
dim = tensor_shape[i]
dim.assert_is_compatible_with(other_shape)  # or using any other shape method

# Do this in V2 instead:
if tensor_shape.rank is None:
  dim = Dimension(None)
else:
  dim = tensor_shape.dims[i]
dim.assert_is_compatible_with(other_shape)  # or using any other shape method

# The V2 suggestion above is more explicit, which will save you from
# the following trap (present in V1):
# you might do in-place modifications to `dim` and expect them to be reflected
# in `tensor_shape[i]`, but they would not be.

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.enable_v2_tensorshape。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。