The main objective of most autoencoders is to learn a new set of features out of nonlinear transformations which present some useful properties, such as lower dimension, distance preservation or noise resilience. In this article we explore the possibilities of using just a basic autoencoder with Ruta.

Ruta provides to ways of creating and training autoencoders. The first is the straight-forward autoencode function. It receives a data matrix as input and returns the learned features by an autoencoder with a single hidden layer of the specified size:

Since this function only returns the learned features, one cannot reuse the trained autoencoder. This is useful, however, for low-dimension visualizations:

For more customization options and reusability, we can build an autoencoder object (of class "ruta_autoencoder"). This can be done via the autoencoder family of functions.

network <- input() + dense(36, "tanh") + output("sigmoid")
my_ae <- autoencoder(network, loss = "binary_crossentropy")

Notice that this allows to define a deeper architecture: in this example we have created an autoencoder with one 36-unit hidden layer, apart from the input and output layers which will have as many units as features in the training data. We have also defined activation functions for each layer: \(\tanh\) is a common activation for the encoding layer, and setting sigmoid as the output will ensure an output in the range \([0,1]\). We can also specify the loss function we want to optimize. Here, "binary_crossentropy" is an objective function which works well with binary data and data in the \([0, 1]\) interval, but we can specify any loss available in Keras, another common one would be "mean_squared_error".

Now we need data to train the autoencoder. The keras package provides several well-known datasets:

To train this autoencoder we can specify the optimizer (e.g. "sgd", "rmsprop"…), number of epochs and other parameters.

model = train(my_ae, x_train, epochs = 10)

Once we have a trained model, we can use it for several purposes. We can encode input data and decode the resulting codifications, for example:

decoded <- reconstruct(model, x_test)

This is equivalent to doing the same task in two steps, as follows:

encoded <- encode(model, x_test)
decoded <- decode(model, encoded)

Lastly, let’s display our reconstructions as images:

Basic autoencoders provide some more options, such as weight decay of the encoding layer and training with validation data as well:

For more details, read the documentation. For the rest of autoencoder variants, see other examples.