PyTorch 中的字典支持

发布于 2025-01-12 23:49:46 字数 316 浏览 0 评论 0原文

PyTorch 是否支持类似字典的对象,通过它们我们可以反向传播梯度,就像 PyTorch 中的张量一样?

我的目标是计算大矩阵中少数(1%)元素的梯度。但如果我使用 PyTorch 的标准张量来存储矩阵,我需要将整个矩阵保留在我的 GPU 中,这会由于训练期间可用的 GPU 内存有限而导致问题。所以我在想是否可以将矩阵存储为字典,仅索引矩阵的相关元素,并仅计算梯度并反向传播那些选择的元素。

到目前为止,我已尝试仅使用张量,但由于上述原因,它导致了内存问题。因此,我在 PyTorch 中广泛搜索了替代选项,例如 dicts,但在 Google 上找不到任何此类信息。

Does PyTorch support dict-like objects, through which we can backpropagate gradients, like Tensors in PyTorch?

My goal is to compute gradients with respect to a few (1%) elements of a large matrix. But if I use PyTorch's standard Tensors to store the matrix, I need to keep the whole matrix in my GPU, which causes problems due to limited GPU memory available during training. So I was thinking whether I could store the matrix as a dict instead, indexing only the relevant elements of the matrix, and computing gradients and backpropagating w.r.t those select elements only.

So far, I have tried using Tensors only, but it's causing memory issues for the above reasons. So I searched extensively for alternate options like dicts in PyTorch but couldn't find any such information on Google.

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

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

发布评论

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

评论(1

一个人的夜不怕黑 2025-01-19 23:49:46

It sounds like you want your parameter to be a torch.sparse tensor.
This interface allows you to have tensors that are mostly zeros, with only a few non-zero elements in known locations. Sparse tensors should allow you to significantly reduce the memory footprint of your model.
Note that this interface is still "under construction": not all operations are supported for sparse tensors. However, it is being constantly improving.

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