Skip to main content

word2ket is an effiecient embedding layer for PyTorch that is inspired by Quantum Entanglement.

Project description

Intro

word2ket is an space-efficient embedding layer that can reduce the space required to store the embeddings by up to 100,000x.

This is a PyTorch implementaion of the embedding layer that is proposed in the paper word2ket: Space-efficient Word Embeddings inspired by Quantum Entanglement.

Installation

Dependencies

# Install PyTorch
conda install pytorch torchvision -c pytorch
# Install GPyTorch
conda install gpytorch -c gpytorch

Install word2ket

pip install word2ket

Getting Started

You can directly use EmbeddingKet and EmbeddingKetXS layers in your model definition or you can use the ketify function to automatically replace all the nn.Embedding layers in your model. Below you can see a model that is using 7,680,000 parameters for its embedding could be ketify to just use 448 parameters.

from word2ket import EmbeddingKet, EmbeddingKetXS, ketify, summary
from torch import nn
import logging
logging.basicConfig(level=logging.INFO)


# Word2Ket Embedding Layer
w2v_embedding_layer = EmbeddingKetXS(num_embeddings=30000, embedding_dim=256, order=4, rank=1)
summary(w2v_embedding_layer)
"""
INFO:root:EmbeddingKetXS num_embeddings_leaf: 14
INFO:root:EmbeddingKetXS embedding_dim_leaf: 4
INFO:root:EmbeddingKetXS weight_leafs shape: torch.Size([4, 1, 14, 4])
Module Name                                                                           Total Parameters  Trainable Parameters # Elements in Trainable Parametrs       
EmbeddingKetXS(30000, 256)                                                            1                 1                    224                                     
Total number of trainable parameters elements 224
"""

# PyTorch Embedding Layer
class MyModel(nn.Module):
    def __init__(self, vocab_size, embedding_dim):
        super(MyModel, self).__init__()
        self.embedding = nn.Embedding(num_embeddings=vocab_size, embedding_dim=embedding_dim)

    def forward(self, x):
        self.embedding()
model = MyModel(vocab_size=30000, embedding_dim=256)
print("embedding.weight.shape: ", model.embedding.weight.shape)
"""
embedding.weight.shape:  torch.Size([30000, 256])
"""
summary(model)
"""
Module Name                                                                           Total Parameters  Trainable Parameters # Elements in Trainable Parametrs       
Embedding(30000, 256)                                                                 1                 1                    7,680,000                               
Total number of trainable parameters elements 7,680,000
"""

# Replace the nn.Embedding to EmbeddingKetXS automatically using the ketify function.
ketify(model, order=4, rank=2, use_EmbeddingKetXS=True)
summary(model)
"""
INFO:root:EmbeddingKetXS num_embeddings_leaf: 14
INFO:root:EmbeddingKetXS embedding_dim_leaf: 4
INFO:root:EmbeddingKetXS weight_leafs shape: torch.Size([4, 2, 14, 4])
INFO:root:Replaced embedding in MyModel
Module Name                                                                           Total Parameters  Trainable Parameters # Elements in Trainable Parametrs       
EmbeddingKetXS(30000, 256)                                                            1                 1                    448                                     
Total number of trainable parameters elements 448
"""

Running examples

Dependencies

# Install Texar-PyTorch
pip install texar-pytorch
# Install Rouge
pip install rouge

Download Datasets

cd ./examples/texar/
python ./prepare_data.py --data iwslt14
python ./prepare_data.py --data giga

Run text summarization

Using GIGAWORD dataset.

# Using Pytorch nn.Embedding Layer
nohup python seq2seq_attn.py --embedding_type nn.Embedding --gpu 4 --runName G_000 --config-model config_model --config-data config_giga &> outputs/giga_run000.txt &

# Using EmbeddingKet Layer
nohup python seq2seq_attn.py --embedding_type EmbeddingKet   --gpu 0 --runName V2K_G_000    --logdir V2K    --config-model config_model --config-data config_giga &> outputs/V2K_G_000.txt    &

# Using EmbeddingKetXS Layer
nohup python seq2seq_attn.py --embedding_type EmbeddingKetXS --gpu 0 --runName V2K_XS_G_000 --logdir V2K_XS --config-model config_model --config-data config_giga &> outputs/V2K_XS_G_000.txt &

Run German-English machine translation

Using IWSLT2014 (DE-EN) dataset

# Using Pytorch nn.Embedding Layer
nohup python seq2seq_attn.py --embedding_type nn.Embedding --gpu 4 --runName I_000 --config-model config_model --config-data config_iwslt14 &> outputs/iwslt14_run000.txt &

# Using EmbeddingKet Layer
nohup python seq2seq_attn.py --embedding_type EmbeddingKet   --gpu 0 --runName V2K_I_000    --logdir V2K    --config-model config_model --config-data config_iwslt14 &> outputs/V2K_I_000.txt    &

# Using EmbeddingKetXS Layer
nohup python seq2seq_attn.py --embedding_type EmbeddingKetXS --gpu 0 --runName V2K_XS_I_000 --logdir V2K_XS --config-model config_model --config-data config_iwslt14 &> outputs/V2K_XS_I_000.txt &

Reference

If you use word2ket, please cite the tech report with the following BibTex entry:

APA

Panahi, A., Saeedi, S., & Arodz, T. (2020). word2ket: Space-efficient Word Embeddings inspired by Quantum Entanglement. In International Conference on Learning Representations. Retrieved from https://openreview.net/forum?id=HkxARkrFwB

BibTex

@inproceedings{panahi2020wordket,
  title={word2ket: Space-efficient Word Embeddings inspired by Quantum Entanglement},
  author={Aliakbar Panahi and Seyran Saeedi and Tom Arodz},
  booktitle={International Conference on Learning Representations},
  year={2020},
  url={https://openreview.net/forum?id=HkxARkrFwB}
}

License

BSD-3-Clause

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

word2ket-0.0.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

word2ket-0.0.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file word2ket-0.0.1.tar.gz.

File metadata

  • Download URL: word2ket-0.0.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for word2ket-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6c44da8e235aefd4dde7aa6f0d607e40d0cbe2769d5f076fd17a4284d2139d6a
MD5 68cdd01ab7619e2b39ebe566d14005b1
BLAKE2b-256 c4e5e6bee40e5862b016a08699385737ea304b236af3a92d2fdd74fc2379a5ea

See more details on using hashes here.

File details

Details for the file word2ket-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: word2ket-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for word2ket-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 16373563568ba9b839ff741a62b3ab2057405ea9a6a0afc31f3a6677894e71c9
MD5 d3bfc72658d9bc3763ca040843478cfb
BLAKE2b-256 073437747b69d8868165aa451ca378d59fd3cc1b9ad1f5ea153318df0080cb0e

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