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

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操作不改变原矩阵

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

(0)

相关推荐

  • pytorch中torch.max和Tensor.view函数用法详解

    torch.max() 1. torch.max()简单来说是返回一个tensor中的最大值. 例如: >>> si=torch.randn(4,5) >>> print(si) tensor([[ 1.1659, -1.5195, 0.0455, 1.7610, -0.2064], [-0.3443, 2.0483, 0.6303, 0.9475, 0.4364], [-1.5268, -1.0833, 1.6847, 0.0145, -0.2088], [-0.86

  • pytorch下的unsqueeze和squeeze的用法说明

    #squeeze 函数:从数组的形状中删除单维度条目,即把shape中为1的维度去掉 #unsqueeze() 是squeeze()的反向操作,增加一个维度,该维度维数为1,可以指定添加的维度.例如unsqueeze(a,1)表示在1这个维度进行添加 import torch a=torch.rand(2,3,1) print(torch.unsqueeze(a,2).size())#torch.Size([2, 3, 1, 1]) print(a.size()) #torch.Size([2,

  • 详解pytorch中squeeze()和unsqueeze()函数介绍

    squeeze的用法主要就是对数据的维度进行压缩或者解压. 先看torch.squeeze() 这个函数主要对数据的维度进行压缩,去掉维数为1的的维度,比如是一行或者一列这种,一个一行三列(1,3)的数去掉第一个维数为一的维度之后就变成(3)行.squeeze(a)就是将a中所有为1的维度删掉.不为1的维度没有影响.a.squeeze(N) 就是去掉a中指定的维数为一的维度.还有一种形式就是b=torch.squeeze(a,N) a中去掉指定的定的维数为一的维度. 再看torch.unsque

  • PyTorch中的squeeze()和unsqueeze()解析与应用案例

    目录 1.torch.squeeze 2.torch.unsqueeze 3.例子 附上官网地址: https://pytorch.org/docs/stable/index.html 1.torch.squeeze squeeze的用法主要就是对数据的维度进行压缩或者解压. 先看torch.squeeze() 这个函数主要对数据的维度进行压缩,去掉维数为1的的维度,比如是一行或者一列这种,一个一行三列(1,3)的数去掉第一个维数为一的维度之后就变成(3)行.squeeze(a)就是将a中所有为

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

    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 4x1x

  • pytorch中的torch.nn.Conv2d()函数图文详解

    目录 一.官方文档介绍 二.torch.nn.Conv2d()函数详解 参数dilation——扩张卷积(也叫空洞卷积) 参数groups——分组卷积 总结 一.官方文档介绍 官网 nn.Conv2d:对由多个输入平面组成的输入信号进行二维卷积 二.torch.nn.Conv2d()函数详解 参数详解 torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1,

  • 浅谈Pytorch中的torch.gather函数的含义

    pytorch中的gather函数 pytorch比tensorflow更加编程友好,所以准备用pytorch试着做最近要做的一些实验. 立个flag开始学习pytorch,新开一个分类整理学习pytorch中的一些踩到的泥坑. 今天刚开始接触,读了一下documentation,写一个一开始每太搞懂的函数gather b = torch.Tensor([[1,2,3],[4,5,6]]) print b index_1 = torch.LongTensor([[0,1],[2,0]]) ind

  • pytorch中tensor.expand()和tensor.expand_as()函数详解

    tensor.expend()函数 >>> import torch >>> a=torch.tensor([[2],[3],[4]]) >>> print(a.size()) torch.Size([3, 1]) >>> a.expand(3,2) tensor([[2, 2], [3, 3], [4, 4]]) >>> a tensor([[2], [3], [4]]) 可以看出expand()函数括号里面为变形

  • PyTorch中topk函数的用法详解

    听名字就知道这个函数是用来求tensor中某个dim的前k大或者前k小的值以及对应的index. 用法 torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor) input:一个tensor数据 k:指明是得到前k个数据以及其index dim: 指定在哪个维度上排序, 默认是最后一个维度 largest:如果为True,按照大到小排序: 如果为False,按照小到大排序

  • pytorch 中pad函数toch.nn.functional.pad()的用法

    padding操作是给图像外围加像素点. 为了实际说明操作过程,这里我们使用一张实际的图片来做一下处理. 这张图片是大小是(256,256),使用pad来给它加上一个黑色的边框.具体代码如下: import torch.nn,functional as F import torch from PIL import Image im=Image.open("heibai.jpg",'r') X=torch.Tensor(np.asarray(im)) print("shape:

  • 浅谈pytorch中torch.max和F.softmax函数的维度解释

    在利用torch.max函数和F.Ssoftmax函数时,对应该设置什么维度,总是有点懵,遂总结一下: 首先看看二维tensor的函数的例子: import torch import torch.nn.functional as F input = torch.randn(3,4) print(input) tensor([[-0.5526, -0.0194, 2.1469, -0.2567], [-0.3337, -0.9229, 0.0376, -0.0801], [ 1.4721, 0.1

随机推荐