"the derivative for 'index' is not implemented"
huipengly opened this issue · comments
我想用pytorch1.0.1跑DenseFusion,就使用了这个库去替换本来带的knn。
我单独测试了您的代码是好用的。我替换了DenseFusion代码后报错,我觉得这个错误跟您的库应该没什么关系,但是我找不到解决办法。看了您的仓库,感觉可能会接触过DenseFusion,想咨询下这个错怎么解决。
"the derivative for 'index' is not implemented"
我比较了原来带的0.4.1的c和cuda实现和您的基本类似,我就是重新编译了so,并修改了__init__.py部分,其实只是修改了from...。我改后的部分代码如下。
from vision_sandbox import _C as knn_pytorch
class KNearestNeighbor(Function):
""" Compute k nearest neighbors for each query point.
"""
def __init__(self, k):
self.k = k
def forward(self, ref, query):
with torch.no_grad():
ref = ref.float().cuda()
query = query.float().cuda()
inds = torch.empty(query.shape[0], self.k, query.shape[2]).long().cuda()
knn_pytorch.knn(ref, query, inds)
return inds
报错的具体语句在loss.py里,我截取了一部分,报错的是下面的第二句
inds = knn(target.unsqueeze(0), pred.unsqueeze(0))
target = torch.index_select(target, 1, inds.view(-1) - 1)
我查到的一个无效的方案是将inds改为inds.detach()。来源:https://github.com/rusty1s/pytorch_sparse/issues/10
感谢您看完了我的问题。希望得到您的回复。
您好, 很巧, 我是把densefusion吸收到我的私有项目vision_sandbox时写的这个模块, 针对这个问题, 我的改动也是detach
inds = knn(target.unsqueeze(0), pred.unsqueeze(0))
# important : detach the index to avoid computing derivatives wrt index
target = torch.index_select(target, 1, inds.view(-1).detach() - 1)
程序运行和训练效果都是正常的, 不知道您说的无效具体指什么情况?
谢谢,按照上面的写法,已经解决了。我是看了我提到的方法里的修改,将detach加到了return的inds里。我这样修改还是继续报"the derivative for 'index' is not implemented"错误。
我的错误的解决方法:下面这个代码的最后一行。
def forward(self, ref, query):
with torch.no_grad():
ref = ref.float().cuda()
query = query.float().cuda()
inds = torch.empty(query.shape[0], self.k, query.shape[2]).long().cuda()
knn_pytorch.knn(ref, query, inds)
return inds.detach()
另外:请问您介意我修改一个支持pytorch 1.0.1版本的DenseFusion然后pr到原作者的仓库么?会备注您为作者。
或者您自己也有计划pr到原仓库?
不介意的, 没事情~