线性网络模型

一:线性网络模型

线性网络模型的基本表现形式为:
Y = w x + b Y = wx + b Y=wx+b
其中 w w w 表示权重, b b b 表示偏置。

如果对于样本 X = [ x 1 , x 2 , x 3 . . . . . . x n ] X = [x_1, x_2, x_3......x_n] X=[x1,x2,x3......xn] 代表样本 X X X n n n 个特征,那么我们对每个特征都设置一个相对应的权重 w i w_i wi , 则改写成:
Y = w 1 x 1 + w 2 x 2 + w 3 x 3 + . . . . . . + w n ∗ x n + b = ∑ i = 1 n w i x i + b \begin{aligned} Y &= w_1x_1 + w_2x_2 + w_3x_3 + ...... + w_n*x_n + b \\ &= \sum_{i=1}^n {w_ix_i} + b \end{aligned} Y=w1x1+w2x2+w3x3+......+wnxn+b=i=1nwixi+b
将上面的公式改写成矩阵的形式得到:
Y = w T x + b Y = w^Tx + b Y=wTx+b
其中 w T = [ w 1 , w 2 , . . . . . . , w n ] , x T = [ x 1 , x 2 , x 3 , . . . . . . , x n ] w^T = [w_1, w_2, ...... , w_n], x^T = [x_1, x_2, x_3,......, x_n] wT=[w1,w2,......,wn],xT=[x1,x2,x3,......,xn]

如果形象化的表示就是:
在这里插入图片描述
在上面这个过程中我们处理的只是一个特征,现在我们将特征扩展为三个:
此时 w , b w, b w,b 变成了:
w = [ w 11 w 12 w 13 w 21 w 22 w 23 w 31 w 32 w 33 ]        b = [ b 1 b 2 b 3 ] w = \begin{bmatrix} w_{11} & w_{12} & w_{13} \\ w_{21} & w_{22} & w_{23} \\ w_{31} & w_{32} & w_{33} \end{bmatrix} {\;}\;\; b = \begin{bmatrix} b_{1} \\ b_{2} \\ b_{3} \end{bmatrix} w= w11w21w31w12w22w32w13w23w33 b= b1b2b3
w w w 的行数代表输出的特征个数,列数代表的是输入的特征数,每个特征对应的权重值。例如,假如我们的样本特征是4维,我们想在第一层神经网络模型构造一个6维的特征。那么这中间 w = ( 6 ∗ 4 ) , b = ( 1 ∗ 6 ) w = (6 * 4), b = (1 * 6) w=(64),b=(16)

二:Linear Model

全连接层: torch.nn.Linear

全连接层就是我们上面提到的线性网络模型:

class Linear(Module):
  
    __constants__ = ['in_features', 'out_features']
    in_features: int
    out_features: int
    weight: Tensor

    def __init__(self, in_features: int, out_features: int, bias: bool = True,
                 device=None, dtype=None) -> None:
        factory_kwargs = {'device': device, 'dtype': dtype}
        super().__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = Parameter(torch.empty((out_features, in_features), **factory_kwargs))
        if bias:
            self.bias = Parameter(torch.empty(out_features, **factory_kwargs))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

全连接层最主要的两个参数是 in_features, out_features分别代表输入特征和输出特征。根据上面的推导我们可以知道根据这两个参数我们就可以构造出矩阵 w = ( o u t _ f e a t u r e s ∗ i n _ f e a t u r e s ) , b = ( 1 ∗ o u t _ f e a t u r e s ) w = (out\_features * in\_features), b = (1 * out\_features) w=(out_featuresin_features),b=(1out_features)

使用
if __name__ == '__main__':
    # 构造有四个特征的样本
    sample = torch.tensor([1., 1., 1., 1.])
    # 构造一个全连接层, 输入是dim(sample), 输出是6维
    linear = torch.nn.Linear(4, 6)
    print(linear.weight)
    print(linear.bias)
    output = linear(sample)
    print(output)
    
"""
weight = tensor([ [ 0.0172,  0.0627,  0.1316,  0.4917],
                  [ 0.4408,  0.0173,  0.0681, -0.2942],
                  [ 0.1543, -0.2808, -0.4453, -0.4480],
                  [-0.4821, -0.0123,  0.0235, -0.1196],
                  [ 0.3925, -0.4515,  0.4247, -0.1525],
                  [ 0.1604,  0.0124,  0.1822, -0.4387] ])

bias = tensor([-0.0178,  0.3837, -0.0688,  0.2666, -0.0250,  0.4139])

output = tensor([ 0.6855,  0.6157, -1.0886, -0.3239,  0.1883,  0.3301])
"""
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值