All Projects → PrashantRanjan09 → Elmo Tutorial

PrashantRanjan09 / Elmo Tutorial

A short tutorial on Elmo training (Pre trained, Training on new data, Incremental training)

Projects that are alternatives of or similar to Elmo Tutorial

Pytorch Sentiment Analysis
Tutorials on getting started with PyTorch and TorchText for sentiment analysis.
Stars: ✭ 3,209 (+2113.1%)
Mutual labels:  jupyter-notebook, tutorial, word-embeddings
Tensorflow Examples
TensorFlow Tutorial and Examples for Beginners (support TF v1 & v2)
Stars: ✭ 41,480 (+28506.9%)
Mutual labels:  jupyter-notebook, tutorial
100daysofcode With Python Course
Course materials and handouts for #100DaysOfCode in Python course
Stars: ✭ 1,391 (+859.31%)
Mutual labels:  jupyter-notebook, tutorial
Cadl
ARCHIVED: Contains historical course materials/Homework materials for the FREE MOOC course on "Creative Applications of Deep Learning w/ Tensorflow" #CADL
Stars: ✭ 1,478 (+919.31%)
Mutual labels:  jupyter-notebook, tutorial
Recommenders
Best Practices on Recommendation Systems
Stars: ✭ 11,818 (+8050.34%)
Mutual labels:  jupyter-notebook, tutorial
Scipy2017 Jupyter Widgets Tutorial
Notebooks for the SciPy 2017 tutorial "The Jupyter Interactive Widget Ecosystem"
Stars: ✭ 102 (-29.66%)
Mutual labels:  jupyter-notebook, tutorial
Mlf Mlt
📚 机器学习基石和机器学习技法作业
Stars: ✭ 112 (-22.76%)
Mutual labels:  jupyter-notebook, tutorial
End To End Sequence Labeling Via Bi Directional Lstm Cnns Crf Tutorial
Tutorial for End-to-end Sequence Labeling via Bi-directional LSTM-CNNs-CRF
Stars: ✭ 87 (-40%)
Mutual labels:  jupyter-notebook, tutorial
Qiskit Tutorials
A collection of Jupyter notebooks showing how to use the Qiskit SDK
Stars: ✭ 1,777 (+1125.52%)
Mutual labels:  jupyter-notebook, tutorial
Pandas Videos
Jupyter notebook and datasets from the pandas Q&A video series
Stars: ✭ 1,716 (+1083.45%)
Mutual labels:  jupyter-notebook, tutorial
Learn jupyter
This is a jupyter practical tutorial. Welcome to edit together!
Stars: ✭ 123 (-15.17%)
Mutual labels:  jupyter-notebook, tutorial
Keras Tutorial
Tutorial teaching the basics of Keras and some deep learning concepts
Stars: ✭ 98 (-32.41%)
Mutual labels:  jupyter-notebook, tutorial
Pytorch Pos Tagging
A tutorial on how to implement models for part-of-speech tagging using PyTorch and TorchText.
Stars: ✭ 96 (-33.79%)
Mutual labels:  jupyter-notebook, tutorial
Python Data Science Handbook
A Chinese translation of Jake Vanderplas' "Python Data Science Handbook". 《Python数据科学手册》在线Jupyter notebook中文翻译
Stars: ✭ 102 (-29.66%)
Mutual labels:  jupyter-notebook, tutorial
H2o Tutorials
Tutorials and training material for the H2O Machine Learning Platform
Stars: ✭ 1,305 (+800%)
Mutual labels:  jupyter-notebook, tutorial
Learning Vis Tools
Learning Vis Tools: Tutorial materials for Data Visualization course at HKUST
Stars: ✭ 108 (-25.52%)
Mutual labels:  jupyter-notebook, tutorial
Deeplearningfornlpinpytorch
An IPython Notebook tutorial on deep learning for natural language processing, including structure prediction.
Stars: ✭ 1,744 (+1102.76%)
Mutual labels:  jupyter-notebook, tutorial
Glove As A Tensorflow Embedding Layer
Taking a pretrained GloVe model, and using it as a TensorFlow embedding weight layer **inside the GPU**. Therefore, you only need to send the index of the words through the GPU data transfer bus, reducing data transfer overhead.
Stars: ✭ 85 (-41.38%)
Mutual labels:  jupyter-notebook, word-embeddings
100 Plus Python Programming Exercises Extended
100+ python programming exercise problem discussed ,explained and solved in different ways
Stars: ✭ 1,250 (+762.07%)
Mutual labels:  jupyter-notebook, tutorial
Vae Tensorflow
A Tensorflow implementation of a Variational Autoencoder for the deep learning course at the University of Southern California (USC).
Stars: ✭ 117 (-19.31%)
Mutual labels:  jupyter-notebook, tutorial

Elmo-Tutorial

This is a short tutorial on using Deep contextualized word representations (ELMo) which is discussed in the paper https://arxiv.org/abs/1802.05365. This tutorial can help in using:

  • Pre Trained Elmo Model - refer Elmo_tutorial.ipynb
  • Training an Elmo Model on your new data from scratch

To train and evaluate a biLM, you need to provide:

  • a vocabulary file
  • a set of training files
  • a set of heldout files

The vocabulary file is a text file with one token per line. It must also include the special tokens , and The vocabulary file should be sorted in descending order by token count in your training data. The first three entries/lines should be the special tokens :
<S> ,
</S> and
<UNK>.

The training data should be randomly split into many training files, each containing one slice of the data. Each file contains pre-tokenized and white space separated text, one sentence per line.

Don't include the <S> or </S> tokens in your training data.

Once done, git clone https://github.com/allenai/bilm-tf.git and run:

python bin/train_elmo.py --train_prefix= <path to training folder> --vocab_file <path to vocab file> --save_dir <path where models will be checkpointed>

To get the weights file, run:

python bin/dump_weights.py --save_dir /output_path/to/checkpoint --outfile/output_path/to/weights.hdf5

In the save dir, one options.json will be dumped and above command will give you a weights file required to create an Elmo model (options file and the weights file)

For more information refer Elmo_tutorial.ipynb

  • Incremental Learning/Training

To incrementally train an existing model with new data

While doing Incremental training : git clone https://github.com/allenai/bilm-tf.git

Once done, replace train_elmo within allenai/bilm-tf/bin/ with train_elmo_updated.py provided at home.

Updated changes :

train_elmo_updated.py

tf_save_dir = args.save_dir
tf_log_dir = args.save_dir
train(options, data, n_gpus, tf_save_dir, tf_log_dir,restart_ckpt_file)

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--save_dir', help='Location of checkpoint files')
parser.add_argument('--vocab_file', help='Vocabulary file')
parser.add_argument('--train_prefix', help='Prefix for train files')
parser.add_argument('--restart_ckpt_file', help='latest checkpoint file to start with')

This takes an argument (--restart_ckpt_file) to accept the path of the checkpointed file.

replace training.py within allenai/bilm-tf/bilm/ with training_updated.py provided at home. Also, make sure to put your embedding layer name in line 758 in training_updated.py :

exclude = ['the embedding layer name you want to remove']

Updated changes :

training_updated.py

    # load the checkpoint data if needed
    if restart_ckpt_file is not None:
        reader = tf.train.NewCheckpointReader(your_checkpoint_file)
        cur_vars = reader.get_variable_to_shape_map()
        exclude = ['the embedding layer name you want to remove']
        variables_to_restore = tf.contrib.slim.get_variables_to_restore(exclude=exclude)
        loader = tf.train.Saver(variables_to_restore)
        #loader = tf.train.Saver()
        loader.save(sess,'/tmp')
        loader.restore(sess, '/tmp')
        with open(os.path.join(tf_save_dir, 'options.json'), 'w') as fout:
            fout.write(json.dumps(options))

    summary_writer = tf.summary.FileWriter(tf_log_dir, sess.graph)

The code reads the checkpointed file and reads all the current variables in the graph and excludes the layers mentioned in the exclude variable, restores rest of the variables along with the associated weights.

For training run:

 python bin/train_elmo_updated.py --train_prefix= <path to training folder> --vocab_file <path to vocab file> --save_dir <path where models will be checkpointed> --restart_ckpt_file <path to checkpointed model>

In the train_elmo_updated.py within bin, set these options based on your data:

batch_size = 128  # batch size for each GPU
n_gpus = 3

# number of tokens in training data 
n_train_tokens = 

options = {
 'bidirectional': True,
 'dropout': 0.1,
 'all_clip_norm_val': 10.0,

 'n_epochs': 10,
 'n_train_tokens': n_train_tokens,
 'batch_size': batch_size,
 'n_tokens_vocab': vocab.size,
 'unroll_steps': 20,
 'n_negative_samples_batch': 8192,

Visualisation

Visualization of the word vectors using Elmo:

  • Tsne Optional Text

  • Tensorboard

Optional Text

Using Elmo Embedding layer in consequent models

if you want to use Elmo Embedding layer in consequent model build refer : https://github.com/PrashantRanjan09/WordEmbeddings-Elmo-Fasttext-Word2Vec

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].