pytorch张量(tensor)常用高级操作函数

张量创建

张量拼接、切分、索引和变换

torch.cat(),torch.concentrate(),torch.concat()

注:这三个函数是等价的

函数:torch.cat(tensors, dim=0, *, out=None) → Tensor
功能:将张量按维度dim进行拼接
说明:
	tensors:任何存放张量的python序列,如(tensor1,tensor2),[tensor1,tensor2,tensor3]
	dim:默认在第0维进行拼接
	out:输出张量

官网文档链接:https://pytorch.org/docs/stable/generated/torch.cat.html#torch.cat
Example:

>>> 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.stack()
函数:torch.stack(tensors, dim=0, *, out=None) → Tensor
功能:在新的维度对张量进行拼接

Example:

>>>import torch
>>>x=torch.randint(1,10,(3,3))
>>>y=torch.randint(11,100,(3,3))
tensor([[[ 4,  6,  1],
         [ 3,  8,  5],
         [ 1,  6,  9]],

        [[86, 61, 38],
         [71, 36, 11],
         [71, 77, 80]]])
>>>torch.stack((x,y),dim=1)
tensor([[[ 4,  6,  1],
         [86, 61, 38]],
        [[ 3,  8,  5],
         [71, 36, 11]],
        [[ 1,  6,  9],
         [71, 77, 80]]])
torch.split()
函数:torch.split(tensor, split_size_or_sections, dim=0)
功能: 将张量按维度dim进行切分
说明:
tensor: 要切分的张量
split_size_or_sections: 为int时,表示每一份的长度;为list时,按list元素切分
dim: 要切分的维度

Example

>>> section = [1,2,1,2,2]
>>> d = torch.randn(8,4)
>>> d
tensor([[ 0.1100,  1.8889,  1.1625,  0.7474],
        [-0.8003, -0.7463, -1.7287,  0.2690],
        [-0.0310, -0.7457,  0.9924,  0.9361],
        [ 0.5262, -2.3752, -0.0715,  0.8350],
        [ 0.8388, -0.3284,  1.6460,  0.9162],
        [-0.1060,  0.4595,  1.2993,  1.0798],
        [ 0.1333, -0.3867,  0.8843, -1.6599],
        [ 0.7458,  1.8324,  0.7660,  0.3449]])
>>> torch.split(d, section, dim=0)
(tensor([[0.1100, 1.8889, 1.1625, 0.7474]]), tensor([[-0.8003, -0.7463, -1.7287,  0.2690],
        [-0.0310, -0.7457,  0.9924,  0.9361]]), tensor([[ 0.5262, -2.3752, -0.0715,  0.8350]]), tensor([[ 0.8388, -0.3284,  1.6460,  0.9162],
        [-0.1060,  0.4595,  1.2993,  1.0798]]), tensor([[ 0.1333, -0.3867,  0.8843, -1.6599],
        [ 0.7458,  1.8324,  0.7660,  0.3449]]))

原文链接:https://blog.csdn.net/weixin_41362649/article/details/112858087

torch.squeeze()
函数:torch.squeeze(input, dim=None, out=None)
功能: 压缩长度为1的维度
说明:
dim: 若为None,移除所有长度为1的维度,若指定维度,当且仅当该长度为1时,可以被移除
>>> a = torch.randn(1,10)
>>> a.shape
torch.Size([1, 10])

>>> a2 = torch.squeeze(a1)
>>> a2
tensor([-1.0263, -0.2194,  1.3285, -1.0941,  0.3763,  0.2249, -1.1332,  0.2836,
       -0.4400, -0.3642])
       
>>> a2.shape
torch.Size([10])
torch.unsqueeze()
torch.unsqueeze(input, dim, out=None)
功能: 依据dim扩展维度
dim: 要扩展的维度
>>>x=torch.rand(2,3)
>>>y=torch.unsqueeze(x,2)
>>>y.shape
torch.Size([2, 3, 1])
torch.chunk()
函数:torch.chunk(input, chunks, dim=0) → List of Tensors
功能:将一个张量分割成指定数字大小的块,需要注意的是,返回的块可能少于指定的数字
>>> torch.arange(11).chunk(6)
(tensor([0, 1]),
 tensor([2, 3]),
 tensor([4, 5]),
 tensor([6, 7]),
 tensor([8, 9]),
 tensor([10]))
>>> torch.arange(12).chunk(6)
(tensor([0, 1]),
 tensor([2, 3]),
 tensor([4, 5]),
 tensor([6, 7]),
 tensor([8, 9]),
 tensor([10, 11]))
>>> torch.arange(13).chunk(6)
(tensor([0, 1, 2]),
 tensor([3, 4, 5]),
 tensor([6, 7, 8]),
 tensor([ 9, 10, 11]),
 tensor([12]))
torch.clamp()
函数:torch.clamp(input, min, max, out=None) → Tensor
功能:将输入input张量每个元素的夹紧到区间 [min,max],并返回结果到一个新张量。

Example

>>>x=torch.randint(1,10,(3,3))
>>>x
tensor([[5, 7, 6],
        [2, 2, 7],
        [4, 1, 9]])
>>>torch.clamp(x,5,8)
tensor([[5, 7, 6],
        [5, 5, 7],
        [5, 5, 8]])
torch.transpose()

功能:转置,一次只能对两个维度进行转换

>>>x=torch.randint(1,100,(3,1,4,2))
>>>torch.transpose(x,0,3).shape
torch.Size([2, 1, 4, 3])
torch.permute()

功能:转置,一次可以对多个维度进行转置,但要将所有的维度写入

>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
>>> torch.permute(x, (2, 0, 1)).size()
torch.Size([5, 2, 3])

张量数学操作

torch.abs(input, out=None) 
 
torch.acos(input, out=None) 
 
torch.cosh(input, out=None) 
 
torch.cos(input, out=None) 
 
torch.asin(input, out=None) 
 
torch.atan(input, out=None) 
 
torch.atan2(input, other, out=None)

torch.log(input, out=None) 
 
torch.log10(input, out=None) 
 
torch.log2(input, out=None) 
 
torch.exp(input, out=None) 
 
torch.sigmoid(input,out=None)

参考文献:
https://blog.csdn.net/weixin_41362649/article/details/112858087

你可能感兴趣的:(计算机视觉,python,深度学习,开发语言)