电棘缣 发表于 2025-10-15 12:05:00

pytorch的矩阵操作分类

PyTorch 的矩阵操作

注意:

[*]无论是torch.f()还是tensor.f(),都是返回新的Tensor,不会修改原始的tensor
单个tensor

初始化


[*]empty
用于创建一个未初始化的张量,其值是随机的
与torch.randn的区别在于,torch.randn是从正态分布中采样的
torch.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format) → Tensor

torch.empty((2,3), dtype=torch.int64)
tensor([[ 9.4064e+13,2.8000e+01,9.3493e+13],
      [ 7.5751e+18,7.1428e+18,7.5955e+18]])
[*]zeros
torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

torch.zeros(2, 3)
tensor([[ 0.,0.,0.],
      [ 0.,0.,0.]])
[*]randn
\(out_i \sim \mathcal{N}(0, 1)\),满足正态分布
torch.randn(*size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False)

torch.randn(2, 3)
tensor([[ 1.5954,2.8929, -1.0923],
      [ 1.1719, -0.4709, -0.1996]])
[*]randint
生成制定范围[low, high) 和形状size的tensor
torch.randint(low=0, high, size, \*, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

torch.randint(3, 10, (2, 2))
tensor([,
      ])
[*]arange
和list(range())的原理相同
torch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

torch.arange(5)
tensor([ 0,1,2,3,4])
torch.arange(1, 4)
tensor([ 1,2,3])
torch.arange(1, 2.5, 0.5)
tensor([ 1.0000,1.5000,2.0000])
[*]range(deprecated)
类似于list(range())的用法,但是,torch.range的返回的最后一个元素是可以为end的
torch.range(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

# 0.5 指的是每步的大小
torch.range(1, 4, 0.5)
tensor([ 1.0000,1.5000,2.0000,2.5000,3.0000,3.5000,4.0000])
[*]linspace
不同于torch.range,这里的step指的是有多少步,根据步数,计算每步的大小
torch.linspace的第一个元素一定是start,最后一个元素一定是end
torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

torch.linspace(start=-10, end=10, steps=5)
tensor([-10.,-5.,   0.,   5.,10.])
torch.linspace(start=-10, end=10, steps=1)
tensor([-10.]
[*]eye
返回对角线矩阵
torch.eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

torch.eye(3)
tensor([[ 1.,0.,0.],
      [ 0.,1.,0.],
      [ 0.,0.,1.]])
[*]full
把一个数字扩展到指定的形状上,是ones zeros的一般化
torch.full((2,3), 0.0) = torch.zeros((2,3))
torch.full((2,3), 1.0) = torch.ones((2,3))
torch.full(size, fill_value, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

torch.full((2, 3), 3.141592)
tensor([[ 3.1416,3.1416,3.1416],
      [ 3.1416,3.1416,3.1416]])
[*]zeros_like
返回于input tensor形状相同的元素全是0的tensor
torch.zeros_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor

input = torch.empty(2, 3)
torch.zeros_like(input)
tensor([[ 0.,0.,0.],
      [ 0.,0.,0.]])
改变形状


[*]premute
改变维度的顺序
torch.permute(input, dims) -> Tensor

x = torch.randn(2, 3, 5)
x.size()
torch.Size()
torch.permute(x, (2, 0, 1)).size()
torch.Size()
[*]reshape
改变tensor的形状,但是元素的数量和值不改变
torch.reshape(input, shape) → Tensor

a = torch.arange(4.)
torch.reshape(a, (2, 2))
tensor([[ 0.,1.],
      [ 2.,3.]])
b = torch.tensor([, ])
torch.reshape(b, (-1,))
tensor([ 0,1,2,3])
[*]transpose
将两个指定维度的位置进行替换
torch.permute(x, (0,2,1)) = torch.transpose(x, 1, 2)
torch.transpose(input, dim0, dim1) -> Tensor

x = torch.randn(2, 3)
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]])
[*]view
tensor.view 创建的张量 tensor_view 是原始张量 tensor 的一个视图(view),而不是一个新的张量。因此,tensor_view 不会单独存储梯度信息。梯度信息会直接存储在原始张量 tensor 中。
Tensor.view而不是torch.view
Tensor.view(*shape) → Tensor

x = torch.randn(4, 4)
x.size()
torch.Size()
y = x.view(16)
y.size()
torch.Size()
z = x.view(-1, 8)# the size -1 is inferred from other dimensions
z.size()
torch.Size()b_view 只是b的一个不同形状的视图,后续使用b_view导致的属性的修改还是保存在b中
a = torch.randn(1,6)
b = torch.randn(3,2,requires_grad=True)
b_view = b.view(6,1)
loss = a@b_view
loss.backward()

b_view.grad

b.grad
tensor([[-0.3020, -1.4392],
      [ 0.7194,0.1363],
      [-1.3413, -0.2453]])此外,只有在内存中连续存储的tensor才可以使用view,否则使用reshape,reshape和view的性质一致
其中,tensor的转置会导致tensor是不连续的
tensor = torch.randn(2,3)
>>> # 转置张量,使其变为非连续
>>> tensor_transposed = tensor.transpose(0, 1)
>>> print("Transposed tensor:")
Transposed tensor:
>>> print(tensor_transposed)
tensor([[ 2.2194, -0.6988],
      [ 0.5496,0.2167],
      [-0.2635, -2.5029]])
>>> print("Is the transposed tensor contiguous?", tensor_transposed.is_contiguous())
Is the transposed tensor contiguous? False
[*]squeeze
把大小是1的维度 remove掉
When dim is given, a squeeze operation is done only in the given dimension(s). If input is of shape: (A×1×B)(A×1×B), squeeze(input, 0) leaves the tensor unchanged, but squeeze(input, 1) will squeeze the tensor to the shape (A×B)(A×B).
torch.squeeze(input: Tensor, dim: Optional]]) → Tensor

