项目场景:

深度学习训练后,调用网络预测数据时报错


问题描述

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

原因分析:

待转换类型的PyTorch Tensor变量带有梯度,直接将其转换为numpy数据将破坏计算图,因此numpy拒绝进行数据转换,实际上这是对开发者的一种提醒。如果自己在转换数据时不需要保留梯度信息,可以在变量转换之前添加detach()调用。


解决方案:

y.numpy() ---> y.detach().numpy()

若是数据部署在GPU上时,则修改为

y.cpu().numpy() ---> y.cpu().detach().numpy()

例子如下:

import torch
import math
import matplotlib.pyplot as plt


class Fitting_polynomial(torch.nn.Module):
    def __init__(self):
        super(Fitting_polynomial, self).__init__()
        self.a = torch.nn.Parameter(torch.randn(()))
        self.b = torch.nn.Parameter(torch.randn(()))
        self.c = torch.nn.Parameter(torch.randn(()))
        self.d = torch.nn.Parameter(torch.randn(()))

    def forward(self, x):
        y = self.a + self.b * x + self.c * x ** 2 + self.d * x ** 3

        return y

    def string(self):
        """
        Just like any class in Python, you can also define custom method on PyTorch modules
        """
        return f'y = {self.a.item()} + {self.b.item()} x + {self.c.item()} x^2 + {self.d.item()} x^3'

    def plot_poly(self, x):
        fig = plt.figure(figsize=(14, 8))
        y = self.a + self.b * x + self.c * x ** 2 + self.d * x ** 3
        y = y
        plt.plot(x, y, label="fitting")
        plt.legend()


# Create Tensors to hold input and outputs.
x = torch.linspace(-math.pi, math.pi, 1000)
y = torch.sin(x)

# Construct our model by instantiating the class defined above
model = Fitting_polynomial()

criterion = torch.nn.MSELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=1e-8, momentum=0.9)

dynamic_y = []
for t in range(10000):

    y_pred = model(x)

    dynamic_y.append(y_pred.detach().numpy())
    loss = criterion(y_pred, y)
    if t % 2000 == 1999:
        print("epoch:{},mse:{}".format(t + 1, loss.item()))
        print(f'Result: {model.string()}')


    # Zero gradients, perform a backward pass, and update the weights.
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
model.plot_poly(x)



RuntimeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_9096\2077543339.py in <cell line: 59>()
57 loss.backward()
58 optimizer.step()
—> 59 model.plot_poly(x)
60

RuntimeError: Can’t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

def plot_poly(self, x):
    fig = plt.figure(figsize=(14, 8))
    y = self.a + self.b * x + self.c * x ** 2 + self.d * x ** 3
    y = y
    plt.plot(x, y, label="fitting")
    plt.legend()

修改为

def plot_poly(self, x):
    fig = plt.figure(figsize=(14, 8))
    y = self.a + self.b * x + self.c * x ** 2 + self.d * x ** 3
    y = y.detach().numpy()
    plt.plot(x, y, label="fitting")
    plt.legend()
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