The Data
MNIST Pixel Data
The first dataset employed here is the usual MNIST pixel data, comprised by hand-written numbers. Here, the background is black and the digits are white.
Anomalous MNIST Pixel Data
To test the new procedure and compare it to the usual one, I created four simple types of anomalous data.
The goal was to test each methodâs detection capabilities across a small spectrum of noise variations, incrementally intensified from one anomaly type to the next.
The noise rate increases from the first to the fourth type of anomalous data. As you can see in the figure below, in the first and second types of data, the noise is not even detectable to the naked eye, while in the third type, you can already spot some white pixels.
Model-state data
While MNIST pixel data, with its hand-written digits against a stark backdrop, provides a classic foundation for anomaly detection, weâre trying something else. Itâs a bit of a leap, taking us right into the core of the trained ANN to see what the neurons are up to. This could give us a whole new angle on spotting anomalies.
As mentioned, this model state data is comprised by the state of the neurons in an ANN when trained with MNIST data. As such, to generate this data, we start with training a simple ANN with MNIST pixel data, both with normal and anomalous data (the anomalous are comprised by the noisy data showed before in Figure 2).
We then perform the usual: split the data into training and testing, and then we fit the ANN model:
model.fit(X_train,Y_cat_train,epochs=8, validation_data=(X_test, y_cat_test))
After that, we want to retrieve the names of the layers in model and store them in a list:
list(map(lambda x: x.name, model.layers))
Finally, we create a new model that takes the same input as the original model but produces output from a specific layer called âdenseâ:
intermediate_layer_model=Model(inputs=model.input, outputs=model.get_layer(\”dense\”).output)
This is useful for extracting information from intermediate layers of a neural network.
Letâs take a look at this model-state data:
model_state_data_layer1=pd.read_csv(\”first_layer_dense.csv\”,header=None)model_state_data_layer2=pd.read_csv(\”second_layer_dense.csv\”,header=None)
model_state_data_layer1.head(4)
The model-state data of the first neural layer is comprised by 32 columns and 4 rows.
With just a few lines of code, we are able to extract data from the intermediate layers of a neural network.
To study the effectiveness of the new method, Iâll be using data from both the first and second layers of the neural network.