Skip to content

optimizer.zero_grad() before loss.backward()? #19

Open
@ale94mleon

Description

@ale94mleon

Hi, Excellent tutorials! But I have a question. Form tutorial 13 and on you change the place where the zero_grad method is called and I do not get why?
Before 13 was:

loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
optimizer.zero_grad()

After 13:

loss = criterion(outputs, labels)
optimizer.zero_grad() # Here is the change
loss.backward()
optimizer.step()

Now I am wondering if you set to zero the gradients, then, how the optimizer could update the parameters without any information about the gradient?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions