Skip to main content

Flexi Hash Embeddings

This PyTorch Module hashes and sums variably-sized dictionaries of features into a single fixed-size embedding. Feature keys are hashed, which is ideal for streaming contexts and online-learning such that we don't have to memorize a mapping between feature keys and indices. Multiple variable-length features are grouped by example and then summed. Feature embeddings are scaled by their values, enabling linear features rather than just one-hot features.

Uses the wonderful torch scatter library and sklearn's feature hashing under the hood.

So for example:

>>> from flexi_hash_embedding import FlexiHashEmbedding
>>> X = [{'dog': 1, 'cat':2, 'elephant':4},
         {'dog': 2, 'run': 5}]
>>> embed = FlexiHashEmbedding(dim=5)
>>> embed(X)
tensor([[  1.0753,  -5.3999,   2.6890,   2.4224,  -2.8352],
        [  2.9265,   5.1440,  -4.1737, -12.3070,  -8.2725]],
       grad_fn=<ScatterAddBackward>)

Example

Frequently, we'll have data for a single event or user that's, for example, in a JSON blob format which lends itself to features which may be missing, incomplete or never before seen. Furthermore, there may be a variable number of features defined. This use case is ideal for feature hashing and for groupby summing of feature embeddings.

In this example we have a total of six features across our whole dataset, but we compute three vectors, one for every input row:

img

In the example above we have a total of six features but they're spread out across three clients. The first client has three active features, the second client two features (and only one feature that overlaps with the first client) and the third client has a single feature active. Flexi Hash Embeddings returns three vectors, one for each client, and not six vectors even though there are six features present. The first client's vector is a sum of three feature vectors (plus_flag, age_under, luxe_flag) while the second client's vector is a sum of just two feature vectors (lppish, luxe_flag) and the third client's vector is just a single feature.

Speed

Online feature hasing and groupby are relatively fast. For a large batchsize of 4096 with on average 5 features per row equals 20,000 total features. This module will hash that many features in about 20ms on a modern MacBook Pro.

Installation

Install from PyPi do pip install flexi-hash-embedding

Install locally by doing git@github.com:cemoody/flexi_hash_embedding.git.

Testing

>>> pip install -e .
>>> py.test

To publish a new package version:

python3 setup.py sdist bdist_wheel
twine upload dist/*
pip install --index-url https://pypi.org/simple/ --no-deps flexi_hash_embedding

Download files

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

Source Distribution

flexi_hash_embedding-0.0.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

flexi_hash_embedding-0.0.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file flexi_hash_embedding-0.0.2.tar.gz.

File metadata

  • Download URL: flexi_hash_embedding-0.0.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for flexi_hash_embedding-0.0.2.tar.gz
Algorithm Hash digest
SHA256 096f4f76ce3e78474e74d9fc57495f2dfa8fa53143d4a0f1ac55f00d97490e14
MD5 3241556f6c25501351b4d1bfafcb6daa
BLAKE2b-256 5ffc46992faf2f9ca79166f3c2ad7f90372e57391b34759e1dddb2f22dc3931e

See more details on using hashes here.

File details

Details for the file flexi_hash_embedding-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: flexi_hash_embedding-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for flexi_hash_embedding-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 adf7a849d365dfd3020ac141c8242e10c747772b875963b65b36d274103bc7ce
MD5 2f176a5a4cfe365d34d3c997a5db1acb
BLAKE2b-256 35efa3cd7ab618e11cfa3e76e9f24b1e50e7b5f3fa753d114a0e6bc8c91539af

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 Sentry Error logging StatusPage Status page