TensorFlow用expand_dim()来增加维度的方法


Posted in Python onJuly 26, 2018

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]

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用expand_dim()来增加维度的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中的两个内置模块介绍
Apr 05 Python
python如何将图片转换为字符图片
Aug 19 Python
通过Python 接口使用OpenCV的方法
Apr 02 Python
python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程
May 22 Python
python多进程使用及线程池的使用方法代码详解
Oct 24 Python
python语言元素知识点详解
May 15 Python
python 并发编程 非阻塞IO模型原理解析
Aug 20 Python
Python使用matplotlib 画矩形的三种方式分析
Oct 31 Python
Selenium 滚动页面至元素可见的方法
Mar 18 Python
Python3爬虫关于识别检验滑动验证码的实例
Jul 30 Python
python实现AdaBoost算法的示例
Oct 03 Python
python使用pycharm安装pyqt5以及相关配置
Apr 22 Python
Python迭代器与生成器基本用法分析
Jul 26 #Python
Python实现矩阵相乘的三种方法小结
Jul 26 #Python
python实现泊松图像融合
Jul 26 #Python
python中的decorator的作用详解
Jul 26 #Python
python opencv实现旋转矩形框裁减功能
Jul 25 #Python
Python3匿名函数用法示例
Jul 25 #Python
Python实现动态添加属性和方法操作示例
Jul 25 #Python
You might like
一拳超人中怪人协会钦定! S级别最强四人!
2020/03/02 日漫
深入php之规范编程命名小结
2013/05/15 PHP
WordPress开发中的get_post_custom()函数使用解析
2016/01/04 PHP
php+redis在实际项目中HTTP 500: Internal Server Error故障排除
2017/02/05 PHP
用Javascript做flash做的事..才完成的一个类.Auntion Action var 0.1
2007/02/23 Javascript
图片连续滚动代码[兼容IE/firefox]
2009/06/11 Javascript
自写的一个jQuery圆角插件
2010/10/26 Javascript
JavaScript:Div层拖动效果实例代码
2013/08/06 Javascript
javascript实现计时器的简单方法
2016/02/21 Javascript
js操作XML文件的实现方法兼容IE与FireFox
2016/06/25 Javascript
jQuery实现左侧导航模块的显示与隐藏效果
2016/07/04 Javascript
Ajax的概述与实现过程
2016/11/18 Javascript
JavaScript原型继承_动力节点Java学院整理
2017/06/30 Javascript
在小程序中使用腾讯视频插件播放教程视频的方法
2018/07/10 Javascript
vue 地图可视化 maptalks 篇实例代码详解
2019/05/21 Javascript
vue2.0项目集成Cesium的实现方法
2019/07/30 Javascript
基于vue手写tree插件的那点事儿
2019/08/20 Javascript
vue.js中ref及$refs的使用方法解析
2019/10/08 Javascript
解决echarts数据二次渲染不成功的问题
2020/07/20 Javascript
Vue +WebSocket + WaveSurferJS 实现H5聊天对话交互的实例
2020/11/18 Vue.js
浅谈python for循环的巧妙运用(迭代、列表生成式)
2017/09/26 Python
浅谈Python中的作用域规则和闭包
2018/03/20 Python
python实现嵌套列表平铺的两种方法
2018/11/08 Python
Python实现微信自动好友验证,自动回复,发送群聊链接方法
2019/02/21 Python
Python数据类型之Set集合实例详解
2019/05/07 Python
selenium获取当前页面的url、源码、title的方法
2019/06/12 Python
Python如何调用JS文件中的函数
2019/08/16 Python
python爬虫爬取监控教务系统的思路详解
2020/01/08 Python
css3强大的动画效果animate使用说明及浏览器兼容介绍
2013/01/09 HTML / CSS
关于前端上传文件全面基础扫盲贴(入门)
2019/08/01 HTML / CSS
Bibloo奥地利:购买女装、男装、童装、鞋和配件
2018/10/18 全球购物
Book Depository欧盟:一家领先的国际图书零售商
2019/05/21 全球购物
护理专业个人求职简历的自我评价
2013/10/13 职场文书
后勤主管工作职责
2013/12/07 职场文书
单位未婚证明范本
2014/01/18 职场文书
关于python中模块和重载的问题
2021/11/02 Python