Git Product home page Git Product logo

esim's Introduction

ESIM - Enhanced Sequential Inference Model

Implementation of the ESIM model for natural language inference with PyTorch

This repository contains an implementation with PyTorch of the sequential model presented in the paper "Enhanced LSTM for Natural Language Inference" by Chen et al. in 2016.

The figure below illustrates a high-level view of the model's architecture.

alt-text

This model was implemented in the context of my master's thesis at the university of Geneva.

How to

Install the package

To use the model defined in this repository, you will first need to install PyTorch on your machine by following the steps described on the package's official page (this step is only necessary if you use Windows). Then, to install the dependencies necessary to run the model, simply execute the command pip install --upgrade . from within the cloned repository (at the root, and preferably inside of a virtual environment).

Fetch the data to train and test the model

The fetch_data.py script located in the scripts/ folder of this repository can be used to download some NLI dataset and pretrained word embeddings. By default, the script fetches the SNLI corpus and the GloVe 840B 300d embeddings. Other datasets can be downloaded by simply passing their URL as argument to the script (for example, the MultNLI dataset).

The script's usage is the following:

fetch_data.py [-h] [--dataset_url DATASET_URL]
              [--embeddings_url EMBEDDINGS_URL]
              [--target_dir TARGET_DIR]

where target_dir is the path to a directory where the downloaded data must be saved (defaults to ../data/).

For MultiNLI, the matched and mismatched test sets need to be manually downloaded from Kaggle and the corresponding .txt files copied in the multinli_1.0 dataset folder.

Preprocess the data

Before the downloaded corpus and embeddings can be used in the ESIM model, they need to be preprocessed. This can be done with the preprocess_*.py scripts in the scripts/preprocessing folder of this repository. The preprocess_snli.py script can be used to preprocess SNLI, preprocess_mnli.py to preprocess MultiNLI, and preprocess_bnli.py to preprocess the Breaking NLI (BNLI) dataset. Note that when calling the script fot BNLI, the SNLI data should have been preprocessed first, so that the worddict produced for it can be used on BNLI.

The scripts' usage is the following (replace the * with snli, mnli or bnli):

preprocess_*.py [-h] [--config CONFIG]

where config is the path to a configuration file defining the parameters to be used for preprocessing. Default configuration files can be found in the config/preprocessing folder of this repository.

Train the model

The train_*.py scripts in the scripts/training folder can be used to train the ESIM model on some training data and validate it on some validation data.

The script's usage is the following (replace the * with snli or mnli):

train_*.py [-h] [--config CONFIG] [--checkpoint CHECKPOINT]

where config is a configuration file (default ones are located in the config/training folder), and checkpoint is an optional checkpoint from which training can be resumed. Checkpoints are created by the script after each training epoch, with the name esim_*.pth.tar, where '*' indicates the epoch's number.

Test the model

The test_*.py scripts in the scripts/testing folder can be used to test a pretrained ESIM model on some test data.

To test on SNLI, use the test_snli.py script as follows:

test_snli.py [-h] test_data checkpoint

where test_data is the path to some preprocessed test set, and checkpoint is the path to a checkpoint produced by the train_snli.py script (either one of the checkpoints created after the training epochs, or the best model seen during training, which is saved in data/checkpoints/SNLI/best.pth.tar - the difference between the esim_*.pth.tar files and best.pth.tar is that the latter cannot be used to resume training, as it doesn't contain the optimizer's state).

The test_snli.py script can also be used on the Breaking NLI dataset with a model pretrained on SNLI.

To test on MultiNLI, use the test_mnli.py script as follows:

test_mnli.py [-h] [--config CONFIG] checkpoint

where config is a configuration file (a default one is available in config/testing) and checkpoint is a checkpoint produced by the train_mnli.py script.

The test_mnli.py script makes predictions on MultiNLI's matched and mismatched test sets and saves them in .csv files. To get the classification accuracy associated to the model's predictions, the .csv files it produces need to be submitted to the Kaggle competitions for MultiNLI.

Results

