【tensorflow】tf.squeeze用法

idea 编译打包nacos2.0.3源码,生成可执行jar 包常见问题 阅读详情

tf.squeeze用法

  • 作用
    tf.squeeze的作用是移除给定的tensor中大小为1的维度。
    源码是:
def squeeze(input, axis=None, name=None, squeeze_dims=None):
  # pylint: disable=redefined-builtin
  """Removes dimensions of size 1 from the shape of a tensor.

  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
  `axis`.

  For example:


  # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
  tf.shape(tf.squeeze(t))  # [2, 3]


  Or, to remove specific size 1 dimensions:


  # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
  tf.shape(tf.squeeze(t, [2, 4]))  # [1, 2, 3, 1]


  Args:
    input: A `Tensor`. The `input` to squeeze.
    axis: 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.
      Must be in the range `[-rank(input), rank(input))`.
    name: A name for the operation (optional).
    squeeze_dims: Deprecated keyword argument that is now axis.

  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.

  Raises:
    ValueError: When both `squeeze_dims` and `axis` are specified.
  """
  if squeeze_dims is not None:
    if axis is not None:
      raise ValueError("Cannot specify both 'squeeze_dims' and 'axis'")
    axis = squeeze_dims
  if np.isscalar(axis):
    axis = [axis]
  return gen_array_ops.squeeze(input, axis, name)

举例说明用法:

import tensorflow as tf
a = tf.ones(shape=[3,1,4,1,2,1,5])
b = tf.squeeze(a)
sess = tf.InteractiveSession()

print(a.shape)

print(b)

输出为:

(3, 1, 4, 1, 2, 1, 5)
Tensor("Squeeze:0", shape=(3, 4, 2, 5), dtype=float32)
从零搭建Obsidian智能知识库:Ollama本地模型与Copilot插件实战指南 本文详细介绍了如何从零搭建Obsidian智能知识库,结合Ollama本地模型与Copilot插件实现高效知识管理。通过本地部署AI助手,用户可以在保证隐私安全的同时,快速检索和整理笔记,提升工作效率。文章涵盖硬件准备、Ollama部署、插件配置及实战技巧,适合需要高效知识管理的专业人士。 阅读详情

相关推荐

GB_T 9254.1-2021.rar

GB_T 9254.1-2021.rar

tf.squeeze()函数

tf.squeeze()函数用于从张量形状中移除大小为1的维度 squeeze( input, axis=None, name=None, squeeze_dims=None ) 给定张量输入,此操作返回相同类型的张量,并删除所有维度为1的维度。 如果不想删除所有维度1维度,可以通过指定squeeze_dims来删除特定维度1维度。 如果不想删除所有大...

qq_42450404的博客 2万+

tensorflow 利用expand_dims和squeeze扩展和压缩tensor维度方式

今天小编就为大家分享一篇tensorflow 利用expand_dims和squeeze扩展和压缩tensor维度方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

tf.squeeze

可以通过指定axis来删除特定维度1的维度如values为shape([1,2,1,3,1,1]),经历tf.squeeze(values,[0]),shape变成([2,1,3,1,1])压缩这个张量如values为shape([1,2,1,3,1,1]),经历tf.squeeze(values),shape变成([2,3])

Fwuyi的博客 1449

TensorFlow 移除所有尺度为1的维度 tf.squeeze 的基本用法及实例代码

一、环境 TensorFlow API r1.12 CUDA 9.2 V9.2.148 cudnn64_7.dll Python 3.6.3 Windows 10   二、官方说明 从张量的形状中移除所有尺寸为1的维数。(弃用参数) https://tensorflow.google.cn/api_docs/python/tf/squeeze tf.squeeze( ...

sdnuwjw的博客 1万+

tensorflow去掉某一维度_TensorFlow 移除所有尺度为1的维度 tf.squeeze 的基本用法及实例代码...

一、环境TensorFlow API r1.12CUDA 9.2 V9.2.148cudnn64_7.dllPython 3.6.3Windows 10二、官方说明从张量的形状中移除所有尺寸为1的维数。(弃用参数)https://tensorflow.google.cn/api_docs/python/tf/squeezetf.squeeze(input,axis=None,name=None,s...

weixin_39760619的博客 2368

tensorflow将张量增/减一个维度:tf.expand_dims()/tf.squeeze()

