Skip to content

Instantly share code, notes, and snippets.

@cjauvin
Created September 12, 2018 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjauvin/97029e403574e787c26497e8c3860e42 to your computer and use it in GitHub Desktop.
Save cjauvin/97029e403574e787c26497e8c3860e42 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@prtos
Copy link

prtos commented Sep 13, 2018

1 - the error comes from the fact that you modify in place a tensor for which the gradient is required.
2 - You can just by using another tensor do the same thing, as demonstrated below:

import torch
x = torch.tensor([[1., 2.], [3., 4.]], requires_grad=True)
z = torch.zeros_like(x)
for i in range(2):
    for j in range(2):
        z[i, j] = x[i, j] ** 2
y = torch.mean(x)
print(y)
y.backward()
x.grad

tensor(2.5000)
Out[6]:
tensor([[ 0.2500, 0.2500],
[ 0.2500, 0.2500]])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment