當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。