Skip to content
Snippets Groups Projects
Commit 34d7c2df authored by Marco Kuhlmann's avatar Marco Kuhlmann
Browse files

Final cleanup

parent ff23dad8
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Basic text processing # Basic text processing
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The purpose of this notebook is to introduce you to some basic concepts and techniques for processing text data. The purpose of this notebook is to introduce you to some basic concepts and techniques for processing text data.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Dataset ## Dataset
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The dataset for this notebook is [WikiText](https://blog.einstein.ai/the-wikitext-long-term-dependency-language-modeling-dataset/), which is based on text extracted from ‘Good’ and ‘Featured’ articles on English Wikipedia. More specifically, we will be using the training portion of the WikiText-2 dataset. The dataset for this notebook is [WikiText](https://blog.einstein.ai/the-wikitext-long-term-dependency-language-modeling-dataset/), which is based on text extracted from ‘Good’ and ‘Featured’ articles on English Wikipedia. More specifically, we will be using the training portion of the WikiText-2 dataset.
We start by opening the file and store its contents in a list: We start by opening the file and store its contents in a list:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
with open('wiki.train.tokens', encoding='utf-8') as fp: with open('wiki.train.tokens', encoding='utf-8') as fp:
wikitext = fp.readlines() wikitext = fp.readlines()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Here is a sample line from the file: Here is a sample line from the file:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
wikitext[1234] wikitext[1234]
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
**🤔 Problem 1: A close look at the data** **🤔 Problem 1: A close look at the data**
> Take a close look at this line and compare it with the [original](https://en.wikipedia.org/wiki/The_Feast_of_the_Goat#Critical_reception). You will notice several differences between the two texts. What are your hypotheses as to how these differences come about? > Take a close look at this line and compare it with the [original](https://en.wikipedia.org/wiki/The_Feast_of_the_Goat#Critical_reception). You will notice several differences between the two texts. What are your hypotheses as to how these differences come about?
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Tokenization ## Tokenization
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Many tasks in natural language processing rely on first segmenting the raw text into words or word-like units. This task is called **tokenization**. Many tasks in natural language processing rely on first segmenting the raw text into words or word-like units. This task is called **tokenization**.
In English, words are often separated from each other by whitespace, but whitespace is neither necessary nor sufficient to define what constitutes a word. In our example, names such as *Latin America*, *Michael Wood*, and *London Review of Books* could arguably be treated as large words even though they contain spaces, while segments such as *day-to-day* need to be separated into several parts. In English, words are often separated from each other by whitespace, but whitespace is neither necessary nor sufficient to define what constitutes a word. In our example, names such as *Latin America*, *Michael Wood*, and *London Review of Books* could arguably be treated as large words even though they contain spaces, while segments such as *day-to-day* need to be separated into several parts.
In real applications, we would have to tokenize the raw text ourselves, or use some library such as [spaCy](https://spacy.io) or [Stanford CoreNLP](https://stanfordnlp.github.io/CoreNLP/). The WikiText dataset, however, is pre-tokenized, so that we can extract the tokens from a given line using a simple helper function that splits on whitespace: In real applications, we would have to tokenize the raw text ourselves, perhaps using some library such as [spaCy](https://spacy.io) or [Stanford CoreNLP](https://stanfordnlp.github.io/CoreNLP/). The WikiText dataset, however, is pre-tokenized, so that we can extract the tokens from a given line using a simple helper function that splits on whitespace:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def tokens(lines): def tokens(lines):
for line in lines: for line in lines:
for token in line.rstrip().split(): for token in line.rstrip().split():
yield token yield token
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
With this, we can count the number of tokens in the dataset: With this, we can count the number of tokens in the dataset:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
sum(1 for t in tokens(wikitext)) sum(1 for t in tokens(wikitext))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Vocabulary ## Vocabulary
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The term *word* is used ambiguously in natural language processing. It can either refer to a *word token*, a word as it occurs in the running text; or to a *word type*, a unique word. To illustrate the distinction: We are interested in *word tokens* when we want to count the length of a text, and in *word types* when we want to compile a dictionary of all words in the text material. The term *word* is used ambiguously in natural language processing. It can either refer to a *word token*, a word as it occurs in the running text; or to a *word type*, a unique word. To illustrate the distinction: We are interested in *word tokens* when we want to count the length of a text, and in *word types* when we want to compile a dictionary of all words in the text material.
The set of unique words in a dataset or task is called the **vocabulary**. The set of unique words in a dataset or task is called the **vocabulary**.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
vocab = set(tokens(wikitext)) vocab = set(tokens(wikitext))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Show the size of the vocabulary: Show the size of the vocabulary:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
len(vocab) len(vocab)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
**🤔 Problem 2: Missing words** **🤔 Problem 2: Missing words**
> When you compare this number to the corresponding number on the [WikiText website](https://blog.einstein.ai/the-wikitext-long-term-dependency-language-modeling-dataset/), you will notice a small difference. Try to find out what causes this difference, and how you can change the code so that you get the same number as on the website. > When you compare this number to the corresponding number on the [WikiText website](https://blog.einstein.ai/the-wikitext-long-term-dependency-language-modeling-dataset/), you will notice a small difference. Try to find out what causes this difference, and how you can change the code so that you get the same number as on the website.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
For some natural language processing tasks, tokenization is the only thing we need to do in order to compile the vocabulary. For other tasks, we may want to apply additional pre-processing, and/or include only the most frequent words into the vocabulary. For some natural language processing tasks, tokenization is the only thing we need to do in order to compile the vocabulary. For other tasks, we may want to apply additional pre-processing, and/or include only the most frequent words into the vocabulary.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
**🤔 Problem 3: Normalization** **🤔 Problem 3: Normalization**
> The term **text normalization** refers to all kinds of pre-processing that we apply to a text in order to bring it into a more standard, convenient form. Two standard normalization techniques are lowercasing and the filtering-out of non-alphabetical tokens. What effect do these techniques have on the size of the vocabulary? > The term **text normalization** refers to all kinds of pre-processing that we apply to a text in order to bring it into a more standard, convenient form. Two standard normalization techniques are lowercasing and the filtering-out of non-alphabetical tokens. What effect do these techniques have on the size of the vocabulary?
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## String-to-index mapping ## String-to-index mapping
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
One common task when processing text with neural networks is to encode words and other strings into contiguous ranges of integers, so that we can map them to the components of a vector. A standard pattern for doing this looks as follows: One common task when processing text with neural networks is to encode words and other strings into contiguous ranges of integers, so that we can map them to the components of a vector. A standard pattern for doing this looks as follows:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
w2i = {} w2i = {}
for token in tokens(wikitext): for token in tokens(wikitext):
if token not in w2i: if token not in w2i:
w2i[token] = len(w2i) w2i[token] = len(w2i)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Assuming that we already have a representation of the vocabulary, we can also use a different pattern: Assuming that we already have a representation of the vocabulary, we can also use a different pattern:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
w2i = {w: i for i, w in enumerate(sorted(vocab))} w2i = {w: i for i, w in enumerate(sorted(vocab))}
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
What index did the word *language* get? What index did the word *language* get?
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
w2i['language'] w2i['language']
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Sometimes we also need to invert the string-to-integer mapping: Sometimes we also need to invert the string-to-integer mapping:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
i2w = {i: s for s, i in w2i.items()} i2w = {i: s for s, i in w2i.items()}
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Decoding should be the inverse of encoding: Decoding should be the inverse of encoding:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
assert i2w[w2i['language']] == 'language' assert i2w[w2i['language']] == 'language'
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Power-law behaviour of language ## Power-law behaviour of language
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Language data often exhibits a power-law behaviour. We want to check whether this also applies to the WikiText data. In particular, we want to check whether the word frequencies follow the expected Zipfian pattern. We start by collecting the frequencies: Language data often exhibits a power-law behaviour. We want to check whether this also applies to the WikiText data. In particular, we want to check whether the word frequencies follow the expected Zipfian pattern. We start by collecting the frequencies:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from collections import Counter from collections import Counter
counter = Counter(tokens(wikitext)) counter = Counter(tokens(wikitext))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
How often does the word *language* occur in the data? How often does the word *language* occur in the data?
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
counter['language'] counter['language']
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Prepare `matplotlib` for plotting: Prepare `matplotlib` for plotting:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%matplotlib inline %matplotlib inline
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
%config InlineBackend.figure_format = 'retina' %config InlineBackend.figure_format = 'retina'
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Produce the rank/frequency plot for the 30,000 most common words: Produce the rank/frequency plot for the 30,000 most common words:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
words, counts = zip(*counter.most_common(30000)) words, counts = zip(*counter.most_common(30000))
plt.xlabel('Rank') plt.xlabel('Rank')
plt.yscale('log') plt.yscale('log')
plt.ylabel('Frequency') plt.ylabel('Frequency')
plt.plot(range(len(words)), counts) plt.plot(range(len(words)), counts)
plt.show() plt.show()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
**🤔 Problem 4: Visualize Heaps’ law** **🤔 Problem 4: Visualize Heaps’ law**
> Produce a plot that visualizes Heaps’ law for the WikiText dataset: on the $x$-axis, plot the text length; on the $y$-axis, plot the number of unique words at any given position in the text. How many new words, approximately, appear in the second half of the text? > Produce a plot that visualizes Heaps’ law for the WikiText dataset: on the $x$-axis, plot the text length; on the $y$-axis, plot the number of unique words encountered up to a given position in the text. How many new words, approximately, appear in the second half of the text?
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
That’s all, folks! That’s all, folks!
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Implementing logistic regression # Implementing logistic regression
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
In this notebook you will learn how to implement logistic regression in PyTorch and apply it to the task of sentiment analysis. In this notebook you will learn how to implement logistic regression in PyTorch, and apply it to the task of sentiment analysis.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Loading the data ## Loading the data
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Our dataset is derived from the [Stanford Sentiment Treebank](https://nlp.stanford.edu/sentiment/), which consists of 11,855 sentences extracted from movie reviews. Each sentence has been manually labelled with a rating between 0 (very negative) and 4 (very positive) towards the movie at hand. For this data, sentiment analysis can be framed as a multi-class classification problem. We have pre-processed the original data by tokenization, removing non-alphabetical tokens, and lowercasing. Our dataset is derived from the [Stanford Sentiment Treebank](https://nlp.stanford.edu/sentiment/), which consists of 11,855 sentences extracted from movie reviews. Each sentence has been manually labelled with a rating between 0 (very negative) and 4 (very positive) towards the movie at hand. For this data, sentiment analysis can be framed as a multi-class classification problem. We have pre-processed the original data by tokenization, removing non-alphabetical tokens, and lowercasing.
The following helper function loads the rating-labelled movie reviews from a tab-separated file. It returns two lists: one containing the tokenized reviews (lists of strings), and one containing the corresponding ratings (integers in the range 0–4). The following helper function loads the rating-labelled movie reviews from a tab-separated file. It returns a list of pairs where the first component of each pair is a tokenized review (represented a lists of string tokens) and the second component is the corresponding ratings (an integer in the range 0–4).
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def load_data(filename): def load_data(filename, max_length=20):
reviews, ratings = [], [] items = []
with open(filename, 'rt', encoding='utf-8') as fp: with open(filename, 'rt', encoding='utf-8') as fp:
for line in fp: for line in fp:
review, rating = line.rstrip().split('\t') review, rating = line.rstrip().split('\t')
reviews.append(review.split()) items.append((review.split()[:max_length], int(rating)))
ratings.append(int(rating)) return items
return reviews, ratings
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We use this function to load the training data and the validation data: We use this function to load the training data and the development data:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
train_reviews, train_ratings = load_data('sst-5-train.txt') train_data = load_data('sst-5-train.txt')
dev_reviews, dev_ratings = load_data('sst-5-dev.txt') dev_data = load_data('sst-5-dev.txt')
```
%% Cell type:markdown id: tags:
The next cell prints the number of examples in the training data:
%% Cell type:code id: tags:
len(train_reviews), len(dev_reviews) ``` python
print(len(train_data))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Vectorizing the data ## Vectorizing the data
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
To process the movie reviews using neural networks, we need to convert them into vectors of numerical values. A simple but common vector representation in natural language processing is the **bag-of-words**. Under this representation, we record *which* words occur in a review and *how often* they occur, but completely ignore their ordering. This is the representation that we will use in this notebook. To process the movie reviews using neural networks, we need to convert them into vectors of numerical values. A simple but common vector representation in natural language processing is the **bag-of-words**. Under this representation, we record *which* words occur in a review and *how often* they occur, but completely ignore their ordering. This is the representation that we will use in this notebook.
We first construct a mapping from words to vector indices. The domain of this mapping will be our vocabulary. We first construct a mapping from words to vector indices. The domain of this mapping will be our vocabulary.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def make_vocab(reviews): def make_vocab(data):
vocab = {} vocab = {}
for review in reviews: for review, rating in data:
for t in review: for t in review:
if t not in vocab: if t not in vocab:
vocab[t] = len(vocab) vocab[t] = len(vocab)
return vocab return vocab
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Running the cell below shows the size of our vocabulary: We create the vocabulary from the training data:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
vocab = make_vocab(train_reviews) vocab = make_vocab(train_data)
```
%% Cell type:markdown id: tags:
Running the cell below prints the size of the vocabulary:
len(vocab) %% Cell type:code id: tags:
``` python
print(len(vocab))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Next, we map each review to a vector that holds the counts for all the words in the vocabulary, and collect all sentence vectors into a PyTorch tensor. We do the same thing for all ratings, which results in a vector of integers. Next, we map each review to a vector that holds the counts for all the words in the vocabulary, and collect all sentence vectors into a PyTorch tensor. We do the same thing for all ratings, which results in a vector of integers.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import torch import torch
def vectorize(vocab, reviews, ratings): def vectorize(vocab, data):
xs = [] xs = []
for review, rating in zip(reviews, ratings): ys = []
for review, rating in data:
x = [0] * len(vocab) x = [0] * len(vocab)
for w in review: for w in review:
if w in vocab: if w in vocab:
x[vocab[w]] += 1 x[vocab[w]] += 1
xs.append(x) xs.append(x)
return torch.FloatTensor(xs), torch.LongTensor(ratings) ys.append(rating)
return torch.FloatTensor(xs), torch.LongTensor(ys)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We vectorize the training data: We vectorize the training data and the development data:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
train_x, train_y = vectorize(vocab, train_reviews, train_ratings) train_x, train_y = vectorize(vocab, train_data)
dev_x, dev_y = vectorize(vocab, dev_data)
train_x.size(), train_y.size()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We also vectorize the development data. The next cell prints the shapes of the resulting tensors:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dev_x, dev_y = vectorize(vocab, dev_reviews, dev_ratings) print('Training data:', train_x.size(), train_y.size())
print('Development data:', dev_x.size(), dev_y.size())
dev_x.size(), dev_y.size()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Evaluation ## Evaluation
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The standard evaluation measure for our dataset is **accuracy** – the percentage of reviews for which the classifier predicts the correct rating. The standard evaluation measure for our dataset is **accuracy** – the percentage of reviews for which the classifier predicts the correct rating.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def accuracy(y_pred, y): def accuracy(y_pred, y):
return torch.mean(torch.eq(y_pred, y).float()).item() return torch.mean(torch.eq(y_pred, y).float()).item()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
By always predicting the majority class in the training data (rating 3), we can get a validation accuracy of slightly above 25%. By always predicting the majority class in the training data (rating 3), we can get an accuracy on the development data of slightly above 25%.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
accuracy(torch.full_like(dev_y, 3), dev_y) accuracy(torch.full_like(dev_y, 3), dev_y)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Training the model ## Training the model
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We are now ready to set up a logistic regression model and train it using the cross-entropy loss function. Recall that a logistic regression model consists of a linear layer followed by the softmax function. In PyTorch, linear layers are implemented by the class [`nn.Linear`](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html), and cross-entropy loss is implemented by the function [`cross_entropy()`](https://pytorch.org/docs/stable/nn.functional.html#cross-entropy). The softmax function will be computed inside the loss function; see the code below. We are now ready to set up a logistic regression model and train it using the categorical cross-entropy loss function. Recall that a logistic regression model consists of a linear layer followed by the softmax function. In PyTorch, linear layers are implemented by the class [`nn.Linear`](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html), and cross-entropy loss is implemented by the function [`cross_entropy()`](https://pytorch.org/docs/stable/nn.functional.html#cross-entropy). The softmax function will be computed inside the loss function; see the note below.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
import torch.optim as optim import torch.optim as optim
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We will train our model using minibatch gradient descent. The following function splits the data into randomly sampled minibatches of the specified size. We will train our model using minibatch gradient descent. The following function splits the data into randomly sampled minibatches of the specified size.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def minibatches(x, y, batch_size): def minibatches(x, y, batch_size):
random_indices = torch.randperm(x.size(0)) random_indices = torch.randperm(x.size(0))
for i in range(0, x.size(0) - batch_size + 1, batch_size): for i in range(0, x.size(0) - batch_size + 1, batch_size):
batch_indices = random_indices[i:i+batch_size] batch_indices = random_indices[i:i+batch_size]
yield x[batch_indices], y[batch_indices] yield x[batch_indices], y[batch_indices]
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
With this we can now write our training loop: With this we can now write our training loop:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def train(vocab, x, y, n_epochs=40, batch_size=1068, lr=1e-4): def train(n_epochs=20, batch_size=48, lr=1e-1):
# Initialize the model # Initialize the model
model = nn.Linear(len(vocab), 5) model = nn.Linear(len(vocab), 5)
# Initialize the optimizer. Here we will use Adam instead of plain SGD. # Initialize the optimizer
optimizer = optim.Adam(model.parameters(), lr=lr) optimizer = optim.SGD(model.parameters(), lr=lr)
# We will train for n_epochs epochs # We train for several epochs
for t in range(n_epochs): for t in range(n_epochs):
# In each epoch, we loop over all the minibatches # In each epoch, we loop over all the minibatches
for bx, by in minibatches(x, y, batch_size): for bx, by in minibatches(train_x, train_y, batch_size):
# Reset the accumulated gradients # Reset the accumulated gradients
optimizer.zero_grad() optimizer.zero_grad()
# Forward pass # Forward pass
output = model.forward(bx) output = model.forward(bx)
# Compute the loss # Compute the loss
loss = F.cross_entropy(output, by) loss = F.cross_entropy(output, by)
# Backward pass; propagates the loss and computes the gradients # Backward pass; propagates the loss and computes the gradients
loss.backward() loss.backward()
# Update the parameters of the model # Update the parameters of the model
optimizer.step() optimizer.step()
return model return model
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
**⚠️ The softmax is implicit!** **⚠️ The softmax is implicit!**
One thing that you will note when you go through the code of the training loop is that there is no explicit call to the softmax function. The outputs of the network are not normalized probabilities but just scores (logits). The softmax is computed inside `cross_entropy()`. One thing that you will note when you go through the code of the training loop is that there is no explicit call to the softmax function. The outputs of the network are not normalized probabilities but just scores (logits). The softmax is computed inside `cross_entropy()`.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Here is an embellished version of the training loop that plots the per-epoch losses and the per-epoch accuracies on the validation data. Here is an embellished version of the training loop that plots the per-epoch losses and the per-epoch accuracies on the development data.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Same training loop with evaluation and plotting # Same training loop with evaluation and plotting
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
%matplotlib inline %matplotlib inline
%config InlineBackend.figure_format = 'retina' %config InlineBackend.figure_format = 'retina'
def train(vocab, x, y, n_epochs=40, batch_size=1068, lr=1e-4, validation_data=None): def train(n_epochs=20, batch_size=48, lr=1e-1):
model = nn.Linear(len(vocab), 5) model = nn.Linear(len(vocab), 5)
optimizer = optim.Adam(model.parameters(), lr=lr) optimizer = optim.SGD(model.parameters(), lr=lr)
losses = [] losses = []
valid_losses = [] dev_losses = []
valid_accuracies = [] dev_accuracies = []
if validation_data:
valid_x, valid_y = validation_data
for t in range(n_epochs): for t in range(n_epochs):
model.train() model.train()
running_loss = 0 running_loss = 0
for bx, by in minibatches(x, y, batch_size): for bx, by in minibatches(train_x, train_y, batch_size):
optimizer.zero_grad() optimizer.zero_grad()
output = model.forward(bx) output = model.forward(bx)
loss = F.cross_entropy(output, by) loss = F.cross_entropy(output, by)
loss.backward() loss.backward()
optimizer.step() optimizer.step()
running_loss += loss.item() * len(bx) running_loss += loss.item() * len(bx)
losses.append(running_loss / len(x)) losses.append(running_loss / len(train_x))
if validation_data: model.eval()
model.eval() with torch.no_grad():
with torch.no_grad(): dev_output = model.forward(dev_x)
valid_output = model.forward(valid_x) dev_loss = F.cross_entropy(dev_output, dev_y)
valid_y_pred = torch.argmax(valid_output, axis=1) dev_losses.append(dev_loss)
valid_loss = F.cross_entropy(valid_output, valid_y) dev_y_pred = torch.argmax(dev_output, axis=1)
valid_losses.append(valid_loss) dev_acc = accuracy(dev_y_pred, dev_y)
valid_acc = accuracy(valid_y_pred, valid_y) dev_accuracies.append(dev_acc)
valid_accuracies.append(valid_acc) print('\repoch {}, dev loss {:.4f}, dev acc {:.4f}'.format(t, dev_loss, dev_acc), end='')
print('\repoch {}, valid loss {:.4f}, valid acc {:.4f}'.format(t, valid_loss, valid_acc), end='')
print() print()
plt.figure(figsize=(15, 6)) plt.figure(figsize=(15, 6))
plt.subplot(121) plt.subplot(121)
plt.plot(losses) plt.plot(losses)
plt.plot(valid_losses) plt.plot(dev_losses)
plt.xlabel('Epoch') plt.xlabel('Epoch')
plt.ylabel('Average loss') plt.ylabel('Average loss')
plt.subplot(122) plt.subplot(122)
plt.plot(valid_accuracies) plt.plot(dev_accuracies)
plt.xlabel('Epoch') plt.xlabel('Epoch')
plt.ylabel('Validation set accuracy') plt.ylabel('Development set accuracy')
return model return model
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We are ready to train: We are ready to train:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
model = train(vocab, train_x, train_y, validation_data=(dev_x, dev_y)) train()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Using a GPU ## Using a GPU
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The data set that we used in this notebook is very small, and the model is very simple. For larger datasets and/or models, you may want to use a GPU, say on a service such as [Colab](http://colab.research.google.com). Fortunately, making a model ready for GPU training is rather straightforward in PyTorch: You only need to ‘send’ the model and its input to the correct device. The data set that we used in this notebook is very small, and the model is very simple. For larger datasets and/or models, you may want to use a GPU, say on a service such as [Colab](http://colab.research.google.com). Fortunately, making a model ready for GPU training is rather straightforward in PyTorch: You only need to ‘send’ the model and its input to the correct device.
The next code cell contains a GPU-enabled version of the (embellished) training loop. Modified lines are marked as `CHANGED`. The next code cell contains a GPU-enabled version of the (embellished) training loop. Modified lines are marked as `CHANGED`.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# GPU-enabled training loop # GPU-enabled training loop
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
%matplotlib inline %matplotlib inline
%config InlineBackend.figure_format = 'retina' %config InlineBackend.figure_format = 'retina'
# CHANGED: Set the device variable # CHANGED: Set the device variable
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def train(vocab, x, y, n_epochs=40, batch_size=1068, lr=1e-4, validation_data=None): def train(n_epochs=20, batch_size=48, lr=1e-1):
# CHANGED: Send the model to the available device # CHANGED: Send the model to the available device
model = nn.Linear(len(vocab), 10).to(device) model = nn.Linear(len(vocab), 5).to(device)
optimizer = optim.Adam(model.parameters(), lr=lr) optimizer = optim.SGD(model.parameters(), lr=lr)
losses = [] losses = []
valid_losses = [] dev_losses = []
valid_accuracies = [] dev_accuracies = []
if validation_data:
valid_x, valid_y = validation_data
# CHANGED: Send the validation data to the available device
valid_x = valid_x.to(device)
valid_y = valid_y.to(device)
for t in range(n_epochs): for t in range(n_epochs):
model.train() model.train()
running_loss = 0 running_loss = 0
for bx, by in minibatches(x, y, batch_size): for bx, by in minibatches(train_x, train_y, batch_size):
# CHANGED: Send the minibatches to the available device # CHANGED: Send the minibatches to the available device
bx = bx.to(device) bx = bx.to(device)
by = by.to(device) by = by.to(device)
optimizer.zero_grad() optimizer.zero_grad()
output = model.forward(bx) output = model.forward(bx)
loss = F.cross_entropy(output, by) loss = F.cross_entropy(output, by)
loss.backward() loss.backward()
optimizer.step() optimizer.step()
running_loss += loss.item() * len(bx) running_loss += loss.item() * len(bx)
losses.append(running_loss / len(x)) losses.append(running_loss / len(train_x))
if validation_data: model.eval()
model.eval() with torch.no_grad():
with torch.no_grad(): dev_output = model.forward(dev_x.to(device))
valid_output = model.forward(valid_x) dev_loss = F.cross_entropy(dev_output, dev_y.to(device))
valid_y_pred = torch.argmax(valid_output, axis=1) dev_losses.append(dev_loss)
valid_loss = F.cross_entropy(valid_output, valid_y) dev_y_pred = torch.argmax(dev_output, axis=1)
valid_losses.append(valid_loss) dev_acc = accuracy(dev_y, dev_y_pred)
valid_acc = accuracy(valid_y, valid_y_pred) dev_accuracies.append(dev_acc)
valid_accuracies.append(valid_acc) print('\repoch {}, dev loss {:.4f}, dev acc {:.4f}'.format(t, dev_loss, dev_acc), end='')
print('\repoch {}, valid loss {:.4f}, valid acc {:.4f}'.format(t, valid_loss, valid_acc), end='')
print() print()
plt.figure(figsize=(15, 6)) plt.figure(figsize=(15, 6))
plt.subplot(121) plt.subplot(121)
plt.plot(losses) plt.plot(losses)
plt.plot(valid_losses) plt.plot(dev_losses)
plt.xlabel('Epoch') plt.xlabel('Epoch')
plt.ylabel('Average loss') plt.ylabel('Average loss')
plt.subplot(122) plt.subplot(122)
plt.plot(valid_accuracies) plt.plot(dev_accuracies)
plt.xlabel('Epoch') plt.xlabel('Epoch')
plt.ylabel('Validation set accuracy') plt.ylabel('Development set accuracy')
return model return model
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can now train on the GPU. (For this very simple model however, the training times are almost identical.) We can now train on the GPU. (For this very simple model however, the training times are almost identical.)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
model = train(vocab, train_x, train_y, validation_data=(dev_x, dev_y)) model = train()
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
That’s all folks! That’s all folks!
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment