Let's compare our simple, Markov chain model benchmark against LSTM networks. You can use the implementation we used earlier in this chapter. We will show how to use the Keras API for this task, as we did in the previous chapter.
We will illustrate this with an example close to one of the authors' hearts, generating names in Spanish.
First, we should load the required libraries:
library(keras)library(readr)library(stringr)library(purrr)library(tokenizers)
Then, define a sampling function based on the probabilities we will estimate:
sample_mod <- function(preds, temperature = 0.8){ preds <- log(preds)/temperature exp_preds <- exp(preds) preds <- exp_preds/sum(exp(preds)) rmultinom(1, 1, preds) %>% as.integer() ...