Pytorch nn.Linear()

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

python 复制代码
#导包
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

相关推荐
IT_陈寒1 小时前
Vite 凭什么比 Webpack 快50%?揭秘闪电构建背后的黑科技
前端·人工智能·后端
寻见9032 小时前
救命!RAG检索总跑偏?bge-reranker-large彻底解决「找错文档」痛点
人工智能·langchain
TechFind2 小时前
我用 OpenClaw 搭了一套运营 Agent,每天自动生产内容、分发、追踪数据——独立开发者的运营平替
人工智能·agent
小成C2 小时前
Vibe Coding 时代,研发体系该怎么重新分工
人工智能·架构·全栈
37手游后端团队2 小时前
全网最简单!从零开始,轻松把 openclaw 小龙虾装回家
人工智能·后端·openai
该用户已不存在2 小时前
月薪2w养不起龙虾?试试OpenClaw+Ollama
人工智能·aigc·ai编程
Seeker2 小时前
别盲目跟风“养龙虾”!OpenClaw爆火背后,这些致命安全风险必须警惕
人工智能·安全
golang学习记2 小时前
Claude Code 官宣新 AI 功能!随时随地 AI 为你打工
人工智能·claude
IvanCodes3 小时前
OpenClaw保姆级安装教程:windows&ubuntu
人工智能
Serverless社区4 小时前
AgentRun实践指南:Agent 的宝藏工具—All-In-One Sandbox
人工智能