From 7e31f48bcd040d94ce92a0b815cd1816c9af0f55 Mon Sep 17 00:00:00 2001 From: Qiming Bao Date: Sat, 7 Nov 2026 03:05:13 +0000 Subject: [PATCH] Fix Lesson 5 NLP PyTorch track for torchtext 0.18 requirements-pytorch.txt was not installable: torch==2.13.0 was pinned next to torchaudio/torchvision/torchtext from the torch 1.8 line, which only ship wheels up to cp39, while nltk 3.10.4 needs Python >= 3.10. - Pin a consistent set for torchtext 0.18.0, the final (deprecated) release: torch 2.3.1, torchaudio 2.3.1, torchvision 0.18.1, torchdata 0.8.0 and portalocker (needed by torchtext.datasets), numpy<2, opencv-python 4.11.0.86 and transformers 4.57.6 (transformers 5.x requires torch >= 2.4). Document the Python 3.11/3.12 constraint. - torchtext 0.18 no longer exposes datasets/data/vocab as attributes of the package, so import the submodules explicitly in the four torchnlp.py copies and the notebooks that use them directly. - CBoW-PyTorch: replace the removed legacy Vocab(Counter, min_freq) constructor and vocab.itos with torchtext.vocab.vocab() and get_itos(), keeping / specials and as default index. --- .../TextRepresentationPyTorch.ipynb | 3 +++ .../14-Embeddings/EmbeddingsPyTorch.ipynb | 1 + lessons/5-NLP/14-Embeddings/torchnlp.py | 3 +++ .../15-LanguageModeling/CBoW-PyTorch.ipynb | 10 +++++--- lessons/5-NLP/16-RNN/torchnlp.py | 3 +++ .../GenerativePyTorch.ipynb | 1 + .../5-NLP/17-GenerativeNetworks/torchnlp.py | 3 +++ lessons/5-NLP/18-Transformers/torchnlp.py | 3 +++ lessons/5-NLP/requirements-pytorch.txt | 24 +++++++++++++------ 9 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lessons/5-NLP/13-TextRep/TextRepresentationPyTorch.ipynb b/lessons/5-NLP/13-TextRep/TextRepresentationPyTorch.ipynb index b19e529915..7129f03171 100644 --- a/lessons/5-NLP/13-TextRep/TextRepresentationPyTorch.ipynb +++ b/lessons/5-NLP/13-TextRep/TextRepresentationPyTorch.ipynb @@ -21,6 +21,9 @@ "source": [ "import torch\n", "import torchtext\n", + "import torchtext.data.utils\n", + "import torchtext.datasets\n", + "import torchtext.vocab\n", "import os\n", "import collections\n", "os.makedirs('./data',exist_ok=True)\n", diff --git a/lessons/5-NLP/14-Embeddings/EmbeddingsPyTorch.ipynb b/lessons/5-NLP/14-Embeddings/EmbeddingsPyTorch.ipynb index 43dff0a738..6fc6bcb33d 100644 --- a/lessons/5-NLP/14-Embeddings/EmbeddingsPyTorch.ipynb +++ b/lessons/5-NLP/14-Embeddings/EmbeddingsPyTorch.ipynb @@ -43,6 +43,7 @@ "source": [ "import torch\n", "import torchtext\n", + "import torchtext.vocab\n", "import numpy as np\n", "from torchnlp import *\n", "train_dataset, test_dataset, classes, vocab = load_dataset()\n", diff --git a/lessons/5-NLP/14-Embeddings/torchnlp.py b/lessons/5-NLP/14-Embeddings/torchnlp.py index 76870f561f..ad57afc5ac 100644 --- a/lessons/5-NLP/14-Embeddings/torchnlp.py +++ b/lessons/5-NLP/14-Embeddings/torchnlp.py @@ -1,6 +1,9 @@ import builtins import torch import torchtext +import torchtext.data.utils +import torchtext.datasets +import torchtext.vocab import collections import os diff --git a/lessons/5-NLP/15-LanguageModeling/CBoW-PyTorch.ipynb b/lessons/5-NLP/15-LanguageModeling/CBoW-PyTorch.ipynb index ea24e4dece..6a4c494df1 100644 --- a/lessons/5-NLP/15-LanguageModeling/CBoW-PyTorch.ipynb +++ b/lessons/5-NLP/15-LanguageModeling/CBoW-PyTorch.ipynb @@ -18,6 +18,9 @@ "source": [ "import torch\n", "import torchtext\n", + "import torchtext.data.utils\n", + "import torchtext.datasets\n", + "import torchtext.vocab\n", "import os\n", "import collections\n", "import builtins\n", @@ -66,7 +69,8 @@ " counter.update(torchtext.data.utils.ngrams_iterator(tokenizer(line),ngrams=ngrams))\n", " if i == lines_cnt:\n", " break\n", - " vocab = torchtext.vocab.Vocab(collections.Counter(dict(counter.most_common(vocab_size))), min_freq=min_freq)\n", + " vocab = torchtext.vocab.vocab(collections.OrderedDict(counter.most_common(vocab_size)), min_freq=min_freq, specials=['', ''])\n", + " vocab.set_default_index(vocab[''])\n", " return train_dataset, test_dataset, classes, vocab, tokenizer" ], "metadata": { @@ -385,7 +389,7 @@ }, "outputs": [], "source": [ - "vectors = torch.stack([embedder(torch.tensor(vocab[s])) for s in vocab.itos], 0)" + "vectors = torch.stack([embedder(torch.tensor(vocab[s])) for s in vocab.get_itos()], 0)" ] }, { @@ -460,7 +464,7 @@ "def close_words(x, n = 5):\n", " vec = embedder(torch.tensor(vocab[x]))\n", " top5 = np.linalg.norm(vectors.detach().numpy() - vec.detach().numpy(), axis = 1).argsort()[:n]\n", - " return [ vocab.itos[x] for x in top5 ]\n", + " return [ vocab.get_itos()[x] for x in top5 ]\n", "\n", "close_words('microsoft')" ] diff --git a/lessons/5-NLP/16-RNN/torchnlp.py b/lessons/5-NLP/16-RNN/torchnlp.py index 76870f561f..ad57afc5ac 100644 --- a/lessons/5-NLP/16-RNN/torchnlp.py +++ b/lessons/5-NLP/16-RNN/torchnlp.py @@ -1,6 +1,9 @@ import builtins import torch import torchtext +import torchtext.data.utils +import torchtext.datasets +import torchtext.vocab import collections import os diff --git a/lessons/5-NLP/17-GenerativeNetworks/GenerativePyTorch.ipynb b/lessons/5-NLP/17-GenerativeNetworks/GenerativePyTorch.ipynb index b9c62dc3c5..cc6109cc15 100644 --- a/lessons/5-NLP/17-GenerativeNetworks/GenerativePyTorch.ipynb +++ b/lessons/5-NLP/17-GenerativeNetworks/GenerativePyTorch.ipynb @@ -30,6 +30,7 @@ "source": [ "import torch\n", "import torchtext\n", + "import torchtext.vocab\n", "import numpy as np\n", "from torchnlp import *\n", "train_dataset,test_dataset,classes,vocab = load_dataset()" diff --git a/lessons/5-NLP/17-GenerativeNetworks/torchnlp.py b/lessons/5-NLP/17-GenerativeNetworks/torchnlp.py index 76870f561f..ad57afc5ac 100644 --- a/lessons/5-NLP/17-GenerativeNetworks/torchnlp.py +++ b/lessons/5-NLP/17-GenerativeNetworks/torchnlp.py @@ -1,6 +1,9 @@ import builtins import torch import torchtext +import torchtext.data.utils +import torchtext.datasets +import torchtext.vocab import collections import os diff --git a/lessons/5-NLP/18-Transformers/torchnlp.py b/lessons/5-NLP/18-Transformers/torchnlp.py index 76870f561f..ad57afc5ac 100644 --- a/lessons/5-NLP/18-Transformers/torchnlp.py +++ b/lessons/5-NLP/18-Transformers/torchnlp.py @@ -1,6 +1,9 @@ import builtins import torch import torchtext +import torchtext.data.utils +import torchtext.datasets +import torchtext.vocab import collections import os diff --git a/lessons/5-NLP/requirements-pytorch.txt b/lessons/5-NLP/requirements-pytorch.txt index f33780f397..20d86bb859 100644 --- a/lessons/5-NLP/requirements-pytorch.txt +++ b/lessons/5-NLP/requirements-pytorch.txt @@ -1,15 +1,25 @@ +# The PyTorch NLP notebooks use torchtext, which is deprecated and no longer +# developed. torchtext 0.18.0 is its final release. It is built against +# torch 2.3 and only ships wheels for Python 3.8-3.12, so use Python 3.11 or +# 3.12 (Pillow 13 needs Python >= 3.11). +# torch, torchaudio, torchdata, torchtext and torchvision must be upgraded together. +# torchtext.datasets needs torchdata < 0.10 (DataPipes) and portalocker. +# torch 2.3 is built against NumPy 1.x, hence numpy < 2. +# transformers 5.x requires torch >= 2.4, so stay on the transformers 4.x line. gensim>=4.3.0,<5 huggingface==0.0.1 matplotlib nltk==3.10.4 -numpy==1.22.0 -opencv-python==4.5.1.48 +numpy>=1.22,<2 +opencv-python==4.11.0.86 Pillow==13.0.0 +portalocker>=2.0.0 scikit-learn scipy -torch==2.13.0 -torchaudio==0.8.1 +torch==2.3.1 +torchaudio==2.3.1 +torchdata==0.8.0 torchinfo==0.0.8 -torchtext==0.9.1 -torchvision==0.9.1 -transformers==5.10.1 \ No newline at end of file +torchtext==0.18.0 +torchvision==0.18.1 +transformers==4.57.6