Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created Sep 21, 2020
Embed
What would you like to do?
# Eval algorithm
def evaluate(model, val_loader):
outputs = [model.validation_step(batch) for batch in val_loader]
return model.validation_epoch_end(outputs)
# Fitting algorithm
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
# Check the initial value that val_loss have
result = evaluate(model, val_loader)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment