Pytorch官方文档(七)翻译版本

Pytorch官方文档(七)翻译版本

torch.t

作用:

  • 矩阵转置。

参数:

  1. input,输入的Tensor。

例子:

>>> x = torch.randn(())
>>> x
tensor(0.1995)
>>> torch.t(x)
tensor(0.1995)
>>> x = torch.randn(3)
>>> x
tensor([ 2.4320, -0.4608,  0.7702])
>>> torch.t(x)
tensor([.2.4320,.-0.4608,..0.7702])
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.4875,  0.9158, -0.5872],
        [ 0.3938, -0.6929,  0.6932]])
>>> torch.t(x)
tensor([[ 0.4875,  0.3938],
        [ 0.9158, -0.6929],
        [-0.5872,  0.6932]])

torch.transpose

作用:

  • 交换指定的维度。

参数:

  1. input,输入的Tensor。
  2. dim0,第一个要交换的维度。
  3. dim1,第二个要交换的维度。

例子:

>>> x = torch.randn(2, 3)
>>> x
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]])
>>> x = torch.randn(2, 3, 1)
>>> x
tensor([[[-0.6987],
         [-1.5592],
         [-1.3666]],
        [[-0.4368],
         [ 0.1533],
         [ 0.0405]]]) # (2, 3, 1)
>>> torch.transpose(x, 0, 2)
 tensor([[[-0.6987, -0.4368],
          [-1.5592,  0.1533],
          [-1.3666,  0.0405]]]) # (1, 3, 2)

torch.where

作用:

  • 条件筛选Tensor1内的值,不符合的位置用Tensor2的值来替换。

参数:

  1. condition,条件表达式。
  2. x,备选Tensor1。
  3. y,备选Tensor2。

例子:

>>> x = torch.randn(3, 2)
>>> y = torch.ones(3, 2)
>>> x
tensor([[-0.4620,  0.3139],
        [ 0.3898, -0.7197],
        [ 0.0478, -0.1657]])
>>> torch.where(x > 0, x, y)
tensor([[ 1.0000,  0.3139],
        [ 0.3898,  1.0000],
        [ 0.0478,  1.0000]])
发布了14 篇原创文章 · 获赞 6 · 访问量 595

猜你喜欢

转载自blog.csdn.net/qq_38883844/article/details/104165749
今日推荐