tf.expand_dims

tf.expand_dims(input, axis=None, name=None, dim=None
)

给定的张量input,axis为需要在第几维度扩充,axis=0表示在原有的张量的第一维扩充,axis=1表示在原有的张量的第二维扩充,axis=-1表示在原有的张量的最后一维扩充

如果要将批次尺寸添加到单个元素,此操作很有用。例如,如果您有一个shape的图像[height, width, channels],则可以用制作一批1张图像expand_dims(image, 0),这将使shape成为[1, height, width, channels]。

示例:

增加一个维度

import numpy as np
import tensorflow as tf
from numpy import arraycurrent = np.array([[0, 7, 1, 2, 2],[1, 7, 3, 4, 3],[2, 7, 5, 6, 6],[3, 7, 7, 8, 7],[4, 7, 7, 8, 7],[5, 7, 7, 8, 7]
])current = array(current)current = tf.constant(current)
points_e = tf.expand_dims(current, axis=0)#在第一维增加一维print(current)   #Tensor("Const:0", shape=(6, 5), dtype=int32)
print(points_e)  #Tensor("ExpandDims:0", shape=(1, 6, 5), dtype=int32)with tf.Session() as sess:print(sess.run(current))print(************************)print(sess.run(points_e))

结果:

[[0 7 1 2 2][1 7 3 4 3][2 7 5 6 6][3 7 7 8 7][4 7 7 8 7][5 7 7 8 7]]
*************************
[[[0 7 1 2 2][1 7 3 4 3][2 7 5 6 6][3 7 7 8 7][4 7 7 8 7][5 7 7 8 7]]]    #增加了一维

官方示例 shape维度

# '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]

tf.squeeze()

tf.squeeze(input, squeeze_dims=None, name=None)Removes dimensions of size 1 from the shape of a tensor.
从tensor中删除所有大小是1的维度

参数解释:

input:张量。 输入要挤压。
squeeze_dims:可选的ints列表。 默认为[]。 如果指定,只能挤压列出的尺寸。 维度索引从0开始。挤压不是1的维度是一个错误。
name:操作的名称(可选)。

给定张量输入,此操作返回相同类型的张量,并删除所有尺寸为1的尺寸。 如果不想删除所有尺寸1尺寸,可以通过指定squeeze_dims来删除特定尺寸1尺寸。
如果不想删除所有大小是1的维度,可以通过squeeze_dims指定需要删除的维度。

示例:

# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
Or, to remove specific size 1 dimensions:# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]

tf.expand_dims()和tf.squeeze()的用法详解相关推荐

  1. tf.expand_dims和tf.squeeze函数

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

  2. linux为什么用tar压缩,linux下tar压缩和解压命令用法详解

    linux下tar压缩和解压命令用法详解 2017-03-25 14:06 分享人:老牛 将/usr/local/test目录下所有文件仅打包,不压缩到 /usr/local/auto_bak/目下 ...

  3. python中permute_PyTorch中permute的用法详解

    PyTorch中permute的用法详解 permute(dims) 将tensor的维度换位. 参数:参数是一系列的整数,代表原来张量的维度.比如三维就有0,1,2这些dimension. 例: i ...

  4. python argv 详解_Python3 sys.argv[ ]用法详解

    sys.argv[]说白了就是一个从程序外部获取参数的桥梁,这个"外部"很关键,因为我们从外部取得的参数可以是多个,所以获得的是一个列表(list),也就是说sys.argv其实可 ...

  5. oracle中的exists 和 not exists 用法详解

    from:http://blog.sina.com.cn/s/blog_601d1ce30100cyrb.html oracle中的exists 和 not exists 用法详解 (2009-05- ...

  6. ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多)

    ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多) https://blog.csdn.net/qq_25221835/article/details/82762416 post ...

  7. python的继承用法_【后端开发】python中继承有什么用法?python继承的用法详解

    本篇文章给大家带来的内容是关于python中继承有什么用法?python继承的用法详解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 面向对象三大特征 1.封装:根据职责将属性和方法 ...

  8. C++中substr()函数用法详解

    C++中substr()函数用法详解 原型: string substr (size_t pos = 0, size_t len = npos) const; 返回一个新构造的string对象,其值初 ...

  9. php theme_path,PHP_Yii2主题(Theme)用法详解,本文实例讲述了Yii2主题(Theme) - phpStudy

    Yii2主题(Theme)用法详解 本文实例讲述了Yii2主题(Theme)用法.分享给大家供大家参考,具体如下: 首先看看主要的配置方式: 'components' => [ 'view' = ...

  10. LayoutInflater的inflate函数用法详解

    LayoutInflater的inflate函数用法详解 LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ...

最新文章

  1. setup vaio winxp
  2. 福禄克2500V数字绝缘电阻测试仪开箱与上手实测
  3. TCP/IP总结(4)TCP 之最大消息长度
  4. 阿里月薪5万招程序员,招聘要求让人窒息!
  5. CodeForces - 622C Not Equal on a Segment(思维+水题)
  6. 一套优雅的 Go 错误问题解决方案
  7. How is data replicted from HANA to AS
  8. oracle 实现 drop table if exists
  9. [转]粤语发音字典-粤语翻译
  10. 织梦cms响应式站长导航分类网站模板(自适应手机版)
  11. 初步了解关于js跨域问题
  12. ROS2 Galactic Depth to World coordinate RGBD相机的深度数据转换到世界坐标系
  13. 传智播客 C/C++学习笔记 字符串替换
  14. linux下创造进程指令,Linux系统创建一个新进程(下)
  15. 网站smtp服务器,SMTP服务器
  16. windows7计算机图片,win7照片查看器无法显示图片计算机可用内存不足 需要技巧...
  17. matlab资产组合最优配置,[转载]资产组合有效前沿的解和最优解(MATLAB语言)
  18. 冷山的博客思听书摘索引页
  19. 【JDBC】连接MySQL数据库实现增删改查
  20. Zookeeper框架Curator使用

热门文章

  1. 操作系统语言包在c盘哪里,win10系统通过卸载语言包释放c盘空间的具体教程
  2. 详解C语言中的#define、#undef、#indef、#ifndef、#else、#endif,#if,#elif
  3. QT多线程之:moveToThread
  4. 74.网络安全渗透测试—[SQL注入篇13]—[SQLSERVER+ASP-执行系统命令]
  5. 2099年的flag——writeup
  6. python基础之文件处理
  7. RS-485详解(一)
  8. 修改opencv3.4 VideoCapture dshow模块以支持圆刚cv710等需要配置CrossBar的采集卡
  9. 英语词汇服饰篇——Bottoms下装
  10. 打造数字化软件工厂 —— 一站式 DevOps 平台全景解读