Giter VIP home page Giter VIP logo

Comments (19)

GSwithAI avatar GSwithAI commented on May 19, 2024

同问

from dive-into-dl-pytorch.

ShusenTang avatar ShusenTang commented on May 19, 2024

基本上每节都有例子呀,如下图
image
即用sys.path.append(PATH) 把你存放d2lzh_pytorch的路径加进来,然后就可以正常导入了

from dive-into-dl-pytorch.

GSwithAI avatar GSwithAI commented on May 19, 2024

基本上每节都有例子呀,如下图
image
即用sys.path.append(PATH) 把你存放d2lzh_pytorch的路径加进来,然后就可以正常导入了

d2lzh_pytorch这个包在哪里下载呢

from dive-into-dl-pytorch.

ShusenTang avatar ShusenTang commented on May 19, 2024

基本上每节都有例子呀,如下图
image
即用sys.path.append(PATH) 把你存放d2lzh_pytorch的路径加进来,然后就可以正常导入了

d2lzh_pytorch这个包在哪里下载呢

就在code文件夹里呀

from dive-into-dl-pytorch.

gggamemaster avatar gggamemaster commented on May 19, 2024

我把这个包下了,在conda虚拟环境安装不成功。

from dive-into-dl-pytorch.

ShusenTang avatar ShusenTang commented on May 19, 2024

我把这个包下了,在conda虚拟环境安装不成功。

不用安装(也安装不上),直接用就是,代码就在code文件夹里

from dive-into-dl-pytorch.

gggamemaster avatar gggamemaster commented on May 19, 2024

我把utils.py下载下来。导入我的jupyter notebook中,在运行3.7代码的时候出现了bug,我的pytorch是1.3

from dive-into-dl-pytorch.

ShusenTang avatar ShusenTang commented on May 19, 2024

我把utils.py下载下来。导入我的jupyter notebook中,在运行3.7代码的时候出现了bug,我的pytorch是1.3

报错信息贴出来看看

from dive-into-dl-pytorch.

gggamemaster avatar gggamemaster commented on May 19, 2024

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)

~\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))

~\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:\anaconda\envs\PyTorch\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:\anaconda\envs\PyTorch\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:\anaconda\envs\PyTorch\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:\anaconda\envs\PyTorch\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:\anaconda\envs\PyTorch\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

from dive-into-dl-pytorch.

gggamemaster avatar gggamemaster commented on May 19, 2024

完整的代码
import torch
from torch import nn
from torch.nn import init
import numpy as np
import sys
sys.path.append("..")
import utils as d2l

print(torch.version)

batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

num_inputs = 784
num_outputs = 10

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 y

net = LinearNet(num_inputs, num_outputs)

class FlattenLayer(nn.Module):
def init(self):
super(FlattenLayer, self).init()
def forward(self, x): # x shape: (batch, *, *, ...)
return x.view(x.shape[0], -1)

from collections import OrderedDict
net = nn.Sequential(
# FlattenLayer(),
# nn.Linear(num_inputs, num_outputs)
OrderedDict([
('flatten', FlattenLayer()),
('linear', nn.Linear(num_inputs, num_outputs))])
)

init.normal_(net.linear.weight, mean=0, std=0.01)
init.constant_(net.linear.bias, val=0)
loss = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.1)
num_epochs = 5
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer)

from dive-into-dl-pytorch.

ShusenTang avatar ShusenTang commented on May 19, 2024

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)

~\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))

~\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:\anaconda\envs\PyTorch\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:\anaconda\envs\PyTorch\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:\anaconda\envs\PyTorch\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:\anaconda\envs\PyTorch\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:\anaconda\envs\PyTorch\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

报错信息说得很清楚啦就是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

from dive-into-dl-pytorch.

gggamemaster avatar gggamemaster commented on May 19, 2024

谢谢您,经过修改以后已经可以运行了

from dive-into-dl-pytorch.

gggamemaster avatar gggamemaster commented on May 19, 2024

代码在实现多层感知机的时候出bug了,bug是由于修改处的代码引起的。
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs,batch_size, params, lr)
AttributeError Traceback (most recent call last)
in
----> 1 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs,batch_size, params, lr)

~\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))

~\utils.py in evaluate_accuracy(data_iter, net, device)
207 def evaluate_accuracy(data_iter, net, device=None):
208 if device is None:
--> 209 device = list(net.parameters())[0].device # net的device
210 acc_sum, n = 0.0, 0
211 with torch.no_grad():

AttributeError: 'function' object has no attribute 'parameters'

from dive-into-dl-pytorch.

ShusenTang avatar ShusenTang commented on May 19, 2024

代码在实现多层感知机的时候出bug了,bug是由于修改处的代码引起的。
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs,batch_size, params, lr)
AttributeError Traceback (most recent call last)
in
----> 1 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs,batch_size, params, lr)

~\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))

~\utils.py in evaluate_accuracy(data_iter, net, device)
207 def evaluate_accuracy(data_iter, net, device=None):
208 if device is None:
--> 209 device = list(net.parameters())[0].device # net的device
210 acc_sum, n = 0.0, 0
211 with torch.no_grad():

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):

应该就可以了。

from dive-into-dl-pytorch.

TengFeiHan0 avatar TengFeiHan0 commented on May 19, 2024

我导入路径了为什么还不行呢?
sys.path.append("/home/tengfeihan/Dive-into-DL-PyTorch/code/d2lzh_pytorch") 新手不太会 @ShusenTang

from dive-into-dl-pytorch.

gggamemaster avatar gggamemaster commented on May 19, 2024

from dive-into-dl-pytorch.

alljie-cool avatar alljie-cool commented on May 19, 2024

image

from dive-into-dl-pytorch.

ShusenTang avatar ShusenTang commented on May 19, 2024

@alljie-cool 首先sys.append.append("..")是为了把父目录加入工作区(因为d2l_pytorch在父目录),我不知道为什么在你这不行。我提供一个解决方案,因为d2l_pytorch所有代码都在d2l_pytorch/utils.y中,所以你可以把这个utils.py放入代码目录里,这样就可以在代码中import utils as d2l,遇到问题可以用python 导入py文件作关键词google

from dive-into-dl-pytorch.

DayBright-David avatar DayBright-David commented on May 19, 2024

截屏2020-05-13 下午1 41 56

请教:这个怎么解决啊

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.