Deleting a branch is permanent. It CANNOT be undone. Continue?
Deleting a branch is permanent. It CANNOT be undone. Continue?
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》
问题所属任务编号(Task 01-05)
4.2.5. 训练
运行环境(操作系统版本、Python 版本)/ 非程序问题
windows 11,Python 3.10.7
完整的报错信息(截图或复制均可)/ 问题详细描述
代码:
import torch
from torch import nn
from d2l import torch as d2l
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
net = nn.Sequential(nn.Flatten(), nn.Linear(784, 10))
'''利用PyTorch中的 nn 库定义了一个简单的神经网络模型 net,
它由两层组成:Flatten 层将输入的 28 x 28 的图像展平成一维的张量,
然后接一个全连接层 Linear,输入维度为 784(展平后的图像大小),
输出维度为 10(表示分类的类别数量)'''
def init_weights(m):
if type(m) == nn.Linear:
nn.init.normal_(m.weight, std=0.01)
net.apply(init_weights);
loss = nn.CrossEntropyLoss(reduction='none')
'''nn.CrossEntropyLoss是一个PyTorch中用于计算交叉熵损失的函数,
其中reduction='none'表示不对每个batch的损失进行平均处理和累加,
而是返回一个张量,该张量包含每个样本的损失值'''
trainer = torch.optim.SGD(net.parameters(), lr=0.1)
'''使用随机梯度下降算法(SGD)来更新神经网络中的所有参数(即net.parameters()),学习率为0.1'''
num_epochs = 10
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
**
报错:**
RuntimeError Traceback (most recent call last)
Cell In [32], line 3
1 num_epochs, lr = 10, 0.1
2 updater = torch.optim.SGD(params, lr=lr)
----> 3 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\d2l\torch.py:318, in train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
315 animator = Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0.3, 0.9],
316 legend=['train loss', 'train acc', 'test acc'])
317 for epoch in range(num_epochs):
--> 318 train_metrics = train_epoch_ch3(net, train_iter, loss, updater)
319 test_acc = evaluate_accuracy(net, test_iter)
320 animator.add(epoch + 1, train_metrics + (test_acc,))
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\d2l\torch.py:257, in train_epoch_ch3(net, train_iter, loss, updater)
254 if isinstance(updater, torch.optim.Optimizer):
255 # Using PyTorch in-built optimizer & loss criterion
256 updater.zero_grad()
--> 257 l.backward()
258 updater.step()
259 metric.add(float(l) * len(y), accuracy(y_hat, y),
260 y.size().numel())
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\torch_tensor.py:396, in Tensor.backward(self, gradient, retain_graph, create_graph, inputs)
387 if has_torch_function_unary(self):
388 return handle_torch_function(
389 Tensor.backward,
390 (self,),
(...)
394 create_graph=create_graph,
395 inputs=inputs)
--> 396 torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\autograd_init_.py:166, in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
162 inputs = (inputs,) if isinstance(inputs, torch.Tensor) else
163 tuple(inputs) if inputs is not None else tuple()
165 grad_tensors_ = tensor_or_tensors_to_tuple(grad_tensors, len(tensors))
--> 166 grad_tensors = make_grads(tensors, grad_tensors, is_grads_batched=False)
167 if retain_graph is None:
168 retain_graph = create_graph
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\autograd_init_.py:67, in _make_grads(outputs, grads, is_grads_batched)
65 if out.requires_grad:
66 if out.numel() != 1:
---> 67 raise RuntimeError("grad can be implicitly created only for scalar outputs")
68 new_grads.append(torch.ones_like(out, memory_format=torch.preserve_format))
69 else:
RuntimeError: grad can be implicitly created only for scalar outputs
描述你期望看到的结果
怎么解决啊,将loss函数的reduction参数设置为"mean"或"sum"也不行
好奇怪,在我的环境里面可以平稳的运行,你要不退了重新再写一边试试?
或者是用这个命令,验证一下net可不可行
devices = d2l.try_all_gpus()
net.to(devices[0]);
X = torch.rand(size=(32, 3, 224, 224)).to(devices[0])
for layer in net:
X = layer(X)
print(layer.class.name,'output shape:\t', X.shape)