目录
参考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。
关于对dim参数的理解参见:pytorch softmax 中dim的理解
prob = F.softmax(logit,1)
logit的shape是B x C,B是batch_size,C是类别数目,softmax的dim=1即对logit这个张量的每个batch_size的列(含C个元素)进行softmax计算。
torch.FloatTensor()
torch.FloatTensor()是torch.Tensor()的别名,可以使用构造函数构造张量:
例子:
>>> torch.tensor([[1., -1.], [1., -1.]])
tensor([[ 1.0000, -1.0000],
[ 1.0000, -1.0000]])
>>> torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
tensor([[ 1, 2, 3],
[ 4, 5, 6]])
>>> torch.zeros([2, 4], dtype=torch.int32)
tensor([[ 0, 0, 0, 0],
[ 0, 0, 0, 0]], dtype=torch.int32)
>>> cuda0 = torch.device('cuda:0')
>>> torch.ones([2, 4], dtype=torch.float64, device=cuda0)
tensor([[ 1.0000, 1.0000, 1.0000, 1.0000],
[ 1.0000, 1.0000, 1.0000, 1.0000]], dtype=torch.float64, device='cuda:0')
torch.Tensor.zero_()
zero_(tensor):将tensor清零,shape不变。
torch.Tensor.scatter_()
Tensor.scatter_(dim, index, src, reduce=None)
将张量src中的所有值写入self中index指定的索引处。对于src中的每个值,其输出索引由src中dimension!=dim的索引和index中dimension=dim的相应值指定。
对于一个三维张量,self的更新如下所示:
self[index[i][j][k]][j][k] = src[i][j][k] # if dim == 0
self[i][index[i][j][k]][k] = src[i][j][k] # if dim == 1
self[i][j][index[i][j][k]] = src[i][j][k] # if dim == 2
scatter_()是gather()的反向操作。
torch.gather()
torch.gather(input, dim, index, *, sparse_grad=False, out=None)
沿dim指定的维度根据index指定的索引gather值。对于三维张量,输出由下式指定:
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2
参数:
例子:
>>> t = torch.tensor([[1, 2], [3, 4]])
>>> torch.gather(t, 1, torch.tensor([[0, 0], [1, 0]]))
tensor([[ 1, 1],
[ 4, 3]])
torch.sum()
torch.sum(input, *, dtype=None)
返回输入的张量中所有元素的和
>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.1133, -0.9567, 0.2958]])
>>> torch.sum(a)
tensor(-0.5475)
torch.sum(input, dim, keepdim=False, *, dtype=None)
>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.0569, -0.2475, 0.0737, -0.3429],
[-0.2993, 0.9138, 0.9337, -1.6864],
[ 0.1132, 0.7892, -0.1003, 0.5688],
[ 0.3637, -0.9906, -0.4752, -1.5197]])
>>> torch.sum(a, 1)
tensor([-0.4598, -0.1381, 1.3708, -2.6217])
>>> b = torch.arange(4 * 5 * 6).view(4, 5, 6)
>>> torch.sum(b, (2, 1))
tensor([ 435., 1335., 2235., 3135.])
torch.clamp()
torch.clamp(input, min=None, max=None, *, out=None)
将input中所有的元素夹到[min,max]区间内,返回:
>>> a = torch.randn(4)
>>> a
tensor([-1.7120, 0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000, 0.1734, -0.0478, -0.0922])
>>> min = torch.linspace(-1, 1, steps=4)
>>> torch.clamp(a, min=min)
tensor([-1.0000, 0.1734, 0.3333, 1.0000])
torch.log()
torch.log(input, *, out=None)
返回输入的Tensor的自然对数Tensor
>>> a = torch.randn(5)
>>> a
tensor([-0.7168, -0.5471, -0.8933, -1.4428, -0.1190])
>>> torch.log(a)
tensor([ nan, nan, nan, nan, nan])