--- title: 'Lab #8: Neural networks' output: html_document: df_print: paged --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) set.seed(24) ``` This lab is closely modeled after the neural network tutorial presented in the Medium post by the PyTorch team: https://medium.com/pytorch/please-allow-me-to-introduce-myself-torch-for-r-9ea0f361ea7e. 1. Install and load the torch package. ```{r installTorch} # install.packages("torch") library(torch) ``` 2. Check that you can make a 1-D tensor with value 1 using the command `torch_tensor(1)`. ```{r} ``` 3. Install and load the `torchvision` package as well. ```{r installTorchVision} # install.packages("torchvision") library(torchvision) ``` 4. Download the mnist dataset by running the following code. The mnist dataset is composed of black and white images of digits 0-9, where each image is 28x28 pixels. Each observation is labeled with the true label. The digits are evenly distributed in both the training and testing datasets. ```{r mnist} train_ds <- mnist_dataset( ".", download = FALSE, train = TRUE, transform = transform_to_tensor ) test_ds <- mnist_dataset( ".", download = FALSE, train = FALSE, transform = transform_to_tensor ) ``` 5. How many train and test observations are there? We'll only use the first 3200 observations in each dataset. Subset the data using the `dataset_subset` function. ```{r} ``` 6. You can access the first observation in the training dataset using the typical numeric indexing: `train_ds[1]`. What is the dimension of the first observation? What is the label of this first observation? ```{r} ``` 7. To train a model in torch, you need to first wrap the subsetted data within a `dataloader`. Do this using the following commands. ```{r} ``` 8. You can access the data in the data loader using its `iter` attribute. ```{r} ``` 9. Let's plot the first 32 images that we retrieve from the training-set dataloader. ```{r} ``` 10. Let's create a neural network with 2 convolutional layers, one max pooling layer, and one fully connected layer. Use the Relu activation function for the hidden nodes. ```{r} ``` 11. To instantiate a neural network, you need to call `net()`. ```{r} ``` 12. Create an optimizer using the Adam algorithm to minimize the training loss. Do this by running the following code. ```{r} ``` 13. Train the neural network for 4 epochs. ```{r} ``` 14. Set the trained neural network in evaluation/testing mode using the following code. ```{r} ``` 15. Evaluate your trained neural network on the test data. What is the test accuracy? ```{r} ``` 16. Is this better than random guessing? How would you further improve the model?