ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PyTorch中常用的一些函数

2022-01-30 18:01:14  阅读:170  来源: 互联网

标签:tensor 常用 函数 torch 张量 PyTorch input Size size


主要用于记录一些比较常用的函数,便于查找。未记录的可查看PYTORCH TUTORIALSPyTorch中文文档

目录

torch.unsqueeze

  • 主要用于在某一位置添加一个维度
x = torch.tensor([1, 2, 3, 4])
torch.unsqueeze(x, 0)
torch.unsqueeze(x, 1)
  • 比如上面两个例子,是分别增加了一个行的维度和一个列的维度,第一个tensor([[ 1, 2, 3, 4]]),第二个为tensor([[ 1],[ 2], [ 3],[ 4]])

torch.transpose

  • 主要用于交换两个位置的形式,比如transpose(0,1)相当于对于个二维矩阵转置。
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 1.0028, -0.9893,  0.5809],
        [-0.1669,  0.7299,  0.4942]])
>>> torch.transpose(x, 0, 1)
tensor([[ 1.0028, -0.1669],
        [-0.9893,  0.7299],
        [ 0.5809,  0.4942]])

*同理对于一个三维矩阵,transpose(0,2)相当于对0和2位置的内容进行转置。

对上面两个以一个例子进行学习:

pe=torch.zeros(12,512)
print(pe.shape)
position =torch.arange(0,max_len,dtype=torch.float).unsqueeze(1)
print(position.shape)
print(pe.unsqueeze(1).shape)
pe=pe.unsqueeze(0)
print(pe.shape)
pe=pe.transpose(0,2)
print(pe.shape)

最后对应的输出如下:

torch.Size([12, 512])
torch.Size([12, 1])
torch.Size([12, 1, 512])
torch.Size([1, 12, 512])
torch.Size([512, 12, 1])

torch.nn.Linear

nn.Linear()是用于设置网络中的全连接层的,需要注意在二维图像处理的任务中,全连接层的输入与输出一般都设置为二维张量,形状通常为[batch_size, size],不同于卷积层要求输入输出是四维张量。其用法与形参说明如下:

* in_features指的是输入的二维张量的大小,即输入的[batch_size, size]中的size。
 * out_features指的是输出的二维张量的大小,即输出的二维张量的形状为[batch_size,output_size],当然,它也代表了该全连接层的神经元个数。
 * 从输入输出的张量的shape角度来理解,相当于一个输入为[batch_size, in_features]的张量变换成了[batch_size, out_features]的输出张量。

实例如下:

import torch as t
from torch import nn

# in_features由输入张量的形状决定,out_features则决定了输出张量的形状 
connected_layer = nn.Linear(in_features = 64*64*3, out_features = 1)

# 假定输入的图像形状为[64,64,3]
input = t.randn(1,64,64,3)

# 将四维张量转换为二维张量之后,才能作为全连接层的输入
input = input.view(1,64*64*3)
print(input.shape)
output = connected_layer(input) # 调用全连接层
print(output.shape)

结果为input shape is %s torch.Size([1, 12288])output shape is %s torch.Size([1, 1])

torch.tensor.repeat

  • 抓要用来对一个矩阵整体进行复制,比如对于x.repeat(2,1),将x横着重复两次,纵着重复两次。
>>> x = torch.tensor([1, 2, 3])
>>> x.repeat(4, 2)
tensor([[ 1,  2,  3,  1,  2,  3],
        [ 1,  2,  3,  1,  2,  3],
        [ 1,  2,  3,  1,  2,  3],
        [ 1,  2,  3,  1,  2,  3]])
>>> x.repeat(4, 2, 1).size()
torch.Size([4, 2, 3])

torch.tensorview

  • 主要用于改变张量的形状
>>> x = torch.randn(4, 4)
>>> x.size()
torch.Size([4, 4])
>>> y = x.view(16)
>>> y.size()
torch.Size([16])
>>> z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
>>> z.size()
torch.Size([2, 8])
  • 但是要注意于transpose相比,view不会改变张量在内存中的存储顺序而仅改变形状,transpose转置会同时改变两者
