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


猜你喜欢
- 本文实例为大家分享了js动态时间显示 的具体代码,供大家参考,具体内容如下<!doctype html><html>
- centos7之Python3.74安装安装版本:Python3.74系统版本:centos7系统默认安装Python2.7,保留。安装/u
- 最基本的抓取网页内容的代码实现:#!/usr/bin/env python from urllib import urlretrieve d
- 本文实例为大家分享了python实现人民币转大写人民币的具体代码,供大家参考,具体内容如下直接上代码:# -*- coding: utf-8
- 1.安装模块Python 要使用 redis,需要先安装 redis 模块:pip install redis测试安装:redis 取出的结
- 在Python中,您可以使用inspect模块来查看一个函数的参数信息。inspect模块提供了许多用于检查对象的工具函数,其中包括用于获取
- Golang连接Redis数据库golang连接数据库,这里博主推荐使用go-redis这个库,理由很简单(连接数据库的操作类似在数据库里面
- 本文实例讲述了python常见数制转换用法。分享给大家供大家参考。具体分析如下:1.进位制度Python中二进制是以0b开头的:例如: 0b
- 一、背景介绍今天,野鸡大学高(三)班的月考成绩出来了,这里先给大家公布一下各位同学的考试成绩。接着,在给大家公布一下各位同学的生活消费情况。
- ASP.NET Core 中,可以使用 ConfigurationBuilder 对象来构建。主要分为三部:配置数据源 -> Conf
- 先看一个例子:<?phpclass A{ public $b; public $c; public function A() { &n
- 本文实例为大家分享了python和shell监控linux服务器的具体代码,供大家参考,具体内容如下1、 shell监控负载监控原理:使用u
- 阅读上一篇:FrontPage2002简明教程一:安装与界面FrontPage中对于文字与图像的处理与Word很相似,用过Word的人对于F
- 实这本是说明一个问题 : 每个人在提高自己能力这件事情上, 需要持续不断地努力。以最典型的例子来看,只有通过学习,程序员才能保证不断进步。
- 需求描述标准网关动态路由功能是重要的一环,将路由、断言以及过滤器信息,持久化到 Mysql 中,通过配置后台页面实现路由、断言、以及过滤器等
- python监控某个进程内存测试场景:某个客户端程序长时间运行后存在内存泄漏问题,现在开发解决了需要去验证这个问题是否还存在,并要求出具相应
- javascript/js的ajax的GET请求:<script type="text/javascript"&g
- 前言ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在2015年6月正式发布了。它的目标,是
- 前言:今天我教大家如何利用Python自动化操作Excel,包括:介绍操作Excel的工具包、安装方法及操作Excel具体方法。对于每天有大
- 有关 Web 字体的话题正在增多,对 Web 设计师来说,他们并不关注技术细节,不管是 TrueType 的 Hinting 技术