[CVPR 2017] Learning a Deep Embedding Model for Zero-Shot Learning

code

需求1: 语义到视觉映射 ⇒ 视觉到语义映射


问题1:Can’t call numpy() on Variable that requires grad. Use var.detach().numpy() instead

分析:
kNNClassify的输入要求是numpy类型的
x_pred[i, :]是GPU上的Tensor

Traceback (most recent call last):
File “feat2attr.py”, line 119, in
print(compute_accuracy(test_x, test_att, test_att2label, test_x2label))
File “feat2attr.py”, line 24, in compute_accuracy
outputLabel = kNN.kNNClassify(x_pred[i, :], test_att, test_id, 1)
File “/home/yhr/DeepEmbeddingModel_ZSL-Pytorch/kNN.py”, line 30, in kNNClassify
diff = tile(newInput, (numSamples, 1)) - dataSet # Subtract element-wise
File “/home/yhr/anaconda2/envs/pytorch1.0/lib/python3.5/site-packages/numpy/lib/shape_base.py”, line 1147, in tile
c = _nx.array(A, copy=False, subok=True, ndmin=d)
File “/home/yhr/anaconda2/envs/pytorch1.0/lib/python3.5/site-packages/torch/tensor.py”, line 458, in array
return self.numpy()
RuntimeError: Can’t call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

解决方案:

x_pred[i, :].cpu().detach().numpy()
  1. cpu()将变量从GPU上转移到CPU上
  2. detach.numpy()将Tensor转成numpy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值