- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我尝试索引叶变量以使用自定义收缩函数更新梯度时,我遇到了就地操作错误。我无法解决它。非常感谢任何帮助!
import torch.nn as nn
import torch
import numpy as np
from torch.autograd import Variable, Function
# hyper parameters
batch_size = 100 # batch size of images
ld = 0.2 # sparse penalty
lr = 0.1 # learning rate
x = Variable(torch.from_numpy(np.random.normal(0,1,(batch_size,10,10))), requires_grad=False) # original
# depends on size of the dictionary, number of atoms.
D = Variable(torch.from_numpy(np.random.normal(0,1,(500,10,10))), requires_grad=True)
# hx sparse representation
ht = Variable(torch.from_numpy(np.random.normal(0,1,(batch_size,500,1,1))), requires_grad=True)
# Dictionary loss function
loss = nn.MSELoss()
# customized shrink function to update gradient
shrink_ht = lambda x: torch.stack([torch.sign(i)*torch.max(torch.abs(i)-lr*ld,0)[0] for i in x])
### sparse reprsentation optimizer_ht single image.
optimizer_ht = torch.optim.SGD([ht], lr=lr, momentum=0.9) # optimizer for sparse representation
## update for the batch
for idx in range(len(x)):
optimizer_ht.zero_grad() # clear up gradients
loss_ht = 0.5*torch.norm((x[idx]-(D*ht[idx]).sum(dim=0)),p=2)**2
loss_ht.backward() # back propogation and calculate gradients
optimizer_ht.step() # update parameters with gradients
ht[idx] = shrink_ht(ht[idx]) # customized shrink function.
RuntimeError Traceback (most recent call last) in ()
15 loss_ht.backward() # back propogation and calculate gradients
16 optimizer_ht.step() # update parameters with gradients
—> 17 ht[idx] = shrink_ht(ht[idx]) # customized shrink function.
18
19
/home/miniconda3/lib/python3.6/site-packages/torch/autograd/variable.py in setitem(self, key, value)
85 return MaskedFill.apply(self, key, value, True)
86 else:
—> 87 return SetItem.apply(self, key, value)
88
89 def deepcopy(self, memo):
RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.
具体来说,下面这行代码似乎会出错,因为它同时索引和更新叶变量。
ht[idx] = shrink_ht(ht[idx]) # customized shrink function.
谢谢。
W.S.
最佳答案
我刚发现:为了更新变量,需要ht.data[idx]
而不是ht[idx]
。我们可以使用 .data
直接访问张量。
关于python - 如果用于梯度更新的索引叶变量如何解决就地操作错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49161652/
我正在尝试从底层叶进程写入命名管道并从顶层进程的管道读取。 为此,我首先在顶级进程中创建 FIFO,然后使用 for 循环派生更多进程。在 for 循环中,我正在检查叶子进程,如果它是叶子,我正在写入
我需要一个XPath表达式来验证XML列表元素是否只有某种类型的节点。 例: 我需要知道列表中是否只有图像。
我想从层次结构表中获取所有叶子及其根。叶子是最低层节点,根是最顶层节点。 给定一棵树: A --B --C --D E --F --G --H 叶子是节点:C, D根是:A、E 该表如下所
尝试从嵌套数组中删除相同的 sibling (叶子)/相同的数组。 例如 $data = [ 'test' => [ 'a' => [
我可以遍历目录并只打印文件夹/目录名称,但我想排除包含其他目录的目录的文件夹名称。出于某种原因,我称其为树结构中的“最终节点”,但我很可能是在自欺欺人,这不是第一次了。 =) 在审查其他答案列表时,也
我现在经常使用 Folium,在 Python 中拥有如此易于使用的东西真的很棒。但是他们的文档严重落后,我理解。所以我有2个问题。 我在尝试获得更多标记颜色时做错了什么?这是我尝试过的:map.si
我正在尝试使用 ape 包中的 plot.phylo 命令为 R 中系统发育类型图的边缘(线)添加颜色。这个例子是一个“粉丝”类型的图,虽然我希望这个方法与“系统图类型”或其他任何东西相同。 libr
我正在尝试呈现一个 JTree 叶以具有包含可编辑 JTextArea 的 JPanel,以便用户可以输入文本并能够在添加文本时向下滚动, 并且还使用包装器来限制文本的宽度,使其向下延伸以留出更多空间
这是要修改的正确 Linux 内核代码吗?我如何进行更改以模拟 CPUID 代码以及我需要更改哪个函数。谢谢 #include #include #include #include #incl
我是一名优秀的程序员,十分优秀!