Home > Mobile >  RuntimeError: Can't call numpy() on Tensor that requires grad
RuntimeError: Can't call numpy() on Tensor that requires grad

Time:09-17

I started studying machine learning, tensors. everything worked until I started displaying the graph. when I used a quadratic equation, I output it, and after using log, I started to output an error. For now, I'm new to python and will be glad of any help.

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.

CodePudding user response:

Try changing the function_parabola to:

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

The prod and log functions are in the numpy module.

  • Related