pytorch错误记录

import torch
import numpy as np
import random
import torch.utils.data as Data
import torch.nn as nn
from torch.nn import init
import torch.optim as optim

num_inputs = 3
num_examples = 2000
features = torch.normal(0, 1, (num_examples, num_inputs))
true_w = torch.tensor([1.0, 2.3, 4.5]).view(-1, 1)
true_b = 3.4
labels = torch.mm(features, true_w) + true_b  # 这样可以略过改形状的步骤
labels += torch.normal(0, 0.01, size=labels.size())

# print(y.shape)

dataset = Data.TensorDataset(features, labels)
# print(dataset[:10])
data_item = Data.DataLoader(dataset, 10, shuffle=True)
# for x,y in data_item:
#   print(x,y)

net = nn.Sequential(nn.Linear(3, 1))
# print(net[0])
# print(net[0].weight,net[0].bias)
init.normal_(net[0].weight, mean=0, std=0.01)
init.constant_(net[0].bias, 0)
# print(net[0].weight,net[0].bias)

y_hat = net(features)

loss = nn.MSELoss()

optimzer = optim.SGD(net.parameters(), lr=0.02)

empochs = 3

for i in range(empochs):
    for x, y in data_item:
     
        l = loss(labels, y_hat)
        optimzer.zero_grad()
        l.backward()
        optimzer.step()
    print('epoch %d ,loss: %f' % (i, l.item()))
print( net[0].weight,net[0].bias)
print(true_w,true_b)



报错:

Trying to backward through the graph a second time (or directly access saved variables after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved variables after calling backward.

修改方法:

把y_hat=net(features)改成y_hat=net(features)放在每一批次的循环里面。放在外面相当于每一次都是对同一个output进行backward()操作。

这样不会报语法上的错误,但实际上真正的错误并没有改正。

应该是y_hat=net(x)

l=loss(y_hat,y)

才能实现真正的小批量随机梯度下降。

参考博文:https://blog.csdn.net/axept/article/details/114370257

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值