python
import torch
import torch.nn as nn
# 定义线性层
linear_layer = nn.Linear(in_features=10, out_features=5, bias=True)
# 输入数据
input_data = torch.randn(32, 10) # (batch_size=32, in_features=10)
# 前向传播
output = linear_layer(input_data)
print(output.shape) # 输出形状: (32, 5)
维度变化
-
输入 :
(batch_size, in_features)
。 -
输出 :
(batch_size, out_features)
。
示例
-
输入形状:
(32, 10)
。 -
线性层:
nn.Linear(10, 5)
。 -
输出形状:
(32, 5)
。
实现细节:矩阵乘法
