Kernel: Python 3 (ipykernel)
Machine Learning with PyTorch and Scikit-Learn
-- Code Examples
Package version checks
Add folder to path in order to load from the check_packages.py script:
In [1]:
Check recommended package versions:
In [2]:
Out[2]:
[OK] Your Python version is 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:24:02)
[Clang 11.1.0 ]
[OK] torch 1.10.2
Chapter 15: Modeling Sequential Data Using Recurrent Neural Networks (part 3/3)
Outline
Implementing RNNs for sequence modeling in PyTorch
Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s).
In [3]:
Project two: character-level language modeling in PyTorch
In [4]:
Out[4]:
Preprocessing the dataset
In [5]:
Out[5]:
Total Length: 1112350
Unique Characters: 80
In [6]:
Out[6]:
In [7]:
Out[7]:
Text encoded shape: (1112350,)
THE MYSTERIOUS == Encoding ==> [44 32 29 1 37 48 43 44 29 42 33 39 45 43 1]
[33 43 36 25 38 28] == Reverse ==> ISLAND
In [8]:
Out[8]:
44 -> T
32 -> H
29 -> E
1 ->
37 -> M
In [9]:
Out[9]:
In [10]:
Out[10]:
In [11]:
Out[11]:
[44 32 29 1 37 48 43 44 29 42 33 39 45 43 1 33 43 36 25 38 28 1 6 6
6 0 0 0 0 0 40 67 64 53 70 52 54 53 1 51] -> 74
'THE MYSTERIOUS ISLAND ***\n\n\n\n\nProduced b' -> 'y'
In [12]:
Out[12]:
/var/folders/jg/tpqyh1fd5js5wsr1d138k3n40000gn/T/ipykernel_44396/2527503007.py:15: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at /Users/runner/miniforge3/conda-bld/pytorch-recipe_1645462109041/work/torch/csrc/utils/tensor_new.cpp:201.)
seq_dataset = TextDataset(torch.tensor(text_chunks))
In [13]:
Out[13]:
Input (x): 'THE MYSTERIOUS ISLAND ***\n\n\n\n\nProduced b'
Target (y): 'HE MYSTERIOUS ISLAND ***\n\n\n\n\nProduced by'
Input (x): 'HE MYSTERIOUS ISLAND ***\n\n\n\n\nProduced by'
Target (y): 'E MYSTERIOUS ISLAND ***\n\n\n\n\nProduced by '
In [14]:
In [15]:
Building a character-level RNN model
In [16]:
Out[16]:
RNN(
(embedding): Embedding(80, 256)
(rnn): LSTM(256, 512, batch_first=True)
(fc): Linear(in_features=512, out_features=80, bias=True)
)
In [17]:
Out[17]:
Epoch 0 loss: 4.3719
Epoch 500 loss: 1.3804
Epoch 1000 loss: 1.2956
Epoch 1500 loss: 1.2816
Epoch 2000 loss: 1.1968
Epoch 2500 loss: 1.2456
Epoch 3000 loss: 1.1763
Epoch 3500 loss: 1.1868
Epoch 4000 loss: 1.1476
Epoch 4500 loss: 1.2118
Epoch 5000 loss: 1.2072
Epoch 5500 loss: 1.1243
Epoch 6000 loss: 1.1637
Epoch 6500 loss: 1.1513
Epoch 7000 loss: 1.1196
Epoch 7500 loss: 1.1439
Epoch 8000 loss: 1.1536
Epoch 8500 loss: 1.1263
Epoch 9000 loss: 1.1597
Epoch 9500 loss: 1.1048
Evaluation phase: generating new text passages
In [18]:
Out[18]:
Probabilities: [0.33333334 0.33333334 0.33333334]
[[0]
[0]
[0]
[0]
[1]
[0]
[1]
[2]
[1]
[1]]
In [19]:
Out[19]:
Probabilities: [0.10650698 0.10650698 0.78698605]
[[0]
[2]
[2]
[1]
[2]
[1]
[2]
[2]
[2]
[2]]
In [20]:
Out[20]:
The island had been neged to reward from them with denies he giving these gigant of some taking two years from the question has been employed without
consequences in his depth. As
to the notice beate can be vapor when there we must forgot.
“Shat’s likely,” said Pencroft.
“What a
region of Neption!” Cyrus Harding would have
been pushed their return to the shored him, to do in the dockyar and formation of the settlers’ animal which would be better dead of freshest.
Cyrus, was well presented by means of t
Predictability vs. randomness
In [21]:
Out[21]:
Probabilities before scaling: [0.10650698 0.10650698 0.78698605]
Probabilities after scaling with 0.5: [0.21194156 0.21194156 0.57611686]
Probabilities after scaling with 0.1: [0.3104238 0.3104238 0.37915248]
In [22]:
Out[22]:
The island on the shore, and thought the island was finished, and the stranger had constituted to this precipitation of the corral, which he suffered in the depths of the cone of a ship was only a long time was a sort of the passage there to the sea, he was ready to
do anything to the south and to the shore, the settlers did not think the convicts were to be on the corral, and the engineer then to hope of the settlers, and shelter at the corral, the colonists had not happened to the settlers had only to t
In [23]:
Out[23]:
The island deep ising side; his”not
awoke-prtaugh? Gideon Spilett too?,”
Helofe, and ihos eyes; “re” helm usn’t produtiuso,
at Tajow him, somucn!”
Above Verishy, silene;
ote fat wire, notwiths, and
bank pludes
attackleep, plaions fraitr byll,
you ironaliers thefee
groat, however
parcht mad. Would wideney twontashmonest.
A fugitors plan ascererver; Thousand-shwark orhrankheaded abope had
let us alwayberrusible, withwh.
Blacaneara,moss, Net susnid! 6
I- Poingin’s new kay was “Muldhidge?”
-!
“It phincip
...
Summary
...
Readers may ignore the next cell.
In [24]:
Out[24]:
[NbConvertApp] Converting notebook ch15_part3.ipynb to script
[NbConvertApp] Writing 7922 bytes to ch15_part3.py