PyTorch常用函数(3)

30.规约操作

  • torch.cumprod(input, dim, out=None) → Tensor:返回沿指定维度的累积积
import torch
a = torch.randn(2, 5)
print(a)
print(torch.cumprod(a, dim=0))
# tensor([[-1.1604, -0.2663,  0.2853, -0.2302,  0.6004],
#         [ 1.0017, -0.4695, -1.0371,  2.2184,  0.3053]])
# tensor([[-1.1604, -0.2663,  0.2853, -0.2302,  0.6004],
#         [-1.1623,  0.1250, -0.2959, -0.5106,  0.1833]])
print(torch.cumprod(a, dim=1))
# tensor([[-1.1604,  0.3090,  0.0882, -0.0203, -0.0122],
#         [ 1.0017, -0.4703,  0.4878,  1.0821,  0.3304]])
  • torch.cumsum(input, dim, out=None) → Tensor:返回沿指定维度的累积和
import torch
a = torch.randn(2, 5)
print(a)
# tensor([[-0.0561, -0.8035, -1.6886,  1.3419, -0.8113],
#         [-1.3108, -0.6712,  1.5329,  0.6765, -0.8279]])
print(torch.cumsum(a, dim=0))
# tensor([[-0.0561, -0.8035, -1.6886,  1.3419, -0.8113],
#         [-1.3669, -1.4747, -0.1558,  2.0184, -1.6392]])
print(torch.cumsum(a, dim=1))
# tensor([[-0.0561, -0.8596, -2.5482, -1.2063, -2.0176],
#         [-1.3108, -1.9820, -0.4491,  0.2274, -0.6005]])
  • torch.dist(input, other, p=2, out=None) → Tensor:返回(input - other) 的 p 范数
import torch
a = torch.randn(2, 3)
b = torch.randn(2, 3)
print(a)
# tensor([[-0.9696, -0.9325, -1.5946],
#         [-0.8043, -0.1118,  0.9441]])
print(b)
# tensor([[ 0.6359, -1.0641,  1.8906],
#         [ 0.2389,  0.4588, -0.5496]])
print(torch.dist(a, b))
# tensor(4.2880)
  • torch.mean(input) → float:返回输入张量所有元素的均值。torch.mean(input, dim, out=None) → Tensor:返回输入张量给定维度 dim 上的均值,输出形状与输入相同,除了给定维度上为 1。
import torch
a = torch.randn(2,4)
print(a)
# tensor([[ 1.2870, -0.3431,  1.8366,  1.3839],
#         [-0.9499, -1.1878, -0.5120, -0.5293]])
print(torch.mean(a))
# tensor(0.1232)
print(torch.mean(a, dim=0))
# tensor([ 0.1685, -0.7654,  0.6623,  0.4273])
print(torch.mean(a, dim=1))
# tensor([ 1.0411, -0.7948])
  • torch.median(input, dim=-1, values=None, indices=None) -> (Tensor,LongTensor):返回输入张量给定维度的中位数,同时返回一个包含中位数的索引的 LongTensor
import torch
a = torch.randn(4, 5)
print(a)
# tensor([[-0.9897,  0.6351, -0.7017, -1.6378,  1.8412],
#         [ 0.2740,  1.2619,  1.9158, -0.4510, -0.0175],
#         [-0.6110, -0.7251, -0.5173,  0.3697,  0.9142],
#         [ 0.8707,  0.0207, -0.0985,  0.3641,  0.6325]])
print( torch.median(a, 1))
# torch.return_types.median(
# values=tensor([-0.7017,  0.2740, -0.5173,  0.3641]),
# indices=tensor([2, 0, 2, 3]))
  • torch.mode(input, dim=-1, values=None, indices=None) -> (Tensor, LongTensor):返回给定维 dim 上的众数值,同时返回一个包含众数值的索引 LongTensor。
