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 = ...