目录
参考pytorch说明文档:torch文档
torch.view()
返回一个数据相同但大小不同的tensor。 返回的tensor必须有与原tensor相同的数据和相同数目的元素,但可以有不同的shape。一个tensor必须是连续的才能被查看。如果参数出现-1,意思就是让电脑自动计算这个位置上的大小。
例子:
>>> x = torch.randn(4, 4)
>>> x.size()
torch.Size([4, 4])、
>>> y = x.view(16)
>>> y.size()
torch.Size([16])
>>> z = x.view(-1, 8) # the size -1 is inferred from other dimensions
>>> z.size()
torch.Size([2, 8])
那么下面这行代码的含义就是将target变成n x 1的张量形式, .long()表示将数字或字符串转换为一个长整型。
target = target.view(-1, 1).long()
torch.nn.functional.softmax()
torch.nn.functional.softmax(input, dim=None, _stacklevel=3, dtype=None)
softmax的定义:
它应用于沿dim的所有slices,并将重新缩放它们,使元素位于[0, 1]范围内并且总和为1。
参数:
-
输入(张量) - input
-
dim (int) – 计算 softmax 的维度。
-
dtype (optional) -- 返回张量的所需数据类型。如果有指定,则在执行操作之前将输入张量转换为dtype。这对于防止数据类型溢出很有用。默认值:None。