深度学习时候d2l报错和使用问题

目录

报错

解决:

使用:


解决AttributeError: module 'd2l.torch' has no attribute 'load_data_time_machine'_attributeerror: module 'torch' has no attribute 'l-CSDN博客文章浏览阅读5.5k次,点赞8次,收藏3次。文章描述了在使用D2L库时遇到的AttributeError,原因在于使用的d2l版本过高。解决方法是通过pip将d2l降级到0.17.5版本。https://blog.csdn.net/Rhett_Butler0922/article/details/137793716?fromshare=blogdetail&sharetype=blogdetail&sharerId=137793716&sharerefer=PC&sharesource=qq_52122048&sharefrom=from_link解决AttributeError: module 'd2l.torch' has no attribute 'load_data_time_machine'_attributeerror: module 'torch' has no attribute 'l-CSDN博客

报错

【d2l包】关于李沐《动手学深度学习》中的attributeerror: module 'd2l.torch' has no attribute 'train_ch3'问题_d2l.train_ch3报错-爱代码爱编程https://icode.best/i/554672373555637

【d2l包】关于李沐《动手学深度学习》中的attributeerror: module 'd2l.torch' has no attribute 'train_ch3'问题_d2l.train_ch3报错-爱代码爱编程

d2l相当于一个收集函数的包,只要在一个py文件中定义函数def func():时在冒号后加上了注释#@save,那就可以在另一个任意路径的py文件下以d2l.pytorch.func调用这个函数。

解决:

我的:

或者

复制代码
(E:\deeplearning202604) C:\Users\Lenovo>python -c "import d2l; print(d2l.__version__)"
1.0.3

(E:\deeplearning202604) C:\Users\Lenovo>
项目 信息
PyPI 包名 d2l(即 pip install d2l
稳定版本 0.17.6(官方配套书版)
最低推荐版本 0.17.5(低于此版缺失 d2l.Image等 API)
安装命令 pip install d2l==0.17.6
复制代码
(E:\deeplearning202604) C:\Users\Lenovo>python --version
Python 3.9.25

(E:\deeplearning202604) C:\Users\Lenovo>

(E:\deeplearning202604) C:\Users\Lenovo>python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
2.6.0+cu126
True

1. 只装 d2l 本体,不装任何依赖(torch、numpy 等都不动)否则会自动修改python和torch等版本

pip install d2l==0.17.5 --no-deps

python 复制代码
(E:\deeplearning202604) C:\Users\Lenovo>pip install d2l==0.17.5 --no-deps
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting d2l==0.17.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6a/be/eb453dbdd80d0563e2a6dac4aff8b76b0c24641d20b05e85b560f90282f9/d2l-0.17.5-py3-none-any.whl (82 kB)
Installing collected packages: d2l
  Attempting uninstall: d2l
    Found existing installation: d2l 1.0.3
    Uninstalling d2l-1.0.3:
      Successfully uninstalled d2l-1.0.3
Successfully installed d2l-0.17.5

(E:\deeplearning202604) C:\Users\Lenovo>python -c "import d2l; print(d2l.__version__)"
0.17.5

(E:\deeplearning202604) C:\Users\Lenovo>python -c "import torch; print(torch.__version__)"
2.6.0+cu126

使用:

自己也可以创建一个类似d2l这样子的模块

要点 说明
不要与 Python 内置名称冲突 比如别注册一个叫 list的函数,否则会把原生的 list覆盖掉。
模块热更新 如果在 Jupyter 里修改了 mylib.py,需要 import importlib; importlib.reload(mylib)才能生效。
跨项目共享 mylib.py放到项目根目录,或者做成 pip 包,就能到处用了。
相关推荐
深小乐2 小时前
我在 Anthropic ELI5 上加了5样东西,做成了更好用的 ELI5+
人工智能
东风破_3 小时前
《LangGraph Memory:为什么 Agent 需要 Checkpointer?》
人工智能
锋行天下3 小时前
LangGraph 同级节点之间修改数据先后问题
人工智能
u1301303 小时前
AI 日报(2026年9月12日)
人工智能
东风破_3 小时前
《LangGraph Human-in-the-loop:Interrupt 如何让 Agent 等待人工确认》
人工智能
2601_962218613 小时前
万象生鲜系统业财一体化底层打通技术自动生成经营账单
大数据·数据库·人工智能·python·算法
智塑未来3 小时前
中文短剧出海翻译工具横评:VMEG AI、鬼手、Vozo AI、ElevenLabs怎么选?
人工智能
东风破_3 小时前
《LangGraph 条件路由与循环:如何让 Agent 自己决定下一步?》
人工智能
我爱吃土豆13 小时前
AI 编程工具远程开发指南:Codex 桌面版与 WorkBuddy 通过 SSH 连接云服务器实践
服务器·人工智能·ssh
袁俪4 小时前
推理成本门槛
人工智能