A model pre-trained on SNLI is made available in the data/checkpoints/SNLI folder of this repository. The model was trained with the parameters defined in the default configuration files provided in config/. To test it, simply execute python test_snli.py ../../preprocessed/SNLI/test_data.pkl ../../data/checkpoints/best.pth.tar from within the scripts/testing folder.

The pretrained model achieves the following performance on the SNLI dataset:

Split Accuracy (%)
Train 93.2
Dev 88.4
Test 88.0

The results are in line with those presented in the paper by Chen et al.

On the Breaking NLI dataset, published by Glockner et al. in 2018, the model reaches 65.5% accuracy, as reported in the paper.

On MultiNLI, the model reaches the following accuracy:

Split Matched Mismatched
Dev 77.0 % 76.8 %
Test 76.6 % 75.8 %

These results are slightly above what was reported by Williams et al. in their MultiNLI paper.

esim's People

Contributors

coetaur0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

esim's Issues

What is the BNLI dataset?

What is the Breaking NLI (BNLI) dataset? And where can I download the BNLI dataset? I cannot find the BNLI dataset by searching "Breaking NLI (BNLI)" or "Breaking NLI (BNLI) dataset" in Google.
Thank you very much!

Validation loss lower than training loss?

Throughout my first 7 epochs, loss is always lower on validation set rather than training set. Did anything go wrong or the validation set is just too easy?

`* Training epoch 2:
Avg. batch proc. time: 0.0477s, loss: 0.4858: 100%|█████████████████████████████████████████████████████████| 17168/17168 [14:15<00:00, 20.07it/s]
-> Training time: 855.3887s, loss = 0.4858, accuracy: 81.1468%

  • Validation for epoch 2:
    -> Valid. time: 3.0217s, loss: 0.3845, accuracy: 85.4095%

  • Training epoch 3:
    Avg. batch proc. time: 0.0476s, loss: 0.4385: 100%|█████████████████████████████████████████████████████████| 17168/17168 [14:13<00:00, 20.12it/s]
    -> Training time: 853.2559s, loss = 0.4385, accuracy: 83.2263%

  • Validation for epoch 3:
    -> Valid. time: 2.9044s, loss: 0.3668, accuracy: 86.1613%

  • Training epoch 4:
    Avg. batch proc. time: 0.0477s, loss: 0.4120: 100%|█████████████████████████████████████████████████████████| 17168/17168 [14:14<00:00, 20.08it/s]
    -> Training time: 854.8605s, loss = 0.4120, accuracy: 84.4212%

  • Validation for epoch 4:
    -> Valid. time: 2.9331s, loss: 0.3626, accuracy: 86.4966%

  • Training epoch 5:
    Avg. batch proc. time: 0.0477s, loss: 0.3917: 100%|█████████████████████████████████████████████████████████| 17168/17168 [14:14<00:00, 20.08it/s]
    -> Training time: 854.8143s, loss = 0.3917, accuracy: 85.3156%

  • Validation for epoch 5:
    -> Valid. time: 2.9344s, loss: 0.3559, accuracy: 86.7608%

  • Training epoch 6:
    Avg. batch proc. time: 0.0476s, loss: 0.3766: 100%|█████████████████████████████████████████████████████████| 17168/17168 [14:14<00:00, 20.10it/s]
    -> Training time: 854.1151s, loss = 0.3766, accuracy: 85.9788%

  • Validation for epoch 6:
    -> Valid. time: 2.9510s, loss: 0.3426, accuracy: 87.2892%

  • Training epoch 7:
    Avg. batch proc. time: 0.0477s, loss: 0.3639: 100%|█████████████████████████████████████████████████████████| 17168/17168 [14:15<00:00, 20.08it/s]
    -> Training time: 855.0051s, loss = 0.3639, accuracy: 86.5372%

  • Validation for epoch 7:
    -> Valid. time: 2.9162s, loss: 0.3464, accuracy: 87.7058%`

50% train/dev accuracy for a binary classification task

Hi, here is a question that I try to use the ESIM to realize a binary classification task. I got more than 90% train/dev accuracy by using other models, but I got 50% accuracy by using ESIM. Does anyone have met similar issue before? Any suggestion will be great! Thanks a lot.

