1. 数学运算
数学运算¶
方法(method) 和 属性(attribute)。
是否带 (),取决于它是“需要执行的函数”,还是“已经存在的数据”。
一、什么时候要用 ()?
当它是一个“方法”时,需要用括号。
方法的本质是:
需要执行一次计算或操作,才能得到结果。
例如:
y.bool()
y.float()
y.view(2, 3)
y.reshape(3, 2)
y.sum()
这些都在做“动作”:
- 类型转换
- 形状变换
- 计算求和
- 生成新张量
它们必须被调用,因此需要 ()。
可以这样记忆:
如果它“动起来了”,就要加括号。
二、什么时候不用 ()?
当它是一个“属性”时,不用括号。
属性的本质是:
对象已经拥有的描述信息。
例如:
y.shape
y.dtype
y.device
y.ndim
y.size()
注意:
shape是属性 → 不加括号dtype是属性device是属性
它们只是读取元信息,不做计算。
可以理解为:
它只是“查看标签”,不是“执行操作”。
三、一个判断技巧(非常实用)
如果它需要参数,几乎一定是方法。
例如:
y.view(2,3)
如果你看到有参数,就必然要加 ()。
四、为什么 size() 要加括号,但 shape 不用?
这是 PyTorch 的接口设计问题。
shape是属性(返回 tuple)size()是方法(功能类似)
本质一样,但实现方式不同。
因此:
y.shape
y.size()
都可以获得尺寸。
In [31]:
Copied!
import torch
# torch.add: 对两个张量逐元素相加,支持广播机制,等价于 a + b
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([4.0, 5.0, 6.0])
print(torch.add(a, b)) # tensor([5., 7., 9.])
# torch.sub: 对两个张量逐元素相减,支持广播机制,等价于 a - b
print(torch.sub(a, b)) # tensor([-3., -3., -3.])
# torch.mul: 对两个张量逐元素相乘(Hadamard积),等价于 a * b
print(torch.mul(a, b)) # tensor([ 4., 10., 18.])
print(a*b)
# torch.div: 对两个张量逐元素相除,等价于 a / b
print(torch.div(a, b)) # tensor([0.2500, 0.4000, 0.5000])
import torch
# torch.add: 对两个张量逐元素相加,支持广播机制,等价于 a + b
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([4.0, 5.0, 6.0])
print(torch.add(a, b)) # tensor([5., 7., 9.])
# torch.sub: 对两个张量逐元素相减,支持广播机制,等价于 a - b
print(torch.sub(a, b)) # tensor([-3., -3., -3.])
# torch.mul: 对两个张量逐元素相乘(Hadamard积),等价于 a * b
print(torch.mul(a, b)) # tensor([ 4., 10., 18.])
print(a*b)
# torch.div: 对两个张量逐元素相除,等价于 a / b
print(torch.div(a, b)) # tensor([0.2500, 0.4000, 0.5000])
tensor([5., 7., 9.]) tensor([-3., -3., -3.]) tensor([ 4., 10., 18.]) tensor([ 4., 10., 18.]) tensor([0.2500, 0.4000, 0.5000])
In [32]:
Copied!
# torch.mul: 同上,逐元素相乘(此处与 torch.dot 对比)
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([4.0, 5.0, 6.0])
print(torch.mul(a, b)) # tensor([ 4., 10., 18.]) —— 逐元素
# torch.dot: 计算两个一维张量的点积(内积),返回标量
print(torch.dot(a, b)) # tensor(32.) (1*4 + 2*5 + 3*6)
# torch.mul: 同上,逐元素相乘(此处与 torch.dot 对比)
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([4.0, 5.0, 6.0])
print(torch.mul(a, b)) # tensor([ 4., 10., 18.]) —— 逐元素
# torch.dot: 计算两个一维张量的点积(内积),返回标量
print(torch.dot(a, b)) # tensor(32.) (1*4 + 2*5 + 3*6)
tensor([ 4., 10., 18.]) tensor(32.)
In [33]:
Copied!
# torch.mm: 计算两个二维矩阵的矩阵乘法(不支持广播)
A = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
B = torch.tensor([[5.0, 6.0], [7.0, 8.0]])
print(torch.mm(A, B))
# tensor([[19., 22.],
# [43., 50.]])
# torch.matmul: 通用矩阵乘法,支持广播和批量维度,是 torch.mm 的超集
print(torch.matmul(A, B))
# tensor([[19., 22.],
# [43., 50.]])
# torch.mm: 计算两个二维矩阵的矩阵乘法(不支持广播)
A = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
B = torch.tensor([[5.0, 6.0], [7.0, 8.0]])
print(torch.mm(A, B))
# tensor([[19., 22.],
# [43., 50.]])
# torch.matmul: 通用矩阵乘法,支持广播和批量维度,是 torch.mm 的超集
print(torch.matmul(A, B))
# tensor([[19., 22.],
# [43., 50.]])
tensor([[19., 22.],
[43., 50.]])
tensor([[19., 22.],
[43., 50.]])
In [34]:
Copied!
# torch.pow: 对张量逐元素求幂,即 x^n
import numpy as np
x = torch.tensor([2.0, 3.0, 4.0])
print(torch.pow(x, 2)) # tensor([ 4., 9., 16.])
# torch.exp: 对张量逐元素计算自然常数 e 的指数,即 e^x
print(torch.exp(torch.tensor([0.0, 1.0, 2.0])))
# tensor([1.0000, 2.7183, 7.3891])
# torch.sqrt: 对张量逐元素计算平方根,即 √x
print(torch.sqrt(torch.tensor([1.0, 4.0, 9.0])))# tensor([1., 2., 3.])
# torch.pow: 对张量逐元素求幂,即 x^n
import numpy as np
x = torch.tensor([2.0, 3.0, 4.0])
print(torch.pow(x, 2)) # tensor([ 4., 9., 16.])
# torch.exp: 对张量逐元素计算自然常数 e 的指数,即 e^x
print(torch.exp(torch.tensor([0.0, 1.0, 2.0])))
# tensor([1.0000, 2.7183, 7.3891])
# torch.sqrt: 对张量逐元素计算平方根,即 √x
print(torch.sqrt(torch.tensor([1.0, 4.0, 9.0])))# tensor([1., 2., 3.])
tensor([ 4., 9., 16.]) tensor([1.0000, 2.7183, 7.3891]) tensor([1., 2., 3.])
In [35]:
Copied!
# torch.sum: 计算张量所有元素(或指定维度)的求和,dim = k → 沿着第 k 个轴进行运算,把这个轴“消掉”。
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(torch.sum(x)) # tensor(10.) 全局求和
print(torch.sum(x, dim=0)) # tensor([4., 6.]) 按列求和
# torch.mean: 计算张量所有元素(或指定维度)的均值
print(torch.mean(x)) # tensor(2.5000)
print(torch.mean(x, dim=1)) # tensor([1.5000, 3.5000]) 按行均值
# torch.std: 计算张量所有元素(或指定维度)的标准差(默认无偏估计)
print(torch.std(x)) # tensor(1.2910)
# torch.sum: 计算张量所有元素(或指定维度)的求和,dim = k → 沿着第 k 个轴进行运算,把这个轴“消掉”。
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(torch.sum(x)) # tensor(10.) 全局求和
print(torch.sum(x, dim=0)) # tensor([4., 6.]) 按列求和
# torch.mean: 计算张量所有元素(或指定维度)的均值
print(torch.mean(x)) # tensor(2.5000)
print(torch.mean(x, dim=1)) # tensor([1.5000, 3.5000]) 按行均值
# torch.std: 计算张量所有元素(或指定维度)的标准差(默认无偏估计)
print(torch.std(x)) # tensor(1.2910)
tensor(10.) tensor([4., 6.]) tensor(2.5000) tensor([1.5000, 3.5000]) tensor(1.2910)
In [36]:
Copied!
# torch.max: 返回张量的最大值;若指定 dim,则同时返回最大值和对应索引
x = torch.tensor([3.0, 1.0, 4.0, 1.0, 5.0])
print(torch.max(x)) # tensor(5.)
print(torch.max(x, dim=0)) # torch.return_types.max(values=tensor(5.), indices=tensor(4))
# torch.min: 返回张量的最小值;若指定 dim,则同时返回最小值和对应索引
print(torch.min(x)) # tensor(1.)
# torch.max: 返回张量的最大值;若指定 dim,则同时返回最大值和对应索引
x = torch.tensor([3.0, 1.0, 4.0, 1.0, 5.0])
print(torch.max(x)) # tensor(5.)
print(torch.max(x, dim=0)) # torch.return_types.max(values=tensor(5.), indices=tensor(4))
# torch.min: 返回张量的最小值;若指定 dim,则同时返回最小值和对应索引
print(torch.min(x)) # tensor(1.)
tensor(5.) torch.return_types.max( values=tensor(5.), indices=tensor(4)) tensor(1.)
In [37]:
Copied!
# torch.cumsum: 沿指定维度计算张量元素的累积和(前缀和)
x = torch.tensor([1.0, 2.0, 3.0, 4.0])
print(torch.cumsum(x, dim=0)) # tensor([ 1., 3., 6., 10.])
# torch.cumprod: 沿指定维度计算张量元素的累积积(前缀积)
print(torch.cumprod(x, dim=0)) # tensor([ 1., 2., 6., 24.])
# torch.cumsum: 沿指定维度计算张量元素的累积和(前缀和)
x = torch.tensor([1.0, 2.0, 3.0, 4.0])
print(torch.cumsum(x, dim=0)) # tensor([ 1., 3., 6., 10.])
# torch.cumprod: 沿指定维度计算张量元素的累积积(前缀积)
print(torch.cumprod(x, dim=0)) # tensor([ 1., 2., 6., 24.])
tensor([ 1., 3., 6., 10.]) tensor([ 1., 2., 6., 24.])
In [38]:
Copied!
# torch.clamp: 将张量中每个元素限制在 [min, max] 区间内(低于 min 则置为 min,高于 max 则置为 max)
x = torch.tensor([-2.0, 0.5, 1.5, 3.0])
print(torch.clamp(x, min=0.0, max=1.0))
# tensor([0.0000, 0.5000, 1.0000, 1.0000])
# torch.clamp: 将张量中每个元素限制在 [min, max] 区间内(低于 min 则置为 min,高于 max 则置为 max)
x = torch.tensor([-2.0, 0.5, 1.5, 3.0])
print(torch.clamp(x, min=0.0, max=1.0))
# tensor([0.0000, 0.5000, 1.0000, 1.0000])
tensor([0.0000, 0.5000, 1.0000, 1.0000])
In [39]:
Copied!
# torch.einsum: 使用爱因斯坦求和约定,以简洁的字符串表达式描述并计算任意多线性代数运算
A = torch.randn(3, 4)
B = torch.randn(4, 5)
# 等价于矩阵乘法 A @ B
# 此处:A 的第 0 维叫 i,第 1 维叫 j;B 的第 0 维叫 j,B 的第 1 维叫 k
C = torch.einsum('ij,jk->ik', A, B)
print(C.shape) # torch.Size([3, 5])
# 逐元素相乘后求和(批量点积)
a = torch.randn(3, 4)
b = torch.randn(3, 4)
dot1 = torch.einsum('ij,ij->i', a, b)
dot2 = torch.einsum('ij,ij->j', a, b)
print(dot1.shape) # torch.Size([3])
print(dot2.shape) # torch.Size([4])
# torch.einsum: 使用爱因斯坦求和约定,以简洁的字符串表达式描述并计算任意多线性代数运算
A = torch.randn(3, 4)
B = torch.randn(4, 5)
# 等价于矩阵乘法 A @ B
# 此处:A 的第 0 维叫 i,第 1 维叫 j;B 的第 0 维叫 j,B 的第 1 维叫 k
C = torch.einsum('ij,jk->ik', A, B)
print(C.shape) # torch.Size([3, 5])
# 逐元素相乘后求和(批量点积)
a = torch.randn(3, 4)
b = torch.randn(3, 4)
dot1 = torch.einsum('ij,ij->i', a, b)
dot2 = torch.einsum('ij,ij->j', a, b)
print(dot1.shape) # torch.Size([3])
print(dot2.shape) # torch.Size([4])
torch.Size([3, 5]) torch.Size([3]) torch.Size([4])
In [40]:
Copied!
# torch.bmm: 批量矩阵乘法,输入须为三维张量 (batch, n, m) x (batch, m, p),不支持广播
batch_A = torch.randn(10, 3, 4)
batch_B = torch.randn(10, 4, 5)
print(torch.bmm(batch_A, batch_B).shape) # torch.Size([10, 3, 5])
# torch.batch_matmul: 即 torch.matmul 处理批量场景,支持广播,是 torch.bmm 的广义版
# (PyTorch 中通常直接用 torch.matmul 或 @ 运算符)
print(torch.matmul(batch_A, batch_B).shape) # torch.Size([10, 3, 5])
# torch.bmm: 批量矩阵乘法,输入须为三维张量 (batch, n, m) x (batch, m, p),不支持广播
batch_A = torch.randn(10, 3, 4)
batch_B = torch.randn(10, 4, 5)
print(torch.bmm(batch_A, batch_B).shape) # torch.Size([10, 3, 5])
# torch.batch_matmul: 即 torch.matmul 处理批量场景,支持广播,是 torch.bmm 的广义版
# (PyTorch 中通常直接用 torch.matmul 或 @ 运算符)
print(torch.matmul(batch_A, batch_B).shape) # torch.Size([10, 3, 5])
torch.Size([10, 3, 5]) torch.Size([10, 3, 5])
In [41]:
Copied!
# torch.scatter: 将源张量的值沿指定轴散布到目标张量的指定索引处(out-of-place 写入)
# 给定“写入位置索引index”,把 src 中的值写到 dst 的指定位置。
src = torch.tensor([[1.0, 2.0, 3.0]])
index = torch.tensor([[2, 0, 1]])
dst = torch.zeros(1, 5)
dst.scatter_(1, index, src)
print(dst) # tensor([[2., 3., 1., 0., 0.]])
# torch.scatter_add: 与 scatter 类似,但对重复索引处的值执行累加而非覆盖
dst2 = torch.zeros(1, 5)
index2 = torch.tensor([[2, 2, 1]])
dst2.scatter_add_(1, index2, src)
print(dst2) # tensor([[0., 3., 3., 0., 0.]]) index=2 处累加了 1+2=3
# torch.scatter: 将源张量的值沿指定轴散布到目标张量的指定索引处(out-of-place 写入)
# 给定“写入位置索引index”,把 src 中的值写到 dst 的指定位置。
src = torch.tensor([[1.0, 2.0, 3.0]])
index = torch.tensor([[2, 0, 1]])
dst = torch.zeros(1, 5)
dst.scatter_(1, index, src)
print(dst) # tensor([[2., 3., 1., 0., 0.]])
# torch.scatter_add: 与 scatter 类似,但对重复索引处的值执行累加而非覆盖
dst2 = torch.zeros(1, 5)
index2 = torch.tensor([[2, 2, 1]])
dst2.scatter_add_(1, index2, src)
print(dst2) # tensor([[0., 3., 3., 0., 0.]]) index=2 处累加了 1+2=3
tensor([[2., 3., 1., 0., 0.]]) tensor([[0., 3., 3., 0., 0.]])
In [42]:
Copied!
# torch.gather: 根据索引张量,从输入张量沿指定维度取值(scatter 的逆操作)
x = torch.tensor([[10, 20, 30],
[40, 50, 60]])
index = torch.tensor([[1, 0],
[0, 1]])
print(torch.gather(x, dim=0, index=index))
print(torch.gather(x, dim=1, index=index))
# torch.gather: 根据索引张量,从输入张量沿指定维度取值(scatter 的逆操作)
x = torch.tensor([[10, 20, 30],
[40, 50, 60]])
index = torch.tensor([[1, 0],
[0, 1]])
print(torch.gather(x, dim=0, index=index))
print(torch.gather(x, dim=1, index=index))
tensor([[40, 20],
[10, 50]])
tensor([[20, 10],
[40, 50]])
In [43]:
Copied!
# torch.index_select: 根据给定的一维索引张量,从输入张量的指定维度选取对应的切片
x = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
idx = torch.tensor([0, 2]) # 选取第 0 行和第 2 行
print(torch.index_select(x, dim=0, index=idx))
# tensor([[1, 2, 3],
# [7, 8, 9]])
# torch.index_select: 根据给定的一维索引张量,从输入张量的指定维度选取对应的切片
x = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
idx = torch.tensor([0, 2]) # 选取第 0 行和第 2 行
print(torch.index_select(x, dim=0, index=idx))
# tensor([[1, 2, 3],
# [7, 8, 9]])
tensor([[1, 2, 3],
[7, 8, 9]])
In [44]:
Copied!
# torch.masked_select: 根据布尔掩码(BoolTensor)从输入张量中选取为 True 的元素,返回一维张量
x = torch.tensor([1.0, -2.0, 3.0, -4.0, 5.0])
mask = x > 0
print(mask)
print(torch.masked_select(x, mask))
# tensor([1., 3., 5.])
# torch.masked_select: 根据布尔掩码(BoolTensor)从输入张量中选取为 True 的元素,返回一维张量
x = torch.tensor([1.0, -2.0, 3.0, -4.0, 5.0])
mask = x > 0
print(mask)
print(torch.masked_select(x, mask))
# tensor([1., 3., 5.])
tensor([ True, False, True, False, True]) tensor([1., 3., 5.])
In [45]:
Copied!
# torch.split: 将张量沿指定维度按给定大小(或大小列表)分割为多个子张量
x = torch.arange(10.0)
chunks = torch.split(x, split_size_or_sections=3, dim=0)
for c in chunks:
print(c)
# tensor([0., 1., 2.]) tensor([3., 4., 5.]) tensor([6., 7., 8.]) tensor([9.])
# torch.chunk: 将张量沿指定维度尽量均等地分割为 n 块(最后一块可能更小)
chunks2 = torch.chunk(x, chunks=3, dim=0)
for c in chunks2:
print(c)
# tensor([0., 1., 2., 3.]) tensor([4., 5., 6., 7.]) tensor([8., 9.])
# torch.split: 将张量沿指定维度按给定大小(或大小列表)分割为多个子张量
x = torch.arange(10.0)
chunks = torch.split(x, split_size_or_sections=3, dim=0)
for c in chunks:
print(c)
# tensor([0., 1., 2.]) tensor([3., 4., 5.]) tensor([6., 7., 8.]) tensor([9.])
# torch.chunk: 将张量沿指定维度尽量均等地分割为 n 块(最后一块可能更小)
chunks2 = torch.chunk(x, chunks=3, dim=0)
for c in chunks2:
print(c)
# tensor([0., 1., 2., 3.]) tensor([4., 5., 6., 7.]) tensor([8., 9.])
tensor([0., 1., 2.]) tensor([3., 4., 5.]) tensor([6., 7., 8.]) tensor([9.]) tensor([0., 1., 2., 3.]) tensor([4., 5., 6., 7.]) tensor([8., 9.])
In [46]:
Copied!
# torch.sort: 对张量沿指定维度排序,返回排序后的值张量和对应原始索引张量
x = torch.tensor([3.0, 1.0, 4.0, 1.0, 5.0, 9.0])
values, indices = torch.sort(x)
print(values) # tensor([1., 1., 3., 4., 5., 9.])
print(indices) # tensor([1, 3, 0, 2, 4, 5])
# torch.topk: 返回张量在指定维度上最大(或最小)的 k 个元素及其索引,常用于推荐召回阶段取 Top-K
values_k, indices_k = torch.topk(x, k=3)
print(values_k) # tensor([9., 5., 4.])
print(indices_k) # tensor([5, 4, 2])
# torch.sort: 对张量沿指定维度排序,返回排序后的值张量和对应原始索引张量
x = torch.tensor([3.0, 1.0, 4.0, 1.0, 5.0, 9.0])
values, indices = torch.sort(x)
print(values) # tensor([1., 1., 3., 4., 5., 9.])
print(indices) # tensor([1, 3, 0, 2, 4, 5])
# torch.topk: 返回张量在指定维度上最大(或最小)的 k 个元素及其索引,常用于推荐召回阶段取 Top-K
values_k, indices_k = torch.topk(x, k=3)
print(values_k) # tensor([9., 5., 4.])
print(indices_k) # tensor([5, 4, 2])
tensor([1., 1., 3., 4., 5., 9.]) tensor([1, 3, 0, 2, 4, 5]) tensor([9., 5., 4.]) tensor([5, 4, 2])
In [47]:
Copied!
# torch.view_as_real: 将复数张量视作实数张量,最后一维扩展为 2(分别存储实部和虚部)
z = torch.tensor([1+2j, 3+4j])
print(torch.view_as_real(z))
# tensor([[1., 2.],
# [3., 4.]])
# torch.view_as_complex: 将实数张量的最后一维(大小须为2)解释为复数的实部和虚部,转为复数张量
r = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(torch.view_as_complex(r))
# tensor([1.+2.j, 3.+4.j])
# torch.view_as_real: 将复数张量视作实数张量,最后一维扩展为 2(分别存储实部和虚部)
z = torch.tensor([1+2j, 3+4j])
print(torch.view_as_real(z))
# tensor([[1., 2.],
# [3., 4.]])
# torch.view_as_complex: 将实数张量的最后一维(大小须为2)解释为复数的实部和虚部,转为复数张量
r = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(torch.view_as_complex(r))
# tensor([1.+2.j, 3.+4.j])
tensor([[1., 2.],
[3., 4.]])
tensor([1.+2.j, 3.+4.j])
In [48]:
Copied!
# torch.eig: (已废弃,推荐用 torch.linalg.eig)计算方阵的特征值和特征向量
A = torch.tensor([[4.0, 1.0], [2.0, 3.0]])
eigenvalues, eigenvectors = torch.linalg.eig(A)
print(eigenvalues) # tensor([5.+0.j, 2.+0.j])
# torch.svd: (已废弃,推荐用 torch.linalg.svd)对矩阵进行奇异值分解,返回 U、S、V
U, S, Vh = torch.linalg.svd(A)
print(S) # 奇异值
# torch.qr: (已废弃,推荐用 torch.linalg.qr)对矩阵进行 QR 分解,返回正交矩阵 Q 和上三角矩阵 R
Q, R = torch.linalg.qr(A)
print(Q.shape, R.shape) # torch.Size([2, 2]) torch.Size([2, 2])
# torch.eig: (已废弃,推荐用 torch.linalg.eig)计算方阵的特征值和特征向量
A = torch.tensor([[4.0, 1.0], [2.0, 3.0]])
eigenvalues, eigenvectors = torch.linalg.eig(A)
print(eigenvalues) # tensor([5.+0.j, 2.+0.j])
# torch.svd: (已废弃,推荐用 torch.linalg.svd)对矩阵进行奇异值分解,返回 U、S、V
U, S, Vh = torch.linalg.svd(A)
print(S) # 奇异值
# torch.qr: (已废弃,推荐用 torch.linalg.qr)对矩阵进行 QR 分解,返回正交矩阵 Q 和上三角矩阵 R
Q, R = torch.linalg.qr(A)
print(Q.shape, R.shape) # torch.Size([2, 2]) torch.Size([2, 2])
tensor([5.0000+0.j, 2.0000+0.j]) tensor([5.1167, 1.9544]) torch.Size([2, 2]) torch.Size([2, 2])
In [49]:
Copied!
# torch.inverse: 计算方阵的逆矩阵,要求矩阵可逆(推荐新版用 torch.linalg.inv)
A = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
A_inv = torch.inverse(A) # 或 torch.linalg.inv(A)
print(A_inv)
# tensor([[-2.0000, 1.0000],
# [ 1.5000, -0.5000]])
print(torch.mm(A, A_inv)) # 验证:应接近单位矩阵
# torch.inverse: 计算方阵的逆矩阵,要求矩阵可逆(推荐新版用 torch.linalg.inv)
A = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
A_inv = torch.inverse(A) # 或 torch.linalg.inv(A)
print(A_inv)
# tensor([[-2.0000, 1.0000],
# [ 1.5000, -0.5000]])
print(torch.mm(A, A_inv)) # 验证:应接近单位矩阵
tensor([[-2.0000, 1.0000],
[ 1.5000, -0.5000]])
tensor([[1., 0.],
[0., 1.]])
In [50]:
Copied!
# tensor.t: 对二维张量进行转置(仅适用于二维,等价于 tensor.T)
A = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(A.t())
# tensor([[1, 4],
# [2, 5],
# [3, 6]])
# tensor.transpose: 交换张量的任意两个指定维度,适用于任意维度的张量
B = torch.randn(2, 3, 4)
print(B.transpose(0, 2).shape) # torch.Size([4, 3, 2]) 交换 dim0 和 dim2
# tensor.t: 对二维张量进行转置(仅适用于二维,等价于 tensor.T)
A = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(A.t())
# tensor([[1, 4],
# [2, 5],
# [3, 6]])
# tensor.transpose: 交换张量的任意两个指定维度,适用于任意维度的张量
B = torch.randn(2, 3, 4)
print(B.transpose(0, 2).shape) # torch.Size([4, 3, 2]) 交换 dim0 和 dim2
tensor([[1, 4],
[2, 5],
[3, 6]])
torch.Size([4, 3, 2])