|
25 | 25 |
|
26 | 26 | Specifically, we'll train on a few thousand surnames from 18 languages
|
27 | 27 | of origin, and predict which language a name is from based on the
|
28 |
| -spelling: |
| 28 | +spelling. |
29 | 29 |
|
30 | 30 | Recommended Preparation
|
31 | 31 | =======================
|
|
79 | 79 | # line, mostly romanized (but we still need to convert from Unicode to
|
80 | 80 | # ASCII).
|
81 | 81 | #
|
82 |
| -# The first thing we need to define and clean our data. First off, we need to convert Unicode to plain ASCII to |
83 |
| -# limit the RNN input layers. This is accomplished by converting Unicode strings to ASCII and allowing a small set of allowed characters (allowed_characters) |
| 82 | +# The first step is to define and clean our data. Initially, we need to convert Unicode to plain ASCII to |
| 83 | +# limit the RNN input layers. This is accomplished by converting Unicode strings to ASCII and allowing only a small set of allowed characters. |
84 | 84 |
|
85 | 85 | import string
|
86 | 86 | import unicodedata
|
@@ -141,9 +141,9 @@ def lineToTensor(line):
|
141 | 141 | # Congratulations, you have built the foundational tensor objects for this learning task! You can use a similar approach
|
142 | 142 | # for other RNN tasks with text.
|
143 | 143 | #
|
144 |
| -# Next, we need to combine all our examples into a dataset so we can train, text and validate our models. For this, |
145 |
| -# we will use the `Dataset and DataLoader <https://pytorch.org/tutorials/beginner/basics/data_tutorial.html>` classes |
146 |
| -# to hold our dataset. Each Dataset needs to implement three functions: __init__, __len__, and __getitem__. |
| 144 | +# Next, we need to combine all our examples into a dataset so we can train, test and validate our models. For this, |
| 145 | +# we will use the `Dataset and DataLoader <https://pytorch.org/tutorials/beginner/basics/data_tutorial.html>`__ classes |
| 146 | +# to hold our dataset. Each Dataset needs to implement three functions: ``__init__``, ``__len__``, and ``__getitem__``. |
147 | 147 | from io import open
|
148 | 148 | import glob
|
149 | 149 | import os
|
@@ -194,7 +194,7 @@ def __getitem__(self, idx):
|
194 | 194 |
|
195 | 195 |
|
196 | 196 | #########################
|
197 |
| -#Here we can load our example data into the NamesDataset |
| 197 | +#Here we can load our example data into the ``NamesDataset`` |
198 | 198 |
|
199 | 199 | alldata = NamesDataset("data/names")
|
200 | 200 | print(f"loaded {len(alldata)} items of data")
|
@@ -286,8 +286,8 @@ def label_from_output(output, output_labels):
|
286 | 286 | #
|
287 | 287 | # We do this by defining a train() function which trains on a given dataset with minibatches. RNNs
|
288 | 288 | # train similar to other networks so for completeness we include a batched training method here.
|
289 |
| -# The loop (for i in batch) computes the losses for each of the items in the batch before adjusting the |
290 |
| -# weights. This is repeated until the number of epochs is reached. |
| 289 | +# The loop (``for i in batch``) computes the losses for each of the items in the batch before adjusting the |
| 290 | +# weights. This operation is repeated until the number of epochs is reached. |
291 | 291 |
|
292 | 292 | import random
|
293 | 293 | import numpy as np
|
@@ -338,7 +338,7 @@ def train(rnn, training_data, n_epoch = 10, n_batch_size = 64, report_every = 50
|
338 | 338 | return all_losses
|
339 | 339 |
|
340 | 340 | ##########################################################################
|
341 |
| -# We can now train a dataset with mini batches for a specified number of epochs |
| 341 | +# We can now train a dataset with minibatches for a specified number of epochs |
342 | 342 |
|
343 | 343 | start = time.time()
|
344 | 344 | all_losses = train(rnn, train_set, n_epoch=55, learning_rate=0.15, report_every=5)
|
@@ -425,9 +425,9 @@ def evaluate(rnn, testing_data, classes):
|
425 | 425 | #
|
426 | 426 | # - Get better results with a bigger and/or better shaped network
|
427 | 427 | #
|
428 |
| -# - Vary the hyperparameters to improve performance (e.g. change epochs, batch size, learning rate ) |
| 428 | +# - Adjust the hyperparameters to enhance performance, such as changing the number of epochs, batch size, and learning rate |
429 | 429 | # - Try the ``nn.LSTM`` and ``nn.GRU`` layers
|
430 |
| -# - Change the size of the layers (e.g. fewer or more hidden nodes, additional linear layers) |
| 430 | +# - Modify the size of the layers, such as increasing or decreasing the number of hidden nodes or adding additional linear layers |
431 | 431 | # - Combine multiple of these RNNs as a higher level network
|
432 | 432 | #
|
433 | 433 | # - Try with a different dataset of line -> label, for example:
|
|
0 commit comments