TensorFlow用expand_dim()来增加维度的方法
TensorFlow中,想要维度增加一维,可以使用tf.expand_dims(input,dim,name=None)函数。当然,我们常用tf.reshape(input,shape=[])也可以达到相同效果,但是有些时候在构建图的过程中,placeholder没有被feed具体的值,这时就会包下面的错误:TypeError:Expectedbinaryorunicodestring,got1
在这种情况下,我们就可以考虑使用expand_dims来将维度加1。比如我自己代码中遇到的情况,在对图像维度降到二维做特定操作后,要还原成四维[batch,height,width,channels],前后各增加一维。如果用reshape,则因为上述原因报错
one_img2=tf.reshape(one_img,shape=[1,one_img.get_shape()[0].value,one_img.get_shape()[1].value,1])
用下面的方法可以实现:
one_img=tf.expand_dims(one_img,0) one_img=tf.expand_dims(one_img,-1)#-1表示最后一维
在最后,给出官方的例子和说明
#'t'isatensorofshape[2] shape(expand_dims(t,0))==>[1,2] shape(expand_dims(t,1))==>[2,1] shape(expand_dims(t,-1))==>[2,1] #'t2'isatensorofshape[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]
Args:
input:ATensor.
dim:ATensor.Mustbeoneofthefollowingtypes:int32,int64.0-D(scalar).Specifiesthedimensionindexatwhichtoexpandtheshapeofinput.
name:Anamefortheoperation(optional).
Returns:
ATensor.Hasthesametypeasinput.Containsthesamedataasinput,butitsshapehasanadditionaldimensionofsize1added.
以上这篇TensorFlow用expand_dim()来增加维度的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下