Implementation of Bidirectional Attention Flow for Machine Comprehension in Keras 2
Machine Comprehension is a task in the field of NLP & NLU where the machine is provided with a passage and a question, and the machine tries to find an answer to the asked question from that given passage, by understanding the syntax and semantics of human language (here, English) and by establishing and understanding the relations betweeen the passage and the question.
We have implemented a model suggested in the paper Bidirectional Attention Flow for Machine Comprehension by a team of allennlp, popularly known as BiDAF.
Checkout this video to understand more:
Execute this command pip install bidaf-keras
Note that the above code won't install tensorflow as there is no way to detecting if your system has GPU while installing this package. But you can explicitly mention if you want to install tensorflow (CPU/GPU) while installing this package.
Installing Tensorflow CPU along with this package:
Execute this command pip install bidaf-keras[cpu]
Installing Tensorflow GPU along with this package:
Execute this command pip install bidaf-keras[gpu]
This project is available for use as a complete module. You can use this project via command-line arguments or by importing functionalities from it.:
Usage via command-line arguments:
To see the usage info, run python3 -m bidaf --help
or bidaf-keras --help
.
Using this module via command-line arguments will provide you with limited functionalities. To get the real power, we recommend using it by importing its functionalities.
The CLI mode will provide you with two basic functionalities: train and predict.
python3 -m bidaf train --help
or bidaf-keras train --help
For example:
python3 -m bidaf --model_name=bidaf_50.h5 --do_lowercase train --epochs=1 --steps_per_epoch=1 --validation_steps=1
or
bidaf-keras --model_name=bidaf_50.h5 --do_lowercase train --epochs=1 --steps_per_epoch=1 --validation_steps=1
python3 -m bidaf predict --help
or bidaf-keras predict --help
For example:
python3 -m bidaf --model_name=bidaf_50.h5 --do_lowercase predict --passage "This is tree." --question "What is this?" --return_char_loc --return_confidence_score
or
bidaf-keras --model_name=bidaf_50.h5 --do_lowercase predict --passage "This is tree." --question "What is this?" --return_char_loc --return_confidence_score
Note that, some parameters like "--do_lowercase" are dependent on some other parameters like "--model_name". For example, if the model is trained to perform predictions on lowercase inputs, it might not work well if the inputs are not in lowercase.
If you still have any queries on the need/use of any CLI argument, open an issue and we will reach you as soon as possible.
Usage by importing functionalities:
You can use the module by importing it in many different ways. So, we will just mention two example code snippets from which you will gain most of the knowledge required.
from bidaf.models import BidirectionalAttentionFlow
from bidaf.scripts import load_data_generators
bidaf_model = BidirectionalAttentionFlow(400) bidaf_model.load_bidaf("/path/to/model.h5") # when you want to resume training train_generator, validation_generator = load_data_generators(24, 400) keras_model = bidaf_model.train_model(train_generator, validation_generator=validation_generator)
- **Predict:**
```python
from bidaf.models import BidirectionalAttentionFlow
bidaf_model = BidirectionalAttentionFlow(400)
bidaf_model.load_bidaf("/path/to/model.h5")
bidaf_model.predict_ans("This is a tree", "What is this?")
Note that the above are the simplest code snippets. You can explore all functionalities available by looking at the parameters avaiable at different level. If you can't understand the parameters, just open an issue here. We may write the usage of all parameters in future if required.
Also note that, in the above snippets, bidaf_model
is just an object of class BidirectionalAttentionFlow
and not the real Keras model. You can access the Keras model by using keras_model = bidaf_model.model
.
Model Name: bidaf_50.h5
Model Configuration:
Model Name: bidaf_10.h5
Model Configuration:
python3 -m pip install -U git+https://github.com/ParikhKadam/magnitude.git@patch-1
Thoughts, samples codes, modifications and any other type of contributions are appreciated. This is my first project in Deep Learning and also first being open source. I will need as much help as possible as I don't know the path I need to follow. Thank you..