ESIM using keras

Hi
Since I don't have access to GPU, I can't execute your code, but there is another code in the github that implements your model with the keras Library . Are you confirming the following code and correct?

"""
Implementation of ESIM(Enhanced LSTM for Natural Language Inference)
https://arxiv.org/abs/1609.06038
"""
import numpy as np
from keras.layers import *
from keras.activations import softmax
from keras.models import Model

def StaticEmbedding(embedding_matrix):
in_dim, out_dim = embedding_matrix.shape
return Embedding(in_dim, out_dim, weights=[embedding_matrix], trainable=False)

def subtract(input_1, input_2):
minus_input_2 = Lambda(lambda x: -x)(input_2)
return add([input_1, minus_input_2])

def aggregate(input_1, input_2, num_dense=300, dropout_rate=0.5):
feat1 = concatenate([GlobalAvgPool1D()(input_1), GlobalMaxPool1D()(input_1)])
feat2 = concatenate([GlobalAvgPool1D()(input_2), GlobalMaxPool1D()(input_2)])
x = concatenate([feat1, feat2])
x = BatchNormalization()(x)
x = Dense(num_dense, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(dropout_rate)(x)
x = Dense(num_dense, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(dropout_rate)(x)
return x

def align(input_1, input_2):
attention = Dot(axes=-1)([input_1, input_2])
w_att_1 = Lambda(lambda x: softmax(x, axis=1))(attention)
w_att_2 = Permute((2,1))(Lambda(lambda x: softmax(x, axis=2))(attention))
in1_aligned = Dot(axes=1)([w_att_1, input_1])
in2_aligned = Dot(axes=1)([w_att_2, input_2])
return in1_aligned, in2_aligned

def build_model(embedding_matrix, num_class=1, max_length=30, lstm_dim=300):
q1 = Input(shape=(max_length,))
q2 = Input(shape=(max_length,))

Embedding

embedding = StaticEmbedding(embedding_matrix)
q1_embed = BatchNormalization(axis=2)(embedding(q1))
q2_embed = BatchNormalization(axis=2)(embedding(q2))

Encoding

encode = Bidirectional(LSTM(lstm_dim, return_sequences=True))
q1_encoded = encode(q1_embed)
q2_encoded = encode(q2_embed)

Alignment

q1_aligned, q2_aligned = align(q1_encoded, q2_encoded)

Compare

q1_combined = concatenate([q1_encoded, q2_aligned, subtract(q1_encoded, q2_aligned), multiply([q1_encoded, q2_aligned])])
q2_combined = concatenate([q2_encoded, q1_aligned, subtract(q2_encoded, q1_aligned), multiply([q2_encoded, q1_aligned])])
compare = Bidirectional(LSTM(lstm_dim, return_sequences=True))
q1_compare = compare(q1_combined)
q2_compare = compare(q2_combined)

Aggregate

x = aggregate(q1_compare, q2_compare)
x = Dense(num_class, activation='sigmoid')(x)

return Model(inputs=[q1, q2], outputs=x)

link github: https://gist.github.com/namakemono/b74547e82ef9307da9c29057c650cdf1

Buffered data was truncated after reaching the output size limit.

Hi
Thanks for the implementation.
Is this code executed on Google Colab?

I ran it on Google Colab and got an error on epoch 15?

Buffered data was truncated after reaching the output size limit.

Do I need to run until epoch 64?
On which epochs will perform best?

training loss is not reduced and accuracy is not improved during training

Dear author,

When I was running train_mnli.py and train_snli.py, I met the same device problem with #15. Then I set the device of idx_range (line 40-41 in esim/utils.py) to the correct device and it was solved.
396d7d00343b6b0b603916421b9f1a2

(I don't know whether this change leads to the following problem so I list the change here.)

But I met a new problem, i.e., the training loss is not reduced and accuracy is not improved during training.
69ba4609f904f4274251520b3658157
image

Could you please help me on this? Thanks a lot!

Getting Segmentation fault while training on MNLI

==================== Training ESIM model on device: cuda:0 ====================

  • Training epoch 1:
    Avg. batch proc. time: 0.0299s, loss: 0.8712: 100%|█████████████████████████████████████████████████████████████| 49088/49088 [26:49<00:00, 30.49it/s]
    Segmentation fault (core dumped)

No such file or directory: worddict.pkl

Hello, genius, I just want to know where is the worddict.pkl when I run 'preprocess_bnli.py --config ...bnli_preprocessing.json'.
File "preprocess_bnli.py", line 73, in preprocess_BNLI_data
with open(worddict, 'rb') as pkl:
FileNotFoundError: [Errno 2] No such file or directory: '../data/preprocessed/SNLI/worddict.pkl'

ModuleNotFoundError: No module named 'esim'

No matter in which way, I get the error "ModuleNotFoundError: No module named 'esim'". Thank you very much!

➜  preprocessing git:(master) pwd
~/tmp/ESIM/scripts/preprocessing
➜  preprocessing git:(master) python preprocess_snli.py
Traceback (most recent call last):
  File "preprocess_snli.py", line 12, in <module>
    from esim.data import Preprocessor
ModuleNotFoundError: No module named 'esim'
➜  scripts git:(master) pwd
~/tmp/ESIM/scripts
➜  scripts git:(master) python preprocessing/preprocess_snli.py
Traceback (most recent call last):
  File "preprocessing/preprocess_snli.py", line 12, in <module>
    from esim.data import Preprocessor
ModuleNotFoundError: No module named 'esim'
➜  ESIM git:(master) pwd
~/tmp/ESIM
➜  ESIM git:(master) python scripts/preprocessing/preprocess_snli.py
Traceback (most recent call last):
  File "scripts/preprocessing/preprocess_snli.py", line 12, in <module>
    from esim.data import Preprocessor
ModuleNotFoundError: No module named 'esim'

dataset scitail

Hi
Don't have the preprocess_scitail.py?
Didn't check the scitail dataset?

ImportError in train_snli.py

-sorry, stupid question, found it myself.-

If I try to run train_snli.py, an error occurs:

Traceback (most recent call last):
File "train_snli.py", line 19, in
from utils import train, validate
ImportError: cannot import name 'train'

This seems justified, because esim/esim/utils.py do not include train and validate.
Is this call referring to another script? Or what shall be done?

Test on MNLI model which was trained on SNLI

Hi,
For research purposes, I am attempting to train the ESIM model on the SNLI dataset, and then evaluate the classifier on the MNLI dataset (e.g. on the dev set). I tried to do this by calling the test_snli.py script and give it as a test-set the dev set for the (matched) MNLI dataset (after preprocessing). I get the following error:

Traceback (most recent call last):
File "test_snli.py", line 132, in
args.batch_size)
File "test_snli.py", line 113, in main
batch_time, total_time, accuracy = test(model, test_loader)
File "test_snli.py", line 54, in test
hypotheses_lengths)
File "/specific/netapp5/joberant/nlp_fall_2020/liaderez/nlp_project/ESIM/esim_env/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in call
result = self.forward(*input, **kwargs)
File "/lib/python3.7/site-packages/esim/model.py", line 128, in forward
premises_lengths)
File "/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in call
result = self.forward(*input, **kwargs)
File "/lib/python3.7/site-packages/esim/layers.py", line 117, in forward
batch_first=True)
File "/lib/python3.7/site-packages/torch/nn/utils/rnn.py", line 223, in pack_padded_sequence
lengths = torch.as_tensor(lengths, dtype=torch.int64)
RuntimeError: CUDA error: device-side assert triggered

I looked at both datasets and it seems the data is formatted in the same way, do I do not know why there is an issue.
Does the code perhaps support training a model on some dataset and testing it on another? If not, any suggestions about how I should do it?

Thanks!

Complete Esim implementation

Can you please state the reason as to why HIM (Hybrid Inference Model) is not implemented in many ESIM implementations.
Is their no visible improvement when Tree LSTM is added ?
which is advisable plain ESIM or HIM (considering the time to get an inference too)
or is BiMPM better of the three?

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.