>>> a = torch.randn(1, 2, 3, 4)
>>> a.size()
torch.Size([1, 2, 3, 4])
>>> b = a.transpose(1, 2)  # Swaps 2nd and 3rd dimension
>>> b.size()
torch.Size([1, 3, 2, 4])
>>> c = a.view(1, 3, 2, 4)  # Does not change tensor layout in memory
>>> c.size()
torch.Size([1, 3, 2, 4])
>>> torch.equal(b, c)
False

上面中,a,b,c分别的形式如下:

-------a:---------
tensor([[[[ 1.3258, -0.7289, -1.5796,  0.1997],
          [-1.4013, -0.7111,  0.3893,  0.0850],
          [-1.8743, -0.6299, -0.0428, -1.5769]],

         [[-0.0670,  0.5076,  0.2844, -0.4623],
          [-0.6529,  1.0558,  0.8000,  0.8263],
          [ 0.5389, -0.8800, -1.4782, -1.7605]]]])
-------c:---------
tensor([[[[ 1.3258, -0.7289, -1.5796,  0.1997],
          [-1.4013, -0.7111,  0.3893,  0.0850]],

         [[-1.8743, -0.6299, -0.0428, -1.5769],
          [-0.0670,  0.5076,  0.2844, -0.4623]],

         [[-0.6529,  1.0558,  0.8000,  0.8263],
          [ 0.5389, -0.8800, -1.4782, -1.7605]]]])
-------b:---------
tensor([[[[ 1.3258, -0.7289, -1.5796,  0.1997],
          [-0.0670,  0.5076,  0.2844, -0.4623]],

         [[-1.4013, -0.7111,  0.3893,  0.0850],
          [-0.6529,  1.0558,  0.8000,  0.8263]],

         [[-1.8743, -0.6299, -0.0428, -1.5769],
          [ 0.5389, -0.8800, -1.4782, -1.7605]]]])

torch.cat(tensors, dim=0, *, out=None)

  • 用来连接两个张量
  • tensors--任何相同类型的张量的Python序列。提供的非空张象必须具有相同的形状。
  • dim (int, optional)--连接张量的维度
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497],
        [ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497],
        [ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497]])
>>> torch.cat((x, x, x), 1)
tensor([[ 0.6580, -1.0969, -0.4614,  0.6580, -1.0969, -0.4614,  0.6580,
         -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497, -0.1034, -0.5790,  0.1497, -0.1034,
         -0.5790,  0.1497]])
>>> torch.cat((x,x),-1)
tensor([[-0.1049, -1.2885,  0.2667, -0.1049, -1.2885,  0.2667],
        [ 1.2355,  0.4063, -0.0956,  1.2355,  0.4063, -0.0956]])

torch.squeeze(input, dim=None, *, out=None)

  • 用来对张量某一维度进行降维,返回一个已移除大小为1的输入的所有维度的张量。
  • input (Tensor) – the input tensor.
  • dim (int, optional) – if given, the input will be squeezed only in this dimension
  • 返回的张量与输入张量共享存储空间,因此更改一个张量的内容将更改另一个张量的内容。
  • 如果张量的批次维度( batch dimension)大小为1,则挤压(输入)也会删除批次维度,这可能会导致意外错误。
>>> x = torch.zeros(2, 1, 2, 1, 2)
>>> x.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x)
>>> y.size()
torch.Size([2, 2, 2])
>>> y = torch.squeeze(x, 0)
>>> y.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x, 1)
>>> y.size()
torch.Size([2, 2, 1, 2])
  • 从上面要注意,第一次消除0维度上的,但是维度值不是1而是2,所以不消除。而对1位置上的,恰好为1所以可以消除。

torch.dtype

下面给出中文的翻译版本,原版请点击标题链接.

标签:tensor,常用,函数,torch,张量,PyTorch,input,Size,size
来源: https://www.cnblogs.com/xyzhrrr/p/15857292.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有