Skip to content

Instantly share code, notes, and snippets.

@cjauvin

cjauvin/torch.ipynb

Created Sep 12, 2018
Embed
What would you like to do?
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@prtos

This comment has been minimized.

Copy link

@prtos 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
You can’t perform that action at this time.