在PyTorch中進(jìn)行超參數(shù)搜索通常有兩種常用的方法:
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from torch import nn, optim
from torch.utils.data import DataLoader
# Define your model
class MyModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(MyModel, self).__init__()
self.hidden = nn.Linear(input_dim, hidden_dim)
self.relu = nn.ReLU()
self.output = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
x = self.hidden(x)
x = self.relu(x)
x = self.output(x)
return x
# Define your dataset and dataloader
# dataset = ...
# dataloader = DataLoader(dataset, batch_size=64, shuffle=True)
# Define parameter grid
param_grid = {
'hidden_dim': [64, 128, 256],
'learning_rate': [0.001, 0.01, 0.1]
}
# Create a GridSearchCV object
grid_search = GridSearchCV(MyModel, param_grid, scoring='accuracy', cv=3)
# Fit the model
grid_search.fit(dataloader)
# Print best parameters
print(grid_search.best_params_)
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import accuracy_score
from torch import nn, optim
from torch.utils.data import DataLoader
# Define your model
# Define your model
class MyModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(MyModel, self).__init__()
self.hidden = nn.Linear(input_dim, hidden_dim)
self.relu = nn.ReLU()
self.output = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
x = self.hidden(x)
x = self.relu(x)
x = self.output(x)
return x
# Define your dataset and dataloader
# dataset = ...
# dataloader = DataLoader(dataset, batch_size=64, shuffle=True)
# Define parameter grid
param_dist = {
'hidden_dim': [64, 128, 256],
'learning_rate': [0.001, 0.01, 0.1]
}
# Create a RandomizedSearchCV object
random_search = RandomizedSearchCV(MyModel, param_dist, n_iter=10, scoring='accuracy', cv=3)
# Fit the model
random_search.fit(dataloader)
# Print best parameters
print(random_search.best_params_)
無論選擇哪種方法,超參數(shù)搜索是一個耗時的過程,需要謹(jǐn)慎選擇超參數(shù)的范圍和步長,以及合適的評估指標(biāo)來評估模型性能。