1、TensorFlow中,想要维度增加一维,可以使用tf.expand_dims(input, dim, name=None)函数。 tf.expand_dims( input, axis=None, name=None, dim=None ) 参数: input是输入张量。 axis是指定扩大输入张量形状的维度索引值。 dim等同...

遇事不决可问春风的博客 8341

tensorflow 笔记14:tf.expand_dims和tf.squeeze函数

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 ...

weixin_33842328的博客 207

TensorFlow常用函数tf.where()、tf.gather()、tf.squeeze()详解!!

1.tf.where() 第一种用法: where(condition)的用法 where(condition, x=None, y=None, name=None)  condition是bool型值,True/False 返回值,是condition中元素为True对应的索引 例如: import tensorflow as tf a = [[1,2,3],[4,5,6]] b = [[1,0,3],[1,5,1]] condition1 = [[True,False,False],

wangxuecheng666的博客 2293

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. (给input的形状在第axis位置增加一个维度) Given a tensor input, this operation inser...

遥远的她 527

tensorflow】: 理解tf.squeeze()

理解tf.squeeze() squeeze( input, axis=None, name=None, squeeze_dims=None ) 该函数返回一个张量,这个张量是将原始input中所有维度为1的那些维都删掉的结果 转自:https://www.jianshu.com/p/a21c0bc10a38

Jack_Kuo的博客 986

tf计算矩阵维度_在TensorFlow中实现矩阵维度扩展

一般TensorFlow中扩展维度可以使用tf.expand_dims()。近来发现另一种可以直接运用取数据操作符[]就能扩展维度的方法。用法很简单,在要扩展的维度上加上tf.newaxis就行了。foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])print(foo[tf.newaxis, :, :].eval()) # => [[[1,2,3], ...

weixin_34276402的博客 345

tensorflow学习笔记(2):tf.clip_by_value,tf.expand_dims等函数的用法

1)tf.clip_by_value的用法 该函数主要是为了防止,gradiant计算得到的值太大或者太小 tf.clip_by_value(A, min, max):输入一个张量A,把A中的每一个元素的值都压缩在min和max之间。小于min的让它等于min,大于max的元素的值等于max。 import tensorflow as tf import numpy as np dat

自然语言处理技术 1105

tf.squeeze() Function函数作用

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...

云中寻雾的博客 1554

python global用法_python+tensorflow 阅读笔记

202009181、if __name__ == '__main__':的作用Python中if __name__ == '__main__':的作用和原理​blog.csdn.net2、如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test)如果你的代码中的入口函数叫main(),则你就可以把入口写成tf.app.run...

weixin_39638086的博客 143

tf.gather和tf.gather_nd

转自:https://blog.csdn.net/Cyiano/article/details/76087747 Tensorflow常用函数笔记 tf.concat 把一组向量从某一维上拼接起来,很向numpy中的Concatenate,官网例子: t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat(...

orangefly0214的博客 1万+

tf 矩阵行和列交换_【TF2.1学习笔记4】张量基本操作

大纲维度变换改变视图:tf.reshape(x, shape)增、删维度:tf.expand_dims(x, axis)和tf.squeeze(x, axis)变换维度:tf.transpose(x, perm)复制数据:tf.tile(x, multiples)广播四则运算乘方运算指数和对数矩阵运算1. 维度变换1.1 改变视图一个张量有存储和视图两个概念。存储指将张量保存在一个连续的内存区域内...

weixin_28818559的博客 376

TensforFlow中常用函数总结

TensforFlow中常用函数总结 tf.nn.bias_add(value,bias,name = None): 一个叫bias的向量加到一个叫value的矩阵上,是向量与矩阵的每一行进行相加,得到的结果和value矩阵大小相同。 import tensorflow as tf a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32) b=tf...

gzr2018的博客 396

TensorFlow各种操作汇总

TensorFlow各种操作 一图看懂tensorflow核心概念和流程 https://zhuanlan.zhihu.com/p/33801947 一个框架看懂优化算法之异同 https://zhuanlan.zhihu.com/p/32230623 详解深度学习中的normalization https://zhuanlan.zhihu.com/p/33173246 https://yq.al...

haoshan4783的博客 599
上一篇: 【tensorflow】tensorflow:Scale of 0 disables regularizer原因
下一篇: 【Leetcode】Leetcode 75.颜色分类
voidfaceless
博客等级 码龄10年 100粉丝 114原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值