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


Python tf.compat.dimension_at_index用法及代碼示例

允許 TF 中的 V1 和 V2 行為所需的兼容性實用程序。

用法

tf.compat.dimension_at_index(
    shape, index
)

參數

  • shape 一個 TensorShape 實例。
  • index 整數索引。

返回

  • 維度對象。

在 TF 2.0 發布之前,我們需要 TensorShape 的舊行為與新行為共存。該實用程序是兩者之間的橋梁。

如果要檢索與 TensorShape 實例中某個索引對應的 Dimension 實例,請使用此實用程序,如下所示:

# If you had this in your V1 code:
dim = tensor_shape[i]

# Use `dimension_at_index` as direct replacement compatible with both V1 & V2:
dim = dimension_at_index(tensor_shape, i)

# Another possibility would be this, but WARNING:it only works if the
# tensor_shape instance has a defined rank.
dim = tensor_shape.dims[i]  # `dims` may be None if the rank is undefined!

# In native V2 code, we recommend instead being more explicit:
if tensor_shape.rank is None:
  dim = Dimension(None)
else:
  dim = tensor_shape.dims[i]

# Being more explicit 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 (as the Dimension object was
# instantiated on the fly.

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.dimension_at_index。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。