pytorch构建神经网络进行贝叶斯超参数优化的代码
时间: 2024-02-06 16:40:10 浏览: 338
下面是使用PyTorch构建神经网络进行贝叶斯超参数优化的代码示例。使用了贝叶斯优化库 `BayesianOptimization`。
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from bayes_opt import BayesianOptimization
class Net(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(Net, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
def train(model, device, train_loader, optimizer, criterion):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
def test(model, device, test_loader, criterion):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += criterion(output, target).item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
accuracy = 100.0 * correct / len(test_loader.dataset)
return test_loss, accuracy
def train_and_evaluate(hidden_size, learning_rate):
# Set up device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Set up data loaders
batch_size = 64
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
train_data = datasets.MNIST("data", train=True, download=True, transform=transform)
test_data = datasets.MNIST("data", train=False, download=True, transform=transform)
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False)
# Set up model
input_size = 784
output_size = 10
model = Net(input_size, int(hidden_size), output_size).to(device)
# Set up optimizer and loss function
optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9)
criterion = nn.CrossEntropyLoss()
# Train and test the model
for epoch in range(10):
train(model, device, train_loader, optimizer, criterion)
test_loss, accuracy = test(model, device, test_loader, criterion)
print(f"Epoch {epoch + 1} - Test Loss: {test_loss:.4f}, Accuracy: {accuracy:.2f}%")
return accuracy
if __name__ == "__main__":
# Define hyperparameter search space
pbounds = {"hidden_size": (50, 500), "learning_rate": (1e-5, 1e-1)}
# Set up optimizer
optimizer = BayesianOptimization(f=train_and_evaluate, pbounds=pbounds)
# Run optimizer
optimizer.maximize(init_points=5, n_iter=20)
# Print best hyperparameters
print(optimizer.max)
```
在这个例子中,我们使用 MNIST 数据集作为我们的训练数据,并构建了一个简单的两层全连接神经网络作为我们的模型。然后,我们使用 `BayesianOptimization` 来搜索隐藏层大小和学习率的最佳值。在每次迭代中,我们使用 `train_and_evaluate` 函数来训练和测试模型,并返回测试准确率作为优化目标。最后,我们打印出找到的最佳超参数组合。
阅读全文