Code Monkey home page Code Monkey logo

Comments (4)

flybiubiu avatar flybiubiu commented on August 25, 2024

bug描述

运行d2l.train_ch3()报错
报错位置:
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer)
报错信息:
RuntimeError Traceback (most recent call last)
in
1 num_epochs = 5
----> 2 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer)

D:\Users\Administrator\Anaconda3\Dive-into-DL-PyTorch-master\code\d2lzh_pytorch\utils.py in train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr, optimizer)
140 train_acc_sum += (y_hat.argmax(dim=1) == y).sum().item()
141 n += y.shape[0]
--> 142 test_acc = evaluate_accuracy(test_iter, net)
143 print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f'
144 % (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc))

D:\Users\Administrator\Anaconda3\Dive-into-DL-PyTorch-master\code\d2lzh_pytorch\utils.py in evaluate_accuracy(data_iter, net, device)
212 if isinstance(net, torch.nn.Module):
213 net.eval() # 评估模式, 这会关闭dropout
--> 214 acc_sum += (net(X.to(device)).argmax(dim=1) == y.to(device)).float().sum().cpu().item()
215 net.train() # 改回训练模式
216 else: # 自定义的模型, 3.13节之后不会用到, 不考虑GPU

d:\program files\python36\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
--> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)

d:\program files\python36\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
---> 92 input = module(input)
93 return input
94

d:\program files\python36\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
--> 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)

d:\program files\python36\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
85
86 def forward(self, input):
---> 87 return F.linear(input, self.weight, self.bias)
88
89 def extra_repr(self):

d:\program files\python36\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
1368 if input.dim() == 2 and bias is not None:
1369 # fused op is marginally faster
-> 1370 ret = torch.addmm(bias, input, weight.t())
1371 else:
1372 output = input.matmul(weight.t())

RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _th_addmm

版本信息
pytorch: 1.3.0
torchvision: 0.4.1
torchtext: 0.4.0
...
前面有手动写的函数,你可以查看下前面的,这个train是用gpu的,你可以把util里的注释掉用书本前面的手动写的

from dive-into-dl-pytorch.

ShusenTang avatar ShusenTang commented on August 25, 2024

这个问题在 #29 讨论过一次了,可以看看。

因为后面章节中修改了d2lzh_pytorch中的evaluate_accuracy函数,所以导致如果你机器带GPU的话运行该函数时会默认将所有数据搬到GPU上,但是你的net却是在CPU上的,所以就报错了,解决方案是将evaluate_accuracy的前几行改成

def evaluate_accuracy(data_iter, net, device=None):
    if device is None and isinstance(net, torch.nn.Module):
        # 如果没指定device就使用net的device
        device = list(net.parameters())[0].device
    acc_sum, n = 0.0, 0

这样当没指定device时就以net的device为准。

from dive-into-dl-pytorch.

ButuSun avatar ButuSun commented on August 25, 2024

您好,请问我出现了以下报错应该怎么解决呀---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[6], line 2
1 num_epochs = 10
----> 2 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

File ~/miniconda3/lib/python3.8/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 ~/miniconda3/lib/python3.8/site-packages/d2l/torch.py:259, in train_epoch_ch3(net, train_iter, loss, updater)
257 l.mean().backward()
258 updater.step()
--> 259 metric.add(float(l) * len(y), accuracy(y_hat, y),
260 y.size().numel())
261 else:
262 # Using custom built optimizer & loss criterion
263 l.sum().backward()

ValueError: only one element tensors can be converted to Python scalars

from dive-into-dl-pytorch.

wangzhongqing avatar wangzhongqing commented on August 25, 2024

from dive-into-dl-pytorch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.