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

Posted

技术标签:

【中文标题】RuntimeError:无法在需要 grad 的张量上调用 numpy()【英文标题】:RuntimeError: Can't call numpy() on Tensor that requires grad 【发布时间】:2021-11-11 07:56:35 【问题描述】:

我开始学习机器学习,张量。一切正常,直到我开始显示图表。当我使用二次方程时,我输出它,使用log后,我开始输出错误。目前,我是 python 新手,很高兴得到任何帮助。

import torch
import numpy as np
import matplotlib.pyplot as plt

x = torch.tensor([[5., 10.],
                  [1., 2.]], requires_grad=True)
var_history = []
fn_history = []
alpha = 0.001
optimizer = torch.optim.SGD([x], lr=alpha)

def function_parabola(variable):
    return (variable + 7).log().log().prod()


def make_gradient_step(function, variable):
    function_result = function(variable)
    function_result.backward()
    optimizer.step()
    optimizer.zero_grad()


for i in range(500):
    var_history.append(x.data.numpy().copy())
    fn_history.append(function_parabola(x).data.cpu().numpy().copy())
    make_gradient_step(function_parabola, x)
print(x)
def show_contours(objective,
                  x_lims=[-10.0, 10.0],
                  y_lims=[-10.0, 10.0],
                  x_ticks=100,
                  y_ticks=100):
    x_step = (x_lims[1] - x_lims[0]) / x_ticks
    y_step = (y_lims[1] - y_lims[0]) / y_ticks
    X, Y = np.mgrid[x_lims[0]:x_lims[1]:x_step, y_lims[0]:y_lims[1]:y_step]
    res = []
    for x_index in range(X.shape[0]):
        res.append([])
        for y_index in range(X.shape[1]):
            x_val = X[x_index, y_index]
            y_val = Y[x_index, y_index]
            res[-1].append(objective(np.array([[x_val, y_val]]).T))
    res = np.array(res)
    plt.figure(figsize=(7,7))
    plt.contour(X, Y, res, 100)
    plt.xlabel('$x_1$')
    plt.ylabel('$x_2$')
show_contours(function_parabola)
plt.scatter(np.array(var_history)[:,0], np.array(var_history)[:,1], s=10, c='r');
plt.show()
Traceback (most recent call last):
  File "C:\Users\KP\PycharmProjects\pythonProject\HomeWork\ClassWork.py", line 25, in <module>
    fn_history.append(function_parabola(x).data.cpu().detach().numpy().copy())
  File "C:\Users\KP\PycharmProjects\pythonProject\HomeWork\ClassWork.py", line 13, in function_parabola
    return np.prod(np.log(np.log(variable + 7)))
  File "C:\Users\KP\PycharmProjects\pythonProject\venv\lib\site-packages\torch\_tensor.py", line 643, in __array__
    return self.numpy()
RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

【问题讨论】:

【参考方案1】:

尝试将function_parabola 更改为:

def function_parabola(variable):
    return np.prod(np.log(np.log(variable + 7)))

prodlog 函数在 numpy 模块中。

【讨论】:

非常感谢您的回答,它解决了我的问题,但又出现了一个新问题。 RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead. @Альберт 导致错误的行是什么? ` 文件“C:\Users\KP\PycharmProjects\pythonProject\HomeWork\ClassWork.py”,第 25 行,在 fn_history.append(function_parabola(x).data.cpu() .nu​​mpy().copy()) 文件“C:\Users\KP\PycharmProjects\pythonProject\HomeWork\ClassWork.py”,第 13 行,在 function_parabola 返回 np.prod(np.log(np.log(variable + 7) )))` @Альберт 将fn_history.append(function_parabola(x).data.cpu().numpy().copy()) 替换为fn_history.append(function_parabola(x).data.cpu().detach().numpy().copy()) 很遗憾,这并没有解决问题。

以上是关于RuntimeError:无法在需要 grad 的张量上调用 numpy()的主要内容,如果未能解决你的问题,请参考以下文章

Pytorch RuntimeError:张量的元素 0 不需要 grad 并且没有 grad_fn

E-03RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

PyTorch 后向函数中发生 RuntimeError

(已解决)多卡训练时报错RuntimeError: grad can be implicitly created only for scalar outputs