tf.expand_dims和tf.squeeze函数

一、tf.expand_dims()

Function

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

Inserts a dimension of 1 into a tensor’s shape. 
在第axis位置增加一个维度

Given a tensor input, this operation inserts a dimension of 1 at the dimension index axis of input’s shape. The dimension index axis starts at zero; if you specify a negative number for axis it is counted backward from the end.

给定张量输入,此操作在输入形状的维度索引轴处插入1的尺寸。 尺寸索引轴从零开始; 如果您指定轴的负数,则从最后向后计数。

This operation is useful if you want to add a batch dimension to a single element. For example, if you have a single image of shape [height, width, channels], you can make it a batch of 1 image with expand_dims(image, 0), which will make the shape [1, height, width, channels].

如果要将批量维度添加到单个元素,则此操作非常有用。 例如,如果您有一个单一的形状[height,width,channels],您可以使用expand_dims(image,0)使其成为1个图像,这将使形状[1,高度,宽度,通道]。

Args:

input: A Tensor. 
axis: 0-D (scalar). Specifies the dimension index at which to expand the shape of input. 
name: The name of the output Tensor. 
dim: 0-D (scalar). Equivalent to axis, to be deprecated.

输入:张量。
轴:0-D(标量)。 指定扩大输入形状的维度索引。
名称:输出名称Tensor。
dim:0-D(标量)。 等同于轴,不推荐使用。

Returns:

A Tensor with the same data as input, but its shape has an additional dimension of size 1 added.

For example:

# '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()

Function

tf.squeeze(input, squeeze_dims=None, name=None)

Removes dimensions of size 1 from the shape of a tensor. 
从tensor中删除所有大小是1的维度

Given a tensor input, this operation returns a tensor of the same type with all dimensions of size 1 removed. If you don’t want to remove all size 1 dimensions, you can remove specific size 1 dimensions by specifying squeeze_dims.

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

Args:

input: A Tensor. The input to squeeze. 
squeeze_dims: An optional list of ints. Defaults to []. If specified, only squeezes the dimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. 
name: A name for the operation (optional).

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

Returns:

A Tensor. Has the same type as input. Contains the same data as input, but has one or more dimensions of size 1 removed.

张量。 与输入的类型相同。 包含与输入相同的数据,但具有一个或多个删除尺寸1的维度。

For example:

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

tensorflow 笔记14:tf.expand_dims和tf.squeeze函数的更多相关文章

  1. tf.expand_dims和tf.squeeze函数

    from http://blog.csdn.net/qq_31780525/article/details/72280284 tf.expand_dims() Function tf.expand_d ...

  2. tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask

    1.  tf.split(3, group, input)  # 拆分函数    3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...

  3. tensorflow笔记:使用tf来实现word2vec

    (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 (四) tensorflow笔 ...

  4. tensorflow笔记4:函数:tf.assign()、tf.assign_add()、tf.identity()、tf.control_dependencies()

    函数原型: tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)   Defined in tensorflo ...

  5. tensorflow 笔记11:tf.nn.dropout() 的使用

    tf.nn.dropout:函数官网说明: tf.nn.dropout( x, keep_prob, noise_shape=None, seed=None, name=None ) Defined ...

  6. tensorflow笔记6:tf.nn.dynamic_rnn 和 bidirectional_dynamic_rnn:的输出,output和state,以及如何作为decoder 的输入

    一.tf.nn.dynamic_rnn :函数使用和输出 官网:https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn 使用说明: A ...

  7. tensorflow 笔记 16:tf.pad

    函数: tf.compat.v1.pad tf.pad 函数表达式如下: tf.pad(    tensor,    paddings,    mode='CONSTANT',    name=Non ...

  8. tensorflow踩坑合集2. TF Serving & gRPC 踩坑

    这一章我们借着之前的NER的模型聊聊tensorflow serving,以及gRPC调用要注意的点.以下代码为了方便理解做了简化,完整代码详见Github-ChineseNER ,里面提供了训练好的 ...

  9. 机器学习笔记5-Tensorflow高级API之tf.estimator

    前言 本文接着上一篇继续来聊Tensorflow的接口,上一篇中用较低层的接口实现了线性模型,本篇中将用更高级的API--tf.estimator来改写线性模型. 还记得之前的文章<机器学习笔记 ...

随机推荐

  1. [NOIp2018提高组]货币系统

    [NOIp2018提高组]货币系统 题目大意: 有\(n(n\le100)\)种不同的货币,每种货币的面额为\([1,25000]\)之间的一个整数.若两种货币系统能够组合出来的数是相同的的,那我们就 ...

  2. BZOJ4912 : [Sdoi2017]天才黑客

    建立新图,原图中每条边在新图中是点,点权为$w_i$,边权为两个字符串的LCP. 对字典树进行DFS,将每个点周围一圈边对应的字符串按DFS序从小到大排序. 根据后缀数组利用height数组求LCP的 ...

  3. CDATA(不应由XML解析器进行解析的文本数据)、CDATA的使用场景

    1.1. CDATA: CDATA(Unparsed Character Data)指的是不应由XML解析器进行解析的文本数据. 因为XML解析器会将“<”(新元素的开始)和“&”(字符 ...

  4. vue中 如何使用less

    首先肯定是vue-cli全部就位: 1,安装依赖: npm install less less-loader --save 2,修改build-webpack.base.config.js文件,配置l ...

  5. JS的document.anchors函数使用示例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 两个UITableView的级联菜单

    效果 源码 https://github.com/YouXianMing/Animations 说明 1. TwoLevelLinkageView封装了两个tableView,左边tableView中 ...

  7. Unity插件扩展中组件常用的几个方法

    最近为美术编写一个Unity编辑器的扩展,主要为了减轻美术在修改预制对象时的机械化操作的繁琐和出错.具体实现的几个功能: 1.删除指定组件: 2.复制.粘贴指定的组件: 3.重新关联新的属性: 4.重 ...

  8. 对Android中的堆栈的理解(Stack)

      版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Ln_ZooFa/article/details/50337529 堆栈空间分配 栈(操作系统): ...

  9. 微信小程序开发注意事项总结:上拉加载失效、转义字符等

    1.上拉加载失效 问题背景:部分页面上拉加载失效.当使用flex布局,底部固定,中间采用自适应撑满全屏实现滚动时,发现上拉加载失效,不知道是什么原因. 解决问题: 在小程序中,官方为我们提供了原生的下 ...

  10. EasyMock 简单使用

    参考案例:(本位使用markdown编写)https://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/https://www.yii ...