Pytorch nn.Linear()

nn.Linear就是神经网络中的线性层,类似于数学中的线性函数,可以实现形如y=X*weight^T+b的功能。

#导包
import torch.nn as nn
import torch

#创建1个张量
sample=torch.tensor([1.,10.,100.])

#nn.Linear(in_feature,out_feature,bias),这里设置了不需要bias,即函数为y=k*x

linear=nn.Linear(3,3,bias=False)

print(linear.weight)

#weight为一个3*3的张量
out:
Parameter containing:
tensor([[ 0.0777,  0.1295, -0.3284],
        [-0.5325,  0.2380,  0.1290],
        [ 0.3780, -0.1113,  0.3035]], requires_grad=True)

output=linear(sample)

print(output)
out:
tensor([-31.4626,  14.7472,  29.6170], grad_fn=<SqueezeBackward3>)

我们输入有3个特征 x1,x2,x3,bias=False,所以方程式为y=w1*x1+w2*x2+w3*x3

y1=0.0777*1+0.1295*10+(-0.3284)*100=-31.46

y2=-0.5325*1+0.2380*10+0.1290*100=14.74

y3=0.3780*1+(-0.1113)*10+0.3035*100=29.61

输出特征也为3

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 14:36:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 14:36:07       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 14:36:07       87 阅读
  4. Python语言-面向对象

    2024-03-29 14:36:07       96 阅读

热门阅读

  1. mybatis plus 数据权限插件在项目中的使用

    2024-03-29 14:36:07       38 阅读
  2. C语言——函数练习程序

    2024-03-29 14:36:07       44 阅读
  3. 解决跨域问题

    2024-03-29 14:36:07       45 阅读
  4. svg怎么用,后端返回svg文件流引入

    2024-03-29 14:36:07       38 阅读
  5. 2024.03.15 校招 实习 内推 面经

    2024-03-29 14:36:07       41 阅读
  6. NGINX安装Stream模块

    2024-03-29 14:36:07       43 阅读
  7. 山东专精特新申报基本要求

    2024-03-29 14:36:07       33 阅读
  8. 四、分治算法

    2024-03-29 14:36:07       42 阅读
  9. 2024蓝桥杯每日一题(背包)

    2024-03-29 14:36:07       47 阅读
  10. Vue3 实现基于token 用户登录

    2024-03-29 14:36:07       40 阅读
  11. 前端Vue开发技术总结

    2024-03-29 14:36:07       45 阅读