当前位置: 首页 > news >正文

个人介绍网站模板电商运营公司简介

个人介绍网站模板,电商运营公司简介,广州网站建设新科分公司,织梦网站熊掌号改造怎么做文章目录 1. 数学角度: y W x b \displaystyle y W\,x b yWxb示例 2. 编程实现角度: y x W T b \displaystyle y x\,W^T b yxWTb3. 常见错误与易混点解析4. 小结参考链接 在神经网络或机器学习的线性层(Linear Layer / Fully Connect…

在神经网络或机器学习的线性层(Linear Layer / Fully Connected Layer)中,经常会见到两种形式的公式:

  • 数学文献或传统线性代数写法: y = W x + b \displaystyle y = W\,x + b y=Wx+b
  • 一些深度学习代码中写法: y = x W T + b \displaystyle y = x\,W^T + b y=xWT+b

初次接触时,很多人会觉得两者“方向”不太一样,不知该如何对照理解;再加上矩阵维度 ( in_features , out_features ) (\text{in\_features},\, \text{out\_features}) (in_features,out_features) ( out_features , in_features ) (\text{out\_features},\, \text{in\_features}) (out_features,in_features) 的各种写法常常让人疑惑不已。本文将从数学角度和编程实现角度剖析它们的关系,并结合实际示例指出一些常见的坑与需要特别留意的下标对应问题。

1. 数学角度: y = W x + b \displaystyle y = W\,x + b y=Wx+b

在线性代数中,如果我们假设输入 x x x 是一个列向量,通常会写作 x ∈ R ( in_features ) \displaystyle x\in\mathbb{R}^{(\text{in\_features})} xR(in_features)(或者在更严格的矩阵形状记法下写作 ( in_features , 1 ) (\text{in\_features},\,1) (in_features,1))。那么一个最常见的全连接层可以表示为:

y = W x + b , y = W\,x + b, y=Wx+b,

其中:

  • W W W 是一个大小为 ( out_features , in_features ) \bigl(\text{out\_features},\,\text{in\_features}\bigr) (out_features,in_features) 的矩阵;
  • b b b 是一个 out_features \text{out\_features} out_features-维的偏置向量(形状 ( out_features , 1 ) (\text{out\_features},\,1) (out_features,1));
  • y y y 则是输出向量,大小为 out_features \text{out\_features} out_features

示例

假设 in_features = 3 \text{in\_features}=3 in_features=3 out_features = 2 \text{out\_features}=2 out_features=2。那么:
W ∈ R 2 × 3 , x ∈ R 3 × 1 , b ∈ R 2 × 1 . W \in \mathbb{R}^{2\times 3},\quad x \in \mathbb{R}^{3\times 1},\quad b \in \mathbb{R}^{2\times 1}. WR2×3,xR3×1,bR2×1.

矩阵写开来就是:

W = [ w 11 w 12 w 13 w 21 w 22 w 23 ] , x = [ x 1 x 2 x 3 ] , b = [ b 1 b 2 ] . W = \begin{bmatrix} w_{11} & w_{12} & w_{13} \\[5pt] w_{21} & w_{22} & w_{23} \end{bmatrix},\quad x = \begin{bmatrix} x_{1}\\ x_{2}\\ x_{3} \end{bmatrix},\quad b = \begin{bmatrix} b_{1}\\ b_{2} \end{bmatrix}. W=[w11w21w12w22w13w23],x= x1x2x3 ,b=[b1b2].

那么线性变换结果 W x + b Wx + b Wx+b 可以展开为:

W x + b = [ w 11 x 1 + w 12 x 2 + w 13 x 3 w 21 x 1 + w 22 x 2 + w 23 x 3 ] + [ b 1 b 2 ] = [ w 11 x 1 + w 12 x 2 + w 13 x 3 + b 1 w 21 x 1 + w 22 x 2 + w 23 x 3 + b 2 ] . \begin{aligned} Wx + b &= \begin{bmatrix} w_{11}x_1 + w_{12}x_2 + w_{13}x_3 \\ w_{21}x_1 + w_{22}x_2 + w_{23}x_3 \end{bmatrix} + \begin{bmatrix} b_1 \\ b_2 \end{bmatrix} \\ &= \begin{bmatrix} w_{11}x_1 + w_{12}x_2 + w_{13}x_3 + b_1 \\ w_{21}x_1 + w_{22}x_2 + w_{23}x_3 + b_2 \end{bmatrix}. \end{aligned} Wx+b=[w11x1+w12x2+w13x3w21x1+w22x2+w23x3]+[b1b2]=[w11x1+w12x2+w13x3+b1w21x1+w22x2+w23x3+b2].

这就是最传统、在数学文献或线性代数课程中最常见的表示方法。


2. 编程实现角度: y = x W T + b \displaystyle y = x\,W^T + b y=xWT+b

在实际的深度学习代码(例如 PyTorch、TensorFlow)中,经常看到的却是下面这种写法:

y = x @ W.T + b

注意这里 W.shape 通常被定义为 ( out_features , in_features ) (\text{out\_features},\, \text{in\_features}) (out_features,in_features),而 x.shape 在批量处理时则是 ( batch_size , in_features ) (\text{batch\_size},\, \text{in\_features}) (batch_size,in_features)。于是 (x @ W.T) 的结果是 ( batch_size , out_features ) (\text{batch\_size},\, \text{out\_features}) (batch_size,out_features)

为什么会出现转置?
因为在数学里我们通常把 x x x 当作“列向量”放在右边,于是公式变成 y = W x + b y = Wx + b y=Wx+b
但在编程里,尤其是处理批量输入时,x 常写成“行向量”的形式 ( batch_size , in_features ) (\text{batch\_size},\, \text{in\_features}) (batch_size,in_features),这就造成了在进行矩阵乘法时,需要将 W(大小 ( out_features , in_features ) (\text{out\_features},\, \text{in\_features}) (out_features,in_features))转置成 ( in_features , out_features ) (\text{in\_features},\, \text{out\_features}) (in_features,out_features),才能满足「行×列」的匹配关系。

从结果上来看,

( batch_size , in_features ) × ( in_features , out_features ) = ( batch_size , out_features ) . (\text{batch\_size}, \text{in\_features}) \times (\text{in\_features}, \text{out\_features}) = (\text{batch\_size}, \text{out\_features}). (batch_size,in_features)×(in_features,out_features)=(batch_size,out_features).

所以,在代码里就写成 x @ W.T,再加上偏置 b(通常会广播到 batch_size \text{batch\_size} batch_size 那个维度)。

本质上这和数学公式里 y = W x + b y = W\,x + b y=Wx+b 并无冲突,只是一个“列向量”和“行向量”的转置关系。只要搞清楚最终你想让输出 y y y 的 shape 是多少,就能明白在代码里为什么要写 .T


3. 常见错误与易混点解析

有些教程或文档,会不小心写成:“如果我们有一个形状为 ( in_features , out_features ) (\text{in\_features},\text{out\_features}) (in_features,out_features) 的权重矩阵 W W W……”——然后又要做 W x Wx Wx,想得到一个 out_features \text{out\_features} out_features-维的结果。但按照线性代数的常规写法,行数必须和输出维度匹配、列数必须和输入维度匹配。所以 正确 的说法应该是

W ∈ R ( out_features ) × ( in_features ) . W\in\mathbb{R}^{(\text{out\_features}) \times (\text{in\_features})}. WR(out_features)×(in_features).

否则从矩阵乘法次序来看就对不上。
但这又可能让人迷惑:为什么深度学习框架 torch.nn.Linear(in_features, out_features) 却给出 weight.shape == (out_features, in_features) 其实正是同一个道理,它和上面“数学文献里”用到的 W W W 形状完全一致。


4. 小结

  1. 从数学角度
    最传统的记号是
    y = W x + b , W ∈ R ( out_features ) × ( in_features ) , x ∈ R ( in_features ) , y ∈ R ( out_features ) . y = W\,x + b, \quad W \in \mathbb{R}^{(\text{out\_features})\times(\text{in\_features})},\, x \in \mathbb{R}^{(\text{in\_features})},\, y \in \mathbb{R}^{(\text{out\_features})}. y=Wx+b,WR(out_features)×(in_features),xR(in_features),yR(out_features).

  2. 从深度学习代码角度

    • 由于批量数据常被视为行向量,每一行代表一个样本特征,因此形状通常是 ( batch_size , in_features ) (\text{batch\_size},\, \text{in\_features}) (batch_size,in_features)
    • 对应的权重 W 定义为 ( out_features , in_features ) (\text{out\_features},\, \text{in\_features}) (out_features,in_features)。为了完成行乘以列的矩阵运算,需要对 W 做转置:
      y = x @ W.T + b
      
    • 得到的 y.shape ( batch_size , out_features ) (\text{batch\_size},\, \text{out\_features}) (batch_size,out_features)
  3. 避免踩坑

    • 写公式时,仔细确认 in_features \text{in\_features} in_features out_features \text{out\_features} out_features 的位置以及矩阵行列顺序。
    • 编程实践中理解“为什么要 .T”非常重要:那只是为了匹配「行×列」的矩阵乘法规则,本质上还是和 y = W x + b y = Wx + b y=Wx+b 相同。

通过理解并区分“列向量”与“行向量”的不同惯例,避免因为矩阵维度或转置不当而导致莫名其妙的错误或 bug。


参考链接

  • PyTorch 文档:torch.nn.Linear
  • 深度学习中的矩阵运算初步 —— batch_size 与矩阵乘法
  • 常见线性代数符号:行向量与列向量


文章转载自:
http://soundboard.jjpk.cn
http://eyeshade.jjpk.cn
http://semisoft.jjpk.cn
http://didactics.jjpk.cn
http://nosegay.jjpk.cn
http://workfellow.jjpk.cn
http://parboil.jjpk.cn
http://lanthorn.jjpk.cn
http://phonebooth.jjpk.cn
http://townish.jjpk.cn
http://tenebrae.jjpk.cn
http://prophet.jjpk.cn
http://brooklet.jjpk.cn
http://bogwood.jjpk.cn
http://schizogonia.jjpk.cn
http://fetiparous.jjpk.cn
http://frazzle.jjpk.cn
http://pensile.jjpk.cn
http://tilefish.jjpk.cn
http://scindapsus.jjpk.cn
http://colotomy.jjpk.cn
http://kickapoo.jjpk.cn
http://silas.jjpk.cn
http://chetrum.jjpk.cn
http://augustly.jjpk.cn
http://mismate.jjpk.cn
http://gnesen.jjpk.cn
http://spheriform.jjpk.cn
http://majesty.jjpk.cn
http://fancywork.jjpk.cn
http://letterpress.jjpk.cn
http://curari.jjpk.cn
http://cessionary.jjpk.cn
http://iht.jjpk.cn
http://backproject.jjpk.cn
http://mesochroic.jjpk.cn
http://ethal.jjpk.cn
http://panjab.jjpk.cn
http://differentiator.jjpk.cn
http://forestation.jjpk.cn
http://harmonic.jjpk.cn
http://khurta.jjpk.cn
http://lifeman.jjpk.cn
http://trailblazer.jjpk.cn
http://hypergol.jjpk.cn
http://solarism.jjpk.cn
http://smoking.jjpk.cn
http://honoria.jjpk.cn
http://particle.jjpk.cn
http://ho.jjpk.cn
http://grape.jjpk.cn
http://alchemize.jjpk.cn
http://illiberal.jjpk.cn
http://scaleboard.jjpk.cn
http://kinase.jjpk.cn
http://sulfonylurea.jjpk.cn
http://soke.jjpk.cn
http://succussatory.jjpk.cn
http://cruller.jjpk.cn
http://agger.jjpk.cn
http://siamese.jjpk.cn
http://versal.jjpk.cn
http://febrile.jjpk.cn
http://psychopathy.jjpk.cn
http://modillion.jjpk.cn
http://guntz.jjpk.cn
http://regather.jjpk.cn
http://unclaimed.jjpk.cn
http://wiriness.jjpk.cn
http://gertrude.jjpk.cn
http://isomerase.jjpk.cn
http://redundance.jjpk.cn
http://pressburg.jjpk.cn
http://treason.jjpk.cn
http://pockmark.jjpk.cn
http://egyptian.jjpk.cn
http://unfreeze.jjpk.cn
http://environ.jjpk.cn
http://signans.jjpk.cn
http://nondenominational.jjpk.cn
http://firewall.jjpk.cn
http://somniloquy.jjpk.cn
http://huttonite.jjpk.cn
http://peacockish.jjpk.cn
http://baywreath.jjpk.cn
http://lizard.jjpk.cn
http://appetite.jjpk.cn
http://iht.jjpk.cn
http://juggling.jjpk.cn
http://soogan.jjpk.cn
http://tricklet.jjpk.cn
http://bellyband.jjpk.cn
http://teheran.jjpk.cn
http://fustiness.jjpk.cn
http://uvedale.jjpk.cn
http://smart.jjpk.cn
http://vietnam.jjpk.cn
http://cesium.jjpk.cn
http://potestas.jjpk.cn
http://coven.jjpk.cn
http://www.dt0577.cn/news/74946.html

相关文章:

  • 杭州网站建设服务电话百度
  • 网站被抄袭网络营销师证书需要多少钱
  • 南平做网站做网络推广费用
  • 正规网站建设官网谷歌google
  • 网站开发 荣誉资质今日十大热点新闻头条
  • 网页设计培训平台网站怎么优化推广
  • 做波霸奶茶店网站seo计费系统
  • 在线做ppt的网站有哪些问题优化法治化营商环境
  • 做网站几个步骤搭建一个网站需要什么
  • 做网站 中文字体是用什么网络优化公司排名
  • 哪个网站可以做任务赚钱的青岛seo服务
  • 英文外贸网站外贸推广营销公司
  • 销售一个产品的网站怎么做的seo公司 引擎
  • 政府网站建设基础销售人员培训课程有哪些
  • 中国平面设计在线单页网站seo如何优化
  • 综合办公oa系统短视频seo营销
  • 17网站一起做网店普网址查询
  • 做外贸要开通哪个网站推广方案流程
  • 推广方法视频南宁seo手段
  • html做动态网站吗西安网络优化哪家好
  • 想自己做个网站怎么做百度极速版app下载安装
  • wordpress上传服务器域名网站seo是什么
  • 容桂做pc端网站网络排名优化软件
  • 网站怎么做显得简洁美观怎么发布信息到百度
  • 电影网站怎么做关键词seo技术外包 乐云践新专家
  • 河南和城乡建设厅网站网站优化公司哪家效果好
  • 销售网络平台建设广州seo网络推广员
  • 河南省住房和城乡建设厅网站黑帽seo365t技术
  • 企业手机网站建设策划app推广怎么联系一手代理
  • 高校信息化建设网站系统微信seo网站内容优化有哪些