This example shows how to detect out-of-distribution text data using a variational autoencoder (VAE).
VAEs are a neural network architecture composed of two parts:
- An encoder that encodes data in a lower-dimensional parameter space.
- A decoder that reconstructs the input data by mapping the lower-dimensional representation back into the original space.
You can use a VAE to detect anomalies in your dataset. To do this, train a VAE on your data. Then, encode and decode a test data point. Compare the output of the decoder with the input data. If the input and output are similar, then the data is in-distribution. If the input and output are dissimilar, then the data is out-of-distribution, or anomalous.
This example includes three steps.
- Load and preprocess the text data.
- Set up and train the encoder and decoder networks.
- Use the VAE to detect anomalies in test data
Clone the repository in a local directory. If you would like to use this repository with MATLAB Online, clink
The main live script is AnomalyDetectionwithTextusingVAE.mlx. The other .m files are supporting functions for sampling the latent space, projecting and reshaping after sampling from latent space, and initializations of the project and reshape layer. You can either open the .mlx for demo or open the .prj file which will automatically open .mlx file.
Before running the file, get the data using the following steps:
- Go to https://www.mathworks.com/help/textanalytics/ug/create-simple-text-model-for-classification.html.
- Click on the button "Copy Command" on the top right of the page and paste it in MATLAB CLI. This will open the example in the directory where the .csv file is stored.
- Copy the .csv file from the example, and paste it in the cloned repo.
- If the file is saved in a different location, make sure to change the code that points to it in the .mlx file.
- MATLAB (R2023a or later)
- Text Analytics Toolbox™ (R2023a or later)
- Deep Learning Toolbox™ (R2023a or later)
Sohini Sarkar, [email protected]
The license is available in license.txt file in this GitHub repository.
Copyright 2024, The MathWorks, Inc.