iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >pytorch中的squeeze函数、cat函数使用
  • 324
分享到

pytorch中的squeeze函数、cat函数使用

2024-04-02 19:04:59 324人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

1 squeeze(): 去除size为1的维度,包括行和列。 至于维度大于等于2时,squeeze()不起作用。 行、例: >>> torch.rand(4,

1 squeeze(): 去除size为1的维度,包括行和列。

至于维度大于等于2时,squeeze()不起作用。

行、例:


>>> torch.rand(4, 1, 3)
 
(0 ,.,.) =
  0.5391  0.8523  0.9260
 
(1 ,.,.) =
  0.2507  0.9512  0.6578
 
(2 ,.,.) =
  0.7302  0.3531  0.9442
 
(3 ,.,.) =
  0.2689  0.4367  0.6610
[torch.FloatTensor of size 4x1x3]

>>> torch.rand(4, 1, 3).squeeze()
 
 0.0801  0.4600  0.1799
 0.0236  0.7137  0.6128
 0.0242  0.3847  0.4546
 0.9004  0.5018  0.4021
[torch.FloatTensor of size 4x3]

列、例:


>>> torch.rand(4, 3, 1)
 
(0 ,.,.) =
  0.7013
  0.9818
  0.9723
 
(1 ,.,.) =
  0.9902
  0.8354
  0.3864
 
(2 ,.,.) =
  0.4620
  0.0844
  0.5707
 
(3 ,.,.) =
  0.5722
  0.2494
  0.5815
[torch.FloatTensor of size 4x3x1]

>>> torch.rand(4, 3, 1).squeeze()
 
 0.8784  0.6203  0.8213
 0.7238  0.5447  0.8253
 0.1719  0.7830  0.1046
 0.0233  0.9771  0.2278
[torch.FloatTensor of size 4x3]

不变、例:


>>> torch.rand(4, 3, 2)
 
(0 ,.,.) =
  0.6618  0.1678
  0.3476  0.0329
  0.1865  0.4349
 
(1 ,.,.) =
  0.7588  0.8972
  0.3339  0.8376
  0.6289  0.9456
 
(2 ,.,.) =
  0.1392  0.0320
  0.0033  0.0187
  0.8229  0.0005
 
(3 ,.,.) =
  0.2327  0.6264
  0.4810  0.6642
  0.8625  0.6334
[torch.FloatTensor of size 4x3x2]

>>> torch.rand(4, 3, 2).squeeze()
 
(0 ,.,.) =
  0.0593  0.8910
  0.9779  0.1530
  0.9210  0.2248
 
(1 ,.,.) =
  0.7938  0.9362
  0.1064  0.6630
  0.9321  0.0453
 
(2 ,.,.) =
  0.0189  0.9187
  0.4458  0.9925
  0.9928  0.7895
 
(3 ,.,.) =
  0.5116  0.7253
  0.0132  0.6673
  0.9410  0.8159
[torch.FloatTensor of size 4x3x2]

2 cat函数


>>> t1=torch.FloatTensor(torch.randn(2,3))
>>> t1
 
-1.9405  1.2009  0.0018
 0.9463  0.4409 -1.9017
[torch.FloatTensor of size 2x3]

>>> t2=torch.FloatTensor(torch.randn(2,2))
>>> t2
 
 0.0942  0.1581
 1.1621  1.2617
[torch.FloatTensor of size 2x2]

>>> torch.cat((t1, t2), 1)
 
-1.9405  1.2009  0.0018  0.0942  0.1581
 0.9463  0.4409 -1.9017  1.1621  1.2617
[torch.FloatTensor of size 2x5]

补充:pytorch中 max()、view()、 squeeze()、 unsqueeze()

查了好多博客都似懂非懂,后来写了几个小例子,瞬间一目了然。

一、torch.max()


import torch  
a=torch.randn(3)
print("a:\n",a)
print('max(a):',torch.max(a))
 
b=torch.randn(3,4)
print("b:\n",b)
print('max(b,0):',torch.max(b,0))
print('max(b,1):',torch.max(b,1))

输出:

a:
tensor([ 0.9558, 1.1242, 1.9503])
max(a): tensor(1.9503)
b:
tensor([[ 0.2765, 0.0726, -0.7753, 1.5334],
[ 0.0201, -0.0005, 0.2616, -1.1912],
[-0.6225, 0.6477, 0.8259, 0.3526]])
max(b,0): (tensor([ 0.2765, 0.6477, 0.8259, 1.5334]), tensor([ 0, 2, 2, 0]))
max(b,1): (tensor([ 1.5334, 0.2616, 0.8259]), tensor([ 3, 2, 2]))

max(a),用于一维数据,求出最大值。

max(a,0),计算出数据中一列的最大值,并输出最大值所在的行号。

max(a,1),计算出数据中一行的最大值,并输出最大值所在的列号。


print('max(b,1):',torch.max(b,1)[1])

输出:只输出行最大值所在的列号


max(b,1): tensor([ 3,  2,  2])

torch.max(b,1)[0], 只返回最大值的每个数

二、view()

a.view(i,j)表示将原矩阵转化为i行j列的形式

i为-1表示不限制行数,输出1列


a=torch.randn(3,4)
print(a)

输出:

tensor([[-0.8146, -0.6592, 1.5100, 0.7615],
[ 1.3021, 1.8362, -0.3590, 0.3028],
[ 0.0848, 0.7700, 1.0572, 0.6383]])

b=a.view(-1,1)
print(b)

输出:

tensor([[-0.8146],
[-0.6592],
[ 1.5100],
[ 0.7615],
[ 1.3021],
[ 1.8362],
[-0.3590],
[ 0.3028],
[ 0.0848],
[ 0.7700],
[ 1.0572],
[ 0.6383]])

i为1,j为-1表示不限制列数,输出1行


b=a.view(1,-1)
print(b)

输出:

tensor([[-0.8146, -0.6592, 1.5100, 0.7615, 1.3021, 1.8362, -0.3590,
0.3028, 0.0848, 0.7700, 1.0572, 0.6383]])

i为-1,j为2表示不限制行数,输出2列


b=a.view(-1,2)
print(b)

输出:

tensor([[-0.8146, -0.6592],
[ 1.5100, 0.7615],
[ 1.3021, 1.8362],
[-0.3590, 0.3028],
[ 0.0848, 0.7700],
[ 1.0572, 0.6383]])

i为-1,j为3表示不限制行数,输出3列

i为4,j为3表示输出4行3列


b=a.view(-1,3)
print(b)
b=a.view(4,3)
print(b)

输出:

tensor([[-0.8146, -0.6592, 1.5100],
[ 0.7615, 1.3021, 1.8362],
[-0.3590, 0.3028, 0.0848],
[ 0.7700, 1.0572, 0.6383]])
tensor([[-0.8146, -0.6592, 1.5100],
[ 0.7615, 1.3021, 1.8362],
[-0.3590, 0.3028, 0.0848],
[ 0.7700, 1.0572, 0.6383]])

三、

1.torch.squeeze()

压缩矩阵,我理解为降维

a.squeeze(i) 压缩第i维,如果这一维维数是1,则这一维可有可无,便可以压缩


import torch  
a=torch.randn(1,3,4)
print(a)
b=a.squeeze(0)
print(b)
c=a.squeeze(1)
print(c

输出:

tensor([[[ 0.4627, 1.6447, 0.1320, 2.0946],
[-0.0080, 0.1794, 1.1898, -1.2525],
[ 0.8281, -0.8166, 1.8846, 0.9008]]])

一页三行4列的矩阵

第0维为1,则可以通过squeeze(0)删掉,转化为三行4列的矩阵

tensor([[ 0.4627, 1.6447, 0.1320, 2.0946],
[-0.0080, 0.1794, 1.1898, -1.2525],
[ 0.8281, -0.8166, 1.8846, 0.9008]])

第1维不为1,则不可以压缩

tensor([[[ 0.4627, 1.6447, 0.1320, 2.0946],
[-0.0080, 0.1794, 1.1898, -1.2525],
[ 0.8281, -0.8166, 1.8846, 0.9008]]])

2.torch.unsqueeze()

unsqueeze(i) 表示将第i维设置为1

对压缩为3行4列后的矩阵b进行操作,将第0维设置为1


c=b.unsqueeze(0)
print(c)

输出一个一页三行四列的矩阵

tensor([[[ 0.0661, -0.2386, -0.6610, 1.5774],
[ 1.2210, -0.1084, -0.1166, -0.2379],
[-1.0012, -0.4363, 1.0057, -1.5180]]])

将第一维设置为1


c=b.unsqueeze(1)
print(c)

输出一个3页,一行,4列的矩阵

tensor([[[-1.0067, -1.1477, -0.3213, -1.0633]],
[[-2.3976, 0.9857, -0.3462, -0.3648]],
[[ 1.1012, -0.4659, -0.0858, 1.6631]]])

另外,squeeze、unsqueeze操作不改变原矩阵

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: pytorch中的squeeze函数、cat函数使用

本文链接: https://www.lsjlt.com/news/126422.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • pytorch中的squeeze函数、cat函数使用
    1 squeeze(): 去除size为1的维度,包括行和列。 至于维度大于等于2时,squeeze()不起作用。 行、例: >>> torch.rand(4,...
    99+
    2024-04-02
  • 如何在pytorch中使用squeeze和cat函数
    今天就跟大家聊聊有关如何在pytorch中使用squeeze和cat函数,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1 squeeze(): 去除size为1的维度,包括行和列。至...
    99+
    2023-06-15
  • numpy中怎么使用squeeze函数
    这篇文章主要介绍了numpy中怎么使用squeeze函数,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。reshape函数:改变数组的维数(注意不是shape大小)>&g...
    99+
    2023-06-14
  • numpy的squeeze函数使用方法
    reshape函数:改变数组的维数(注意不是shape大小) >>> e= np.arange(10) >>> e array([0, 1, ...
    99+
    2024-04-02
  • python中squeeze函数的用法是什么
    在Python中,squeeze()函数用于去除数组中的单维度条目。具体来说,它会删除shape为1的维度,即将维度大小为1的维度去...
    99+
    2024-03-15
    python
  • python中squeeze函数的作用是什么
    在Python中,squeeze函数的作用是从数组中删除所有单维度的条目,即将所有维度中大小为1的维度去除。这可以帮助简化数组的维度...
    99+
    2024-03-15
    python
  • python中squeeze函数的功能有哪些
    在Python中,numpy库中的squeeze函数主要用于从数组的形状中删除单维度条目。squeeze函数的功能包括: 删除数组...
    99+
    2024-03-15
    python
  • pytorch中的view()函数怎么使用
    这篇文章主要介绍了pytorch中的view()函数怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇pytorch中的view()函数怎么使用文章都会有所收获,下面我们一起来看看吧。一、普通用法 (手动调...
    99+
    2023-06-29
  • pytorch中nn.Flatten()函数如何使用
    这篇文章主要介绍了pytorch中nn.Flatten()函数如何使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇pytorch中nn.Flatten()函数如何使用文章都会有所收获,下面我们一起来看看吧。t...
    99+
    2023-07-04
  • PyTorch中torch.matmul()函数怎么使用
    这篇文章主要介绍了PyTorch中torch.matmul()函数怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇PyTorch中torch.matmul()函数怎么使用文章都会有所收获,下面我们一起来看...
    99+
    2023-07-06
  • pytorch中关于distributedsampler函数的使用
    目录关于distributedsampler函数的使用1.如何使用这个分布式采样器2.关于用不用这个采样器的区别总结关于distributedsampler函数的使用 1.如何使用这...
    99+
    2023-02-02
    pytorch distributedsampler distributedsampler函数 pytorch使用distributedsampler
  • pytorch中BatchNorm2d函数的参数怎么使用
    本篇内容主要讲解“pytorch中BatchNorm2d函数的参数怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“pytorch中BatchNorm2d函数的参数怎么使用”吧!BN原理、作...
    99+
    2023-07-04
  • pytorch中Parameter函数怎么使用
    这篇文章主要介绍了pytorch中Parameter函数怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇pytorch中Parameter函数怎么使用文章都会有所收获,下面我们一起来看看吧。用法介绍pyt...
    99+
    2023-06-29
  • Pytorch损失函数torch.nn.NLLLoss()的使用
    目录Pytorch损失函数torch.nn.NLLLoss()交叉熵计算公式nn.NLLLoss计算公式log_softmaxnn.NLLLossnn.CrossEntropyLos...
    99+
    2023-02-01
    Pytorch损失函数 torch.nn.NLLLoss() 使用torch.nn.NLLLoss()
  • Pytorch中torch.cat()函数的使用及说明
    目录一. torch.cat()函数解析1. 函数说明2. 代码举例总结一. torch.cat()函数解析 1. 函数说明 1.1 官网:torch.cat() 函数定义及参数说明...
    99+
    2023-01-03
    Pytorch torch.cat() torch.cat()函数 Pytorch函数
  • 如何在pytorch中使用numel函数
    本篇文章给大家分享的是有关如何在pytorch中使用numel函数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。获取tensor中一共包含多少个元素import to...
    99+
    2023-06-15
  • Pytorch中torch.argmax()函数使用及说明
    目录torch.argmax()函数解析1. 官网链接2. torch.argmax(input)函数解析3. 代码举例4. torch.argmax(input,dim) 函数解析...
    99+
    2023-01-03
    Pytorch torch.argmax() Pytorch函数使用 Pytorch torch.argmax()函数
  • Pytorch中torch.repeat_interleave()函数使用及说明
    目录torch.repeat_interleave()函数解析1.函数说明2. 函数原型3. 函数功能4. 输入参数5. 注意6. 代码例子7. 与torch.repeat()函数区...
    99+
    2023-01-03
    Pytorch torch torch.repeat_interleave() pytorch repeat函数
  • Pytorch中的torch.gather()函数怎么用
    这篇文章将为大家详细讲解有关Pytorch中的torch.gather()函数怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。参数说明以官方说明为例,gather()函数需要三个参数,输入input,...
    99+
    2023-06-25
  • pytorch中的torch.nn.Conv2d()函数怎么用
    这篇文章主要为大家展示了“pytorch中的torch.nn.Conv2d()函数怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“pytorch中的torch.nn.Conv2d()函数怎么...
    99+
    2023-06-29
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作