x = torch.zeros(2, 1, 2, 1, 2)
x.size()
torch.Size()
y = torch.squeeze(x)
y.size()
torch.Size()
y = torch.squeeze(x, 0)
y.size()
torch.Size()
y = torch.squeeze(x, 1)
y.size()
torch.Size()
y = torch.squeeze(x, (1, 2, 3))
torch.Size()
[*]unsqueeze
添加维度
x = torch.randn(4)
torch.unsqueeze(x, 0).size()
torch.Size(1,4)
torch.unsqueeze(x, 1).size()
torch.Size(4,1)
[*]size
t.size() = t.shape. tuple(t.size())返回一个维度的元组
索引

待更新。。。
多个tensor之间的计算

待更新。。。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

恃液 发表于 2025-10-29 08:03:51

感谢分享,下载保存了,貌似很强大

枢覆引 发表于 2025-11-19 06:40:38

感谢,下载保存了

鞍汉 发表于 2025-12-13 06:49:45

谢谢分享,辛苦了

笃迩讦 发表于 2025-12-15 21:58:39

谢谢楼主提供!

零幸 发表于 2026-1-13 19:10:11

过来提前占个楼

赵淳美 发表于 2026-1-13 19:34:42

谢谢分享,辛苦了

馑妣窟 发表于 2026-1-15 04:14:06

感谢发布原创作品,程序园因你更精彩

溶绚 发表于 2026-1-17 03:30:10

感谢分享,下载保存了,貌似很强大

阎逼 发表于 2026-1-19 00:13:59

感谢,下载保存了

羊舌正清 发表于 2026-1-23 09:34:58

感谢分享,下载保存了,貌似很强大

焦和玉 发表于 2026-1-24 04:27:56

喜欢鼓捣这些软件,现在用得少,谢谢分享!

赵淳美 发表于 2026-1-26 10:58:02

收藏一下   不知道什么时候能用到

甄婉丽 发表于 2026-1-29 03:43:26

这个有用。

歇凛尾 发表于 2026-1-29 05:14:57

分享、互助 让互联网精神温暖你我

哈妙思 发表于 2026-2-1 05:23:11

感谢发布原创作品,程序园因你更精彩

氛疵 发表于 2026-2-2 17:50:25

这个有用。

尤晓兰 发表于 2026-2-4 05:41:26

懂技术并乐意极积无私分享的人越来越少。珍惜

啦汇 发表于 2026-2-7 18:48:52

yyds。多谢分享

颐港 发表于 2026-2-8 09:53:56

感谢分享,下载保存了,貌似很强大
页: [1] 2
查看完整版本: pytorch的矩阵操作分类