# Keras: Using Autoencoders for prediction

I have a dataset which I divided into two sections horizontally. Column A of first section is the input variable and Column A of second section is the target variable. I am trying to build a Denoising Autoencoder that is trained using the target variable. Column A consists of string values which are one-hot encoded before giving it into the autoencoder. This is what I have done:

# Import Libraries
import pandas as pd
import numpy as np
from numpy import array
from numpy import argmax
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from keras.models import Model
from keras.layers import Dense, Input

# Divide dataset into sections
section_1 = df[df['D'] == 'Safe']

section_2 = df[df['D'] == 'Unsafe']

# One-Hot Encode values of Column A
target_values = array(section_2['A'])
input_values = array(section_1['A'])

# integer encode
label_encoder = LabelEncoder()
target_integer_encoded = label_encoder.fit_transform(target_values)
input_integer_encoded = label_encoder.fit_transform(input_values)

# binary encode
onehot_encoder = OneHotEncoder(sparse=False)
target_integer_encoded = target_integer_encoded.reshape(len(target_integer_encoded), 1)
input_integer_encoded = input_integer_encoded.reshape(len(input_integer_encoded), 1)
onehot_encoded_target = onehot_encoder.fit_transform(target_integer_encoded)
onehot_encoded_input = onehot_encoder.fit_transform(input_integer_encoded)

# Setup train and test sets
x_train, y_train = train_test_split(onehot_encoded_target, train_size=0.50)
x_test, y_test = train_test_split(onehot_encoded_input, test_size=0.50)
x_test = np.hstack((x_test, np.tile(x_test[:, [-1]], 3))) # Replicated the last column of x_test so it will be in same shape as x_train and y_train
print(x_train.shape, y_train.shape, x_test.shape) # (50, 95), (50, 95), (50, 95)

# Setup the Autoencoder
input_size = 95
hidden_size = 128
code_size = 32

input_values = Input(shape=(input_size,))
hidden_1 = Dense(hidden_size, activation='relu')(input_values)
code = Dense(code_size, activation='relu')(hidden_1)
hidden_2 = Dense(hidden_size, activation='relu')(code)
output_values = Dense(input_size, activation='sigmoid')(hidden_2)

autoencoder = Model(input_values, output_values)
autoencoder.fit(x_train, y_train, epochs=100)

# Predict the values
ypred = autoencoder.predict(x_test)

# Evaluate the model
print("MAE test score:", mean_absolute_error(y_test, ypred)) # MAE test score: 0.026
print("RMSE test score:", sqrt(mean_squared_error(y_test, ypred))) # RMSE test score: 0.103


I am very new to machine learning and I would like to know whether the logic I have written above is correct or not. Any feedback is appreciated.

• Have you done some testing? Does it seem to work correctly, as far as you can tell? It's worth telling us how you tested, as that can give clues to what you might have overlooked. – Toby Speight Apr 30 at 12:06
• @Toby Speight, Thank you for the reply. I have added how I evaluated the model at the end. Please check the edits! – Animeartist Apr 30 at 12:51