MCPcopy Index your code
hub / github.com/pytorch/tutorials / train

Function train

beginner_source/basics/quickstart_tutorial.py:138–155  ·  view source on GitHub ↗
(dataloader, model, loss_fn, optimizer)

Source from the content-addressed store, hash-verified

136# backpropagates the prediction error to adjust the model's parameters.
137
138def train(dataloader, model, loss_fn, optimizer):
139 size = len(dataloader.dataset)
140 model.train()
141 for batch, (X, y) in enumerate(dataloader):
142 X, y = X.to(device), y.to(device)
143
144 # Compute prediction error
145 pred = model(X)
146 loss = loss_fn(pred, y)
147
148 # Backpropagation
149 loss.backward()
150 optimizer.step()
151 optimizer.zero_grad()
152
153 if batch % 100 == 0:
154 loss, current = loss.item(), (batch + 1) * len(X)
155 print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
156
157##############################################################################
158# We also check the model's performance against the test dataset to ensure it is learning.

Callers 1

Calls 4

loss_fnFunction · 0.85
stepMethod · 0.80
modelFunction · 0.50
backwardMethod · 0.45

Tested by

no test coverage detected