求大家帮忙看看pytorch写的bp为啥梯度不更新?

博主使用PyTorch实现BP神经网络时遇到梯度不更新的问题,代码中已确认设备为CPU,但所有参数的grad_fn均为None。博主寻求帮助以解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pytorch的bp梯度不更新的问题

最近学习pytorch框架,写了个bp做练习。反向传递以后所有参数的grad_fn全部都是None。查一查大家遇到这个问题都是因为device不匹配。可是我只用cpu没用gpu。请大佬帮忙看看哪里出现了问题

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
def load_mnist():
    path = r'./dataset/mnist.npz' #放置mnist.py的目录。注意斜杠
    f = np.load(path)
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    f.close()
    return (x_train, y_train), (x_test, y_test)
(x_train, y_train), (x_test, y_test)=load_mnist()
x_train=torch.from_numpy(x_train).float()
x_train=x_train.reshape(-1, 784)
y_train=torch.from_numpy(y_train).float()
x_test=torch.from_numpy(x_test).float()
x_test=x_test.reshape(-1, 784)
y_test=torch.from_numpy(y_test).float()
y_train_onehot=torch.zeros(y_train.shape[0],10)
for i in range(y_train.shape[0]):
    y_train_onehot[i][int(y_train[i])]=1

input_layer=784
hidden_layer1=256
hidden_layer2=128
hidden_layer3=64
output_layer=10
batch_size=25
epoch=10
#搭建三层网络
w1=torch.rand(hidden_layer1,input_layer,requires_grad=True)
b1=torch.rand(hidden_layer1,requires_grad=True)
w2=torch.rand(hidden_layer2,hidden_layer1,requires_grad=True)
b2=torch.rand(hidden_layer2,requires_grad=True)
w3=torch.rand(hidden_layer3,hidden_layer2,requires_grad=True)
b3=torch.rand(hidden_layer3,requires_grad=True)
w4=torch.rand(output_layer,hidden_layer3,requires_grad=True)
b4=torch.rand(output_layer,requires_grad=True)
loss_func = nn.MSELoss()
optimizer = torch.optim.SGD([w1,w2,w3,w4,b1,b2,b3,b4], lr = 0.005)

def forward(input_x):
    x=input_x@(w1.t())+b1
    x=F.relu(x)
    x=x@(w2.t())+b2
    x=F.relu(x)
    x=x@(w3.t())+b3
    x=F.relu(x)
    x=x@(w4.t())+b4
    x=F.softmax(x,dim=1)
    return x

for j in range(epoch):
    for i in range(int(60000/25)):
        y_pred=forward(x_train[i*25:i*25+25])
        loss=loss_func(y_pred,y_train_onehot[i*25:i*25+25])
        #print(loss)
        optimizer.zero_grad()
        loss.backward()
        #print(w4.grad_fn)
        optimizer.step()
        #print(w1[0][0])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值