其他分享
首页 > 其他分享> > pytorch的杂七杂八

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]])

标签:unsqueeze,tensor,杂七杂八,torch,hot,label,pytorch,expand
来源: https://www.cnblogs.com/xiaoqian-shen/p/15706134.html