"# TP 2: Linear Algebra and Feedforward neural network\n",
"Master LiTL - 2022-2023\n",
"\n",
"## Requirements\n",
"In this section, we will go through some code to learn how to manipulate matrices and tensors, and we will take a look at some PyTorch code that allows to define, train and evaluate a simple neural network.\n",
"The modules used are the the same as in the previous session, *Numpy* and *Scikit*, with the addition of *PyTorch*. They are all already available within colab.\n",
"\n",
"## Part 1: Linear Algebra\n",
"\n",
"In this section, we will go through some python code to deal with matrices and also tensors, the data structures used in PyTorch.\n",
"\n",
"Sources: \n",
"* Linear Algebra explained in the context of deep learning: https://towardsdatascience.com/linear-algebra-explained-in-the-context-of-deep-learning-8fcb8fca1494\n",
"x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data\n",
"print(f\"From Random Tensor: \\n {x_rand} \\n\")"
],
"metadata": {
"id": "f1Ffpc6dwJ4l"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "oFDVEZcBCWF_"
},
"source": [
"### 1.2.2 Tensor attributes\n",
"\n",
"▶▶ **A tensor has different attributes, print the values for:**\n",
"* shape of the tensor\n",
"* type of the data stored\n",
"* device on which data are stored\n",
"\n",
"Look at the doc here: https://www.tensorflow.org/api_docs/python/tf/Tensor#shape"
]
},
{
"cell_type": "code",
"metadata": {
"id": "kS4TtR9DCJcq"
},
"source": [
"# Tensor attributes\n",
"tensor = torch.rand(3, 4)\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "tu8RM6O7CaKO"
},
"source": [
"### 1.2.3 Move to GPU\n",
"\n",
"The code below is used to:\n",
"* check on which device the code is running, 'cuda' stands for GPU. If not GPU is found that we use CPU.\n",
"\n",
"\n",
"▶▶ **Check and move to GPU:**\n",
"* Run the code, it should say 'no cpu'\n",
"* Move to GPU: in Colab, allocate a GPU by going to Edit > Notebook Settings (Modifier > Paramètres du notebook)\n",
" * you'll see an indicator of connexion in the uppper right part of the screen\n",
"* Run the code from 1.2 again and the cell below (you can use the function Run / Run before or Exécution / Exécuter avant), you'll need to do all the imports again. You see the difference?"
]
},
{
"cell_type": "code",
"metadata": {
"id": "nT7n30VpCOzF"
},
"source": [
"# We move our tensor to the GPU if available\n",
"if torch.cuda.is_available():\n",
" tensor = tensor.to('cuda')\n",
" print(f\"Device tensor is stored on: {tensor.device}\")\n",
"else:\n",
" print(\"no gpu\")\n",
"\n",
"print(tensor)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "VdqHVRkHCcgq"
},
"source": [
"Below, run after moving to GPU."
]
},
{
"cell_type": "code",
"metadata": {
"id": "nyZPKBvOGsyf"
},
"source": [
"# We move our tensor to the GPU if available\n",
"if torch.cuda.is_available():\n",
" tensor = tensor.to('cuda')\n",
" print(f\"Device tensor is stored on: {tensor.device}\")\n",
"# Define W: generate a random matrix (with e generator, for reproducible results)\n",
"# ...\n",
"\n",
"# Bias, a scalar\n",
"# ...\n",
"\n",
"# Now, try to multiply\n",
"# ..."
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "na_tJOnfGDIz"
},
"source": [
"### Last minor note"
]
},
{
"cell_type": "code",
"metadata": {
"id": "lql9bH39G4Mw"
},
"source": [
"## Operations that have a _ suffix are in-place. For example: x.copy_(y), x.t_(), will change x.\n",
"print(tensor, \"\\n\")\n",
"tensor.add_(5)\n",
"print(tensor)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "DGmy-dtuOtiw"
},
"source": [
"# Part 2: Feedforward Neural Network\n",
"\n",
"In this practical session, we will explore a simple neural network architecture for NLP applications ; specifically, we will train a feedforward neural network for sentiment analysis, using the same dataset of reviews as in the previous session. We will also keep the bag of words representation.\n",
"\n",
"\n",
"Sources:\n",
"* This TP is inspired by a TP by Tim van de Cruys\n",
"Count_Vectorizer returns sparse arrays (for computational reasons) but PyTorch will expect dense input:"
],
"metadata": {
"id": "PpeZ5ZtSVDSt"
}
},
{
"cell_type": "code",
"source": [
"# from sparse to dense\n",
"x_train = x_train.toarray()\n",
"x_dev = x_dev.toarray()\n",
"\n",
"print(\"Train:\", x_train.shape)\n",
"print(\"Dev:\", x_dev.shape)"
],
"metadata": {
"id": "PuE-xtwAVDeR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "6FzA7bOIZd2_"
},
"source": [
"#### 2.1.2 Transform to tensors\n",
"\n",
"▶▶ **Create a dataset object within the PyTorch library:**\n",
"\n",
"The easiest way to load datasets with PyTorch is to use the DataLoader class. Here we're going to give our numpy array to this class, and first, we need to transform our data to tensors. Follow the following steps:\n",
"* 1- **torch.from_numpy( A_NUMPY_ARRAY )**: transform your array into a tensor\n",
" * Note: you need to transform tensor type to float (for x), with **MY_TENSOR.to(torch.float)** (or cryptic error saying it was expecting long...).\n",
" * Print the shape of the tensor for your training data.\n",
"For this TP, we're going to walk through the code of a **simple feedforward neural network, with one hidden layer**.\n",
"\n",
"This network takes as input bag of words vectors, exactly as our 'classic' models: each review is represented by a vector of the size the number of tokens in the vocabulary with '1' when a word is present and '0' for the other words."
]
},
{
"cell_type": "markdown",
"source": [
"### 2.2.1 Questions\n",
"\n",
"▶▶ **What is the input dimension?**\n",
"\n",
"▶▶ **What is the output dimension?**"
],
"metadata": {
"id": "5KOM7ofrKUte"
}
},
{
"cell_type": "markdown",
"source": [
"### 2.2.2 Write the skeleton of the class\n",
"\n",
"▶▶ We're going to **define our own neural network type**, by defining a new class:\n",
"* The class is called **FeedforwardNeuralNetModel**\n",
"* it inherits from the class **nn.Module**\n",
"* the constructor takes the following arguments:\n",
" * size of the input (i.e. **input_dim**)\n",
" * size of the hidden layer (i.e. **hidden_dim**)\n",
" * size of the output layer (i.e. **output_dim**)\n",
"* in the constructor, we will call the constructor of the parent class\n",
"\n"
],
"metadata": {
"id": "bE4RgHUkGnGl"
}
},
{
"cell_type": "code",
"source": [
"# Start to define the class corresponding to our type of neural network\n",
"\n"
],
"metadata": {
"id": "uKcge-oBG1HV"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 2.2.3 Write the constructor\n",
"\n",
"▶▶ To continue the definition of our class, we need to explain how are built each layer of our network.\n",
"\n",
"More precisely, we're going to define a few fields:\n",
"* a function corresponding to the action of our hidden layer:\n",
" * what kind of function is it ?\n",
" * you need to indicate the size of the input and output for this function, what are they?\n",
"* a non linear function, that will be used on the ouput of our hidden layer\n",
"* a final output function:\n",
" * what kind of function is it ?\n",
" * you need to indicate the size of the input and output for this function, what are they?\n",
"\n",
"All the functions that can be used in Pytorch are defined here: https://pytorch.org/docs/stable/nn.functional.html\n",
"Hint: here you define fields of your class, these fields corresponding to specific kind of functions.\n",
"E.g. you're going to initialize a field such as **self.fc1=SPECIFIC_TYPE_OF_FCT(expected arguments)**."
],
"metadata": {
"id": "0BHUuGKCHoU9"
}
},
{
"cell_type": "code",
"source": [
"# Continue the definition of the class by defining three functions in your constructor\n",
"\n"
],
"metadata": {
"id": "LN3aSTSaJNkp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 2.2.4 Write the **forward** method\n",
"\n",
"The main function we have to write when defining a neural network is called the **forward** function.\n",
"This function computes the outputs of the network (the logit), it is thus used to train the network.\n",
"It details how we apply the functions defined in the constructor.\n",
"\n",
"Let's define this function, with the following signature, where x is the input to the network:\n",
"```\n",
"def forward(self, x):\n",
"```\n",
"\n",
"▶▶ Follow the steps:\n",
"* 1- Apply the first linear functiond defined in the constructor to **x**, i.e. go through the hidden layer.\n",
"* 2- Apply the non linear function to the output of step 1, i.e. use the activation function.\n",
"* 3- Apply the second linear function defined in the constructor to the output of step 2, i.e. go through the output layer.\n",
"* 4- Return the output of step 3.\n",
"\n",
"You're done!"
],
"metadata": {
"id": "e2IMSprgKJ7K"
}
},
{
"cell_type": "code",
"source": [
" # Copy paste the rest of the definition of the class below\n",
" # ...\n",
"\n",
" # Define the forward function, used to make all the calculations\n",
" # through the network\n",
" def forward(self, x):\n",
" ''' y = g(x.W1+b).W2 '''\n",
" # ..."
],
"metadata": {
"id": "8z-QpBt2NOlu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 2.3 Training the network\n",
"\n",
"Now we can use our beautiful class to define and then train our own neural network."
],
"metadata": {
"id": "sBrDXfQbO5yq"
}
},
{
"cell_type": "markdown",
"metadata": {
"id": "oWLDfLGxpBvn"
},
"source": [
"### 2.3.1 Hyper-parameters\n",
"\n",
"We need to set up the values for the hyper-parameters, and define the form of the loss and the optimization methods.\n",
"\n",
"▶▶ **Check that you understand what are each of the variables below**\n",
"* one that you probably don't know is the learning rate, we'll explain it in the next course. Broadly speaking, it corresponds to the amount of update used during training."
]
},
{
"cell_type": "code",
"metadata": {
"id": "fcGyjXbUoxx9"
},
"source": [
"# Many choices here!\n",
"VOCAB_SIZE = MAX_FEATURES\n",
"input_dim = VOCAB_SIZE\n",
"hidden_dim = 4\n",
"output_dim = 2\n",
"num_epochs = 5\n",
"learning_rate = 0.1"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 2.3.2 Loss function\n",
"\n",
"Another thing that has to be decided is the kind of loss function we want to use.\n",
"Here we use a common one, called CrossEntropy.\n",
"We will come back in more details on this loss.\n",
"One important note is that this function in PyTorch includes the SoftMax function that should be applied after the output layer to get labels."
],
"metadata": {
"id": "yyJINiVHPoWq"
}
},
{
"cell_type": "code",
"source": [
"criterion = nn.CrossEntropyLoss()"
],
"metadata": {
"id": "TVVy7hhrPl-K"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 2.3.3 Initialization of the model\n",
"\n",
"Now you can instantiate your class: define a model that is of the type FeedforwardNeuralNetModel using the values defined before as hyper-parameters."
],
"metadata": {
"id": "kyY91BtPQIeo"
}
},
{
"cell_type": "code",
"source": [
"# Initialization of the model\n",
"# ..."
],
"metadata": {
"id": "hk_nev2-Q0m-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 2.3.4 Optimizer\n",
"\n",
"At last, we need to indicate the method we want to use to optimize our network.\n",
"Here, we use a common one called Stochastic Gradient Descent.\n",
"We will also go back on that later on.\n",
"\n",
"Note that its arguments are:\n",
"* the parameters of our models (the Ws)\n",
"* the learning rate\n",
"\n",
"Based on these information, it can make the necessary updates.\n"
# TP 2: Linear Algebra and Feedforward neural network
Master LiTL - 2022-2023
## Requirements
In this section, we will go through some code to learn how to manipulate matrices and tensors, and we will take a look at some PyTorch code that allows to define, train and evaluate a simple neural network.
The modules used are the the same as in the previous session, *Numpy* and *Scikit*, with the addition of *PyTorch*. They are all already available within colab.
## Part 1: Linear Algebra
In this section, we will go through some python code to deal with matrices and also tensors, the data structures used in PyTorch.
Sources:
* Linear Algebra explained in the context of deep learning: https://towardsdatascience.com/linear-algebra-explained-in-the-context-of-deep-learning-8fcb8fca1494
shape = (2, 3,) # shape is a tuple of tensor dimensions
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")
```
%% Cell type:code id: tags:
```
## from another tensor
x_ones = torch.ones_like(x_data) # retains the properties of x_data
print(f"\nFrom Ones Tensor: \n {x_ones} \n")
x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data
print(f"From Random Tensor: \n {x_rand} \n")
```
%% Cell type:markdown id: tags:
### 1.2.2 Tensor attributes
▶▶ **A tensor has different attributes, print the values for:**
* shape of the tensor
* type of the data stored
* device on which data are stored
Look at the doc here: https://www.tensorflow.org/api_docs/python/tf/Tensor#shape
%% Cell type:code id: tags:
```
# Tensor attributes
tensor = torch.rand(3, 4)
```
%% Cell type:markdown id: tags:
### 1.2.3 Move to GPU
The code below is used to:
* check on which device the code is running, 'cuda' stands for GPU. If not GPU is found that we use CPU.
▶▶ **Check and move to GPU:**
* Run the code, it should say 'no cpu'
* Move to GPU: in Colab, allocate a GPU by going to Edit > Notebook Settings (Modifier > Paramètres du notebook)
* you'll see an indicator of connexion in the uppper right part of the screen
* Run the code from 1.2 again and the cell below (you can use the function Run / Run before or Exécution / Exécuter avant), you'll need to do all the imports again. You see the difference?
%% Cell type:code id: tags:
```
# We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to('cuda')
print(f"Device tensor is stored on: {tensor.device}")
else:
print("no gpu")
print(tensor)
```
%% Cell type:markdown id: tags:
Below, run after moving to GPU.
%% Cell type:code id: tags:
```
# We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to('cuda')
print(f"Device tensor is stored on: {tensor.device}")
else:
print("no gpu")
print(tensor)
```
%% Cell type:markdown id: tags:
### 1.2.4 Tensor operations
Doc: https://pytorch.org/docs/stable/torch.html
▶▶ **Slicing operations:**
* Below we use slicing operations to modify tensors
# Define W: generate a random matrix (with e generator, for reproducible results)
# ...
# Bias, a scalar
# ...
# Now, try to multiply
# ...
```
%% Cell type:markdown id: tags:
### Last minor note
%% Cell type:code id: tags:
```
## Operations that have a _ suffix are in-place. For example: x.copy_(y), x.t_(), will change x.
print(tensor, "\n")
tensor.add_(5)
print(tensor)
```
%% Cell type:markdown id: tags:
# Part 2: Feedforward Neural Network
In this practical session, we will explore a simple neural network architecture for NLP applications ; specifically, we will train a feedforward neural network for sentiment analysis, using the same dataset of reviews as in the previous session. We will also keep the bag of words representation.
Count_Vectorizer returns sparse arrays (for computational reasons) but PyTorch will expect dense input:
%% Cell type:code id: tags:
```
# from sparse to dense
x_train = x_train.toarray()
x_dev = x_dev.toarray()
print("Train:", x_train.shape)
print("Dev:", x_dev.shape)
```
%% Cell type:markdown id: tags:
#### 2.1.2 Transform to tensors
▶▶ **Create a dataset object within the PyTorch library:**
The easiest way to load datasets with PyTorch is to use the DataLoader class. Here we're going to give our numpy array to this class, and first, we need to transform our data to tensors. Follow the following steps:
%% Cell type:code id: tags:
```
# Useful imports
import torch
from torch.utils.data import TensorDataset, DataLoader
```
%% Cell type:markdown id: tags:
* 1- **torch.from_numpy( A_NUMPY_ARRAY )**: transform your array into a tensor
* Note: you need to transform tensor type to float (for x), with **MY_TENSOR.to(torch.float)** (or cryptic error saying it was expecting long...).
* Print the shape of the tensor for your training data.
For this TP, we're going to walk through the code of a **simple feedforward neural network, with one hidden layer**.
This network takes as input bag of words vectors, exactly as our 'classic' models: each review is represented by a vector of the size the number of tokens in the vocabulary with '1' when a word is present and '0' for the other words.
%% Cell type:markdown id: tags:
### 2.2.1 Questions
▶▶ **What is the input dimension?**
▶▶ **What is the output dimension?**
%% Cell type:markdown id: tags:
### 2.2.2 Write the skeleton of the class
▶▶ We're going to **define our own neural network type**, by defining a new class:
* The class is called **FeedforwardNeuralNetModel**
* it inherits from the class **nn.Module**
* the constructor takes the following arguments:
* size of the input (i.e. **input_dim**)
* size of the hidden layer (i.e. **hidden_dim**)
* size of the output layer (i.e. **output_dim**)
* in the constructor, we will call the constructor of the parent class
%% Cell type:code id: tags:
```
# Start to define the class corresponding to our type of neural network
```
%% Cell type:markdown id: tags:
### 2.2.3 Write the constructor
▶▶ To continue the definition of our class, we need to explain how are built each layer of our network.
More precisely, we're going to define a few fields:
* a function corresponding to the action of our hidden layer:
* what kind of function is it ?
* you need to indicate the size of the input and output for this function, what are they?
* a non linear function, that will be used on the ouput of our hidden layer
* a final output function:
* what kind of function is it ?
* you need to indicate the size of the input and output for this function, what are they?
All the functions that can be used in Pytorch are defined here: https://pytorch.org/docs/stable/nn.functional.html
Hint: here you define fields of your class, these fields corresponding to specific kind of functions.
E.g. you're going to initialize a field such as **self.fc1=SPECIFIC_TYPE_OF_FCT(expected arguments)**.
%% Cell type:code id: tags:
```
# Continue the definition of the class by defining three functions in your constructor
```
%% Cell type:markdown id: tags:
### 2.2.4 Write the **forward** method
The main function we have to write when defining a neural network is called the **forward** function.
This function computes the outputs of the network (the logit), it is thus used to train the network.
It details how we apply the functions defined in the constructor.
Let's define this function, with the following signature, where x is the input to the network:
```
def forward(self, x):
```
▶▶ Follow the steps:
* 1- Apply the first linear functiond defined in the constructor to **x**, i.e. go through the hidden layer.
* 2- Apply the non linear function to the output of step 1, i.e. use the activation function.
* 3- Apply the second linear function defined in the constructor to the output of step 2, i.e. go through the output layer.
* 4- Return the output of step 3.
You're done!
%% Cell type:code id: tags:
```
# Copy paste the rest of the definition of the class below
# ...
# Define the forward function, used to make all the calculations
# through the network
def forward(self, x):
''' y = g(x.W1+b).W2 '''
# ...
```
%% Cell type:markdown id: tags:
## 2.3 Training the network
Now we can use our beautiful class to define and then train our own neural network.
%% Cell type:markdown id: tags:
### 2.3.1 Hyper-parameters
We need to set up the values for the hyper-parameters, and define the form of the loss and the optimization methods.
▶▶ **Check that you understand what are each of the variables below**
* one that you probably don't know is the learning rate, we'll explain it in the next course. Broadly speaking, it corresponds to the amount of update used during training.
%% Cell type:code id: tags:
```
# Many choices here!
VOCAB_SIZE = MAX_FEATURES
input_dim = VOCAB_SIZE
hidden_dim = 4
output_dim = 2
num_epochs = 5
learning_rate = 0.1
```
%% Cell type:markdown id: tags:
### 2.3.2 Loss function
Another thing that has to be decided is the kind of loss function we want to use.
Here we use a common one, called CrossEntropy.
We will come back in more details on this loss.
One important note is that this function in PyTorch includes the SoftMax function that should be applied after the output layer to get labels.
%% Cell type:code id: tags:
```
criterion = nn.CrossEntropyLoss()
```
%% Cell type:markdown id: tags:
### 2.3.3 Initialization of the model
Now you can instantiate your class: define a model that is of the type FeedforwardNeuralNetModel using the values defined before as hyper-parameters.
%% Cell type:code id: tags:
```
# Initialization of the model
# ...
```
%% Cell type:markdown id: tags:
### 2.3.4 Optimizer
At last, we need to indicate the method we want to use to optimize our network.
Here, we use a common one called Stochastic Gradient Descent.
We will also go back on that later on.
Note that its arguments are:
* the parameters of our models (the Ws)
* the learning rate
Based on these information, it can make the necessary updates.