赞
踩
torch.nn
是 PyTorch 中专门用于构建和训练神经网络的模块。它的整体架构分为几个主要部分,每部分的原理、要点和使用场景如下:
nn.Module
nn.Module
是所有神经网络组件的基类。任何神经网络模型都应该继承 nn.Module
,并实现其 forward
方法。__init__
: 初始化模型参数。forward
: 定义前向传播逻辑。parameters
: 返回模型的所有参数。import torch
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.linear = nn.Linear(10, 1)
def forward(self, x):
return self.linear(x)
model = MyModel()
print(model)
nn.Linear
(全连接层)linear = nn.Linear(10, 5)
input = torch.randn(1, 10)
output = linear(input)
print(output)
nn.Conv2d
(二维卷积层)conv = nn.Conv2d(in_channels=1, out_channels=3, kernel_size=3)
input = torch.randn(1, 1, 5, 5)
output = conv(input)
print(output)
nn.MaxPool2d
(二维最大池化层)maxpool = nn.MaxPool2d(kernel_size=2)
input = torch.randn(1, 1, 4, 4)
output = maxpool(input)
print(output)
nn.MSELoss
(均方误差损失)mse_loss = nn.MSELoss()
input = torch.randn(3, 5)
target = torch.randn(3, 5)
loss = mse_loss(input, target)
print(loss)
nn.CrossEntropyLoss
(交叉熵损失)cross_entropy_loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5)
target = torch.tensor([1, 0, 4])
loss = cross_entropy_loss(input, target)
print(loss)
torch.optim.SGD
(随机梯度下降)import torch.optim as optim
model = MyModel()
optimizer = optim.SGD(model.parameters(), lr=0.01)
criterion = nn.MSELoss()
# Training loop
for epoch in range(100):
optimizer.zero_grad()
output = model(torch.randn(1, 10))
loss = criterion(output, torch.randn(1, 1))
loss.backward()
optimizer.step()
torch.optim.Adam
(自适应矩估计)optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training loop
for epoch in range(100):
optimizer.zero_grad()
output = model(torch.randn(1, 10))
loss = criterion(output, torch.randn(1, 1))
loss.backward()
optimizer.step()
nn.ReLU
(修正线性单元)relu = nn.ReLU()
input = torch.randn(2)
output = relu(input)
print(output)
nn.BatchNorm2d
(二维批量归一化)batch_norm = nn.BatchNorm2d(3)
input = torch.randn(1, 3, 5, 5)
output = batch_norm(input)
print(output)
nn.Dropout
dropout = nn.Dropout(p=0.5)
input = torch.randn(2, 3)
output = dropout(input)
print(output)
nn.Sequential
(顺序容器)model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 5)
)
input = torch.randn(1, 10)
output = model(input)
print(output)
nn.ModuleList
(模块列表)layers = nn.ModuleList([
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 5)
])
input = torch.randn(1, 10)
for layer in layers:
input = layer(input)
print(input)
torch.nn.functional
)F.relu
(ReLU 激活函数)import torch.nn.functional as F
input = torch.randn(2)
output = F.relu(input)
print(output)
F.cross_entropy
(交叉熵损失函数)input = torch.randn(3, 5)
target = torch.tensor([1, 0, 4])
loss = F.cross_entropy(input, target)
print(loss)
F.conv2d
(二维卷积)input = torch.randn(1, 1, 5, 5)
weight = torch.randn(3, 1, 3, 3) # Manually defined weights
output = F.conv2d(input, weight)
print(output)
torch.nn.Parameter
)torch.nn.Parameter
是 torch.Tensor
的一种特殊子类,用于表示模型的可学习参数。它们在 nn.Module
中会自动注册为参数。class MyModelWithParam(nn.Module): def __init__(self): super(MyModelWithParam, self).__init__() self.my_param = nn.Parameter(torch.randn(10, 10)) def forward(self, x): return x @ self.my_param model = MyModelWithParam() input = torch.randn(1, 10) output = model(input) print(output) # 查看模型参数 for name, param in model.named_parameters(): print(name, param.size())
下面是一个结合上述各个部分的综合示例:
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim class MyComplexModel(nn.Module): def __init__(self): super(MyComplexModel, self).__init__() self.conv1 = nn.Conv2d(1, 32, kernel_size=3) self.bn1 = nn.BatchNorm2d(32) self.conv2 = nn.Conv2d(32, 64, kernel_size=3) self.bn2 = nn.BatchNorm2d(64) self.dropout = nn.Dropout(0.25) self.fc1 = nn.Linear(64*12*12, 128) self.fc2 = nn.Linear(128, 10) self.custom_param = nn.Parameter(torch.randn(128, 128)) def forward(self, x): x = F.relu(self .bn1(self.conv1(x))) x = F.max_pool2d(x, 2) x = F.relu(self.bn2(self.conv2(x))) x = F.max_pool2d(x, 2) x = self.dropout(x) x = x.view(x.size(0), -1) x = F.relu(self.fc1(x)) x = x @ self.custom_param x = self.fc2(x) return F.log_softmax(x, dim=1) model = MyComplexModel() criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.001) for epoch in range(10): optimizer.zero_grad() input = torch.randn(64, 1, 28, 28) target = torch.randint(0, 10, (64,)) output = model(input) loss = criterion(output, target) loss.backward() optimizer.step() print(f'Epoch {epoch+1}, Loss: {loss.item()}')
通过以上示例,可以更清晰地理解 torch.nn
模块的整体架构、原理、要点及其具体使用场景。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。