import torch
a = torch.randn(4, 5)
print(a)
# tensor([[ 1.0266,  0.0899,  1.3393,  0.9917,  0.7374],
#         [-0.2971, -1.1028,  0.5906, -0.8490,  1.0486],
#         [ 0.2292,  0.0793, -1.5121, -0.9094,  0.4897],
#         [-0.6471,  0.3729,  0.1440,  0.3499, -1.2974]])
print( torch.mode(a, 1))
# torch.return_types.mode(
# values=tensor([ 0.0899, -1.1028, -1.5121, -1.2974]),
# indices=tensor([1, 1, 2, 4]))
  • torch.norm(input, p=2) → float:返回输入张量 input 的 p 范数。torch.norm(input, p, dim, out=None) → Tensor:返回输入张量给定维 dim 上的p 范数。 输出形状与输入相同,除了给定维度上为 1。
import torch
a = torch.randn(4, 5)
print(a)
# tensor([[-1.0609,  0.1965, -1.2835,  2.1013, -0.0954],
#         [-0.8029, -0.7379,  0.0645, -0.5621,  2.1143],
#         [-0.2236, -0.7421, -0.0287,  0.1756, -0.8095],
#         [-1.2289,  0.4863,  0.9667, -0.6227,  0.3788]])
print(torch.norm(a, 1))
# tensor(14.6822)
print(torch.norm(a, 2, 1))
# tensor([2.6900, 2.4453, 1.1348, 1.7923])
  • torch.prod(input) → float:返回输入张量 input 所有元素的积。torch.prod(input, dim, out=None) → Tensor:返回输入张量给定维度上的积。 输出形状与输入相同,除了给定维度上为 1.
import torch
a = torch.randn(2, 3)
print(a)
# tensor([[ 0.4589,  0.2302, -0.6103],
#         [-0.0586,  0.1665,  0.7223]])
print(torch.prod(a))
# tensor(0.0005)
print(torch.prod(a, 1))
# tensor([-0.0645, -0.0070])
  • torch.std(input) → float:返回输入张量 input 所有元素的标准差。torch.std(input, dim, out=None) → Tensor:返回输入张量给定维度上的标准差。 输出形状与输入相同,除了给定维度上为 1.
import torch
a = torch.randn(2, 3)
print(a)
# tensor([[ 0.2685, -0.4144,  1.2848],
#         [-0.2983,  0.3613,  2.4993]])
print(torch.std(a))
# tensor(1.1025)
print(torch.std(a, 1))
# tensor([0.8550, 1.4625])
  • torch.sum(input) → float:返回输入张量 input 所有元素的标准差。torch.sum(input, dim, out=None) → Tensor:返回输入张量给定维度上的标准差。 输出形状与输入相同,除了给定维度上为 1。
import torch
a = torch.randn(2, 3)
print(a)
# tensor([[ 0.7401, -1.1846, -0.1362],
#         [-1.3965,  0.6241,  1.0224]])
print(torch.sum(a))
# tensor(-0.3308)
print(torch.sum(a, 1))
# tensor([-0.5808,  0.2500])
  • torch.var(input) → float:返回输入张量 input 所有元素的方差。torch.var(input, dim, out=None) → Tensor:返回输入张量给定维度上的方差。 输出形状与输入相同,除了给定维度上为 1。
import torch
a = torch.randn(2, 3)
print(a)
# tensor([[-0.3314,  0.3066,  0.6469],
#         [-0.0995, -0.7963,  1.1473]])
print(torch.var(a))
# tensor(0.4910)
print(torch.var(a, 1))
# tensor([0.2467, 0.9695])

31.比较操作符

  • torch.eq(input, other, out=None) → Tensor:比较元素相等性。第二个参数可为一个数或与第一个参数同类型形状的张量。
import torch
print(torch.eq(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])))
# tensor([[ True, False],
#         [False,  True]])
  • torch.equal(tensor1, tensor2) → bool:如果两个张量有相同的形状和元素值,则返回 True ,否则 False。
import torch
print(torch.equal(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])))
# False
  • torch.ge(input, other, out=None) → Tensor:逐元素比较 input 和 other,即是否 input>=other
