Package: vae_anomaly_detection
Variational autoencoder for anomaly detectionPytorch/TF1 implementation of Variational AutoEncoder for anomaly detection following the paperVariational Autoencoder based Anomaly Detection using Reconstruction Probability by Jinwon An, Sungzoon ChoHow to installPython package waypippackage containing the model and training_step onlypip install vae-anomaly-detectionHack this repositorya. Clone the repogit clone git@github.com:Michedev/VAE_anomaly_detection.gitb. Install hatchpip install hatchc. Make the environment with torch gpu supporthatch env createor with cpu supporthatch env create cpud. Run the trainhatch run trainor in cpuhatch run cpu:trainTo know all the train parameters runanaconda-project run train --helpThis version contains the model and the training procedureHow To Train your ModelDefine your dataset into dataset.py and overwrite the linetrain_set = rand_dataset()  # set here your datasetintrain.pySubclass VAEAnomalyDetection and define the methodsmake_encoderandmake_decoder. The output ofmake_encodershould be a flat vector while the output of `make_decoder should have the same shape of the input.Make your modelSubclassVAEAnomalyDetectionand define your encoder and decoder like inVaeAnomalyTabularclassVAEAnomalyTabular(VAEAnomalyDetection):defmake_encoder(self,input_size,latent_size):"""Simple encoder for tabular data.If you want to feed image to a VAE make another encoder function with Conv2d instead of Linear layers.:param input_size: number of input variables:param latent_size: number of output variables i.e. the size of the latent space since it's the encoder of a VAE:return: The untrained encoder model"""returnnn.Sequential(nn.Linear(input_size,500),nn.ReLU(),nn.Linear(500,200),nn.ReLU(),nn.Linear(200,latent_size*2)# times 2 because this is the concatenated vector of latent mean and variance)defmake_decoder(self,latent_size,output_size):"""Simple decoder for tabular data.:param latent_size: size of input latent space:param output_size: number of output parameters. Must have the same value of input_size:return: the untrained decoder"""returnnn.Sequential(nn.Linear(latent_size,200),nn.ReLU(),nn.Linear(200,500),nn.ReLU(),nn.Linear(500,output_size*2)# times 2 because this is the concatenated vector of reconstructed mean and variance)How to make predictions:Once the model is trained (suppose for simplicity that it is undersaved_models/{train-datetime}/) just load and predict with this code snippet:importtorch#load X_testmodel=VaeAnomalyTabular.load_checkpoint('saved_models/2022-01-06_15-12-23/last.ckpt')# load saved parameters from a runoutliers=model.is_anomaly(X_test)train.py helpusage: train.py [-h] --input-size INPUT_SIZE --latent-size LATENT_SIZE
                    [--num-resamples NUM_RESAMPLES] [--epochs EPOCHS] [--batch-size BATCH_SIZE]
                    [--device {cpu,gpu,tpu}] [--lr LR] [--no-progress-bar]
                    [--steps-log-loss STEPS_LOG_LOSS]
                    [--steps-log-norm-params STEPS_LOG_NORM_PARAMS]

    options:
    -h, --help            show this help message and exit
    --input-size INPUT_SIZE, -i INPUT_SIZE
                            Number of input features. In 1D case it is the vector length, in 2D
                            case it is the number of channels
    --latent-size LATENT_SIZE, -l LATENT_SIZE
                            Size of the latent space
    --num-resamples NUM_RESAMPLES, -L NUM_RESAMPLES
                            Number of resamples in the latent distribution during training
    --epochs EPOCHS, -e EPOCHS
                            Number of epochs to train for
    --batch-size BATCH_SIZE, -b BATCH_SIZE
    --device {cpu,gpu,tpu}, -d {cpu,gpu,tpu}, --accelerator {cpu,gpu,tpu}
                            Device to use for training. Can be cpu, gpu or tpu
    --lr LR               Learning rate
    --no-progress-bar
    --steps-log-loss STEPS_LOG_LOSS
                            Number of steps between each loss logging
    --steps-log-norm-params STEPS_LOG_NORM_PARAMS
                            Number of steps between each model parameters logging