pytorch的杂七杂八

数据

contiguous()

博客
相当于深拷贝

scatter_()

博客
可以利用这个功能将pytorch 中mini batch中的返回的label转为one-hot类型的label

label = torch.tensor([1,3,3,5])
one_hot_label = torch.zeros(mini_batch, out_planes).scatter_(1,label.unsqueeze(1),1)
print(one_hot_label)
tensor([[0., 1., 0., 0., 0., 0.],
        [0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 1., 0., 0.],

unsqueeze()

label = torch.tensor([1,3,3,5])
print(label.unsqueeze(1))
tensor([[1],
        [3],
        [3],
        [5]])

expand(size) expand_as(other)

>>> x = torch.tensor([[1], [2], [3]])
            >>> x.size()
            torch.Size([3, 1])
            >>> x.expand(3, 4)
            tensor([[ 1,  1,  1,  1],
                    [ 2,  2,  2,  2],
                    [ 3,  3,  3,  3]])
            >>> x.expand(-1, 4)   # -1 means not changing the size of that dimension
            tensor([[ 1,  1,  1,  1],
                    [ 2,  2,  2,  2],
                    [ 3,  3,  3,  3]])

autograd.grad()

博客

narrow()

link

>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> torch.narrow(x, 0, 0, 2)
tensor([[ 1,  2,  3],
        [ 4,  5,  6]])
>>> torch.narrow(x, 1, 1, 2)
tensor([[ 2,  3],
        [ 5,  6],
        [ 8,  9]])
posted @ 2021-12-18 21:46  梦想家肾小球  阅读(22)  评论(0编辑  收藏  举报