10.3 Lab: A Single Layer Network on the Hitters Data
How to perform deep learning in RStudio:
- Single Layer
 
library(ISLR2)
Gitters <- na.omit(Hitters)
n <- nrow(Gitters)
set.seed(13)
ntest <- trunc(n / 3) #rounding numbers
testid <- sample(1:n, ntest)Fit the model with testid selection
training <- Gitters[-testid, ]
testing <- Gitters[testid, ]
lfit <- lm(Salary ~ ., data = training)
lpred <- predict(lfit, testing)
pred_test <- cbind(testing,lpred)
mean(abs(pred_test$lpred-pred_test$Salary))## [1] 254.6687
Standardize the matrix and fit the lasso using glmnet
x <- scale(model.matrix(Salary ~ . - 1, data = Gitters))
y <- Gitters$Salary
library(glmnet)## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## Loaded glmnet 4.1-8
cvfit <- cv.glmnet(x[-testid, ], y[-testid], type.measure = "mae")
cpred <- predict(cvfit, x[testid, ], s = "lambda.min")
mean(abs(y[testid] - cpred))## [1] 252.2994
There are two ways to fit the Neural Network:
- using {keras} implies pyton
 - using {torch}
 
Fit the neural network with {keras}:
keras_model_sequential()layer_dense()layer-dropout()
Keras requires some installation on RStudio:
library(ISLR2)
tryCatch(
  remove.packages(c("keras", "tensorflow", "reticulate")),
  error = function(e) "Some or all packages not previously installed, that's ok!"
)
install.packages("keras", repos = 'https://cloud.r-project.org')
write('RETICULATE_AUTOCONFIGURE=FALSE', file = "~/.Renviron", append = TRUE)
write(sprintf('RETICULATE_MINICONDA_PATH=%s',
           normalizePath("~/islr-miniconda", winslash = "/", mustWork = FALSE)),
   file = "~/.Renviron", append = TRUE)
# restart R
source(system.file("helpers", "install.R", package = "ISLR2"))
reticulate::install_miniconda(force = TRUE)
tensorflow::install_tensorflow()library(tidyverse)
library(keras)
library(tensorflow)
modnn <- keras_model_sequential() %>%
     layer_dense(units = 50, activation = "relu",
        input_shape = ncol(x)) %>%
     layer_dropout(rate = 0.4) %>%
     layer_dense(units = 1)Resources:
The second way is to use {torch}:
library(torch)
library(luz) # high-level interface for torch
library(torchvision) # for datasets and image transformation
library(torchdatasets) # for datasets we are going to use
library(zeallot)
torch_manual_seed(13)modnn <- nn_module(
  initialize = function(input_size) {
    self$hidden <- nn_linear(input_size, 50)
    self$activation <- nn_relu()
    self$dropout <- nn_dropout(0.4)
    self$output <- nn_linear(50, 1)
  },
  forward = function(x) {
    x %>% 
      self$hidden() %>% 
      self$activation() %>% 
      self$dropout() %>% 
      self$output()
  }
)