import torch
print(torch.ge(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])))
# tensor([[ True,  True],
#         [False,  True]])
  • torch.gt(input, other, out=None) → Tensor:逐元素比较 input 和 other,即是否 input>other
import torch
print(torch.gt(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])))
# tensor([[False,  True],
#         [False, False]])
  • torch.kthvalue(input, k, dim=None, out=None) -> (Tensor, LongTensor):取输入张量 input 指定维上第 k 个最小值,返回一个元组 (values,indices),默认是最后一维度
import torch
x = torch.randint(10, size=(3,4))
print(x)
# tensor([[8, 1, 7, 3],
#         [2, 7, 8, 2],
#         [6, 3, 4, 4]])
print(torch.kthvalue(x, 3))
# torch.return_types.kthvalue(
# values=tensor([7, 7, 4]),
# indices=tensor([2, 1, 2]))
  • torch.le(input, other, out=None) → Tensor:比较元素是否 input<=other。第二个参数可为一个数或与第一个参数同类型形状的张量。
import torch
print(torch.le(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])))
# tensor([[ True, False],
#         [ True,  True]])
  • torch.lt(input, other, out=None) → Tensor:比较元素是否 input<other。第二个参数可为一个数或与第一个参数同类型形状的张量。
import torch
print(torch.lt(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])))
# tensor([[False, False],
#         [ True, False]])
  • torch.max():返回输入张量所有元素的最大值。torch.max(input, dim, max=None, max_indices=None) -> (Tensor, LongTensor):返回输入张量给定维度上的最大值,并同时返回每个最大值的位置索引。
import torch
x = torch.randint(10, size=(3,4))
print(x)
# tensor([[4, 0, 3, 8],
#         [8, 9, 4, 4],
#         [0, 7, 3, 5]])
print(torch.max(x))
# tensor(9)
print(torch.max(x, 1))
# torch.return_types.max(
# values=tensor([8, 9, 7]),
# indices=tensor([3, 1, 1]))
  • torch.min():返回输入张量所有元素的最大值。torch.min(input, dim, min=None, min_indices=None) -> (Tensor, LongTensor):返回输入张量给定维度上的最小值,并同时返回每个最小值的位置索引。
import torch
x = torch.randint(10, size=(3,4))
print(x)
# tensor([[4, 0, 3, 8],
#         [8, 9, 4, 4],
#         [0, 7, 3, 5]])
print(torch.min(x))
# tensor(0)
print(torch.min(x, 1))
# torch.return_types.min(
# values=tensor([0, 4, 0]),
# indices=tensor([1, 2, 0]))
  • torch.min(input, other, out=None) → Tensor:input 中逐元素与 other 相应位置的元素对比,返回最小值到输出张量。
import torch
x = torch.randint(10, size=(3,4))
y = torch.randint(10, size=(3,4))
print(x)
# tensor([[6, 2, 0, 3],
#         [9, 6, 3, 3],
#         [2, 4, 2, 2]])
print(y)
# tensor([[8, 5, 3, 3],
#         [3, 5, 8, 5],
#         [9, 7, 0, 6]])
print( torch.min(a, b))
# tensor([[-0.3314, -1.0641,  0.6469],
#         [-0.0995, -0.7963, -0.5496]])
  • torch.ne(input, other, out=None) → Tensor:逐元素比较 input 和 other , 即是否 input!=other
import torch
print( torch.ne(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])))
# tensor([[False,  True],
#         [ True, False]])
  • torch.sort(input, dim=None, descending=False, out=None) -> (Tensor,LongTensor):对输入张量 input 沿着指定维按升序排序。如果不给定 dim,则默认为输入的最后一维。如果指定参数 descending 为 True,则按降序排序
