# MatchZoo [](https://twitter.com/intent/tweet?text=MatchZoo:%20deep%20learning%20for%20semantic%20matching&url=https://github.com/NTMC-Community/MatchZoo)
> Facilitating the design, comparison and sharing of deep text matching models.
> MatchZoo
[](https://www.python.org/downloads/release/python-360/)
[](https://gitter.im/NTMC-Community/community)
[](https://pypi.org/project/MatchZoo/)
[](https://matchzoo.readthedocs.io/en/master/?badge=master)
[](https://travis-ci.org/NTMC-Community/MatchZoo/)
[](https://codecov.io/gh/NTMC-Community/MatchZoo)
[](https://opensource.org/licenses/Apache-2.0)
[](https://requires.io/github/NTMC-Community/MatchZoo/requirements/?branch=master)
---
The goal of MatchZoo is to provide a high-quality codebase for deep text matching research, such as document retrieval, question answering, conversational response ranking, and paraphrase identification. With the unified data processing pipeline, simplified model configuration and automatic hyper-parameters tunning features equipped, MatchZoo is flexible and easy to use.
| Tasks |
Text 1 |
Text 2 |
Objective |
| Paraphrase Indentification |
string 1 |
string 2 |
classification |
| Textual Entailment |
text |
hypothesis |
classification |
| Question Answer |
question |
answer |
classification/ranking |
| Conversation |
dialog |
response |
classification/ranking |
| Information Retrieval |
query |
document |
ranking |
## Get Started in 60 Seconds
To train a [Deep Semantic Structured Model](https://www.microsoft.com/en-us/research/project/dssm/), import matchzoo and prepare input data.
```python
import matchzoo as mz
train_pack = mz.datasets.wiki_qa.load_data('train', task='ranking')
valid_pack = mz.datasets.wiki_qa.load_data('dev', task='ranking')
predict_pack = mz.datasets.wiki_qa.load_data('test', task='ranking')
```
Preprocess your input data in three lines of code, keep track parameters to be passed into the model.
```python
preprocessor = mz.preprocessors.DSSMPreprocessor()
train_processed = preprocessor.fit_transform(train_pack)
valid_processed = preprocessor.transform(valid_pack)
```
Make use of MatchZoo customized loss functions and evaluation metrics:
```python
ranking_task = mz.tasks.Ranking(loss=mz.losses.RankCrossEntropyLoss(num_neg=4))
ranking_task.metrics = [
mz.metrics.NormalizedDiscountedCumulativeGain(k=3),
mz.metrics.NormalizedDiscountedCumulativeGain(k=5),
mz.metrics.MeanAveragePrecision()
]
```
Initialize the model, fine-tune the hyper-parameters.
```python
model = mz.models.DSSM()
model.params['input_shapes'] = preprocessor.context['input_shapes']
model.params['task'] = ranking_task
model.params['mlp_num_layers'] = 3
model.params['mlp_num_units'] = 300
model.params['mlp_num_fan_out'] = 128
model.params['mlp_activation_func'] = 'relu'
model.guess_and_fill_missing_params()
model.build()
model.compile()
```
Generate pair-wise training data on-the-fly, evaluate model performance using customized callbacks on validation data.
```python
train_generator = mz.PairDataGenerator(train_processed, num_dup=1, num_neg=4, batch_size=64, shuffle=True)
valid_x, valid_y = valid_processed.unpack()
evaluate = mz.callbacks.EvaluateAllMetrics(model, x=valid_x, y=valid_y, batch_size=len(pred_x))
history = model.fit_generator(train_generator, epochs=20, callbacks=[evaluate], workers=5, use_multiprocessing=False)
```
## References
[Tutorials](https://github.com/NTMC-Community/MatchZoo/tree/master/tutorials)
[English Documentation](https://matchzoo.readthedocs.io/en/master/)
[](https://matchzoo.readthedocs.io/zh/latest/)
If you're interested in the cutting-edge research progress, please take a look at [awaresome neural models for semantic match](https://github.com/NTMC-Community/awaresome-neural-models-for-semantic-match).
## Install
MatchZoo is dependent on [Keras](https://github.com/keras-team/keras), please install one of its backend engines: TensorFlow, Theano, or CNTK. We recommend the TensorFlow backend. Two ways to install MatchZoo:
**Install MatchZoo from Pypi:**
```python
pip install matchzoo
```
**Install MatchZoo from the Github source:**
```
git clone https://github.com/NTMC-Community/MatchZoo.git
cd MatchZoo
python setup.py install
```
## Models:
1. [DRMM](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/drmm.py): this model is an implementation of
A Deep Relevance Matching Model for Ad-hoc Retrieval.
2. [MatchPyramid](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/match_pyramid.py): this model is an implementation of
Text Matching as Image Recognition
3. [ARC-I](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/arci.py): this model is an implementation of
Convolutional Neural Network Architectures for Matching Natural Language Sentences
4. [DSSM](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/dssm.py): this model is an implementation of
Learning Deep Structured Semantic Models for Web Search using Clickthrough Data
5. [CDSSM](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/cdssm.py): this model is an implementation of
Learning Semantic Representations Using Convolutional Neural Networks for Web Search
6. [ARC-II](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/arcii.py): this model is an implementation of
Convolutional Neural Network Architectures for Matching Natural Language Sentences
7. [MV-LSTM](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/mvlstm.py):this model is an implementation of
A Deep Architecture for Semantic Matching with Multiple Positional Sentence Representations
8. [aNMM](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/anmm.py): this model is an implementation of
aNMM: Ranking Short Answer Texts with Attention-Based Neural Matching Model
9. [DUET](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/duet.py): this model is an implementation of
Learning to Match Using Local and Distributed Representations of Text for Web Search
10. [K-NRM](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/knrm.py): this model is an implementation of
End-to-End Neural Ad-hoc Ranking with Kernel Pooling
11. [CONV-KNRM](https://github.com/NTMC-Community/MatchZoo/tree/master/matchzoo/models/conv_knrm.py): this model is an implementation of
Convolutional neural networks for soft-matching n-grams in ad-hoc search
12. models under development:
Match-SRNN,
DeepRank,
BiMPM ....
## Citation
If you use MatchZoo in your research, please use the following BibTex entry.
```
@article{fan2017matchzoo,
title={Matchzoo: A toolkit for deep text matching},
author={Fan, Yixing and Pang, Liang and Hou, JianPeng and Guo, Jiafeng and Lan, Yanyan and Cheng, Xueqi},
journal={arXiv preprint arXiv:1707.07270},
year={2017}
}
```
## Development Team
## Contribution
Please make sure to read the [Contributing Guide](./CONTRIBUTING.md) before creating a pull request. If you have a MatchZoo-related paper/project/compnent/tool, send a pull request to [this awesome list](https://github.com/NTMC-Community/awaresome-neural-models-for-semantic-match)!
Thank you to all the people who already contributed to MatchZoo!
[Jianpeng Hou](https://github.com/HouJP), [Lijuan Chen](https://github.com/githubclj), [Yukun Zheng](https://github.com/zhengyk11), [Niuguo Cheng](https://github.com/niuox), [Dai Zhuyun](https://github.com/AdeDZY), [Aneesh Joshi](https://github.com/aneesh-joshi), [Zeno Gantner](https://github.com/zenogantner), [Kai Huang](https://github.com/hkvision), [stanpcf](https://github.com/stanpcf), [ChangQF](https://github.com/ChangQF), [Mike Kellogg
](https://github.com/wordreference)
## Project Organizers
- Jiafeng Guo
* Institute of Computing Technology, Chinese Academy of Sciences
* [Homepage](http://www.bigdatalab.ac.cn/~gjf/)
- Yanyan Lan
* Institute of Computing Technology, Chinese Academy of Sciences
* [Homepage](http://www.bigdatalab.ac.cn/~lanyanyan/)
- Xueqi Cheng
* Institute of Computing Technology, Chinese Academy of Sciences
* [Homepage](http://www.bigdatalab.ac.cn/~cxq/)
## License
[Apache-2.0](https://opensource.org/licenses/Apache-2.0)
Copyright (c) 2015-present, Yixing Fan (faneshion)