C/C++教程

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]])
这篇关于pytorch的杂七杂八的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!