tf.expand_dims(input, dim, name=None)
我们平常在改变一个Tensor的shape时,一般会使用tf.reshape(input, shape=[]),但是有些时候在构建图的过程中,placeholder没有被feed具体的值,这时就会出现一个异常:TypeError: Expected binary or unicode string, got 1,
此时,我们就可以使用expand_dims来将维度加1。
方法实现:
参数为0代表添加在第一维,为-1代表添加到最后一维
1
2
one_img
=
tf.expand_dims(one_img,
0
)
one_img
=
tf.expand_dims(one_img,
-
1
)
实例测试:
1
2
3
4
5
6
7
8
9
# 't' is a tensor of shape [2]
shape(expand_dims(t,
0
))
=
=
> [
1
,
2
]
shape(expand_dims(t,
1
))
=
=
> [
2
,
1
]
shape(expand_dims(t,
-
1
))
=
=
> [
2
,
1
]
# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2,
0
))
=
=
> [
1
,
2
,
3
,
5
]
shape(expand_dims(t2,
2
))
=
=
> [
2
,
3
,
1
,
5
]
shape(expand_dims(t2,
3
))
=
=
> [
2
,
3
,
5
,
1
]