Search code examples
rkerascompressionmnistautoencoder

Simple keras autoencoder with MNIST sample data not working


I'm trying to implement a simple keras autoencoder in R using the MNIST sample dataset. I got my example from a blog but it doesn't work. I get almost a 0 % accuracy.

The objective is to compress each 28 x 28 image (784 entries) into a vector of 32 entries:

Here's my code:

library(keras)
mnist <- dataset_mnist()
x_train <- mnist$train$x

# reshape
x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_train <- x_train / 255

model <- keras_model_sequential() 
model %>% 
 layer_dense(units = 32, activation = 'relu', input_shape = c(784)) %>% 
 layer_dense(units=784, activation='sigmoid')

model %>% compile(
  loss = 'categorical_crossentropy',
  optimizer = 'adam',
  metrics = c('accuracy')
)

history <- model %>% fit(
  x_train, x_train, 
  epochs = 15, batch_size = 128, 
  validation_split = 0.2
)

Solution

  • You want to use binary_crossentropy as a loss function here. categorical_crossentropy is intended for multi-class classification problems (only one output is 1), binary_crossentropy is suitable for multi-label classification.