Skip to content

Instantly share code, notes, and snippets.

@GitHubEmploy
Created December 30, 2021 21:08
Show Gist options
  • Save GitHubEmploy/a870195a4c7318ee64eb406b26fc706a to your computer and use it in GitHub Desktop.
Save GitHubEmploy/a870195a4c7318ee64eb406b26fc706a to your computer and use it in GitHub Desktop.
# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Move tensors to the configured device
images = images.to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
loss = loss_function(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment