-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在本地上运动我该怎么导入d2lzh_pytorch #29
Comments
同问 |
我把这个包下了,在conda虚拟环境安装不成功。 |
不用安装(也安装不上),直接用就是,代码就在code文件夹里 |
我把utils.py下载下来。导入我的jupyter notebook中,在运行3.7代码的时候出现了bug,我的pytorch是1.3 |
报错信息贴出来看看 |
RuntimeError Traceback (most recent call last) ~\utils.py in train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr, optimizer) ~\utils.py in evaluate_accuracy(data_iter, net, device) D:\anaconda\envs\PyTorch\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs) D:\anaconda\envs\PyTorch\lib\site-packages\torch\nn\modules\container.py in forward(self, input) D:\anaconda\envs\PyTorch\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs) D:\anaconda\envs\PyTorch\lib\site-packages\torch\nn\modules\linear.py in forward(self, input) D:\anaconda\envs\PyTorch\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias) RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _th_addmm |
完整的代码 print(torch.version) batch_size = 256 num_inputs = 784 class LinearNet(nn.Module):def init(self, num_inputs, num_outputs):super(LinearNet, self).init()self.linear = nn.Linear(num_inputs, num_outputs)def forward(self, x): # x shape: (batch, 1, 28, 28)y = self.linear(x.view(x.shape[0], -1))return ynet = LinearNet(num_inputs, num_outputs)class FlattenLayer(nn.Module): from collections import OrderedDict init.normal_(net.linear.weight, mean=0, std=0.01) |
报错信息说得很清楚啦就是device不匹配,应该是你的机器有GPU,你把utils.py里面关于evaluate_accuracy的前几行改成以下应该就可以了: # ############################ 5.5 #########################
def evaluate_accuracy(data_iter, net, device=None):
if device is None:
device = list(net.parameters())[0].device # net的device
acc_sum, n = 0.0, 0 |
谢谢您,经过修改以后已经可以运行了 |
代码在实现多层感知机的时候出bug了,bug是由于修改处的代码引起的。 ~\utils.py in train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr, optimizer) ~\utils.py in evaluate_accuracy(data_iter, net, device) AttributeError: 'function' object has no attribute 'parameters' |
这里是因为传入net不是一个PyTorch模型而是我们自己写的一个函数,所以没有parameter这个是属性,所以我任务把utils里面的evaluate_accuracy的第一行 if device is None: 改成 if device is None and isinstance(net, torch.nn.Module): 应该就可以了。 |
我导入路径了为什么还不行呢? |
把里面的py文件下载下来直接用就行
…------------------ 原始邮件 ------------------
发件人: "Tengfei han"<notifications@github.com>;
发送时间: 2019年10月16日(星期三) 下午4:02
收件人: "ShusenTang/Dive-into-DL-PyTorch"<Dive-into-DL-PyTorch@noreply.github.com>;
抄送: "553270311"<553270311@qq.com>; "Author"<author@noreply.github.com>;
主题: Re: [ShusenTang/Dive-into-DL-PyTorch] 在本地上运动我该怎么导入d2lzh_pytorch (#29)
我导入路径了为什么还不行呢?
sys.path.append("/home/tengfeihan/Dive-into-DL-PyTorch/code/d2lzh_pytorch") 新手不太会 @ShusenTang
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
* save changes * 保存更改 * 增加说明 * 修正文档标题 * fix typo (#24) 1.补全括号 2.badbmm -> baddbmm * MathJax渲染BUG修复 * Update issue templates * Update issue templates * fix bug in d2l evaluate_accuracy * 修复偶现数学公式渲染问题,兼容性增强 * 微调_格式,增加markdown在web端docsify的兼容性,同时也方便突出_字面意义 * 同步https://github.com/d2l-ai/d2l-zh该文档公式展示,推导清晰多了 * w,b为float32在macbook终端下编译出错,报RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'mat2',修改成float64,更为兼容多数平台。 * fix bug: issue #29 #30 * 增加单个文档二级标题导航 * 增加单个文档内二三级导航动态合并到左边sidebar中,增加search,微调样式等 * 代码格式以及文辞微调
@alljie-cool 首先 |
求解
The text was updated successfully, but these errors were encountered: