搭建深度学习网络时常用的一些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。  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值