如何创建torch.tensor对象并仅更新其某些元素? [英] How to create torch.tensor object and to update only some of its elements?

查看:429
本文介绍了如何创建torch.tensor对象并仅更新其某些元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要创建大小为[2,3]的torch.tensor对象,其中填充了随机元素,并且打算在网络中使用此矩阵并优化其值.但是,我只想更新矩阵中的某些值.

Let's say I want to create torch.tensor object of size [2,3] filled with random elements, and I intend to use this matrix in the network and optimize it's values. However, I want to update only some of the the values in the matrix.

我知道可以通过将参数 requires_grad 设置为 True False 来完成张量.但是,以下代码

I know that it can be done for a tensor by setting up parameter requires_grad To True or False. However, the following code

z = torch.rand([2,3], requires_grad=True)
z[-1][-1].requires_grad=False

不能按预期工作

RuntimeError: you can only change requires_grad flags of leaf variables. If you want to use a computed variable in a subgraph that doesn't require differentiation use var_no_grad = var.detach().

如何修复此RuntimeError?如何初始化割炬张量,然后定义其中哪些元素具有 requires_grad = True ?

How to fix this RuntimeError? How to initialize torch tensor and then define which elements there would have requires_grad =True?

如果我以类似的方式编写代码:

If I write code in a similar manner:

z = torch.rand([2,3], requires_grad=False)
z[-1][-1].requires_grad=True

没有错误,但是require_grad也没有变化.

There will be no error, but no change of the requires_grad as well.

推荐答案

只有一个张量的 single 张量实际上并没有多大意义.条目.
为什么没有两个我们更新的单独张量( requires_grad = True )和另一个固定的( requires_grad = False )?然后,您可以合并它们以简化计算:

It does not really make much sense to have a single tensor which requires_grad for only part of its entries.
Why not have two separate tensors one that us updated (requires_grad=True) and another one fixed (requires_grad=False)? You can then merge them for computational ease:

fixed = torch.rand([2, 3], require_grad=False)
upd = torch.rand([2, 3], require_grad=True)
mask = torch.tensor([[0, 1, 0], [1, 0, 1]], require_grad=False)  # how to combine the two
# combine them using fixed "mask":
z = mask * fixed + (1-mask) * upd

除了使用二进制 mask 以外,显然还有其他组合 fixed upd 的方法.
例如,如果 upd 占据 z 的前两列,而 fixed 固定的其余两列,则:

You can obviously have other methods of combining fixed and upd other than using a binary mask.
For example, if upd occupies the first two columns of z and fixed the rest, then:

fixed = torch.rand([2, 1], require_grad=False)
upd = torch.rand([2, 2], require_grad=True)
# combine them using concatination
z = torch.cat((upd, fixed),dim=1)

或者,如果您知道索引

fidx = torch.tensor([0, 2], dtype=torch.long)
uidx = torch.tensor([1, 3, 4, 5], dtype=torch.long)
fixed = torch.rand([2,], require_grad=False)
upd = torch.rand([4,], require_grad=True)
z = torch.empty([2, 3])
z[fidx] = fixed 
z[uidx] = upd

这篇关于如何创建torch.tensor对象并仅更新其某些元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