Pytorch之扩充tensor的操作
作者:有为少年 发布时间:2023-10-09 01:41:59
我就废话不多说了,大家还是直接看代码吧~
b = torch.zeros((3, 2, 6, 6))
a = torch.zeros((3, 2, 1, 1))
a.expand_as(b).size()
Out[32]: torch.Size([3, 2, 6, 6])
a = torch.zeros((3, 2, 2, 1))
a.expand_as(b).size()
Traceback (most recent call last):
File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-34-972575f79e92>", line 1, in <module>
a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 2. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 2, 1]
a = torch.zeros((3, 2, 1, 2))
a.expand_as(b).size()
Traceback (most recent call last):
File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-36-972575f79e92>", line 1, in <module>
a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 1, 2]
a = torch.zeros((3, 2, 2, 2))
a.expand_as(b).size()
Traceback (most recent call last):
File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-38-972575f79e92>", line 1, in <module>
a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 2, 2]
a = torch.zeros((3, 2, 6, 2))
a.expand_as(b).size()
Traceback (most recent call last):
File "/home/lart/.conda/envs/pt/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-40-972575f79e92>", line 1, in <module>
a.expand_as(b).size()
RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 3. Target sizes: [3, 2, 6, 6]. Tensor sizes: [3, 2, 6, 2]
a = torch.zeros((3, 2, 6, 1))
a.expand_as(b).size()
Out[44]: torch.Size([3, 2, 6, 6])
a = torch.zeros((3, 2, 1, 6))
a.expand_as(b).size()
Out[46]: torch.Size([3, 2, 6, 6])
tensor.expand_as在这里用于扩展tensor到目标形状,常用的多是在H和W方向上的扩展。
假设目标形状为N, C, H, W,则要求tensor.size()=n, c, h, w(这里假设N,C不变):
1、h=w=1
2、h=1, w!=1
3、h!=1, w=1
补充:tensorflow 利用expand_dims和squeeze扩展和压缩tensor维度
在利用tensorflow进行文本挖掘工作的时候,经常涉及到维度扩展和压缩工作。
比如对文本进行embedding操作完成之后,若要进行卷积操作,就需要对embedded的向量扩展维度,将[batch_size, embedding_dims]扩展成为[batch_size, embedding_dims, 1],利用tf.expand_dims(input, -1)就可实现,反过来用squeeze(input, -1)或者tf.squeeze(input)也可以把最第三维去掉。
tf.expand_dims()
tf.squeeze()
tf.expand_dims()
tf.expand_dims(input, axis=None, name=None, dim=None)
在第axis位置增加一个维度.
给定张量输入,此操作在输入形状的维度索引轴处插入1的尺寸。 尺寸索引轴从零开始; 如果您指定轴的负数,则从最后向后计数。
如果要将批量维度添加到单个元素,则此操作非常有用。 例如,如果您有一个单一的形状[height,width,channels],您可以使用expand_dims(image,0)使其成为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]
tf.squeeze()
tf.squeeze(input, axis=None, name=None, squeeze_dims=None)
直接上例子
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/P_LarT/article/details/90145088
猜你喜欢
- 缘起最近复习设计模式拜读谭勇德的<<设计模式就该这样学>>该书以java语言演绎了常见设计模式本系列笔记拟采用gol
- 如何对设计进行评判,一定有很多答案,有利有弊。问100位设计师,会得到100种回答。用线上PV、UV等数据说话,更多受产品属性、运营动作影响
- 朴素贝叶斯算法简单高效,在处理分类问题上,是应该首先考虑的方法之一。通过本教程,你将学到朴素贝叶斯算法的原理和Python版本的逐步实现。更
- 本文实例讲述了Python堆排序原理与实现方法。分享给大家供大家参考,具体如下:在这里要事先说明一下我也是新手,很多东西我了解不是很深入,写
- 如何显示数据库里的图片?asp调用数据库中的图片并显示。怎样把数据库里的图片显示出来?我们以gif格式的图片为例,代码如下:showimag
- 上一篇我们写了怎么将xmind转换成想要的excel格式,这篇再讲一下用Python自带的tkinter库设计一个简单的gui界面,让我们的
- 前言TensorFlow目前在移动端是无法training的,只能跑已经训练好的模型,但一般的保存方式只有单一保存参数或者graph的,如何
- 简介使用python实现pygame版的飞机大战游戏;环境:Windows系统+python3.8.0游戏规则:1.点击&ldquo
- python获取当前运行函数名称的方法实例代码摘要: c/c++中获取函数所在源码名,函数名和行号的方法很简单 __FILE__,__FUN
- 1.介绍DeLorean是一个Python的第三方模块,基于 pytz 和 dateutil 开发,用于处理Python中日期时间的格式转换
- 定义线程最简单的方法:使用target指定线程要执行的目标函数,再使用start()启动。语法:class threading.Thread
- 本文实例讲述了python开发中range()函数用法。分享给大家供大家参考,具体如下:python中的range()函数的功能很强大,所以
- 本文实例为大家分享了python实现单链表反转的具体代码,供大家参考,具体内容如下代码如下:class Node(object): 
- 前面的文章,主要讲到如何使用无序列表ul元素来实现复杂柱状图,但是在Web标准中,除了注重表现外,更加注重语意,所谓的语意就是样式和内容的相
- 今天看了微软JScript官方blog上去年的两篇文章: http://blogs.msdn.com/jscript/archive/200
- 话不多说,小工具需求如下: 功能需求 -- 电脑开机后自动执行时间同步 非功能需求 -- 安装执行简单,无需安装额外环境一、代码实现基于以上
- 1.SGD随机梯度下降随机梯度下降和其他的梯度下降主要区别,在于SGD每次只使用一个数据样本,去计算损失函数,求梯度,更新参数。这种方法的计
- 1. 引言星号* 往往被称为乘法运算符,是所有程序中最为常用的运算符号之一,在Python 中,星号还有很多隐藏的强大功能。本文将用最容易理
- 以下函数采用FSO对象,文件位置在FSO.ASP。FSO对象的文件编码属性只有三种,系统默认,Unicode,ASCII,并没有我们要的ut
- 【原文地址】New "Orcas" Language Feature: Query Syntax 【原文发表日期】 Sa