Skip to content

Instantly share code, notes, and snippets.

@dvgodoy
Last active May 18, 2020 20:46
Show Gist options
  • Save dvgodoy/01010718f1fbf9a2ad4946740711b266 to your computer and use it in GitHub Desktop.
Save dvgodoy/01010718f1fbf9a2ad4946740711b266 to your computer and use it in GitHub Desktop.
losses = []
val_losses = []
train_step = make_train_step(model, loss_fn, optimizer)
for epoch in range(n_epochs):
for x_batch, y_batch in train_loader:
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
loss = train_step(x_batch, y_batch)
losses.append(loss)
with torch.no_grad():
for x_val, y_val in val_loader:
x_val = x_val.to(device)
y_val = y_val.to(device)
model.eval()
yhat = model(x_val)
val_loss = loss_fn(y_val, yhat)
val_losses.append(val_loss.item())
print(model.state_dict())
@stas00
Copy link

stas00 commented Jan 13, 2020

a few improvements:

  1. model.eval() is best to be placed before: for x_val, y_val in val_loader - why re-run it in the loop?
  2. you're missing model.train(), so on epoch 2 and onward you will run in eval mode when you want train mode - i.e. no dropout, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment