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

Function train_loop

beginner_source/basics/optimization_tutorial.py:150–167  ·  view source on GitHub ↗
(dataloader, model, loss_fn, optimizer)

Source from the content-addressed store, hash-verified

148# evaluates the model's performance against our test data.
149
150def train_loop(dataloader, model, loss_fn, optimizer):
151 size = len(dataloader.dataset)
152 # Set the model to training mode - important for batch normalization and dropout layers
153 # Unnecessary in this situation but added for best practices
154 model.train()
155 for batch, (X, y) in enumerate(dataloader):
156 # Compute prediction and loss
157 pred = model(X)
158 loss = loss_fn(pred, y)
159
160 # Backpropagation
161 loss.backward()
162 optimizer.step()
163 optimizer.zero_grad()
164
165 if batch % 100 == 0:
166 loss, current = loss.item(), batch * batch_size + len(X)
167 print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
168
169
170def test_loop(dataloader, model, loss_fn):

Callers 1

Calls 4

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

Tested by

no test coverage detected