Giter Club home page Giter Club logo

word2vec-pytorch's Introduction

Word2Vec in PyTorch

Implementation of the first paper on word2vec - Efficient Estimation of Word Representations in Vector Space. For detailed explanation of the code here, check my post - Word2vec with PyTorch: Reproducing Original Paper.

Word2Vec Overview

There 2 model architectures desctibed in the paper:

  • Continuous Bag-of-Words Model (CBOW), that predicts word based on its context;
  • Continuous Skip-gram Model (Skip-Gram), that predicts context for a word.

Difference with the original paper:

  • Trained on WikiText-2 and WikiText103 inxtead of Google News corpus.
  • Context for both models is represented as 4 history and 4 future words.
  • For CBOW model averaging for context word embeddings used instead of summation.
  • For Skip-Gram model all context words are sampled with the same probability.
  • Plain Softmax was used instead of Hierarchical Softmax. No Huffman tree used either.
  • Adam optimizer was used instead of Adagrad.
  • Trained for 5 epochs.
  • Regularization applied: embedding vector norms are restricted to 1.

CBOW Model in Details

High-Level Model

alt text

Model Architecture

alt text

Skip-Gram Model in Details

High-Level Model

alt text

Model Architecture

alt text

Project Structure

.
├── README.md
├── config.yaml
├── notebooks
│   └── Inference.ipynb
├── requirements.txt
├── train.py
├── utils
│   ├── constants.py
│   ├── dataloader.py
│   ├── helper.py
│   ├── model.py
│   └── trainer.py
└── weights
  • utils/dataloader.py - data loader for WikiText-2 and WikiText103 datasets

  • utils/model.py - model architectures

  • utils/trainer.py - class for model training and evaluation

  • train.py - script for training

  • config.yaml - file with training parameters

  • weights/ - folder where expriments artifacts are stored

  • notebooks/Inference.ipynb - demo of how embeddings are used

Usage

python3 train.py --config config.yaml

Before running the command, change the training parameters in the config.yaml, most important:

  • model_name ("skipgram", "cbow")
  • dataset ("WikiText2", "WikiText103")
  • model_dir (directory to store experiment artifacts, should start with "weights/")

License

This project is licensed under the terms of the MIT license.

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.