可以修改r割炬中张量的毕业

发布于 2025-01-24 14:25:40 字数 634 浏览 2 评论 0原文

我正在尝试修改火炬中的张量的梯度,以r

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

但是,我会遇到以下错误:

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

我以为我认为我做错了什么(有时梯度有些棘手),但是当我在Python中尝试同一件事,它可以正常工作:

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

当我调用x $ grad在r中时,我得到[tensor(undefined)],但这不应该是问题由于在Python中效果很好。

谢谢。

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: `
lt;-.R7`(`*tmp*`, grad, value = <pointer: 0x55cd40aefae0>)
1: `
lt;-`(`*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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2025-01-31 14:25:40

当我在评论中指出时,这看起来像是一个错误。但是,作为解决方法,您可以做类似的事情:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文