MCPcopy Create free account
hub / github.com/pytorch/tutorials / train

Function train

intermediate_source/optimizer_step_in_backward_tutorial.py:46–56  ·  view source on GitHub ↗
(model, optimizer)

Source from the content-addressed store, hash-verified

44IMAGE_SIZE = 224
45
46def train(model, optimizer):
47 # create our fake image input: tensor shape is batch_size, channels, height, width
48 fake_image = torch.rand(1, 3, IMAGE_SIZE, IMAGE_SIZE).cuda()
49
50 # call our forward and backward
51 loss = model.forward(fake_image)
52 loss.sum().backward()
53
54 # optimizer update
55 optimizer.step()
56 optimizer.zero_grad()
57
58###############################################################################
59# Memory usage during training

Calls 3

stepMethod · 0.80
forwardMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected