tf.expand_dims()
转载:https://blog.csdn.net/jasonzzj/article/details/60811035
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.
tf.expand_dims()相关推荐
- tf.expand_dims() 的用法
tf.expand_dims() 增加张量的维度 [ ]代表维度,由外向里数维度. import tensorflow as tf t = tf.constant([1,2]) # 创建一个张量 t ...
- tf.expand_dims 来增加维度
主要是因为tflearn官方的例子总是有embeding层,去掉的话要conv1d正常工作,需要加上expand_dims network = input_data(shape=[None, 100] ...
- TensorFlow tf.expand_dims
在axis的索引中,插入大小为1的维度.维度索引从0开始,负数是从尾部开始索引. 当有一个图像的数据[height,width,channels],可以使用expand_dims(image,0),将 ...
- tensorflow常用数据函数总结(tf.tile()、tf.expand_dims())
tf.tile() tensorflow中的tile()函数是用来对张量(Tensor)进行扩展的,其特点是对当前张量内的数据进行一定规则的复制.最终的输出张量维度不变.也就是说tile可以某一维度的 ...
- tf.expand_dims()和tf.squeeze()的用法详解
tf.expand_dims tf.expand_dims(input, axis=None, name=None, dim=None ) 给定的张量input,axis为需要在第几维度扩充,axis ...
- tf.expand_dims
tf.expand_dims(input, #输入tensoraxis, #要在输入的tensor的第几个维度增加1个维度name=None ) 这个函数根据axis在原本的tensor的某个维度上增 ...
- tf.expand_dims和tf.squeeze函数
tf.expand_dims() Function tf.expand_dims(input, axis=None, name=None, dim=None) Inserts a dimension ...
- 深度学习原理与框架-CNN在文本分类的应用 1.tf.nn.embedding_lookup(根据索引数据从数据中取出数据) 2.saver.restore(加载sess参数)...
1. tf.nn.embedding_lookup(W, X) W的维度为[len(vocabulary_list), 128], X的维度为[?, 8],组合后的维度为[?, 8, 128] 代码说 ...
- 通俗易懂!使用Excel和TF实现Transformer
作者 | 石晓文 转载自小小挖掘机(ID:wAIsjwj) 本文旨在通过最通俗易懂的过程来详解Transformer的每个步骤! 假设我们在做一个从中文翻译到英文的过程,我们的词表很简单如下: 中文词 ...
最新文章
- 使用Prometheus和Grafana实现SLO
- wxWidgets:wxChoice类用法
- 清橙 A1120 拦截导弹 -- 动态规划(最长上升子序列)
- 每周一书《Spark与Hadoop大数据分析》分享!
- SAPGUI里实现自定义的语法检查
- 关于es查询dsl的filter与must,term与match的区别
- Java加密与解密的艺术~数字签名~模型分析
- 东欧视频游戏市场概况
- CCNA认证(2)--网络互联基础
- android Camera 中的相关概念
- 如何将exe文件在linux下执行,[操作系统]如何在linux centos 环境下运行.exe文件
- C语言程序设计答何钦铭答案,c语言程序设计何钦铭课后题答案
- 深度学习:卷积神经网络(详解版)
- 【观察】飞象工业互联网平台,阿里云IoT的沉淀与释放
- Gartner:新兴技术成熟度曲线2018(中文—历年)
- Excel批量调整图片大小适应单元格且整齐排列
- 项目管理铁三角:追求价值还是约束条件
- 【NISP一级】4.1 Windows终端安全
- XSS挑战之旅(1-9)
- 用svg画一个微信订阅号的图标
热门文章
- 【golang程序包推荐分享】分享亿点点golang json操作及myJsonMarshal程序包开发的踩坑经历 :)
- IDEA集成Docker插件实现一键自动打包部署微服务项目
- Go 学习笔记(63)— Go 中的 for ... range 对切片和数组的差异
- Mysql创建数据库用户
- RPC远程调用通俗理解
- Pytorch的网络结构可视化(tensorboardX)(详细)
- 从PyTorch到ONNX的端到端AlexNet
- 时间和邮箱的正则表达式,获取当前时间函数
- A + B Problem
- 微信小程序模板template