Tensor 数据类型 Data type dtype CPU Tensor GPU Tensor Boolean torch.bool torch.BoolTensor torch.cuda.BoolTensor 8-bit integer (unsigned) torch.uint8 torch.ByteTensor torch.cuda.ByteTensor 8-bit integer (signed) torch.int8 torch.CharTensor torch.cuda.CharTensor 16-bit integer (signed) torch.int16 or torch.short torch.ShortTensor torch.cuda.ShortTensor 32-bit integer (signed) torch.int32 or torch.int torch.IntTensor torch.cuda.IntTensor 64-bit integer (signed) torch.int64 or torch.long torch.LongTensor torch.cuda.LongTensor 16-bit floating point torch.float16 or torch.half torch.HalfTensor torch.cuda.HalfTensor 16-bit floating point torch.bfloat16 torch.BFloat16Tensor torch.cuda.BFloat16Tensor 32-bit floating point torch.float32 or torch.float torch.FloatTensor torch.cuda.FloatTensor 64-bit floating point torch.float64 or torch.double torch.DoubleTensor torch.cuda.DoubleTensor
设置 Tensor 默认类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import torchprint (torch.tensor(True ).dtype) print (torch.tensor(True ).type ()) print (torch.tensor(1 ).dtype) print (torch.tensor(1 ).type ()) print (torch.tensor(1. ).dtype) print (torch.tensor(1. ).type ()) print (torch.tensor(3j ).dtype) print (torch.tensor(3j ).type ()) torch.set_default_dtype(torch.double) print (torch.tensor(True ).dtype) print (torch.tensor(True ).type ()) print (torch.tensor(1 ).dtype) print (torch.tensor(1 ).type ()) print (torch.tensor(1. ).dtype) print (torch.tensor(1. ).type ()) print (torch.tensor(3j ).dtype) print (torch.tensor(3j ).type ())
set_default_dtype()只能设置floating-point类型,否则会报TypeError: only floating-point types are supported as the default type错误。
标量与张量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import torcha = torch.tensor(1 ) print (a.shape) print (a.size()) print (a.ndim) print (a.dim()) b = torch.tensor([1 , 2 ]) print (b.shape) print (b.size()) print (b.ndim) print (b.dim()) c = torch.tensor([[1 , 2 ]]) print (c.shape) print (c.size()) print (c.ndim) print (c.dim())
标量是一个单独的数,ndim为0。
创建 Tensor .tensor 1 2 3 4 5 6 7 8 9 import torchprint (torch.tensor(1 )) print (torch.tensor(1 , dtype=torch.float64)) print (torch.tensor([1 , 2 ])) print (torch.tensor([1 , 2 ], dtype=torch.float64))
.from_numpy 1 2 3 4 5 6 7 import numpy as npimport torchdata = np.array([1 , 2 , 3 ]) print (torch.from_numpy(data)) data = np.array([1 , 2 , 3 ], dtype=np.float64) print (torch.from_numpy(data))
Tensor 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import torchprint (torch.Tensor(1 )) print (torch.Tensor(1 , 2 )) print (torch.Tensor(1 , 2 , 3 ))""" tensor([[[-3.0741e+31, 1.6031e-42, 0.0000e+00], [ 0.0000e+00, 0.0000e+00, 0.0000e+00]]]) """ print (torch.Tensor([1 ])) print (torch.Tensor([1 , 2 ])) print (torch.Tensor([1 , 2 , 3 ]))
Tensor支持两种传参方式:
当参数为列表时,创建列表对应维度的Tensor并初始化数据为列表数据。 当参数不为列表时,与.empty()类似,创建参数指定的shape的空的Tensor。 BoolTensor、ByteTensor、CharTensor、ShortTensor、IntTensor、LongTensor、HalfTensor、FloatTensor、DoubleTensor也是一样。
.empty/.zeros/.ones/.full/.eye 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import torchinput = torch.tensor([1 , 1 , 1 , 1 , 1 ])print (torch.empty(())) print (torch.empty((1 , 5 ))) print (torch.empty_like((input ))) print (torch.zeros(())) print (torch.zeros((1 , 5 ))) print (torch.zeros_like((input ))) print (torch.ones(())) print (torch.ones((1 , 5 ))) print (torch.ones_like((input ))) print (torch.full((), 100 )) print (torch.full((1 , 5 ), 100 )) print (torch.full_like((input ), 100 )) print (torch.eye(3 ))""" tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) """ print (torch.eye(3 , 5 ))""" tensor([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.]]) """
.arange/.linspace/.logspace 1 2 3 4 5 6 7 8 9 10 11 12 import torchprint (torch.arange(0 , 10 )) print (torch.arange(0 , 10 , step=3 )) print (torch.linspace(0 , 1 , steps=5 )) print (torch.logspace(0 , 1 , steps=5 )) print (torch.logspace(0 , 1 , steps=5 , base=2 ))
随机采样 随机种子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import torchtorch.manual_seed(1 ) torch.cuda.manual_seed(1 ) print (torch.initial_seed()) torch.seed() print (torch.initial_seed())
随机函数 .rand/.rand_like 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import torchtorch.manual_seed(1 ) input = torch.empty(3 , 3 )print (torch.rand(3 , 3 ))""" tensor([[0.7576, 0.2793, 0.4031], [0.7347, 0.0293, 0.7999], [0.3971, 0.7544, 0.5695]]) """ print (torch.rand_like(input ))""" tensor([[0.4388, 0.6387, 0.5247], [0.6826, 0.3051, 0.4635], [0.4550, 0.5725, 0.4980]]) """
torch.rand()返回在区间[0, 1)均匀分布的随机数填充的张量。
.randint/.randint_like 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import torchtorch.manual_seed(1 ) input = torch.empty(3 , 3 )print (torch.randint(low=0 , high=10 , size=(3 , 3 )))""" tensor([[5, 9, 4], [8, 3, 3], [1, 1, 9]]) """ print (torch.randint_like(input , high=10 ))""" tensor([[2., 8., 9.], [6., 3., 3.], [0., 2., 1.]]) """
torch.randint(low, high)返回在区间[low, high)的随机数填充的张量。
.randperm 1 2 3 4 5 6 import torchtorch.manual_seed(1 ) print (torch.randperm(10 ))
torch.randperm(n)返回在区间[0, n)的随机排列整数。
.randn/.randn_like 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import torchtorch.manual_seed(1 ) input = torch.empty(3 , 3 )print (torch.randn(3 , 3 ))""" tensor([[ 0.6614, 0.2669, 0.0617], [ 0.6213, -0.4519, -0.1661], [-1.5228, 0.3817, -1.0276]]) """ print (torch.randn_like(input ))""" tensor([[-0.5631, -0.8923, -0.0583], [-0.1955, -0.9656, 0.4224], [ 0.2673, -0.4212, -0.5107]]) """
torch.randn()从标准正态分布中随机采样。
.normal 1 2 3 4 5 6 7 import torchtorch.manual_seed(1 ) print (torch.normal(mean=torch.full((5 ,), 0. ), std=torch.arange(0 , 1 , 0.2 )))
torch.normal(mean, std)从给定参数mean、std的离散正态分布中随机采样。
.bernoulli 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import torchinput = torch.empty(3 , 3 ).uniform_(0 , 1 ) print (torch.bernoulli(input ))""" tensor([[1., 0., 0.], [1., 0., 1.], [0., 1., 1.]]) """ print (torch.bernoulli(torch.ones(3 , 3 ))) """ tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]) """ print (torch.bernoulli(torch.zeros(3 , 3 ))) """ tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) """
torch.bernoulli()从伯努利分布中抽取二进制随机数(0 或 1)。输入的值必须在[0, 1]范围内。
.poisson 1 2 3 4 5 6 7 8 9 10 11 import torchtorch.manual_seed(1 ) rates = torch.rand(3 , 3 ) * 5 print (torch.poisson(rates))""" tensor([[5., 1., 1.], [3., 0., 2.], [0., 2., 0.]]) """
torch.poisson()从泊松分布中随机采样。
.multinomial 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import torchweights = torch.tensor([[1 , 3 , 5 , 7 , 9 ], [2 , 4 , 6 , 8 , 10 ]], dtype=torch.float ) print (torch.multinomial(weights, 5 ))""" tensor([[2, 3, 4, 1, 0], [3, 2, 1, 4, 0]]) """ print (torch.multinomial(weights, 6 , replacement=True ))""" tensor([[3, 0, 3, 3, 4, 2], [0, 1, 4, 4, 2, 4]]) """
torch.multinomial(input, num_samples, replacement)对input的每一行做从多项式分布中采样num_samples次,输出的张量是每一次取值时input张量对应行的下标。
索引与切片 Python 语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import torchimages = torch.rand(4 , 3 , 28 , 28 ) print (images.shape) print (images[0 ].shape) print (images[0 , 0 ].shape) print (images[:2 ].shape) print (images[:2 , :2 ].shape) print (images[:2 , -1 ].shape) print (images[:, :, ::2 , ::2 ].shape) print (images[...].shape) print (images[0 , ...].shape) print (images[..., ::2 ].shape)
.narrow/.narrow_copy 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import torchdata = torch.tensor([[1 , 2 , 3 , 4 , 5 ], [6 , 7 , 8 , 9 ,10 ], [11 , 12 , 13 , 14 , 15 ]]) print (data)""" tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) """ print (torch.narrow(data, dim=0 , start=1 , length=2 ))""" tensor([[ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) """ print (torch.narrow(data, dim=1 , start=1 , length=3 ))""" tensor([[ 2, 3, 4], [ 7, 8, 9], [12, 13, 14]]) """ print (torch.narrow_copy(data, dim=0 , start=1 , length=2 ))""" tensor([[ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) """ print (torch.narrow_copy(data, dim=1 , start=1 , length=3 ))""" tensor([[ 2, 3, 4], [ 7, 8, 9], [12, 13, 14]]) """
torch.narrow()在指定维度缩小张量,可以简单理解为类似切片,tensor[start: start + length]
torch.narrow_copy()与torch.narrow()相同,但返回的是副本而不是共享存储。
.select/.index_select 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import torchimages = torch.rand(4 , 3 , 28 , 28 ) print (images.shape) print (torch.select(images, dim=0 , index=0 ).shape) print (torch.select(images, dim=1 , index=1 ).shape) print (torch.index_select(images, dim=0 , index=torch.tensor([1 , 3 ])).shape) print (torch.index_select(images, dim=1 , index=torch.tensor([1 , 2 ])).shape) print (torch.index_select(images, dim=2 , index=torch.arange(0 , 28 , 2 )).shape) print (torch.index_select(images, dim=3 , index=torch.arange(0 , 28 , 2 )).shape)
torch.select()沿着维度dim,在给定索引index对input张量进行切片,等价于切片。比如:tensor.select(0, index)等于tensor[index]。tensor.select(2, index)等于tensor[:,:,index]。
torch.index_select()沿着维度dim对input张量进行索引。
.masked_select 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import torchtorch.manual_seed(1 ) data = torch.randn(3 , 4 ) print (data)""" tensor([[ 0.6614, 0.2669, 0.0617, 0.6213], [-0.4519, -0.1661, -1.5228, 0.3817], [-1.0276, -0.5631, -0.8923, -0.0583]]) """ mask = data.ge(0 ) print (mask)""" tensor([[ True, True, True, True], [False, False, False, True], [False, False, False, False]]) """ print (torch.masked_select(data, mask))
torch.masked_select()根据布尔掩码选择数据,返回的是一维数据。
.gather 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import torchtorch.manual_seed(1 ) data = torch.randperm(16 ).view(4 ,4 ) print (data)""" tensor([[ 5, 15, 6, 4], [11, 2, 7, 12], [ 1, 0, 9, 8], [10, 3, 13, 14]]) """ index = torch.tensor([[1 , 0 , 3 , 2 ]]) print (index) print (index.t())""" tensor([[1], [0], [3], [2]]) """ print (torch.gather(data, 0 , index)) print (torch.gather(data, 0 , index.t()))""" tensor([[11], [ 5], [10], [ 1]]) """ print (torch.gather(data, 1 , index)) print (torch.gather(data, 1 , index.t()))""" tensor([[15], [11], [ 8], [13]]) """
torch.gather(data, 0, index)对0维(行)进行
tensor([[1, 0, 3, 2]])
第 0 1 2 3 列
索引: [1 ][0] == 11 [0 ][1] == 15 [3 ][2] == 13 [2 ][3] == 8 加粗的是行索引的值1, 0, 3, 2,没加粗的为什么是0, 1, 2, 3呢?因为索引1是第0列,索引0是第1列,索引3是第2列,索引2是第3列。
torch.gather(data, 0, index.t())对0维(行)进行
tensor([[1], 第0列
[0], 第0列
[3], 第0列
[2]]) 第0列
索引: [1 ][0] == 11 [0 ][0] == 5 [3 ][0] == 10 [2 ][0] == 1 加粗的是行索引的值1, 0, 3, 2,没加粗的都是0,因为索引1, 0, 3, 2都是第0列。
torch.gather(data, 1, index.t())对1维(列)进行
tensor([[1], 第0行
[0], 第1行
[3], 第2行
[2]]) 第3行
索引: [0][1 ] == 15 [1][0 ] == 11 [2][3 ] == 8 [3][2 ] == 13 加粗的是列索引的值1, 0, 3, 2,没加粗的0, 1, 2, 3则是因为索引1是第0行,索引0是第1行,索引3是第2行,索引2是第3行。
.take/.take_along_dim 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 import torchtorch.manual_seed(1 ) data = torch.randperm(16 ).view(4 ,4 ) print (data)""" tensor([[ 5, 15, 6, 4], [11, 2, 7, 12], [ 1, 0, 9, 8], [10, 3, 13, 14]]) """ index1 = torch.tensor([0 , 5 , 10 , 15 ]) index2 = torch.tensor([[1 , 0 , 3 , 2 ]]) print (torch.take(data, index1)) print (torch.take_along_dim(data, index1)) print (torch.take_along_dim(data, index2, dim=0 )) print (torch.take_along_dim(data, index2.t(), dim=0 )) """ tensor([[11, 2, 7, 12], [ 5, 15, 6, 4], [10, 3, 13, 14], [ 1, 0, 9, 8]]) """ print (torch.take_along_dim(data, index2, dim=1 )) """ tensor([[15, 5, 4, 6], [ 2, 11, 12, 7], [ 0, 1, 8, 9], [ 3, 10, 14, 13]]) """ print (torch.take_along_dim(data, index2.t(), dim=1 ))""" tensor([[15], [11], [ 8], [13]]) """
torch.take_along_dim()当dim=None时,等价于torch.take(),先把张量打平转成1维在根据索引获取元素。
当dim不等于None时,则与data.gather()相似。
.argwhere/.nonzero 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import torchdata1 = torch.tensor([1 , 0 , 1 , 0 , 1 , 0 ]) data2 = torch.tensor([[1 , 0 , 1 ], [0 , 1 , 0 ]]) print (data1) print (data2)""" tensor([[1, 0, 1], [0, 1, 0]]) """ print (torch.argwhere(data1))""" tensor([[0], [2], [4]]) """ print (torch.argwhere(data2))""" tensor([[0, 0], [0, 2], [1, 1]]) """ print (torch.nonzero(data1))""" tensor([[0], [2], [4]]) """ print (torch.nonzero(data1, as_tuple=True )) print (torch.nonzero(data2))""" tensor([[0, 0], [0, 2], [1, 1]]) """ print (torch.nonzero(data2, as_tuple=True ))
torch.argwhere()和torch.nonzero()都是返回非0元素的索引。
当torch.nonzero()参数as_tuple=False时,效果与torch.argwhere()相同。
.where 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 import torchtorch.manual_seed(1 ) condition = torch.randn(3 , 5 ) > 0 print (condition)""" tensor([[ True, True, True, True, False], [False, False, True, False, False], [False, False, False, False, True]]) """ data1 = torch.ones(3 , 5 ) print (data1)""" tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]) """ data2 = torch.full_like(data1, 100 ) print (data2)""" tensor([[100., 100., 100., 100., 100.], [100., 100., 100., 100., 100.], [100., 100., 100., 100., 100.]]) """ print (torch.where(condition, data1, data2))""" tensor([[ 1., 1., 1., 1., 100.], [100., 100., 1., 100., 100.], [100., 100., 100., 100., 1.]]) """
.unravel_index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import torchprint (torch.arange(9 ).view(3 , 3 ))""" tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) """ print (torch.arange(9 , 18 ).view(3 , 3 ))""" tensor([[ 9, 10, 11], [12, 13, 14], [15, 16, 17]]) """ print (torch.unravel_index(torch.tensor(2 ), shape=(3 , 3 ))) print (torch.unravel_index(torch.tensor([4 , 6 , 9 , 17 ]), shape=(3 , 3 )))
索引2在shape为(3, 3)的0行2列。 索引9可以理解为9 / prod(shape) == 9 % 9 == 0,所以在0行0列。 索引17可以理解为17 / prod(shape) == 17 % 9 == 8,所以在2行2列。
维度变换 .t/.transpose/.movedim/.permute 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import torchdata = torch.arange(9 ) print (data) print (torch.t(data)) data = torch.arange(9 ).view(3 , 3 ) print (data)""" tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) """ print (torch.t(data))""" tensor([[0, 3, 6], [1, 4, 7], [2, 5, 8]]) """ images = torch.rand(4 , 3 , 1080 , 1920 ) print (images.shape) print (images.transpose(1 , 3 ).transpose(1 , 2 ).shape) print (images.movedim(1 , 3 ).shape) print (images.permute(0 , 2 , 3 , 1 ).shape)
torch.t(input)只能处理维度小于等于2的,否则会报错。当维度是0或1维时,返回相同的结果,当维度为2时,等价于torch.transpose(input, 0, 1)
torch.transpose(input, dim0, dim1)一次只能操作两个维度,对调两个维度的位置。
torch.movedim(input, source, destination)将source维度移动到destination维度。
torch.permute(input, dims)一次可以操作多个维度,dims指定所有维度的顺序。在多维度操作上使用torch.permute()更直观。
torch.swapaxes()是torch.transpose()的别名。torch.swapdims()是torch.transpose()的别名。torch.moveaxis()是torch.movedim()的别名。
.view/.reshape 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import torchimages = torch.rand(4 , 3 , 28 , 28 ) print (images.shape) print (images.view(4 , 3 * 28 * 28 ).shape) print (images.view(4 , -1 ).shape) print (images.transpose(1 , 3 ).transpose(1 , 2 ).contiguous().view(4 , -1 ).shape) print (images.permute(0 , 2 , 3 , 1 ).contiguous().view(4 , -1 ).shape) print (images.reshape(4 , 3 * 28 * 28 ).shape) print (images.reshape(4 , -1 ).shape) print (images.transpose(1 , 3 ).transpose(1 , 2 ).reshape(4 , -1 ).shape) print (images.permute(0 , 2 , 3 , 1 ).reshape(4 , -1 ).shape)
view()和reshape()都可以改变Tensor的维度,区别是:
view()只能对满足连续性的张量进行转换,当对不满足连续性的张量进行操作时会报RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.错误。transpose()和permute()会改变张量连续性,使用view()前需要先执行contiguous()。
reshape()则没有上述要求,可以直接使用,无需先执行contiguous()。
.squeeze/.unsqueeze 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import torchdata = torch.rand(2 , 1 , 2 , 1 ) print (data.shape) print (torch.squeeze(data).shape) print (torch.squeeze(data, dim=0 ).shape) print (torch.squeeze(data, dim=1 ).shape) print (torch.unsqueeze(data, dim=0 ).shape) print (torch.unsqueeze(data, dim=-1 ).shape)
.expand/.repeat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import torchimages = torch.rand(4 , 1 , 28 , 28 ) data = torch.empty(4 , 1 , 1 , 1 ) print (images.expand(-1 , 3 , -1 , -1 ).shape) print (images.expand(4 , 3 , 28 , 28 ).shape) print (data.expand_as(images).shape) print (images.repeat(1 , 3 , 1 , 1 ).shape)
.tile 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import torchdata = torch.arange(9 ).view(3 , 3 ) print (data)""" tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) """ print (torch.tile(data, dims=(2 ,)))""" tensor([[0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8]]) """ print (torch.tile(data, dims=(1 , 2 )))""" tensor([[0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8]]) """ print (torch.tile(data, dims=(3 , 2 )))""" tensor([[0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8], [0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8], [0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8]]) """ print (torch.tile(data, dims=(1 , 3 , 2 )))""" tensor([[[0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8], [0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8], [0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8]]]) """
合并与拆分 .cat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import torchtorch.manual_seed(1 ) data1 = torch.rand(2 , 3 ) print (data1)""" tensor([[0.7576, 0.2793, 0.4031], [0.7347, 0.0293, 0.7999]]) """ data2 = torch.rand(2 , 3 ) print (data1)""" tensor([[0.7576, 0.2793, 0.4031], [0.7347, 0.0293, 0.7999]]) """ print (torch.cat([data1, data1]))""" tensor([[0.7576, 0.2793, 0.4031], [0.7347, 0.0293, 0.7999], [0.7576, 0.2793, 0.4031], [0.7347, 0.0293, 0.7999]]) """ print (torch.cat([data1, data1], dim=1 ))""" tensor([[0.7576, 0.2793, 0.4031, 0.7576, 0.2793, 0.4031], [0.7347, 0.0293, 0.7999, 0.7347, 0.0293, 0.7999]]) """
torch.concat()是torch.cat()的别名。torch.concatenate()是torch.cat()的别名。
.stack/.hstack/.vstack/.column_stack/.dstack 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 import torchdata = torch.tensor(9 ) data1 = torch.arange(9 ) data2 = torch.arange(9 ).view(3 , 3 ) data3 = torch.arange(9 ).view(1 , 3 , 3 ) data4 = torch.arange(9 ).view(1 , 1 , 3 , 3 ) data5 = torch.arange(9 ).view(1 , 1 , 1 , 3 , 3 ) print (data.shape) print (data1.shape) print (data2.shape) print (data3.shape) print (data4.shape) print (data5.shape) print (torch.stack([data, data]).shape) print (torch.stack([data1, data1]).shape) print (torch.stack([data2, data2]).shape) print (torch.stack([data3, data3]).shape) print (torch.stack([data4, data4]).shape) print (torch.stack([data5, data5]).shape) print (torch.stack([data1, data1], dim=1 ).shape) print (torch.stack([data2, data2], dim=1 ).shape) print (torch.stack([data3, data3], dim=1 ).shape) print (torch.stack([data4, data4], dim=1 ).shape) print (torch.stack([data5, data5], dim=1 ).shape) print (torch.hstack([data, data]).shape) print (torch.hstack([data1, data1]).shape) print (torch.hstack([data2, data2]).shape) print (torch.hstack([data3, data3]).shape) print (torch.hstack([data4, data4]).shape) print (torch.hstack([data5, data5]).shape) print (torch.vstack([data, data]).shape) print (torch.vstack([data1, data1]).shape) print (torch.vstack([data2, data2]).shape) print (torch.vstack([data3, data3]).shape) print (torch.vstack([data4, data4]).shape) print (torch.vstack([data5, data5]).shape) print (torch.column_stack([data, data]).shape) print (torch.column_stack([data1, data1]).shape) print (torch.column_stack([data2, data2]).shape) print (torch.column_stack([data3, data3]).shape) print (torch.column_stack([data4, data4]).shape) print (torch.column_stack([data5, data5]).shape) print (torch.dstack((data, data)).shape) print (torch.dstack((data1, data1)).shape) print (torch.dstack((data2, data2)).shape) print (torch.dstack((data3, data3)).shape) print (torch.dstack((data4, data4)).shape) print (torch.dstack((data5, data5)).shape)
torch.hstack)按水平方向(列方向)依次堆叠张量。
torch.vstack)按垂直方向(行方向)依次堆叠张量。
torch.column_stack()除了张量是0维和1维外,等价于torch.hstack()。当张量t是0维或1维时,先reshape重塑为(t.numel(), 1)再水平堆叠。
torch.row_stack()是torch.vstack()的别名。
torch.dstack()在第3维度进行堆叠。
torch.stack()是一个更通用的函数,通过dim指定在任意维度进行堆叠,dim默认等于0。它总是增加一个新的维度来堆叠张量。
.chunk 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import torchdata = torch.arange(20 ).view(4 , 5 ) print (data)""" tensor([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) """ print (torch.chunk(data, 3 ))""" (tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]), tensor([[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])) """ print (torch.chunk(data, 2 , dim=1 ))""" (tensor([[ 0, 1, 2], [ 5, 6, 7], [10, 11, 12], [15, 16, 17]]), tensor([[ 3, 4], [ 8, 9], [13, 14], [18, 19]])) """
.split/.hsplit/.vsplit/.dsplit/.tensor_split 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 import torchdata1 = torch.arange(9 ) data2 = torch.arange(9 ).view(3 , 3 ) data3 = torch.arange(9 ).view(1 , 3 , 3 ) print (torch.split(data1, 2 )) print (torch.split(data1, [1 , 3 , 5 ])) print (torch.split(data2, 2 , dim=1 ))""" (tensor([[0, 1], [3, 4], [6, 7]]), tensor([[2], [5], [8]])) """ print (torch.split(data2, [1 , 2 ], dim=1 ))""" (tensor([[0], [3], [6]]), tensor([[1, 2], [4, 5], [7, 8]])) """ print (torch.hsplit(data1, 3 )) print (torch.hsplit(data1, [3 , 5 , 7 ])) print (torch.hsplit(data2, 3 ))""" (tensor([[0], [3], [6]]), tensor([[1], [4], [7]]), tensor([[2], [5], [8]])) """ print (torch.hsplit(data2, [1 ]))""" (tensor([[0], [3], [6]]), tensor([[1, 2], [4, 5], [7, 8]])) """ print (torch.vsplit(data2, 3 )) print (torch.vsplit(data2, [2 ]))""" (tensor([[0, 1, 2], [3, 4, 5]]), tensor([[6, 7, 8]])) """ print (torch.dsplit(data3, 3 ))""" (tensor([[[0], [3], [6]]]), tensor([[[1], [4], [7]]]), tensor([[[2], [5], [8]]])) """ print (torch.dsplit(data3, [1 , 2 ]))""" (tensor([[[0], [3], [6]]]), tensor([[[1], [4], [7]]]), tensor([[[2], [5], [8]]])) """ print (torch.tensor_split(data1, 3 )) print (torch.tensor_split(data2, 3 , dim=1 ))""" (tensor([[0], [3], [6]]), tensor([[1], [4], [7]]), tensor([[2], [5], [8]])) """ print (torch.tensor_split(data3, 3 , dim=2 ))""" (tensor([[0], [3], [6]]), tensor([[1], [4], [7]]), tensor([[2], [5], [8]])) """
torch.hsplit(input, indices_or_sections)当input是1维时,等价于torch.tensor_split(input, indices_or_sections, dim=0),当input是大于等于2维时,等价于torch.tensor_split(input, indices_or_sections, dim=1)。indices_or_sections如果是数字,必须能被整除,否则会抛出异常。
torch.vsplit(input, indices_or_sections)等价于torch.tensor_split(input, indices_or_sections, dim=0)。input必须大于等于2维。indices_or_sections如果是数字,必须能被整除,否则会抛出异常。
torch.dsplit(input, indices_or_sections)等价于torch.tensor_split(input, indices_or_sections, dim=2)。input必须大于等于3维。indices_or_sections如果是数字,必须能被整除,否则会抛出异常。
.unbind 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import torchdata = torch.arange(9 ).view(3 , 3 ) print (data)""" tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) """ print (torch.unbind(data)) print (torch.unbind(data, dim=1 ))
逐点运算 .add/.sub/.mul/.div/.remainder/.fmod/.positive/.neg/.abs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 import torchtorch.manual_seed(1 ) data = torch.randint(-10 , 10 , (2 , 5 )) print (data)""" tensor([[-5, 9, -6, -2, -7], [ 3, 1, -9, 9, 2]]) """ print (torch.add(data, 2 )) """ tensor([[-3, 11, -4, 0, -5], [ 5, 3, -7, 11, 4]]) """ print (torch.sub(data, 2 )) """ tensor([[ -7, 7, -8, -4, -9], [ 1, -1, -11, 7, 0]]) """ print (torch.mul(data, 2 )) """ tensor([[-10, 18, -12, -4, -14], [ 6, 2, -18, 18, 4]]) """ print (torch.div(data, 2 )) """ tensor([[-2.5000, 4.5000, -3.0000, -1.0000, -3.5000], [ 1.5000, 0.5000, -4.5000, 4.5000, 1.0000]]) """ print (torch.div(data, 2 , rounding_mode='floor' )) """ tensor([[-3, 4, -3, -1, -4], [ 1, 0, -5, 4, 1]]) """ print (torch.div(data, 2 , rounding_mode='trunc' ))""" tensor([[-2, 4, -3, -1, -3], [ 1, 0, -4, 4, 1]]) """ print (torch.remainder(data, 2 )) """ tensor([[1, 1, 0, 0, 1], [1, 1, 1, 1, 0]]) """ print (torch.fmod(data, 2 )) """ tensor([[-1, 1, 0, 0, -1], [ 1, 1, -1, 1, 0]]) """ print (torch.positive(data)) """ tensor([[-5, 9, -6, -2, -7], [ 3, 1, -9, 9, 2]]) """ print (torch.neg(data)) """ tensor([[ 5, -9, 6, 2, 7], [-3, -1, 9, -9, -2]]) """ print (torch.abs (data))""" tensor([[5, 9, 6, 2, 7], [3, 1, 9, 9, 2]]) """
torch.subtract()是torch.sub()的别名。torch.multiply()是torch.mul()的别名。torch.divide()是torch.div()的别名。torch.true_divide()是torch.div()在rounding_mode=None时的别名。torch.negative()是torch.neg()的别名。torch.absolute()是torch.abs()的别名。
在PyTorch 1.13后(含1.13),可以认为torch.floor_divide()是torch.div()在rounding_mode='floor'时的别名,效果是一样的。
.pow/.square/.sqrt/.rsqrt/.reciprocal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import torchdata = torch.full((2 , 2 ), 9 ) print (data)""" tensor([[9, 9], [9, 9]]) """ print (torch.pow (data, 3 )) """ tensor([[729, 729], [729, 729]]) """ print (torch.square(data)) """ tensor([[81, 81], [81, 81]]) """ print (torch.sqrt(data)) """ tensor([[3., 3.], [3., 3.]]) """ print (torch.rsqrt(data)) """ tensor([[0.3333, 0.3333], [0.3333, 0.3333]]) """ print (torch.reciprocal(data)) """ tensor([[0.1111, 0.1111], [0.1111, 0.1111]]) """
.exp/.log/.log2/.log10/.log1p 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 import torchdata1 = torch.exp(torch.tensor([0 , 1 , 2 , 3 , 4 ])) print (data1) data2 = torch.exp(torch.ones(2 , 2 )) print (data2)""" tensor([[2.7183, 2.7183], [2.7183, 2.7183]]) """ print (torch.log(data1)) print (torch.log(data2))""" tensor([[1.0000, 1.0000], [1.0000, 1.0000]]) """ data3 = torch.tensor([1 , 2 , 4 , 8 , 16 ]) print (torch.log2(data3)) data4 = torch.tensor([1 , 10 , 100 , 1000 , 10000 ]) print (torch.log10(data4)) print (torch.log1p(data1 - 1 )) print (torch.log1p(data2 - 1 ))""" tensor([[1.0000, 1.0000], [1.0000, 1.0000]]) """
torch.log1p()计算input + 1的自然对数:。
.sin/.cos/.tan/.asin/.acos/.atan/.atan2/.sinh/.cosh/.tanh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import torchdata = torch.deg2rad(torch.arange(0 , 361 , 30 )) print (data)print (torch.sin(data))print (torch.cos(data))print (torch.tan(data))print (torch.asin(data))print (torch.acos(data))print (torch.atan(data))print (torch.atan2(data, data))print (torch.sinh(data))print (torch.cosh(data))print (torch.tanh(data))
torch.arcsin()是torch.asin()的别名。torch.arccos()是torch.acos()的别名。torch.arctan()是torch.atan()的别名。torch.arctan2()是torch.atan2()的别名。torch.arcsinh()是torch.asinh()的别名。torch.arccosh()是torch.acosh()的别名。torch.arctanh()是torch.atanh()的别名。
.angle/.deg2rad 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import numpy as npimport torchprint (torch.angle(torch.tensor([[1 , 1 + 1j , 1j , -1 + 1j , -1 , -1 -1j , -1j , 1 -1j ]]))) print (torch.angle(torch.tensor([[1 , 1 + 1j , 1j , -1 + 1j , -1 , -1 -1j , -1j , 1 -1j ]])) * 180 / np.pi) print (torch.deg2rad(torch.tensor([[0 , 45 , 90 , 135 , 180 , 360 ], [-360 , -180 , -135 , -90 , -45 , 0 ]])))""" tensor([[ 0.0000, 0.7854, 1.5708, 2.3562, 3.1416, 6.2832], [-6.2832, -3.1416, -2.3562, -1.5708, -0.7854, 0.0000]]) """
.bitwise_and/.bitwise_or/.bitwise_not/.bitwise_xor/.bitwise_left_shift/.bitwise_right_shift 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import torchprint (torch.bitwise_and(torch.tensor([-1 , -2 , 3 ], dtype=torch.int8), torch.tensor([1 , 0 , 3 ], dtype=torch.int8))) print (torch.bitwise_and(torch.tensor([True , True , False ]), torch.tensor([False , True , False ]))) print (torch.bitwise_or(torch.tensor([-1 , -2 , 3 ], dtype=torch.int8), torch.tensor([1 , 0 , 3 ], dtype=torch.int8))) print (torch.bitwise_or(torch.tensor([True , True , False ]), torch.tensor([False , True , False ]))) print (torch.bitwise_not(torch.tensor([-1 , -2 , 3 ], dtype=torch.int8))) print (torch.bitwise_not(torch.tensor([True , True , False ]))) print (torch.bitwise_xor(torch.tensor([-1 , -2 , 3 ], dtype=torch.int8), torch.tensor([1 , 0 , 3 ], dtype=torch.int8))) print (torch.bitwise_xor(torch.tensor([True , True , False ]), torch.tensor([False , True , False ]))) print (torch.bitwise_left_shift(torch.tensor([-1 , -2 , 3 ], dtype=torch.int8), torch.tensor([1 , 0 , 3 ], dtype=torch.int8))) print (torch.bitwise_right_shift(torch.tensor([-1 , -2 , 3 ], dtype=torch.int8), torch.tensor([1 , 0 , 3 ], dtype=torch.int8)))
.floor/.ceil/.round 1 2 3 4 5 6 7 8 9 10 11 12 13 import torchtorch.manual_seed(1 ) data = torch.rand(1 , 5 ) * 10 print (data) print (torch.floor(data)) print (torch.ceil(data)) print (torch.round (data))
torch.floor()向下取整,torch.ceil()向上取整,torch.round()四舍五入。
.trunc/.frac 1 2 3 4 5 6 7 8 9 10 11 import torchtorch.manual_seed(1 ) data = torch.rand(1 , 5 ) * 10 print (data) print (torch.trunc(data)) print (torch.frac(data))
torch.trunc()取整数,torch.frac()取小数。
torch.fix()是torch.trunc()的别名。
.clamp/.clamp_min/.clamp_max 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 import torchtorch.manual_seed(1 ) data = torch.rand((2 , 5 )) * 20 print (data)""" tensor([[15.1526, 5.5862, 8.0614, 14.6937, 0.5856], [15.9972, 7.9427, 15.0874, 11.3902, 8.7756]]) """ print (torch.clamp(data, 10 ))""" tensor([[15.1526, 10.0000, 10.0000, 14.6937, 10.0000], [15.9972, 10.0000, 15.0874, 11.3902, 10.0000]]) """ print (torch.clamp(data, max =15 ))""" tensor([[15.0000, 5.5862, 8.0614, 14.6937, 0.5856], [15.0000, 7.9427, 15.0000, 11.3902, 8.7756]]) """ print (torch.clamp(data, 10 , 15 ))""" tensor([[15.0000, 10.0000, 10.0000, 14.6937, 10.0000], [15.0000, 10.0000, 15.0000, 11.3902, 10.0000]]) """ print (torch.clamp_min(data, 10 ))""" tensor([[15.1526, 10.0000, 10.0000, 14.6937, 10.0000], [15.9972, 10.0000, 15.0874, 11.3902, 10.0000]]) """ print (torch.clamp_max(data, 15 ))""" tensor([[15.0000, 5.5862, 8.0614, 14.6937, 0.5856], [15.0000, 7.9427, 15.0000, 11.3902, 8.7756]]) """
torch.clamp()用于将输入的张量夹紧到区间[min, max]。
torch.clip()是torch.clamp()的别名。
.dot/.mm/.bmm/.matmul 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import torchprint (torch.dot(torch.tensor([1 , 2 ]), torch.tensor([3 , 4 ]))) print (torch.mm(torch.full((2 , 3 ), 2 ), torch.full((3 , 2 ), 3 )))""" tensor([[18, 18], [18, 18]]) """ print (torch.bmm(torch.full((2 , 2 , 3 ), 2 ), torch.full((2 , 3 , 2 ), 3 )))""" tensor([[[18, 18], [18, 18]], [[18, 18], [18, 18]]]) """ data1 = torch.rand(4 , 3 , 800 , 600 ) print (data1.shape) data2 = torch.rand(4 , 3 , 600 , 400 ) print (data2.shape) print (data1.matmul(data2).shape)
dot()只支持一维矩阵相乘,mm()只支持二维矩阵相乘,bmm()只支持三维矩阵相乘,matmul()支持任意维度矩阵相乘。
比较运算 .eq/.ne/.gt/.ge/.lt/.le/.equal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 import torchtorch.manual_seed(1 ) data = torch.randperm(10 ).view(2 , 5 ) print (data)""" tensor([[5, 6, 1, 2, 0], [8, 9, 3, 7, 4]]) """ print (torch.eq(data, 5 )) """ tensor([[ True, False, False, False, False], [False, False, False, False, False]]) """ print (torch.ne(data, 5 )) """ tensor([[False, True, True, True, True], [ True, True, True, True, True]]) """ print (torch.gt(data, 5 )) """ tensor([[False, True, False, False, False], [ True, True, False, True, False]]) """ print (torch.ge(data, 5 )) """ tensor([[ True, True, False, False, False], [ True, True, False, True, False]]) """ print (torch.lt(data, 5 )) """ tensor([[False, False, True, True, True], [False, False, True, False, True]]) """ print (torch.le(data, 5 )) """ tensor([[ True, False, True, True, True], [False, False, True, False, True]]) """ print (torch.equal(torch.tensor([1 , 2 ]), torch.tensor([1 , 2 ]))) print (torch.equal(torch.tensor([1 , 2 ]), torch.tensor([2 , 1 ])))
torch.not_equal()是torch.ne()的别名。torch.greater()是torch.gt()的别名。torch.greater_equal()是torch.ge()的别名。torch.less()是torch.lt()的别名。torch.less_equal()是torch.le()的别名。
.isfinite/.isinf/.isposinf/.isneginf/.isnan/.isreal/.isin 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import torchdata = torch.tensor([1 , float ('inf' ), torch.inf, float ('-inf' ), -torch.inf, float ('nan' ), torch.nan, True , False ]) print (torch.isfinite(data)) print (torch.isinf(data)) print (torch.isposinf(data)) print (torch.isneginf(data)) print (torch.isnan(data)) print (torch.isreal(torch.tensor([1 , 1 +1j , 2 +0j , float ('nan' ), True , False ]))) elements = [[1 , 2 ], [3 , 4 ]] test_elements = [2 , 3 ] print (torch.isin(torch.tensor(elements), torch.tensor(test_elements)))""" tensor([[False, True], [ True, False]]) """
.isclose/.allclose 1 2 3 4 5 6 7 8 9 10 11 import torchprint (torch.isclose(torch.tensor((1. , 2 , 3 )), torch.tensor((1 + 1e-10 , 3 , 4 )))) print (torch.isclose(torch.tensor((float ('inf' ), 4 )), torch.tensor((float ('inf' ), 6 )), rtol=.5 )) print (torch.allclose(torch.tensor([10000. , 1e-07 ]), torch.tensor([10000.1 , 1e-08 ]))) print (torch.allclose(torch.tensor([10000. , 1e-08 ]), torch.tensor([10000.1 , 1e-09 ]))) print (torch.allclose(torch.tensor([1.0 , float ('nan' )]), torch.tensor([1.0 , float ('nan' )]))) print (torch.allclose(torch.tensor([1.0 , float ('nan' )]), torch.tensor([1.0 , float ('nan' )]), equal_nan=True ))
torch.isclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False)
torch.allclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False)
校验是否接近公式:
.maximum/.minimum/.fmax/.fmin 1 2 3 4 5 6 7 8 9 10 import torchdata1 = torch.tensor([9.7 , float ('nan' ), 3.1 , torch.nan, 11.1 ]) data2 = torch.tensor([-2.2 , 0.5 , torch.nan, float ('nan' ), 7.8 ]) print (torch.maximum(data1, data2)) print (torch.minimum(data1, data2)) print (torch.fmax(data1, data2)) print (torch.fmin(data1, data2))
torch.maximum()和torch.minimum()用于比较两数的大小,不支持比较NaN。
torch.fmax()和torch.fmin()用于比较两数的大小,支持比较NaN。
.sort/.msort/.argsort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 import torchtorch.manual_seed(1 ) data = torch.rand(2 , 5 ) print (data)""" tensor([[0.7576, 0.2793, 0.4031, 0.7347, 0.0293], [0.7999, 0.3971, 0.7544, 0.5695, 0.4388]]) """ print (torch.sort(data))""" torch.return_types.sort( values=tensor([[0.0293, 0.2793, 0.4031, 0.7347, 0.7576], [0.3971, 0.4388, 0.5695, 0.7544, 0.7999]]), indices=tensor([[4, 1, 2, 3, 0], [1, 4, 3, 2, 0]])) """ print (torch.sort(data, dim=0 ))""" torch.return_types.sort( values=tensor([[0.7576, 0.2793, 0.4031, 0.5695, 0.0293], [0.7999, 0.3971, 0.7544, 0.7347, 0.4388]]), indices=tensor([[0, 0, 0, 1, 0], [1, 1, 1, 0, 1]])) """ print (torch.sort(data, descending=True ))""" torch.return_types.sort( values=tensor([[0.7576, 0.7347, 0.4031, 0.2793, 0.0293], [0.7999, 0.7544, 0.5695, 0.4388, 0.3971]]), indices=tensor([[0, 3, 2, 1, 4], [0, 2, 3, 4, 1]])) """ print (torch.msort(data)) """ tensor([[0.7576, 0.2793, 0.4031, 0.5695, 0.0293], [0.7999, 0.3971, 0.7544, 0.7347, 0.4388]]) """ print (torch.argsort(data))""" tensor([[4, 1, 2, 3, 0], [1, 4, 3, 2, 0]]) """ print (torch.argsort(data, dim=0 ))""" tensor([[0, 0, 0, 1, 0], [1, 1, 1, 0, 1]]) """ print (torch.argsort(data, descending=True ))""" tensor([[0, 3, 2, 1, 4], [0, 2, 3, 4, 1]]) """
torch.sort()排序后返回排序结果和索引。
torch.msort(input))等价于torch.sort()对第一维度进行排序在取排序结果,torch.sort(input, dim=0)[0],返回结果不包含索引。
torch.argsort()返回排序后的索引。
.topk/.kthvalue 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 import torchtorch.manual_seed(1 ) data = torch.randperm(30 , dtype=torch.double).view(3 , 10 ) print (data)""" tensor([[25., 4., 6., 8., 23., 18., 17., 20., 19., 12.], [ 5., 14., 22., 3., 27., 15., 9., 13., 7., 11.], [10., 2., 24., 29., 21., 26., 28., 1., 16., 0.]], dtype=torch.float64) """ print (torch.topk(data, 3 ))""" torch.return_types.topk( values=tensor([[25., 23., 20.], [27., 22., 15.], [29., 28., 26.]], dtype=torch.float64), indices=tensor([[0, 4, 7], [4, 2, 5], [3, 6, 5]])) """ print (torch.topk(data, 3 , largest=False ))""" torch.return_types.topk( values=tensor([[4., 6., 8.], [3., 5., 7.], [0., 1., 2.]], dtype=torch.float64), indices=tensor([[1, 2, 3], [3, 0, 8], [9, 7, 1]])) """ print (torch.kthvalue(data, 8 ))""" torch.return_types.kthvalue( values=tensor([20., 15., 26.], dtype=torch.float64), indices=tensor([7, 5, 5])) """ print (torch.kthvalue(data, 2 , dim=0 ))""" torch.return_types.kthvalue( values=tensor([10., 4., 22., 8., 23., 18., 17., 13., 16., 11.], dtype=torch.float64), indices=tensor([2, 0, 1, 0, 0, 0, 0, 1, 2, 1])) """
torch.topk()获取前k大的数据,torch.kthvalue()获取第k小的数据。
归约运算 .max/.min/.amax/.amin/.aminmax/.argmax/.argmin 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 import torchtorch.manual_seed(1 ) data = torch.rand(4 , 4 ) print (data)""" tensor([[0.7576, 0.2793, 0.4031, 0.7347], [0.0293, 0.7999, 0.3971, 0.7544], [0.5695, 0.4388, 0.6387, 0.5247], [0.6826, 0.3051, 0.4635, 0.4550]]) """ print (torch.max (data)) print (torch.max (data, dim=0 ))""" torch.return_types.max( values=tensor([0.7576, 0.7999, 0.6387, 0.7544]), indices=tensor([0, 1, 2, 1])) """ print (torch.min (data)) print (torch.min (data, dim=1 ))""" torch.return_types.min( values=tensor([0.2793, 0.0293, 0.4388, 0.3051]), indices=tensor([1, 0, 1, 1])) """ print (torch.amax(data)) print (torch.amax(data, dim=0 )) print (torch.amin(data)) print (torch.amin(data, dim=1 )) print (torch.aminmax(data))""" torch.return_types.aminmax( min=tensor(0.0293), max=tensor(0.7999)) """ print (torch.aminmax(data, dim=0 ))""" torch.return_types.aminmax( min=tensor([0.0293, 0.2793, 0.3971, 0.4550]), max=tensor([0.7576, 0.7999, 0.6387, 0.7544])) """ print (torch.argmax(data)) print (torch.argmax(data, dim=0 )) print (torch.argmin(data)) print (torch.argmin(data, dim=1 ))
torch.max()和torch.min()指定了dim时,返回极值和索引。
torch.amax()和torch.amin()只返回极值,不返回索引。
torch.argmax()和torch.argmin()返回极值对应的索引。不指定dim时,返回的是打平后的索引。
.mean/.nanmean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import torchdata = torch.tensor([[torch.nan, 1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 , 9 ], [10 , 11 , 12 , 13 , torch.nan]]) print (data)""" tensor([[nan, 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [10., 11., 12., 13., nan]]) """ print (torch.mean(data)) print (torch.mean(data, dim=0 )) print (torch.mean(data, dim=1 )) print (torch.nanmean(data)) print (torch.nanmean(data, dim=0 )) print (torch.nanmean(data, dim=1 ))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import torchdata = torch.tensor([[torch.nan, 1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 , 9 ], [10 , 11 , 12 , 13 , torch.nan]]) print (data)""" tensor([[nan, 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [10., 11., 12., 13., nan]]) """ print (torch.median(data)) print (torch.median(data, dim=0 ))""" torch.return_types.median( values=tensor([nan, 6., 7., 8., nan]), indices=tensor([0, 1, 1, 1, 2])) """ print (torch.median(data, dim=1 )) """ torch.return_types.median( values=tensor([nan, 7., nan]), indices=tensor([0, 2, 4])) """ print (torch.nanmedian(data)) print (torch.nanmedian(data, dim=0 ))""" torch.return_types.nanmedian( values=tensor([5., 6., 7., 8., 4.]), indices=tensor([1, 1, 1, 1, 0])) """ print (torch.nanmedian(data, dim=1 ))""" torch.return_types.nanmedian( values=tensor([ 2., 7., 11.]), indices=tensor([2, 2, 1])) """
.sum/.nansum 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import torchdata = torch.tensor([[torch.nan, 1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 , 9 ], [10 , 11 , 12 , 13 , torch.nan]]) print (data)""" tensor([[nan, 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [10., 11., 12., 13., nan]]) """ print (torch.sum (data)) print (torch.sum (data, dim=0 )) print (torch.sum (data, dim=1 )) print (torch.nansum(data)) print (torch.nansum(data, dim=0 )) print (torch.nansum(data, dim=1 ))
.prod 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import torchdata = torch.tensor([[torch.nan, 1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 , 9 ], [10 , 11 , 12 , 13 , torch.nan]]) print (data)""" tensor([[nan, 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [10., 11., 12., 13., nan]]) """ print (torch.prod(data)) print (torch.prod(data, dim=0 )) print (torch.prod(data, dim=1 ))
.var/.var_mean/.std/.std_mean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import torchdata = torch.arange(1 , 10 , dtype=torch.double).view(3 , 3 ) print (data)""" tensor([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]], dtype=torch.float64) """ print (torch.var(data)) print (torch.var(data, unbiased=False )) print (torch.var_mean(data)) print (torch.std(data)) print (torch.std(data, unbiased=False )) print (torch.std_mean(data))
总体方差:
样本方差:
总体标准差:
样本标准差:
.norm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import torchdata = torch.full((2 , 2 ), 2. ) print (data)""" tensor([[2., 2.], [2., 2.]]) """ print (torch.norm(data, p=1 )) print (torch.norm(data)) print (torch.norm(data, p=3 ))
一范数:
二范数:
p范数:
.dist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import torchinput = torch.full((2 , 2 ), 4. )print (input )""" tensor([[4., 4.], [4., 4.]]) """ other = torch.full((2 , 2 ), 2. ) print (other)""" tensor([[2., 2.], [2., 2.]]) """ print (torch.dist(input , other, p=1 )) print (torch.dist(input , other)) print (torch.dist(input , other, p=3 ))
torch.dist()返回(input - other)的p范数。
.any/.all 1 2 3 4 5 6 7 8 9 10 11 import torchprint (torch.any (torch.tensor([]))) print (torch.any (torch.tensor([True , True ]))) print (torch.any (torch.tensor([True , False ]))) print (torch.any (torch.tensor([False , False ]))) print (torch.all (torch.tensor([]))) print (torch.all (torch.tensor([True , True ]))) print (torch.all (torch.tensor([True , False ]))) print (torch.all (torch.tensor([False , False ])))
序列化 .save 1 2 3 4 5 6 7 8 9 10 import ioimport torchx = torch.tensor([0 , 1 , 2 , 3 , 4 ]) torch.save(x, 'tensor.pt' ) buffer = io.BytesIO() torch.save(x, buffer)
.load 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import ioimport torchtorch.load('tensors.pt' , encoding='ascii' ) torch.load('tensors.pt' , map_location=torch.device('cpu' )) torch.load('tensors.pt' , map_location={'cuda:1' : 'cuda:0' }) with open ('tensor.pt' , 'rb' ) as f: buffer = io.BytesIO(f.read()) torch.load(buffer)