密集层(Dense Layer)是深度学习中常用的一种神经网络层,也被称为全连接层(Fully Connected Layer)或线性层(Linear Layer)。
其公式为:
输出
=
输入
×
权重
+
偏置
输出=输入\times 权重+偏置
输出=输入×权重+偏置
即:
Y
=
X
×
W
T
+
b
Y = X \times W^T + b
Y=X×WT+b
示例代码如下:
import torch
import torch.nn as nn
# 定义线性层
linear_layer = nn.Linear(in_features=10, out_features=5)
# 使用state_dict()方法查看参数
params = linear_layer.state_dict()
# 创建输入
input_data = torch.randn(1, 10) # 假设输入维度为10
# 应用线性变换
print(input_data.size())
print(input_data)
print("--------------------")
output = linear_layer(input_data)
print(output.size())
print(output)
print("--------------------")
# 打印参数
print(params['weight'].size())
print(params['bias'].size())
print("模块的参数:", params)
输入X为一个 1 × 10 1\times 10 1×10的矩阵
tensor([[-0.9293, -0.1591, 0.1222, 1.3432, -0.2769, 0.3636, 0.4635, 0.8695,
0.0999, 0.0679]])
输出Y为一个 1 × 5 1 \times 5 1×5的矩阵
tensor([[-0.1930, 0.2412, -0.2174, 0.0605, 0.1455]],
grad_fn=<AddmmBackward0>)
权重W为一个 5 × 10 5 \times 10 5×10的矩阵
tensor([[ 0.2742, -0.1694, -0.2970, -0.2875, -0.1522, 0.1394, 0.2337, 0.2720,
-0.0485, 0.0854],
[ 0.1568, 0.2868, 0.0814, 0.3145, -0.1205, 0.1496, 0.2638, -0.2063,
-0.1005, 0.2868],
[ 0.3038, 0.2409, -0.2489, 0.0208, 0.0560, 0.0564, 0.3016, 0.1039,
0.1176, -0.1743],
[ 0.1479, -0.1633, 0.0063, 0.2720, -0.0071, 0.1717, -0.2909, -0.1842,
0.2239, 0.0726],
[-0.2103, 0.0684, -0.0034, -0.0660, -0.0258, -0.0493, -0.2986, 0.0872,
0.0568, 0.1197]])
偏置b为一个 1 × 5 1 \times 5 1×5的矩阵
tensor([ 0.0186, -0.0399, -0.1293, 0.0093, 0.1097])
上述矩阵尺寸和运算结果都符合公式
Y
=
X
×
W
T
+
b
Y = X \times W^T + b
Y=X×WT+b