Skip to content

Error When Running Automatic Differentiation with Autograd  #11

@epv44

Description

@epv44

When running the last two code blocks in the Autograd notebook I received the following error:

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

[<ipython-input-46-50b2a169b280>](https://localhost:8080/#) in <cell line: 25>()
     23 
     24 plt.subplot(1, 2, 1)
---> 25 plt.plot(epochs, losses)
     26 plt.xlabel("Epoch")
     27 plt.ylabel("Loss")

7 frames

[/usr/local/lib/python3.10/dist-packages/torch/_tensor.py](https://localhost:8080/#) in __array__(self, dtype)
    968             return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
    969         if dtype is None:
--> 970             return self.tensor.detach().numpy()
    971         else:
    972             return self.tensor.detach().numpy().astype(dtype, copy=False)

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

I was able to fix it by changing the line:

losses.append(loss)

to

losses.append(loss.detach())

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