tf.expand_dims(
input,
axis=None,
name=None,
dim=None
)
给定一个张量输入,这个操作在输入形状的维数索引轴上插入一个维数为1的维度。尺寸指标轴从零开始; 如果为轴指定一个负数,则从末尾向后计数。如果希望向单个元素添加批处理维度,此操作非常有用。例如,如果你有一个shape [height, width, channels]的图像,你可以用expand_dims(image, 0)将它做成一批1个图像,这将生成shape [1, height, width, channels]。别的例子:
import tensorflow as tf
# 't' is a tensor of shape [2]
t = tf.constant([1,2])
print(t.shape)
t1 = tf.expand_dims(t, 0)
print(t1.shape)
t2 = tf.expand_dims(t, 1)
print(t2.shape)
t3 = tf.expand_dims(t, 1)
print(t3.shape)
Output:
--------
(2,)
(1, 2)
(2, 1)
(2, 1)
--------
这项操作需要:
-1-input.dims() <= dim <= input.dims()
这个操作与tf.squeeze()相反,tf.squeeze()删除了size 1的维度。
参数:
- input: 一个张量。
- axis: 0-D(标量)。指定要在其中展开输入形状的维度索引。必须在[-rank(输入)- 1,rank(输入)]范围内。
- name: 输出张量的名称。
- dim: 0-D(标量)。相当于轴,要弃用。
返回值:
- 一个与输入数据相同的张量,但它的形状增加了尺寸为1的额外维数。
可能产生的异常:
ValueError
: if bothdim
andaxis
are specified.