# NOT RUN {
library("keras")
# Get IMDb dataset
imdb <- dataset_imdb(num_words = 1000)
c(x_train, y_train) %<-% imdb$train
c(x_test, y_test) %<-% imdb$test
# AutoKeras procceses each text data point as a character vector,
# i.e., x_train[[1]] "<START> this film was just brilliant casting..",
# so we need to transform the dataset.
word_index <- dataset_imdb_word_index()
word_index <- c(
"<PAD>", "<START>", "<UNK>", "<UNUSED>",
names(word_index)[order(unlist(word_index))]
)
x_train <- lapply(x_train, function(x) {
paste(word_index[x + 1], collapse = " ")
})
x_test <- lapply(x_test, function(x) {
paste(word_index[x + 1], collapse = " ")
})
x_train <- matrix(unlist(x_train), ncol = 1)
x_test <- matrix(unlist(x_test), ncol = 1)
y_train <- array(unlist(y_train))
y_test <- array(unlist(y_test))
library("autokeras")
# Initialize the text regressor
reg <- model_text_regressor(max_trials = 10) %>% # It tries 10 different models
fit(x_train, y_train) # Feed the text regressor with training data
# If you want to use own valitadion data do:
reg <- model_text_regressor(max_trials = 10) %>%
fit(
x_train,
y_train,
validation_data = list(x_test, y_test)
)
# Predict with the best model
(predicted_y <- reg %>% predict(x_test))
# Evaluate the best model with testing data
reg %>% evaluate(x_test, y_test)
# Get the best trained Keras model, to work with the keras R library
export_model(reg)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab