用 Pytorch 写一段模型训练的代码
时间: 2023-09-05 13:04:53 浏览: 112
### 回答1:
下面是在 Pytorch 中写的一段模型训练的代码:# 加载数据
train_data = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)# 初始化模型参数
model = Model(input_size, hidden_size, num_classes)# 定义loss和优化器
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)# 开始训练
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# 获取图片及其对应标签
images = images.reshape(-1, input_size).to(device)
labels = labels.to(device)
# 前向传播
outputs = model(images)
loss = criterion(outputs, labels)
# 后向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step() if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
### 回答2:
import torch
import torch.nn as nn
import torch.optim as optim
# 定义一个简单的神经网络模型
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc1 = nn.Linear(10, 20) # 全连接层1
self.fc2 = nn.Linear(20, 1) # 全连接层2
def forward(self, x):
x = torch.relu(self.fc1(x)) # relu激活函数
x = self.fc2(x)
return x
# 模型训练数据准备
input_data = torch.randn(100, 10) # 输入数据(100个训练样本,每个样本10个特征)
target = torch.randn(100, 1) # 目标值
# 创建模型对象
model = SimpleModel()
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
# 模型训练
for epoch in range(100):
optimizer.zero_grad() # 清零梯度
output = model(input_data) # 前向传播
loss = criterion(output, target) # 计算损失
loss.backward() # 反向传播,计算梯度
optimizer.step() # 更新权重
if epoch % 10 == 0:
print ('Epoch [{}/{}], Loss: {:.4f}'.format(epoch, 100, loss.item()))
# 测试模型
test_input = torch.randn(10, 10) # 测试输入(10个样本,每个样本10个特征)
test_output = model(test_input) # 模型输出
print("测试输出:", test_output)
### 回答3:
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
# 准备数据集
train_dataset = ... # 定义训练数据集
test_dataset = ... # 定义测试数据集
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
# 定义模型
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(10, 100)
self.fc2 = nn.Linear(100, 10)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = MyModel()
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
# 训练模型
for epoch in range(10): # 迭代10次
running_loss = 0.0
for i, data in enumerate(train_loader):
inputs, labels = data
optimizer.zero_grad() # 清零梯度
outputs = model(inputs) # 前向传播
loss = criterion(outputs, labels) # 计算损失
loss.backward() # 反向传播
optimizer.step() # 更新参数
running_loss += loss.item()
print(f"Epoch {epoch+1}, Loss: {running_loss / len(train_loader)}")
# 测试模型
total_correct = 0
total_samples = 0
with torch.no_grad():
for data in test_loader:
inputs, labels = data
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
total_correct += (predicted == labels).sum().item()
total_samples += labels.size(0)
accuracy = total_correct / total_samples
print(f"Test Accuracy: {accuracy}")
阅读全文