返回在索引 axis 處插入長度為 1 的軸的張量。
用法
tf.sparse.expand_dims(
sp_input, axis=None, name=None
)參數
-
sp_input一個SparseTensor。 -
axis0-D(標量)。指定展開input形狀的維度索引。必須在[-rank(sp_input) - 1, rank(sp_input)]範圍內。默認為-1。 -
name輸出的名稱SparseTensor。
返回
-
與
sp_input具有相同數據的SparseTensor,但其形狀添加了大小為 1 的附加維度。
給定一個張量 input ,此操作在 input 的形狀的維度索引 axis 處插入一個長度為 1 的維度。維度索引遵循python索引規則:它是從零開始的,一個負數索引它從末尾向後計數。
此操作可用於:
- 將外部"batch" 維度添加到單個元素。
- 對齊軸以進行廣播。
- 將內部向量長度軸添加到標量張量。
例如:
如果您有一個形狀為 [height, width, depth] 的稀疏張量:
sp = tf.sparse.SparseTensor(indices=[[3,4,1]], values=[7,],
dense_shape=[10,10,3])
您可以通過傳遞 axis=0 添加外部 batch 軸:
tf.sparse.expand_dims(sp, axis=0).shape.as_list()
[1, 10, 10, 3]
新軸位置與 Python list.insert(axis, 1) 匹配:
tf.sparse.expand_dims(sp, axis=1).shape.as_list()
[10, 1, 10, 3]
遵循標準的 Python 索引規則,負數 axis 從末尾開始計數,因此 axis=-1 添加了最內層維度:
tf.sparse.expand_dims(sp, axis=-1).shape.as_list()
[10, 10, 3, 1]
注意:與 tf.expand_dims 不同,此函數包含 axis 的默認值:-1。因此,如果未指定 `axis,則添加內部維度。
sp.shape.as_list()
[10, 10, 3]
tf.sparse.expand_dims(sp).shape.as_list()
[10, 10, 3, 1]
此操作要求 axis 是 input.shape 的有效索引,遵循 python 索引規則:
-1-tf.rank(input) <= axis <= tf.rank(input)
此操作與以下內容有關:
tf.expand_dims,它為密集張量提供此函數。tf.squeeze,從密集張量中刪除大小為 1 的維度。tf.sparse.reshape,提供更靈活的整形能力。
相關用法
- Python tf.sparse.cross用法及代碼示例
- Python tf.sparse.mask用法及代碼示例
- Python tf.sparse.split用法及代碼示例
- Python tf.sparse.to_dense用法及代碼示例
- Python tf.sparse.maximum用法及代碼示例
- Python tf.sparse.bincount用法及代碼示例
- Python tf.sparse.concat用法及代碼示例
- Python tf.sparse.transpose用法及代碼示例
- Python tf.sparse.reduce_sum用法及代碼示例
- Python tf.sparse.softmax用法及代碼示例
- Python tf.sparse.to_indicator用法及代碼示例
- Python tf.sparse.from_dense用法及代碼示例
- Python tf.sparse.retain用法及代碼示例
- Python tf.sparse.minimum用法及代碼示例
- Python tf.sparse.segment_sum用法及代碼示例
- Python tf.sparse.reduce_max用法及代碼示例
- Python tf.sparse.fill_empty_rows用法及代碼示例
- Python tf.sparse.slice用法及代碼示例
- Python tf.sparse.cross_hashed用法及代碼示例
- Python tf.sparse.reorder用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.sparse.expand_dims。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
