Skip to main content

PyTorch normalization layers for numerical features, including SmoothBuckets and QuantileNorm

Project description

PyTorch Normalizers: Useful PyTorch Layers for Feature Normalization

This package provides several useful PyTorch layers for normalizing and processing numerical features in deep learning models, with a focus on differentiability, robustness, and handling real-world data issues like outliers and missing values. Currently, it includes SmoothBuckets and QuantileNorm, with more layers (such as DefaultEmbeddings and DetachFrom) planned for future additions.

Overview of Layers

SmoothBuckets

SmoothBuckets is a PyTorch module designed to normalize numerical features using a smooth, differentiable bucketing approach. It generalizes traditional deterministic bucketing and percentile-based normalization, making it suitable for deep learning models where gradients need to flow through the normalization step. Key features include:

  • Soft Bucketing with Gaussians: Instead of hard assignments to buckets, it uses Gaussian distributions to compute soft probabilities (bucket shares) for each feature value, ensuring differentiability.
  • Handling Missing Values: Gracefully manages NaNs by learning replacements during training, moving imputation logic into the model.
  • Robustness: Demonstrates improved performance over standard LayerNorm in scenarios with data corruption or outliers, as shown in the included tests.
  • Additional Percentile Feature: Appends an approximated percentile for each feature, enhancing the representation.
  • Trainable Parameters: Allows fine-tuning of multipliers for means and standard deviations while keeping initial estimates fixed to avoid issues with large value ranges.

This layer is particularly useful in tabular data modeling, such as regression or classification tasks, where numerical features vary widely in scale and distribution.

QuantileNorm

QuantileNorm is a PyTorch module that performs quantile normalization with incremental updates to adapt to data distribution shifts during training. It initializes quantiles from a sample and updates them gradually, handling outliers and providing smooth extrapolation for extreme values. This makes it ideal for streaming data or environments with evolving distributions.

Installation

Install via pip:

pip install pytorch-normalizers

Usage

To integrate the layers into your PyTorch model, import them from the package. Both layers support initialization with a sample of numerical features (as a torch.Tensor, np.ndarray, or pd.DataFrame).

SmoothBuckets Example

import torch.nn as nn
from pytorch_normalizers import SmoothBuckets

class MyModel(nn.Module):
    def __init__(self, numerical_features_sample, *args, **kwargs):
        super().__init__()
        self.input_normalizer = SmoothBuckets(numerical_features_sample) # num_buckets=5, eps=1e-5, dropout=0.1 are optional here
        # Add other layers as needed...

    def forward(self, x):
        x = self.input_normalizer(x)
        # Rest of the forward pass...
        return x
  • num_buckets: Number of buckets per feature (e.g., 5).
  • numerical_features_sample: A sample containing only the numerical features, used to initialize means and stds based on quantiles.
  • eps: Small value to prevent division by zero.
  • dropout: Dropout rate for regularization.

The layer outputs a tensor of shape (batch_size, num_features), after concatenating bucket shares and percentiles, applying layer norm, dropout, and a linear projection back to the original feature dimensionality.

QuantileNorm Example

import torch.nn as nn
from pytorch_normalizers import QuantileNorm

class MyModel(nn.Module):
    def __init__(self, numerical_features_sample, num_buckets=9):
        super().__init__()
        self.input_normalizer = QuantileNorm(numerical_features_sample, num_buckets)
    
    def forward(self, x):
        x = self.input_normalizer(x)
        # Rest of the model...
        return x
  • num_buckets: Number of quantiles/buckets (e.g., 9 for deciles).
  • numerical_features_sample: A sample for initial quantile estimation.

The layer outputs normalized values in [0, 1] range, with incremental updates during training.

Testing

The package includes self-contained tests for each layer:

  • For SmoothBuckets: Run python layers/smooth_buckets.py (or import and run test_me()). It uses the House Prices dataset from Kaggle to compare against LayerNorm and CatBoost in a binary classification task.

Example output (results may vary slightly due to randomness):

Corrupt batch:  False
LayerNorm Classifier Accuracy: 0.8101 
SmoothBuckets Classifier Accuracy: 0.9337 # a single (features, 1) layer over the normalized features
CatBoost Classifier Accuracy: 0.9281

Corrupt batch:  True
LayerNorm Classifier Accuracy: 0.6998  # Degrades due to corruption
SmoothBuckets Classifier Accuracy: 0.9219 # More robust
CatBoost Classifier Accuracy: 0.9281 # Deterministic result, unaffected by a tensor corruption
  • For QuantileNorm: Run python tests/quantile_norm_test.py to verify quantile adaptation on synthetic data.

Dependencies

  • Runtime: PyTorch, NumPy
  • Tests: Pandas, CatBoost

Install core dependencies:

pip install torch numpy

For tests:

pip install pandas catboost

License

MIT License. Feel free to use, modify, and distribute. See LICENSE for details.

Future Plans

Additional layers like DefaultEmbeddings (for efficient categorical embeddings) and DetachFrom (for selective gradient detachment) will be added in upcoming releases. Contributions are welcome!

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

pytorch_normalizers-0.1.3.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytorch_normalizers-0.1.3-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file pytorch_normalizers-0.1.3.tar.gz.

File metadata

  • Download URL: pytorch_normalizers-0.1.3.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for pytorch_normalizers-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a3c3649fed490653525ea9f222eb3d3ab9e43b236fe4b480c1b8b9e30b4fded2
MD5 3540593ba947d5a1c5f321be898348ae
BLAKE2b-256 7b091045b2b0c88a8892c10f55cb041e4854511ac6d1473885d14fdc69ae2a78

See more details on using hashes here.

File details

Details for the file pytorch_normalizers-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch_normalizers-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fe8e84d76a72ce98330c8ae53f331c800fa1e251c9a547a4326827c55630f62a
MD5 ec5ae5e3c822fb8b6a0d72d8bcbf9b8c
BLAKE2b-256 1488e63543376808420b608d50042259dd5866ed09d9fd714714e4cbc1af9390

See more details on using hashes here.

Supported by

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