Code Monkey home page Code Monkey logo

random_embedding's Introduction

Random Embedding Repository

This repository is for generating random circulant embedding matrices, which can be used in the training of PyTorch models. To use the code, simply clone the repo, and import the random_embedding module.

git clone https://github.com/HazyResearch/random_embedding.git
cd random_embedding
# If the following code runs without throwing an error, the module is being
# successfully imported.
python -c "import random_embedding"
# To run our unit tests locally, execute the following command:
python -m unittest random_embedding_test.RandomEmbeddingTest

In random_embedding.py, there is a module called RandomEmbedding, which is a subclass of torch.nn.Embedding. This module is meant to serve as a drop-in replacement for torch.nn.Embedding in the case where you want to use fixed random embeddings during the training of a downstream model. Below is an example of how to create a RandomEmbedding module for an embedding matrix of size n-by-d, where the average norm of an embedding vector is equal to c (for example, c can be chosen to match the average embedding vector norm of the pretrained embedding matrix being replaced); we additionally show how to pass input to this embedding module:

import torch
from random_embedding import RandomEmbedding

n,d,c = 100,10,1
emb = RandomEmbedding(n,d,avg_embedding_norm=c)
# Extract the word embeddings for words corresponding to indices 0, 1, and 2.
word_indices = torch.tensor([0,1,2], dtype=torch.int64)
output = emb(word_indices)
print(output.shape)
# output shape will be 3 by 10

We have implemented the RandomEmbedding module in a memory-efficient manner, such that for a vocabulary of size n, only 33n bits are used (32n bits for a vector of n random floats parameterizing the circulant matrices, and an additional n bits for a boolean random vector of length n). For details about exactly how we define the random circulant embeddings, please see our ACL paper which we cite below.

Our implementation is tested under Python 3.6 and PyTorch 1.0 (for our tests, see random_embedding_test.py).

Citing this Repository

If you use this repository for your research, please cite the following paper:

@inproceedings{arora20,
  title     = {Contextual Embeddings: When Are They Worth It?},
  author    = {Simran Arora and Avner May and Jian Zhang and Christopher Ré},
  booktitle = {{ACL}},
  publisher = {Association for Computational Linguistics},
  year      = {2020}
}

random_embedding's People

Contributors

avnermay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.