小编cod*_*ino的帖子

无法在需要 grad 的张量上调用 numpy()

我有一个变量losses_all = []想要转换为 np.array。我尝试使用代码执行此操作并收到以下错误:

# convert to numpy array
losses = np.array(losses_all)

# ERROR MESSAGE
RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个,但出现以下错误:

# using code from error message above
losses=losses_all.detach().numpy()

# NEW ERROR MESSAGE
AttributeError: 'list' object has no attribute 'detach'
Run Code Online (Sandbox Code Playgroud)

还有其他方法可以将此变量转换为数组吗?

python numpy list tensor

0
推荐指数
1
解决办法
8097
查看次数

标签 统计

list ×1

numpy ×1

python ×1

tensor ×1