import torch.optim as optim
时间: 2023-11-26 11:04:53 浏览: 84
关于torch.optim的灵活使用详解(包括重写SGD,加上L1正则)
5星 · 资源好评率100%
from typing import List,Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self, input_size:int, hidden_size:List[int], output_size:int, dropout:float):
super(Net, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.dropout = dropout
# Construct the hidden layers
self.hidden_layers = nn.ModuleList()
for i in range(len(hidden_size)):
if i == 0:
self.hidden_layers.append(nn.Linear(input_size, hidden_size[i]))
else:
self.hidden_layers.append(nn.Linear(hidden_size[i-1], hidden_size[i]))
# Construct the output layer
self.output_layer = nn.Linear(hidden_size[-1], output_size)
# Set up the dropout layer
self.dropout_layer = nn.Dropout(p=dropout)
def forward(self, x:torch.Tensor) -> torch.Tensor:
# Pass the input through the hidden layers
for layer in self.hidden_layers:
x = F.relu(layer(x))
x = self.dropout_layer(x)
# Pass the output from the last hidden layer through the output layer
x = self.output_layer(x)
return x
def train_model(model:Net, train_data:Tuple[torch.Tensor, torch.Tensor],
test_data:Tuple[torch.Tensor, torch.Tensor],
batch_size:int, num_epochs:int, learning_rate:float):
# Extract the inputs and labels from the training data
train_inputs, train_labels = train_data
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Loop over the training data for the specified number of epochs
for epoch in range(num_epochs):
# Shuffle the training data
perm = torch.randperm(train_inputs.size(0))
train_inputs = train_inputs[perm]
train_labels = train_labels[perm]
# Loop over the training data in batches
for i in range(0, train_inputs.size(0), batch_size):
# Extract the current batch of data
inputs = train_inputs[i:i+batch_size]
labels = train_labels[i:i+batch_size]
# Zero the gradients
optimizer.zero_grad()
# Forward pass
outputs = model(inputs)
loss = criterion(outputs, labels)
# Backward pass and update parameters
loss.backward()
optimizer.step()
# Evaluate the model on the test data
test_inputs, test_labels = test_data
test_outputs = model(test_inputs)
test_loss = criterion(test_outputs, test_labels)
test_accuracy = accuracy(test_outputs, test_labels)
# Print the epoch number, training loss, and test accuracy
print(f"Epoch {epoch+1}/{num_epochs}: Train loss={loss:.4f}, Test loss={test_loss:.4f}, Test accuracy={test_accuracy:.4f}")
def accuracy(outputs:torch.Tensor, labels:torch.Tensor) -> float:
predictions = torch.argmax(outputs, dim=1)
correct_predictions = torch.sum(predictions == labels)
accuracy = correct_predictions.float() / labels.size(0)
return accuracy.item()
阅读全文