Skip to main content

PyTorch autoencoder with additional embeddings layer for categorical data.

Project description

The autoembedder

The Autoembedder

deploy package pypi python version docs license downloads mypy black isort pre-commit

Introduction

The Autoembedder is an autoencoder with additional embedding layers for the categorical columns. Its usage is flexible, and hyperparameters like the number of layers can be easily adjusted and tuned. The data provided for training can be either a path to a Dask or Pandas DataFrame stored in the Parquet format or the DataFrame object directly.

Installation

If you are using Poetry, you can install the package with the following command:

poetry add autoembedder

If you are using pip, you can install the package with the following command:

pip install autoembedder

Installing dependencies

With Poetry:

poetry install

With pip:

pip install -r requirements.txt

Usage

0. Some imports

from autoembedder import Autoembedder, dataloader, fit

1. Create dataloaders

First, we create two dataloaders. One for training, and the other for validation data. As source they either accept a path to a Parquet file, to a folder of Parquet files or a Pandas/Dask DataFrame.

train_dl = dataloader(train_df)
valid_dl = dataloader(vaild_df)

2. Set parameters

Now, we need to set the parameters. They are going to be used for handling the data and training the model. In this example, only parameters for the training are set. Here you find a list of all possible parameters. This should do it:

parameters = {
    "hidden_layers": [[25, 20], [20, 10]],
    "epochs": 10,
    "lr": 0.0001,
    "verbose": 1,
}

3. Initialize the autoembedder

Then, we need to initialize the autoembedder. In this example, we are not using any categorical features. So we can skip the embedding_sizes argument.

model = Autoembedder(parameters, num_cont_features=train_df.shape[1])

4. Train the model

Everything is set up. Now we can fit the model.

fit(parameters, model, train_dl, valid_dl)

Example

Check out this Jupyter notebook for an applied example using the Credit Card Fraud Detection from Kaggle.

Parameters

This is a list of all parameters that can be passed to the Autoembedder for training. The Required, Default value, and Comment columns are only apply if using the training script (training.py):

Argument Type Required Default value Comment
batch_size int False 32
drop_last int False 1 True/False
pin_memory int False 1 True/False
num_workers int False 0 0 means that the data will be loaded in the main process
use_mps int False 0 Set this to 1 if you want to use the MPS Backend for running on Mac using the M1 GPU. process
model_title str False autoembedder_{datetime}.bin
model_save_path str False
n_save_checkpoints int False
lr float False 0.001
amsgrad int False 0 True/False
epochs int True
dropout_rate float False 0 Dropout rate for the dropout layers in the encoder and decoder.
layer_bias int False 1 True/False
weight_decay float False 0
l1_lambda float False 0
xavier_init int False 0 True/False
activation str False tanh Activation function; either tanh, relu, leaky_relu or elu
tensorboard_log_path str False
trim_eval_errors int False 0 Removes the max and min loss when calculating the mean loss diff and median loss diff. This can be useful if some rows create very high losses.
verbose int False 0 Set this to 1 if you want to see the model summary and the validation and evaluation results. set this to 2 if you want to see the training progress bar. 0 means no output.
target str False The target column. If not set no evaluation will be performed.
train_input_path str True
test_input_path str True
eval_input_path str False Path to the evaluation data. If no path is provided no evaluation will be performed.
hidden_layers str True Contains a string representation of a list of list of integers which represents the hidden layer structure. E.g.: "[[64, 32], [32, 16], [16, 8]]" activation
cat_columns str False "[]" Contains a string representation of a list of list of categorical columns (strings). The columns which use the same encoder should be together in a list. E.g.: "[['a', 'b'], ['c']]".

Run the training script

Something like this should do it:

python3 training.py --epochs 20 \
--train_input_path "path/to/your/train_data" \
--test_input_path "path/to/your/test_data" \
--hidden_layers "[[12, 6], [6, 3]]"

Why additional embedding layers?

The additional embedding layers automatically embed all columns with the Pandas category data type. If categorical columns have another data type, they will not be embedded and will be handled like continuous columns. Simply encoding the categorical values (e.g., with the usage of a label encoder) decreases the quality of the outcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

autoembedder-0.1.14.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

autoembedder-0.1.14-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file autoembedder-0.1.14.tar.gz.

File metadata

  • Download URL: autoembedder-0.1.14.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1022-azure

File hashes

Hashes for autoembedder-0.1.14.tar.gz
Algorithm Hash digest
SHA256 977ea4f942cc8236987d934d7fec0371a881a390b63c01b9d2e188a9af193263
MD5 c2f31d12a4a94d095816e466f031c4b7
BLAKE2b-256 92ae331942c393a5d8d83448f17e0a297f6faa270dccae3a311847573ea6cf49

See more details on using hashes here.

File details

Details for the file autoembedder-0.1.14-py3-none-any.whl.

File metadata

  • Download URL: autoembedder-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1022-azure

File hashes

Hashes for autoembedder-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 7ccc3a308b6b9b8f8464a8cf739c33c24a7df9e7763f0ded2978b3b0100f8935
MD5 b00aae75d099e25cd024e1c0f7d8e9b6
BLAKE2b-256 2022ed0b51e906391d67f9353b48633521c566e17adae52a049b284a0535b2e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page