Dive into Deep Learning (d2l)
raw JSON → 1.0.3 verified Fri May 01 auth: no python
The d2l package (v1.0.3) provides utility functions and dataset loaders for the textbook 'Dive into Deep Learning' (d2l.ai). It includes common deep learning building blocks, data iterators, and plotting helpers to accompany the book. Release cadence is irregular, tied to book updates.
pip install d2l Common errors
error ModuleNotFoundError: No module named 'd2l.utils' ↓
cause d2l.utils was removed in v1.0.0; utils are now top-level or in submodules like d2l.data.
fix
Replace
from d2l.utils import something with from d2l import something (e.g., Animator, Accumulator). error AttributeError: module 'd2l' has no attribute 'torch' ↓
cause d2l.torch submodule removed; use `from d2l import torch` or upgrade to d2l>=1.0.0 where torch is re-exported at top level.
fix
Run
pip install --upgrade d2l and change import to from d2l import torch as d2l or import d2l. error FileNotFoundError: [Errno 2] No such file or directory: '/content/d2l-zh.zip' ↓
cause The deprecated d2l-zh package (Chinese version) is often confused with d2l. The wrong package downloaded a zip file instead of a proper Python module.
fix
Uninstall d2l-zh:
pip uninstall d2l-zh. Install the correct package: pip install d2l. Then use import d2l as described. Warnings
deprecated d2l.torch (submodule) is deprecated; use `from d2l import torch` or `import d2l` directly. The old import path `d2l.torch.nn` etc. may break in future versions. ↓
fix Replace `from d2l.torch import ...` with `from d2l import torch as d2l` or simply `import d2l` and use `d2l.xxx`.
breaking In d2l 1.0.0, many utility functions were moved to top-level (e.g., Animator, Accumulator, Timer) and removed from submodules like d2l.utils. Scripts using old paths will raise ImportError. ↓
fix Update imports: use `from d2l import Animator, Accumulator` instead of `from d2l.utils import Animator`.
gotcha The d2l package downloads datasets on first use from a server that may be slow or unreachable in certain regions. If dataset download hangs or fails, check network connectivity or manually download from the book's website. ↓
fix Set environment variable `D2L_DATA_DIR` to a local directory with pre-downloaded datasets. For offline use, manually copy datasets to ~/.d2l/data/.
deprecated The `d2l.mxnet` and `d2l.tensorflow` backends are deprecated and no longer maintained. Only PyTorch (torch) backend is actively developed. ↓
fix Use the torch backend: `from d2l import torch` or `import d2l` (defaults to torch). Avoid importing d2l.mxnet or d2l.tensorflow.
Imports
- d2l
import d2l - torch wrong
from d2l.torch import *correctfrom d2l import torch as d2l_torch - data wrong
from d2l.utils import datacorrectfrom d2l import data - Animator wrong
from d2l.plot import Animatorcorrectfrom d2l import Animator - Accumulator
from d2l import Accumulator
Quickstart
import torch
from d2l import torch as d2l
# Load a small dataset (Fashion-MNIST)
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=256)
# Check batch
for X, y in train_iter:
print(X.shape, y.shape)
break