TensorFlow中,想要维度增加一维,可以使用tf.expand_dims(input, dim, name=None)函数。当然,我们常用tf.reshape(input, shape=[])也可以达到相同效果,但是有些时候在构建图的过程中,placeholder没有被feed具体的值,这时就会包下面的错误:TypeError: Expected binary or unicode string, got 1 
在这种情况下,我们就可以考虑使用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' 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]

举例:

import tensorflow as tfa = tf.constant([[1, 2], [3, 4], [5, 6]], dtype=tf.float32)
a0 = tf.expand_dims(a, 0)
a1 = tf.expand_dims(a, 1)
a2 = tf.expand_dims(a, 2)
with tf.Session() as sess:print(sess.run(a))print('------------')print(sess.run(a0))print('------------')print(sess.run(a1))print('------------')print(sess.run(a2))

结果:

[[1. 2.][3. 4.][5. 6.]]
------------
[[[1. 2.][3. 4.][5. 6.]]]
------------
[[[1. 2.]][[3. 4.]][[5. 6.]]]
------------
[[[1.][2.]][[3.][4.]][[5.][6.]]]

Args: 
input: A Tensor. 
dim: A Tensor. Must be one of the following types: int32, int64. 0-D (scalar). Specifies the dimension index at which to expand the shape of input. 
name: A name for the operation (optional).

Returns: 
A Tensor. Has the same type as input. Contains the same data as input, but its shape has an additional dimension of size 1 added.

【TensorFlow】tf.expand_dims()函数相关推荐

  1. TensorFlow tf.expand_dims

    在axis的索引中,插入大小为1的维度.维度索引从0开始,负数是从尾部开始索引. 当有一个图像的数据[height,width,channels],可以使用expand_dims(image,0),将 ...

  2. Tensorflow tf.placeholder函数

    tf.placeholder(dtype, shape=None, name=None) 此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值 参数: dtype:数据类型.常用的是tf.fl ...

  3. tensorflow常用数据函数总结(tf.tile()、tf.expand_dims())

    tf.tile() tensorflow中的tile()函数是用来对张量(Tensor)进行扩展的,其特点是对当前张量内的数据进行一定规则的复制.最终的输出张量维度不变.也就是说tile可以某一维度的 ...

  4. tensorflow的tf.reduce_mean函数

    tf.reduce_mean函数的作用是求平均值.第一个参数是一个集合,可以是列表.二维数组和多维数组.第二个参数指定在哪个维度上面求平均值.默认对所有的元素求平均.tf.reduce_mean 比如 ...

  5. 【转载】使用tf.py_func函数增加Tensorflow程序的灵活性

    转自:https://blog.csdn.net/jiongnima/article/details/80555387 目录 tf.py_func函数接口 tf.py_func在Faster R-CN ...

  6. 【python】tensorflow框架中的tf.gather_nd()函数对应的 pytorch框架的gather_nd()函数

    tf.gather_nd 函数对应的pytorch函数 1. 简单介绍 2. 步入正题 2.1 tensorflow tf.gather_nd() 2.2 pytorch框架手动实现gather_nd ...

  7. 经验干货:使用tf.py_func函数增加Tensorflow程序的灵活性

    不知不觉,笔者接触Tensorflow也满一年了.在这一年当中,笔者对Tensorflow的了解程度也逐渐加深.相比笔者接触的第一个深度学习框架Caffe而言,笔者认为Tensorflow更适合科研一 ...

  8. tensorflow中tf.get_variable()函数详解

    如果变量存在,函数tf.get_variable()会返回现有的变量:如果变量不存在,会根据给定形状和初始值创建一个新的变量. def get_variable(name, shape=None, d ...

  9. 【tensorflow】tf.reshape函数说明:重塑张量

    转载 [471]tf.reshape函数说明_周小董-CSDN博客 https://blog.csdn.net/xc_zhou/article/details/85342542 函数原型: tf.re ...

  10. tf.expand_dims和tf.squeeze函数

    tf.expand_dims() Function tf.expand_dims(input, axis=None, name=None, dim=None) Inserts a dimension ...

最新文章

  1. 【多标签文本分类】Improved Neural Network-based Multi-label Classification with Better Initialization ……
  2. 开源Math.NET基础数学类库使用(11)C#计算相关系数
  3. MySQL存储过程之代码块、条件控制、迭代
  4. T extends ComparableT和T extends Comparable? super T含义
  5. Asp.net 关于错误提示 类型“XXX1”在未被引用的程序集中定义,必须添加对程序集XXX2的引用...
  6. IrregularGridCollectionView处理不定宽度的标签cell
  7. 软件架构阅读笔记(引)
  8. Meidapipe 3D手势姿态跟踪算法,手机端实时检测 ,多个手势同时捕捉
  9. linux系统下聊天工具,linux系统环境下如何使用amsn聊天工具_linux教程
  10. typecho 去掉index.php,Typecho设置伪静态去掉url中的index.php
  11. 入门OJ:最短路径树入门
  12. 在Windows上安装TeXLive
  13. oracle11g broker,张欣橙Oracle11g配置DGbroker
  14. 利用html2canvas和vue-qr生成带头像二维码的分享海报(二)
  15. 小白入门之HTML--第三章 CSS样式基础
  16. java安卓计时器_如何在android中设置计时器
  17. 视频直播技术详解之现代播放器原理
  18. 深度:养老康复器械龙头即将上市,美的、新松进军养老康复机器人,老龄化加速千亿康复市场到来!
  19. 如何在抖音官网链接中一键复制微信号跳转微信
  20. 生成Aprilgrid标定板

热门文章

  1. Element动态换肤
  2. C语言程序——输入三角形的边长求面积
  3. 【C语言程序】求直角三角形边长
  4. 阿里聚石塔服务张北迁移问题
  5. Android 端发送邮件
  6. Python-PyGame 坦克大战小游戏
  7. 矩阵的特征值和特征向量
  8. linux中PATH环境变量的作用和使用方法
  9. DBVisualizer导入excel数据
  10. C++多线程传参详解