O'Reilly logo

R Deep Learning Projects by Pablo Maldonado, Yuxi Liu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementation without R6

In this section, we include the implementation of the same basic recurrent neural network without using R6 classes. First, some imports and setting the seed:

library(readr)library(stringr)library(purrr)library(tokenizers)set.seed(1234)

We introduce an auxiliary function to initialize to zeros a matrix with the shape of a matrix, M:

zeros_like <- function(M){ return(matrix(0,dim(as.matrix(M))[1],dim(as.matrix(M))[2])) }

We also need the softmax function:

softmax <- function(x){ xt <- exp(x-max(x)) return(xt/sum(xt))}

We will use this for testing the female names data (see the Exercises section):

data <- read_lines("./data/female.txt")

And do some preprocessing:

text <- data %>% str_to_lower() %>% str_c(collapse = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required