Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lessons/5-NLP/13-TextRep/TextRepresentationPyTorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions lessons/5-NLP/14-Embeddings/EmbeddingsPyTorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions lessons/5-NLP/14-Embeddings/torchnlp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import builtins
import torch
import torchtext
import torchtext.data.utils
import torchtext.datasets
import torchtext.vocab
import collections
import os

Expand Down
10 changes: 7 additions & 3 deletions lessons/5-NLP/15-LanguageModeling/CBoW-PyTorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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=['<unk>', '<pad>'])\n",
" vocab.set_default_index(vocab['<unk>'])\n",
" return train_dataset, test_dataset, classes, vocab, tokenizer"
],
"metadata": {
Expand Down Expand Up @@ -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)"
]
},
{
Expand Down Expand Up @@ -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')"
]
Expand Down
3 changes: 3 additions & 0 deletions lessons/5-NLP/16-RNN/torchnlp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import builtins
import torch
import torchtext
import torchtext.data.utils
import torchtext.datasets
import torchtext.vocab
import collections
import os

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
Expand Down
3 changes: 3 additions & 0 deletions lessons/5-NLP/17-GenerativeNetworks/torchnlp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import builtins
import torch
import torchtext
import torchtext.data.utils
import torchtext.datasets
import torchtext.vocab
import collections
import os

Expand Down
3 changes: 3 additions & 0 deletions lessons/5-NLP/18-Transformers/torchnlp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import builtins
import torch
import torchtext
import torchtext.data.utils
import torchtext.datasets
import torchtext.vocab
import collections
import os

Expand Down
24 changes: 17 additions & 7 deletions lessons/5-NLP/requirements-pytorch.txt
Original file line number Diff line number Diff line change
@@ -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
torchtext==0.18.0
torchvision==0.18.1
transformers==4.57.6