Skip to content

Having problems while following the video(section 108) #276

Answered by Watcher2747
Watcher2747 asked this question in Q&A
Discussion options

You must be logged in to vote

Pffft.

Found the issue, a pretty silly one, I somehow forgot to add this line of code:
loss = loss_fn(y_pred, y)

So when I was trying to do loss.backward() the value was not getting reset and just used the loss variable that was defined somewhere earlier in the code(outside of this function)

So the fix would be this:

 # 2. Accumulate the loss and acc(per batch)
    loss = loss_fn(y_pred, y)  # <---------------
    train_loss += loss
    train_acc += accuracy_fn(y_true=y,
                             y_pred=y_pred.argmax(dim=1))

    # 3. Optimizer zero grad
    optimizer.zero_grad()

    # 4. Loss backward
    loss.backward()

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mrdbourke
Comment options

Answer selected by Watcher2747
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants