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

项目场景:

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


问题描述

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()
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ACxz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值