This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
library(keras)
## Warning: package 'keras' was built under R version 3.4.4
max_words <- 1000
batch_size <- 32
epochs <- 5
cat('Loading data...\n')
## Loading data...
reuters <- dataset_reuters(num_words = max_words, test_split = 0.2)
saveRDS( reuters, 'Reuters.RDS')
x_train <- reuters$train$x
y_train <- reuters$train$y
x_test <- reuters$test$x
y_test <- reuters$test$y
cat(length(x_train), 'train sequences\n')
## 8982 train sequences
cat(length(x_test), 'test sequences\n')
## 2246 test sequences
num_classes <- max(y_train) + 1
cat(num_classes, '\n')
## 46
cat('Vectorizing sequence data...\n')
## Vectorizing sequence data...
tokenizer <- text_tokenizer(num_words = max_words)
x_train <- sequences_to_matrix(tokenizer, x_train, mode = 'binary')
x_test <- sequences_to_matrix(tokenizer, x_test, mode = 'binary')
cat('x_train shape:', dim(x_train), '\n')
## x_train shape: 8982 1000
cat('x_test shape:', dim(x_test), '\n')
## x_test shape: 2246 1000
cat('Convert class vector to binary class matrix',
'(for use with categorical_crossentropy)\n')
## Convert class vector to binary class matrix (for use with categorical_crossentropy)
y_train <- to_categorical(y_train, num_classes)
y_test <- to_categorical(y_test, num_classes)
cat('y_train shape:', dim(y_train), '\n')
## y_train shape: 8982 46
cat('y_test shape:', dim(y_test), '\n')
## y_test shape: 2246 46
cat('Building model...\n')
## Building model...
model <- keras_model_sequential()
model %>%
layer_dense(units = 512, input_shape = c(max_words)) %>%
layer_activation(activation = 'relu') %>%
layer_dropout(rate = 0.5) %>%
layer_dense(units = num_classes) %>%
layer_activation(activation = 'softmax')
model %>% compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = c('accuracy')
)
history <- model %>% fit(
x_train, y_train,
batch_size = batch_size,
epochs = epochs,
verbose = 1,
validation_split = 0.1
)
score <- model %>% evaluate(
x_test, y_test,
batch_size = batch_size,
verbose = 1
)
cat('Test score:', score[[1]], '\n')
## Test score: 0.8898819
cat('Test accuracy', score[[2]], '\n')
## Test accuracy 0.7911843
from keras.datasets import reuters
word_index = reuters.get_word_index(path="reuters_word_index.json")
# print(word_index)
## Downloading data from https://s3.amazonaws.com/text-datasets/reuters_word_index.json
##
## 8192/550378 [..............................] - ETA: 0s
## 139264/550378 [======>.......................] - ETA: 0s
## 352256/550378 [==================>...........] - ETA: 0s
## 524288/550378 [===========================>..] - ETA: 0s
## 557056/550378 [==============================] - 0s 0us/step
print(word_index["food"])
## 672