import torch
x = torch.randn(3, 4)
print(x)
# tensor([[-1.2163,  0.1786, -0.7183, -0.9574],
#         [ 1.0628, -1.2083,  1.4363, -1.4453],
#         [-0.3482, -0.2858,  0.0372,  0.3691]])
sorted, indices = torch.sort(x)
print(sorted)
# tensor([[-1.2163, -0.9574, -0.7183,  0.1786],
#         [-1.4453, -1.2083,  1.0628,  1.4363],
#         [-0.3482, -0.2858,  0.0372,  0.3691]])
print(indices)
# tensor([[0, 3, 2, 1],
#         [3, 1, 0, 2],
#         [0, 1, 2, 3]])
sorted, indices = torch.sort(x, 0)
print(sorted)
# tensor([[-1.2163, -1.2083, -0.7183, -1.4453],
#         [-0.3482, -0.2858,  0.0372, -0.9574],
#         [ 1.0628,  0.1786,  1.4363,  0.3691]])
print(indices)
# tensor([[0, 1, 0, 1],
#         [2, 2, 2, 0],
#         [1, 0, 1, 2]])
  • torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) ->(Tensor, LongTensor):沿给定 dim 维度返回输入张量 input 中 k 个最大值。 如果不指定 dim,则默认为 input 的最后一维。 如果 largest 为 False ,则返回最小的 k 个值。
import torch
x = torch.arange(1, 6)
print(torch.topk(x, 3))
# torch.return_types.topk(
# values=tensor([5, 4, 3]),
# indices=tensor([4, 3, 2]))
print(torch.topk(x, 3, 0, largest=False))
# torch.return_types.topk(
# values=tensor([1, 2, 3]),
# indices=tensor([0, 1, 2]))
  • torch.cross(input, other, dim=-1, out=None) → Tensor:返回沿着维度 dim 上,两个张量 input 和 other 的向量积(叉积),input 和 other 必须有相同的形状,且指定的 dim 维上 size 必须为 3
import torch
a = torch.randn(5, 3)
b = torch.randn(5, 3)
print(a)
# tensor([[ 0.5946, -0.8134,  1.0833],
#         [-0.3632, -0.4629,  0.4345],
#         [-1.9171,  1.4207, -0.9783],
#         [ 1.2185,  2.0365,  1.8153],
#         [ 2.2730,  0.3244,  0.7711]])
print(b)
# tensor([[ 0.7127, -2.2413,  0.4926],
#         [ 0.7981, -0.2888, -0.0072],
#         [-1.4785, -0.7190,  0.2710],
#         [-0.2590,  2.7891, -1.5612],
#         [ 0.2246, -1.7790, -0.9638]])
print(torch.cross(a, b, dim=1))
# tensor([[ 2.0273,  0.4792, -0.7529],
#         [ 0.1288,  0.3441,  0.4743],
#         [-0.3184,  1.9658,  3.4788],
#         [-8.2424,  1.4321,  3.9258],
#         [ 1.0591,  2.3639, -4.1166]])
  • torch.diag(input, diagonal=0, out=None) → Tensor:如果输入是一个向量(1D 张量),则返回一个以 input 为对角线元素的 2D 方阵;如果输入是一个矩阵(2D 张量),则返回一个包含 input 对角线元素的 1D 张量。
    参数diagonal指定对角线:
    (1)diagonal=0,表示主对角线
    (2)diagonal>0,表示主对角线之上
    (3)diagonal<0,表示主对角线之下
import torch
a = torch.randn(5)
print(a)
# tensor([ 0.0848,  0.1497, -0.6551, -0.2155,  0.1230])
print(torch.diag(a))
# tensor([[ 0.0848,  0.0000,  0.0000,  0.0000,  0.0000],
#         [ 0.0000,  0.1497,  0.0000,  0.0000,  0.0000],
#         [ 0.0000,  0.0000, -0.6551,  0.0000,  0.0000],
#         [ 0.0000,  0.0000,  0.0000, -0.2155,  0.0000],
#         [ 0.0000,  0.0000,  0.0000,  0.0000,  0.1230]])
b = torch.randn(5, 3)
print(b)
# tensor([[ 0.4291, -0.5330, -0.8266],
#         [ 2.2512,  0.1613,  0.6011],
#         [-0.2989, -0.1972,  1.5158],
#         [ 0.4881,  0.9797, -0.1186],
#         [-0.1443,  0.0389, -0.3714]])
print(torch.diag(b))
# tensor([0.4291, 0.1613, 1.5158])
print(torch.diag(b, diagonal=1))
# tensor([-0.5330,  0.6011])
print(torch.diag(b, diagonal=-1))
# tensor([ 2.2512, -0.1972, -0.1186])
  • torch.addbmm(beta=1, mat, alpha=1, batch1, batch2, out=None) → Tensor:对两个批 batch1 和 batch2 内存储的矩阵进行批矩阵乘操作,附带 reduced add 步骤( 所有矩阵乘结果沿着batch维度相加)。矩阵 mat 加到最终结果。res=(beta∗M)+(alpha∗sum(batch1@batch2));torch.bmm(batch1, batch2, out=None) → Tensor同理
import torch
M = torch.randn(3, 5)
M1 = torch.zeros(3,5)
print(M)
# tensor([[-0.4609,  0.3909,  0.7048, -0.6142,  0.0064],
#         [ 0.5809, -0.1764,  0.5079, -1.0833, -0.4981],
#         [-0.4096, -0.2294, -0.3762,  1.1874,  1.1278]])
batch1 = torch.randn(2, 3, 4)
batch2 = torch.randn(2, 4, 5)
print(torch.bmm(batch1, batch2))
# tensor([[[ 0.8646, -1.2465,  0.9449, -2.4512,  1.8311],
#          [-2.8177, -0.1856,  3.8801,  3.8990, -3.8802],
#          [ 0.5283,  0.4764, -1.4607,  0.2954,  0.9904]],
#         [[ 0.7259,  0.6174, -0.0836,  1.6268,  0.0210],
#          [-0.8627, -2.9571,  0.5739,  0.1079, -3.0108],
#          [-0.5849, -0.9077,  1.1968,  2.8018, -1.1838]]])

print(torch.addbmm(M, batch1, batch2))
# tensor([[ 1.1297, -0.2383,  1.5661, -1.4387,  1.8586],
#         [-3.0995, -3.3192,  4.9619,  2.9236, -7.3891],
#         [-0.4662, -0.6606, -0.6401,  4.2846,  0.9344]])
print(torch.addbmm(M1, batch1, batch2))
# tensor([[ 1.5905, -0.6291,  0.8613, -0.8245,  1.8522],
#         [-3.6804, -3.1427,  4.4540,  4.0069, -6.8910],
#         [-0.0566, -0.4313, -0.2639,  3.0972, -0.1934]])
  • torch.addmm(beta=1, mat, alpha=1, mat1, mat2, out=None) → Tensor:对矩阵 mat1 和 mat2 进行矩阵乘操作。矩阵 mat 加到最终结果。
import torch
M = torch.randn(2, 3)
M1 = torch.zeros(2, 3)
print(M)
# tensor([[ 0.5542, -0.6272,  0.8439],
#         [ 1.0545, -0.2927, -0.3106]])
mat1 = torch.randn(2, 3)
# tensor([[-0.4998,  0.7233,  0.4432],
#         [-1.1333, -0.3719, -1.3564]])
mat2 = torch.randn(3, 3)
# tensor([[-0.2113,  0.5661,  0.1814],
#         [-0.6698, -2.3457,  2.2170],
#         [ 1.5519, -1.5468,  0.9604]])
print(torch.addmm(M, mat1, mat2))
# tensor([[ 0.8632, -3.2925,  2.7825],
#         [-0.5619,  2.0362, -2.6436]])
print(torch.addmm(M1, mat1, mat2))
# tensor([[ 0.3089, -2.6652,  1.9386],
#         [-1.6164,  2.3289, -2.3330]])
  • torch.addmv(beta=1, tensor, alpha=1, mat, vec, out=None) → Tensor:对矩阵 mat 和向量 vec 进行相乘操作。向量 tensor 加到最终结果。如果 mat 是一个 n×m 维矩阵, vec 是一个 m维向量,那么 out 和 mat 的为 n 维向量。out=(beta∗tensor)+(alpha∗(mat@vec))
