Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying optuna to optain a better accuracy #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
import optuna



class Net(nn.Module):
Expand Down Expand Up @@ -35,6 +37,22 @@ def forward(self, x):
output = F.log_softmax(x, dim=1)
return output

def objective(trial, device, args, train_loader, test_loader):

lr = trial.suggest_loguniform('lr', 1e-5, 1e-1)
epochs = trial.suggest_int('epochs', 1, 20)

model = Net().to(device)
optimizer = optim.Adam(model.parameters(), lr=lr)
scheduler = StepLR(optimizer, step_size=1, gamma=args.gamma)

for epoch in range(epochs):
train(args, model, device, train_loader, optimizer, epoch)
accuracy = test(model, device, test_loader, epoch)
scheduler.step()

return -accuracy


def train(args, model, device, train_loader, optimizer, epoch):
model.train()
Expand Down Expand Up @@ -141,6 +159,20 @@ def main():
if args.save_model:
torch.save(model.state_dict(), "mnist_cnn.pt")

def objective_wrapper(trial):
return objective(trial, device, args, train_loader, test_loader)

study = optuna.create_study(direction='maximize')
study.optimize(objective_wrapper, n_trials=100)

print('Best trial:')
trial = study.best_trial

print(f' Value: {-trial.value}')
print(f' Learning Rate: {trial.params["lr"]}')
print(f' Epochs: {trial.params["epochs"]}')



if __name__ == "__main__":
main()