全连接层(线性变换层),实现 y = x @ W + b
DenseLayer
00 Remarks
GNN 模块自带的 LinearLayer 权重形状为 [inFeatures, outFeatures], 但其 Forward 使用 input.MatMul(_weights.Transpose())、Backward 使用 gradient.MatMul(_weights),两者都要求 inFeatures 与 outFeatures 相等, 否则会抛出「矩阵维度不匹配」。GEARS 的隐藏层维度需要自由配置(例如 34 → 32), 因此这里提供一个维度语义正确的全连接实现(权重 [in, out]), 不修改共享运行时代码以免影响 GNN 模块的既有调用方。
本层的参数张量与梯度张量在整个生命周期内保持同一批实例, Adam 优化器持有它们的引用并原地更新。
01 Syntax
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| .ctor | 1 | 创建全连接层 |
| Forward | 1 | 前向传播:y = x @ W + b |
| Backward | 1 | 反向传播:累积 dW / db 并返回输入梯度 dX |
| GetParameters | 1 | 获取本层可训练参数(权重与偏置) |
| GetGradients | 1 | 获取本层参数梯度(顺序与 DenseLayer.GetParameters() 一致) |
| GetWeights | 1 | 获取权重矩阵(用于调试或模型检查) |
| GetBias | 1 | 获取偏置向量(用于调试或模型检查) |
03 Properties
| Name | Overloads | Summary |
|---|---|---|
| InFeatures | 1 | 输入特征维度 |
| OutFeatures | 1 | 输出特征维度 |
| UseBias | 1 | 是否使用偏置项 |
04 Fields
| Name | Overloads | Summary |
|---|---|---|
| weights | 1 | 权重矩阵 [inFeatures, outFeatures] |
| bias | 1 | 偏置向量 [1, outFeatures] |
| weightGrad | 1 | 权重梯度 [inFeatures, outFeatures] |
| biasGrad | 1 | 偏置梯度 [1, outFeatures] |
| lastInput | 1 | 上一次前向传播的输入,用于反向传播 |
05 Members
Int32, Int32, Boolean, Double, Nullable(Of Int32), String)创建全连接层
| Name | Type | Description |
|---|---|---|
inFeatures | Int32 | 输入特征维度 |
outFeatures | Int32 | 输出特征维度 |
useBias | Boolean | 是否使用偏置项 |
scale | Double | 权重初始化的缩放系数,默认 1.0(Xavier 初始化的标准差乘上该系数) |
seed | Nullable(Of Int32) | 权重初始化随机种子;给定后初始化结果可复现 |
name | String | 层名称 |
Tensor)前向传播:y = x @ W + b
| Name | Type | Description |
|---|---|---|
input | Tensor | 输入特征 [batch, inFeatures] |
输出特征 [batch, outFeatures]
Tensor)反向传播:累积 dW / db 并返回输入梯度 dX
| Name | Type | Description |
|---|---|---|
gradient | Tensor | 上游梯度 [batch, outFeatures] |
输入梯度 [batch, inFeatures]
获取本层可训练参数(权重与偏置)
参数张量列表
获取本层参数梯度(顺序与 DenseLayer.GetParameters() 一致)
梯度张量列表
获取权重矩阵(用于调试或模型检查)
权重张量 [inFeatures, outFeatures]
获取偏置向量(用于调试或模型检查)
偏置张量 [1, outFeatures]
输入特征维度
输入维度大小
输出特征维度
输出维度大小
是否使用偏置项
使用偏置则返回 True
权重矩阵 [inFeatures, outFeatures]
偏置向量 [1, outFeatures]
权重梯度 [inFeatures, outFeatures]
偏置梯度 [1, outFeatures]
上一次前向传播的输入,用于反向传播