美文网首页
【Note】PyTorch 文档学习历程

【Note】PyTorch 文档学习历程

作者: 火禾子_ | 来源:发表于2019-03-29 09:41 被阅读0次

    1. torch
    torch.eye(n, m=None, out=None)
    返回 2 维张量,对角线为 1,其他为 0.
    torch.ones(*sizes, out=None) -> Tensor
    返回形状由可变参数 sizes 决定的全为 1 的张量。
    torch.zeros(*sizes, out=None) -> Tensor
    返回形状由可变参数 sizes 决定的全为 0 的张量。

    torch.linspace(start, end, steps=100, out=None) -> Tensor
    返回一个 1 维张量,包含 [start, end] 的 step 个点。
    torch.logspace(start, end, steps=100, out=None) -> Tensor
    返回一个 1 维张量,包含 [10^start, 10^end] 的 step 个点。
    torch.arange(start, end, step=1, out=None) -> Tensor
    [start, end),步长为 step。
    torch.range(start, end, step=1, out=None) -> Tensor
    [start, end],步长为 step。

    torch.rand(*sizes, out=None) -> Tensor
    [0, 1) 均匀分布的一组随机数。
    torch.randn(*sizes, out=None) -> Tensor
    服从标准正态分布的一组随机数。
    torch.randperm(n, out=None) -> Tensor
    [0, n-1] 的随机整数序列。

    torch.gather(input, dim, index:LongTensor, out=None) -> Tensor
    官方定义是这样的:

    沿给定轴 dim,将输入索引张量 index 指定位置的值进行聚合。
    
    对一个3维张量,输出可以定义为:
    
    out[i][j][k] = tensor[index[i][j][k]][j][k]  # dim=0
    out[i][j][k] = tensor[i][index[i][j][k]][k]  # dim=1
    out[i][j][k] = tensor[i][j][index[i][j][k]]  # dim=3
    
    例子:
    >>> t = torch.Tensor([[1,2],[3,4]])
    >>> torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))
     1  1
     4  3
    [torch.FloatTensor of size 2x2]
    

    我是这么理解的,以上面官方例子为例。t 是 [1, 2; 3, 4],对应的 index 是 [(0, 0), (0, 1); (1, 0), (1, 1)]。
    gather 方法中 dim 设置为 1,也就是只有第 1 维索引改变,其他维(在此例中就是 0 维)都不变。
    因此索引就变成 [(0, 0), (0, 0); (1, 1), (1, 0)],对应原先 Tensor 值就是 [1, 1; 4, 3]。

    torch.index_select(input, dim, index:LongTensor, out=None) -> Tensor
    举个例子就明白了:

    >>> x = torch.randn(3, 4)
    >>> x
    
     1.2045  2.4084  0.4001  1.1372
     0.5596  1.5677  0.6219 -0.7954
     1.3635 -1.2313 -0.5414 -1.8478
    [torch.FloatTensor of size 3x4]
    
    >>> indices = torch.LongTensor([0, 2])
    >>> torch.index_select(x, 0, indices)
    
     1.2045  2.4084  0.4001  1.1372
     1.3635 -1.2313 -0.5414 -1.8478
    [torch.FloatTensor of size 2x4]
    
    >>> torch.index_select(x, 1, indices)
    
     1.2045  0.4001
     0.5596  0.6219
     1.3635 -0.5414
    [torch.FloatTensor of size 3x2]
    

    torch.masked_select(input, mask:ByteTensor, out=None) -> Tensor
    根据掩码张量mask中的二元值,取输入张量中的指定项( mask为一个 ByteTensor),将取值返回到一个新的1D张量,

    张量 mask 须跟 input 张量有相同数量的元素数目,但形状或维度不需要相同。 注意: 返回的张量不与原始张量共享内存空间。

    x = torch.randn(3, 4)
    >>> x
    tensor([[-0.1711, -1.1977, -0.4085,  1.6461],
            [ 0.3719, -1.0407,  1.7696, -0.5083],
            [-1.1868, -1.0291,  0.1067,  0.3486]])
    >>> mask = x.ge(0.5)
    >>> mask
    tensor([[0, 0, 0, 1],
            [0, 0, 1, 0],
            [0, 0, 0, 0]], dtype=torch.uint8)
    >>> torch.masked_select(x, mask)
    tensor([1.6461, 1.7696])
    

    torch.nonzero(input, out=None) -> LongTensor
    设 z 为输入张量 input 中所有非零元素的个数,n 为输入张量的维数,那么输出张量的形状为 z*n。

    torch.squeeze(input, dim=None, out=None)
    将输入张量形状中的 1 去除并返回,若指定 dim 不是 1,则保持张量不变进行返回。
    torch.unsqueeze(input, dim, out=None)
    与上述函数操作相反。

    相关文章

      网友评论

          本文标题:【Note】PyTorch 文档学习历程

          本文链接:https://www.haomeiwen.com/subject/spkhbqtx.html