Skip to content

Instantly share code, notes, and snippets.

@fdavidcl
Created November 26, 2018 17:44
Show Gist options
  • Save fdavidcl/f0b42e586abae11f778649e46e037bd5 to your computer and use it in GitHub Desktop.
Save fdavidcl/f0b42e586abae11f778649e46e037bd5 to your computer and use it in GitHub Desktop.
Convolutional autoencoder with Ruta/Simple autoencoder comparison
library(ruta)
library(purrr)
library(keras)
plot_square <- function(square, ...) {
image(t(square)[,28:1], xaxt = "n", yaxt = "n", col = gray((255:0)/255), ...)
}
plot_sample <- function(digits_test, digits_dec, sample) {
sample_size <- length(sample)
layout(
matrix(c(1:(2 * sample_size)), byrow = F, nrow = 2)
)
for (i in sample) {
par(mar = c(0,0,0,0) + 1)
plot_square(digits_test[i,,,1 ])
plot_square(digits_dec[i,,,1 ])
}
}
mnist <- dataset_mnist()
x_train <- mnist$train$x / 255.0
x_test <- mnist$test$x / 255.0
net <- input() +
layer_keras("reshape", target_shape = c(dim(x_train)[-1], 1)) +
layer_keras("conv_2d", filters = 16, kernel_size = 3, activation = "relu", padding = "same") +
layer_keras("max_pooling_2d") +
layer_keras("conv_2d", filters = 8, kernel_size = 3, activation = "relu", padding = "same") +
layer_keras("max_pooling_2d", name = "encoding") +
layer_keras("conv_2d", filters = 8, kernel_size = 3, activation = "relu", padding = "same") +
layer_keras("upsampling_2d") +
layer_keras("conv_2d", filters = 16, kernel_size = 3, activation = "relu", padding = "same") +
layer_keras("upsampling_2d") +
layer_keras("conv_2d", filters = 1, kernel_size = 3, activation = "sigmoid", padding = "same") +
layer_keras("reshape", target_shape = dim(x_train)[-1])
ae <- autoencoder(net, loss = "binary_crossentropy")
model <- ae %>% train(x_train, validation_data = x_test, epochs = 4)
evaluate_mean_squared_error(model, x_test)
enc <- model %>% encode(x_test)
decode <- model %>% reconstruct(x_test)
plot_sample(x_test, decode, 1:10)
xtrain <- quakes[1:750,] %>% as.matrix()
xtest <- quakes[751:1000,] %>% as.matrix()
encoding_dim <- 2
hidden_dim <- 6
# ============== Ruta ==============
library(ruta)
library(purrr)
features <- autoencoder(c(hidden_dim, encoding_dim)) %>%
train(xtrain) %>%
encode(xtest)
# ============== Keras ==============
library(keras)
input_l <- layer_input(shape = 5)
encoded <- layer_dense(input_l, units = hidden_dim)
encoded <- layer_dense(encoded, units = encoding_dim)
decoded <- layer_dense(encoded, units = hidden_dim)
decoded <- layer_dense(decoded, units = 5)
autoe <- keras_model(input_l, decoded)
encoder <- keras_model(input_l, encoded)
compile(autoe, loss = "mean_squared_error", optimizer = "rmsprop")
fit(autoe, xtrain, xtrain)
features <- predict(encoder, xtest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment