搭建深度学习网络时常用的一些pytorch函数:view(),softmax(),FloatTensor(),zero_(),scatter_(),gather(),sum(),clamp(),log

 

目录

torch.view() 

torch.nn.functional.softmax()

torch.FloatTensor()

torch.Tensor.zero_()

torch.Tensor.scatter_()

torch.gather()

torch.sum()

torch.clamp()

torch.log()


参考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

 参数:

  • input (Tensor) – 源张量

  • dim (int) – 要索引的维度

  • index (LongTensor) – 索引

例子:

>>> 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) 
  • input (Tensor) – 输入的张量

  • dim (int or tuple of python:ints) – 要减少的维度

  • keepdim (bool) – 输出张量是否保留dim

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值