返回在索引 axis 處插入長度為 1 的軸的張量。
用法
tf.expand_dims(
    input, axis, name=None
)參數
- 
input一個Tensor。
- 
axis整數,指定展開input形狀的維度索引。給定 D 維度的輸入,axis必須在[-(D+1), D]範圍內(包括)。
- 
name可選字符串。輸出的名稱Tensor。
返回
- 
與 input具有相同數據的張量,在axis指定的索引處插入了一個附加維度。
拋出
- 
TypeError如果未指定axis。
- 
InvalidArgumentError如果axis超出範圍[-(D+1), D]。
給定一個張量 input ,此操作在 input 的形狀的維度索引 axis 處插入一個長度為 1 的維度。維度索引遵循 Python 索引規則:它是從零開始的,一個負數索引它從末尾向後計數。
此操作可用於:
- 將外部"batch" 維度添加到單個元素。
- 對齊軸以進行廣播。
- 將內部向量長度軸添加到標量張量。
例如:
如果您有一個形狀為 [height, width, channels] 的圖像:
image = tf.zeros([10,10,3])您可以通過傳遞 axis=0 添加外部 batch 軸:
tf.expand_dims(image, axis=0).shape.as_list()
[1, 10, 10, 3]新軸位置與 Python list.insert(axis, 1) 匹配:
tf.expand_dims(image, axis=1).shape.as_list()
[10, 1, 10, 3]遵循標準 Python 索引規則,負數 axis 從末尾開始計數,因此 axis=-1 添加了最內層維度:
tf.expand_dims(image, -1).shape.as_list()
[10, 10, 3, 1]此操作要求 axis 是 input.shape 的有效索引,遵循 Python 索引規則:
-1-tf.rank(input) <= axis <= tf.rank(input)此操作與以下內容有關:
- tf.squeeze,刪除大小為 1 的維度。
- tf.reshape,提供更靈活的整形能力。
- tf.sparse.expand_dims,為- tf.SparseTensor提供此函數
相關用法
- Python tf.experimental.dlpack.from_dlpack用法及代碼示例
- Python tf.experimental.numpy.iinfo用法及代碼示例
- Python tf.experimental.Optional.has_value用法及代碼示例
- Python tf.experimental.dispatch_for_unary_elementwise_apis用法及代碼示例
- Python tf.experimental.dispatch_for_api用法及代碼示例
- Python tf.experimental.unregister_dispatch_for用法及代碼示例
- Python tf.experimental.tensorrt.Converter用法及代碼示例
- Python tf.experimental.ExtensionType用法及代碼示例
- Python tf.experimental.Optional.get_value用法及代碼示例
- Python tf.experimental.numpy.issubdtype用法及代碼示例
- Python tf.experimental.numpy.unicode_用法及代碼示例
- Python tf.experimental.dlpack.to_dlpack用法及代碼示例
- Python tf.experimental.async_scope用法及代碼示例
- Python tf.experimental.Optional.empty用法及代碼示例
- Python tf.experimental.numpy.float16.as_integer_ratio用法及代碼示例
- Python tf.experimental.numpy.float64.as_integer_ratio用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.expand_dims。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
