Created
July 2, 2020 08:56
-
-
Save Adityanagraj/deb4e1d593dccacbbcd083bf680c7db3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evaluate(model, val_loader): | |
outputs = [model.validation_step(batch) for batch in val_loader] | |
return model.validation_epoch_end(outputs) | |
def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD): | |
history = [] | |
optimizer = opt_func(model.parameters(), lr) | |
for epoch in range(epochs): | |
# Training Phase | |
for batch in train_loader: | |
loss = model.training_step(batch) | |
loss.backward() | |
optimizer.step() | |
optimizer.zero_grad() | |
# Validation phase | |
result = evaluate(model, val_loader) | |
model.epoch_end(epoch, result, epochs) | |
history.append(result) | |
return history |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment