Can't modify the grad of a tensor in R torch

Viewed 38

I am trying to modify the gradient of a tensor in torch for R:

library(torch)
x <- torch_randn(10,2)
x$grad <- torch_randn(10,2) 

However, I get the following error:

Erreur dans f(value) : argument inutilisé (value)
2: `$<-.R7`(`*tmp*`, grad, value = <pointer: 0x55cd40aefae0>)
1: `$<-`(`*tmp*`, grad, value = <pointer: 0x55cd40aefae0>)

I thought I was doing something wrong (gradients are a little bit tricky sometimes), but when I try the same thing in python it works just fine:

import torch
x = torch.randn(10,2)
x.grad = torch.randn(10,2)

When I call x$grad in R, I get [ Tensor (undefined) ], but this should not be a problem since it works fine in python.

Thanks.

1 Answers

This looks like a bug as I point in the comment. As a workaround, though, you can do something like:

library(torch)
x <- torch_tensor(1)
x$set_grad_(torch_tensor(1))
x$grad
Related