import torch
M = torch.randn(2)
M1 = torch.zeros(2)
mat = torch.randn(2, 3)
vec = torch.randn(3)
print(M)
# tensor([0.5126, 0.0933])
print(mat)
# tensor([[-0.4429, -2.5172, -1.1959],
#         [-0.6290, -2.2428, -0.5434]])
print(vec)
# tensor([-1.0688,  0.1566,  0.4100])
print(torch.addmv(M1, mat, vec))
# tensor([-0.4113,  0.0982])
print(torch.addmv(M, mat, vec))
# tensor([0.1014, 0.1914])
  • torch.addr(beta=1, mat, alpha=1, vec1, vec2, out=None) → Tensor:对向量 vec1 和 vec2 进行张量积操作。矩阵 mat 加到最终结果。如果 vec1 是一个 n 维向量,vec2 是一个 m 维向量,那么矩阵 mat 的形状须为 n×m。 res=(beta∗M)+(alpha∗batch1×batch2)
import torch
vec1 = torch.arange(1, 4)
vec2 = torch.arange(1, 3)
print(vec1)
# tensor([1, 2, 3])
print(vec2)
# tensor([1, 2])
M = torch.zeros(3, 2)
print(torch.addr(M, vec1, vec2))
# tensor([[1., 2.],
#         [2., 4.],
#         [3., 6.]])
  • torch.baddbmm(beta=1, mat, alpha=1, batch1, batch2, out=None) → Tensor:对两个批 batch1 和 batch2 内存储的矩阵进行批矩阵乘操作,矩阵 mat 加到最终结果。
import torch
M = torch.randn(2, 2, 3)
M1 = torch.zeros(2, 2, 3)
batch1 = torch.randn(2, 2, 3)
# tensor([[[-0.4150, -0.5572,  0.6548],
#          [ 0.9330, -0.0112, -0.4726]],
#         [[-0.0515, -0.6934, -0.8679],
#          [-0.7749,  0.0213,  0.1234]]])
batch2 = torch.randn(2, 3, 3)
# tensor([[[-0.0385,  0.2875,  0.3309],
#          [-2.0057,  0.3012,  1.6146],
#          [-0.1306, -0.7572, -0.3370]],
#         [[-1.5283,  0.3138, -2.4520],
#          [-0.2487,  1.2520, -1.6133],
#          [ 1.5800,  0.3899, -1.2000]]])
print(torch.baddbmm(M, batch1, batch2))
# tensor([[[ 1.0141, -1.6858, -0.8429],
#          [ 1.5774,  1.0743,  0.5670]],

#         [[-1.5697,  0.1285,  2.5655],
#          [ 1.0704, -2.2823,  1.9249]]])
print(torch.baddbmm(M1, batch1, batch2))
# tensor([[[ 1.0480, -0.7830, -1.2576],
#          [ 0.0483,  0.6227,  0.4499]],

#         [[-1.1201, -1.2227,  2.2864],
#          [ 1.3739, -0.1684,  1.7176]]])
  • torch.dot(tensor1, tensor2) → float:计算两个张量的点乘(内乘),两个张量都为 1-D 向量.
import torch
a = torch.Tensor([2, 3])
b = torch.Tensor([2, 1])
print(torch.dot(a,b))
# tensor(7.)
  • torch.eig(a, eigenvectors=False, out=None) -> (Tensor, Tensor):计算实方阵 a 的特征值和特征向量
import torch
matrix = torch.randn(2,2)
eigen = torch.eig(matrix, eigenvectors=True)
print(eigen.eigenvalues)
# tensor([[-0.7932,  0.0000],
#         [-2.9416,  0.0000]])
print(eigen.eigenvectors)
# tensor([[ 0.9939, -0.8591],
#         [ 0.1107,  0.5117]])
  • torch.ger(vec1, vec2, out=None) → Tensor:计算两向量 vec1,vec2 的张量积。如果 vec1 的长度为 n,vec2 长度为 m,则输出 out 应为形如 n x m 的矩阵。
import torch
v1 = torch.arange(1, 5)
v2 = torch.arange(1, 4)
print(torch.ger(v1, v2))
# tensor([[ 1,  2,  3],
#         [ 2,  4,  6],
#         [ 3,  6,  9],
#         [ 4,  8, 12]])
  • torch.inverse(input, out=None) → Tensor:对方阵输入 input 取逆
import torch
x = torch.rand(3, 3)
print(x)
# tensor([[0.6569, 0.1394, 0.5145],
#         [0.1524, 0.6159, 0.7129],
#         [0.7501, 0.7421, 0.1414]])
x_inverse = torch.inverse(x)
z = torch.mm(x, x_inverse)
print(z)
# tensor([[ 1.0000e+00,  7.7500e-10, -1.0105e-07],
#         [-8.4655e-08,  1.0000e+00,  5.3328e-09],
#         [ 9.3644e-08, -2.2603e-08,  1.0000e+00]])
print(torch.max(torch.abs(z - torch.eye(3))))
# tensor(1.1921e-07)
  • torch.mm(mat1, mat2, out=None) → Tensor:对矩阵 mat1 和 mat2 进行相乘。 如果 mat1 是一个 n×m 张量, mat2 是一个 m×p 张量,将会输出一个 n×p 张量 out。
import torch
mat1 = torch.randn(1, 2)
print(mat1)
# tensor([[ 0.3352, -1.3459]])
mat2 = torch.randn(2, 2)
print(mat2)
# tensor([[-0.7041,  0.0328],
#         [-2.5814,  0.8214]])
print(torch.mm(mat1, mat2))
# tensor([[ 3.2385, -1.0945]])
  • torch.svd(input, some=True, out=None) -> (Tensor, Tensor, Tensor):U,S,V=torch.svd(A) 。 返 回 对 形 如 n×m 的 实 矩 阵 A 进 行 奇 异 值 分 解 的 结 果
import torch
a = torch.Tensor([[ 9.57, -3.49, 9.84],
                  [1.64, 4.02, 0.15],
                  [8.83, 9.80, -8.99]]).t()
print(a)
# tensor([[ 9.5700,  1.6400,  8.8300],
#         [-3.4900,  4.0200,  9.8000],
#         [ 9.8400,  0.1500, -8.9900]])
u, s, v = torch.svd(a)
print(u)
# tensor([[ 0.2536, -0.9111, -0.3250],
#         [ 0.6487, -0.0891,  0.7558],
#         [-0.7176, -0.4025,  0.5684]])
print(s)
# tensor([16.8078, 13.5914,  2.6578])
print(v) 
# tensor([[-0.4104, -0.9100, -0.0583],
#         [ 0.1735, -0.1407,  0.9747],
#         [ 0.8952, -0.3899, -0.2156]])
print(torch.dist(a, torch.mm(torch.mm(u, torch.diag(s)), v.t())))
# tensor(2.1902e-06)                
  • torch.qr(input, out=None) -> (Tensor, Tensor):计算输入矩阵的 QR 分解:返回两个矩阵 q ,r, 使得 x=q∗r ,这里 q 是一个半正交矩阵与 r 是一个上三角矩阵
import torch
a = torch.Tensor([[12, -51, 4], [6, 167, -68], [-4, 24, -41]])
q, r = torch.qr(a)
print(q)
print(r)
# tensor([[-0.8571,  0.3943,  0.3314],
#         [-0.4286, -0.9029, -0.0343],
#         [ 0.2857, -0.1714,  0.9429]])
# tensor([[ -14.0000,  -21.0000,   14.0000],
#         [   0.0000, -175.0000,   70.0000],
#         [   0.0000,    0.0000,  -35.0000]])

参考目录

https://blog.csdn.net/weixin_40920183/article/details/119814472

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值