PyTorch中nn.Linear()理解

PyTorch中nn.Linear()理解

计算公式

$ y = xA^{T}+b$

这里A为weight,b为bias。

代码部分

初始化部分代码

class Linear(Module):
	...
	__constants__ = ['bias']
	
	def __init__(self, in_features, out_features, bias=True):
	    super(Linear, self).__init__()
	    self.in_features = in_features
	    self.out_features = out_features
	    self.weight = Parameter(torch.Tensor(out_features, in_features))
	    if bias:
	        self.bias = Parameter(torch.Tensor(out_features))
	    else:
	        self.register_parameter('bias', None)
	    self.reset_parameters()

计算部分

@weak_script_method
    def forward(self, input):
        return F.linear(input, self.weight, self.bias)

返回值为: input * weight + bias

bias和weight

weight: the learnable weights of the module of shape
    :math:`(\text{out\_features}, \text{in\_features})`. The values are
    initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
    :math:`k = \frac{1}{\text{in\_features}}`
bias:   the learnable bias of the module of shape :math:`(\text{out\_features})`.
        If :attr:`bias` is ``True``, the values are initialized from
        :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
        :math:`k = \frac{1}{\text{in\_features}}`

示例

>>> import torch
>>> nn1 = torch.nn.Linear(100, 50)
>>> input1 = torch.randn(140, 100)
>>> output1 = nn1(input1)
>>> output1.size()
torch.Size([140, 50])

对于上述描述,我们创建一个input的维度为[140,100], 通过声明线性层会得到根据维度初始化的权重和偏差,其中weight的维度为[50,100]。对于公式中A表示的就是weight,而b表示的就是bias。由于对A进行了转置所以这里weight的维度为[50,100]而不是[100,50]。

具体计算为[140,100] × [50,100]的转置 + bias = [140,100] × [100,50] + bias最后得到的维度为[140,50]。

至于对于bias和weight的初始化,根绝网上所讲的是来有关维度值得均匀分布。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

StriveZs

用爱发电

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值