找回密码
 立即注册
首页 业界区 安全 pytorch的矩阵操作分类

pytorch的矩阵操作分类

电棘缣 10 小时前
PyTorch 的矩阵操作

注意:

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

初始化


  • empty
    用于创建一个未初始化的张量,其值是随机的
    与torch.randn的区别在于,torch.randn是从正态分布中采样的
    1. torch.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format) → Tensor
    2. torch.empty((2,3), dtype=torch.int64)
    3. tensor([[ 9.4064e+13,  2.8000e+01,  9.3493e+13],
    4.         [ 7.5751e+18,  7.1428e+18,  7.5955e+18]])
    复制代码
  • zeros
    1. torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
    2. torch.zeros(2, 3)
    3. tensor([[ 0.,  0.,  0.],
    4.         [ 0.,  0.,  0.]])
    复制代码
  • randn
    \(out_i \sim \mathcal{N}(0, 1)\),满足正态分布
    1. torch.randn(*size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False)
    2. torch.randn(2, 3)
    3. tensor([[ 1.5954,  2.8929, -1.0923],
    4.         [ 1.1719, -0.4709, -0.1996]])
    复制代码
  • randint
    生成制定范围[low, high) 和形状size的tensor
    1. torch.randint(low=0, high, size, \*, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
    2. torch.randint(3, 10, (2, 2))
    3. tensor([[4, 5],
    4.         [6, 7]])
    复制代码
  • arange
    和list(range())的原理相同
    1. torch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
    2. torch.arange(5)
    3. tensor([ 0,  1,  2,  3,  4])
    4. torch.arange(1, 4)
    5. tensor([ 1,  2,  3])
    6. torch.arange(1, 2.5, 0.5)
    7. tensor([ 1.0000,  1.5000,  2.0000])
    复制代码
  • range(deprecated)
    类似于list(range())的用法,但是,torch.range的返回的最后一个元素是可以为end的
    1. torch.range(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
    2. # 0.5 指的是每步的大小
    3. torch.range(1, 4, 0.5)
    4. tensor([ 1.0000,  1.5000,  2.0000,  2.5000,  3.0000,  3.5000,  4.0000])
    复制代码
  • linspace
    不同于torch.range,这里的step指的是有多少步,根据步数,计算每步的大小
    torch.linspace的第一个元素一定是start,最后一个元素一定是end
    1. torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
    2. torch.linspace(start=-10, end=10, steps=5)
    3. tensor([-10.,  -5.,   0.,   5.,  10.])
    4. torch.linspace(start=-10, end=10, steps=1)
    5. tensor([-10.]
    复制代码
  • eye
    返回对角线矩阵
    1. torch.eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
    2. torch.eye(3)
    3. tensor([[ 1.,  0.,  0.],
    4.         [ 0.,  1.,  0.],
    5.         [ 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))
    1. torch.full(size, fill_value, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
    2. torch.full((2, 3), 3.141592)
    3. tensor([[ 3.1416,  3.1416,  3.1416],
    4.         [ 3.1416,  3.1416,  3.1416]])
    复制代码
  • zeros_like
    返回于input tensor形状相同的元素全是0的tensor
    1. torch.zeros_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor
    2. input = torch.empty(2, 3)
    3. torch.zeros_like(input)
    4. tensor([[ 0.,  0.,  0.],
    5.         [ 0.,  0.,  0.]])
    复制代码
改变形状


  • premute
    改变维度的顺序
    1. torch.permute(input, dims) -> Tensor
    2. x = torch.randn(2, 3, 5)
    3. x.size()
    4. torch.Size([2, 3, 5])
    5. torch.permute(x, (2, 0, 1)).size()
    6. torch.Size([5, 2, 3])
    复制代码
  • reshape
    改变tensor的形状,但是元素的数量和值不改变
    1. torch.reshape(input, shape) → Tensor
    2. a = torch.arange(4.)
    3. torch.reshape(a, (2, 2))
    4. tensor([[ 0.,  1.],
    5.         [ 2.,  3.]])
    6. b = torch.tensor([[0, 1], [2, 3]])
    7. torch.reshape(b, (-1,))
    8. tensor([ 0,  1,  2,  3])
    复制代码
  • transpose
    将两个指定维度的位置进行替换
    torch.permute(x, (0,2,1)) = torch.transpose(x, 1, 2)
    1. torch.transpose(input, dim0, dim1) -> Tensor
    2. x = torch.randn(2, 3)
    3. tensor([[ 1.0028, -0.9893,  0.5809],
    4.         [-0.1669,  0.7299,  0.4942]])
    5. torch.transpose(x, 0, 1)
    6. tensor([[ 1.0028, -0.1669],
    7.         [-0.9893,  0.7299],
    8.         [ 0.5809,  0.4942]])
    复制代码
  • view
    tensor.view 创建的张量 tensor_view 是原始张量 tensor 的一个视图(view),而不是一个新的张量。因此,tensor_view 不会单独存储梯度信息。梯度信息会直接存储在原始张量 tensor 中。
    Tensor.view而不是torch.view
    1. Tensor.view(*shape) → Tensor
    2. x = torch.randn(4, 4)
    3. x.size()
    4. torch.Size([4, 4])
    5. y = x.view(16)
    6. y.size()
    7. torch.Size([16])
    8. z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
    9. z.size()
    10. torch.Size([2, 8])
    复制代码
    b_view 只是b的一个不同形状的视图,后续使用b_view导致的属性的修改还是保存在b中
    1. a = torch.randn(1,6)
    2. b = torch.randn(3,2,requires_grad=True)
    3. b_view = b.view(6,1)
    4. loss = a@b_view
    5. loss.backward()
    6. b_view.grad
    7. b.grad
    8. tensor([[-0.3020, -1.4392],
    9.         [ 0.7194,  0.1363],
    10.         [-1.3413, -0.2453]])
    复制代码
    此外,只有在内存中连续存储的tensor才可以使用view,否则使用reshape,reshape和view的性质一致
    其中,tensor的转置会导致tensor是不连续的
    1. tensor = torch.randn(2,3)
    2. >>> # 转置张量,使其变为非连续
    3. >>> tensor_transposed = tensor.transpose(0, 1)
    4. >>> print("Transposed tensor:")
    5. Transposed tensor:
    6. >>> print(tensor_transposed)
    7. tensor([[ 2.2194, -0.6988],
    8.         [ 0.5496,  0.2167],
    9.         [-0.2635, -2.5029]])
    10. >>> print("Is the transposed tensor contiguous?", tensor_transposed.is_contiguous())
    11. 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).
    1. torch.squeeze(input: Tensor, dim: Optional[Union[int, List[int]]]) → Tensor
    2. x = torch.zeros(2, 1, 2, 1, 2)
    3. x.size()
    4. torch.Size([2, 1, 2, 1, 2])
    5. y = torch.squeeze(x)
    6. y.size()
    7. torch.Size([2, 2, 2])
    8. y = torch.squeeze(x, 0)
    9. y.size()
    10. torch.Size([2, 1, 2, 1, 2])
    11. y = torch.squeeze(x, 1)
    12. y.size()
    13. torch.Size([2, 2, 1, 2])
    14. y = torch.squeeze(x, (1, 2, 3))
    15. torch.Size([2, 2, 2])
    复制代码
  • unsqueeze
    添加维度
    1. x = torch.randn(4)
    2. torch.unsqueeze(x, 0).size()
    3. torch.Size(1,4)
    4. torch.unsqueeze(x, 1).size()
    5. torch.Size(4,1)
    复制代码
  • size
    t.size() = t.shape. tuple(t.size())返回一个维度的元组
索引

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

待更新。。。

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

相关推荐

您需要登录后才可以回帖 登录 | 立即注册