Need help with mac-network?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

465 Stars 119 Forks Apache License 2.0 71 Commits 14 Opened issues


Implementation for the paper "Compositional Attention Networks for Machine Reasoning" (Hudson and Manning, ICLR 2018)

Services available


Need anything else?

Contributors list

Compostional Attention Networks for Real-World Reasoning

Drew A. Hudson & Christopher D. Manning

Please note: We have updated the GQA challenge deadline to be May 15. Best of Luck! :)

This is the implementation of Compositional Attention Networks for Machine Reasoning (ICLR 2018) on two visual reasoning datasets: CLEVR dataset and the New GQA dataset (CVPR 2019). We propose a fully differentiable model that learns to perform multi-step reasoning. See our website and blogpost for more information about the model!

In particular, the implementation includes the MAC cell at
. The code supports the standard cell as presented in the paper as well as additional extensions and variants. Run

python -h
or see
for the complete list of options.

The adaptation of MAC as well as several baselines for the GQA dataset are located at the GQA branch.


For MAC:

  title={Compositional Attention Networks for Machine Reasoning},
  author={Hudson, Drew A and Manning, Christopher D},
  journal={International Conference on Learning Representations (ICLR)},

For the GQA dataset:

  title={GQA: A New Dataset for Real-World Visual Reasoning and Compositional Question Answering},
  author={Hudson, Drew A and Manning, Christopher D},
  journal={Conference on Computer Vision and Pattern Recognition (CVPR)},


  • Tensorflow (originally has been developed with 1.3 but should work for later versions as well).
  • We have performed experiments on Maxwell Titan X GPU. We assume 12GB of GPU memory.
  • See
    for the required python packages and run
    pip install -r requirements.txt
    to install them.


Before training the model, we first have to download the CLEVR dataset and extract features for the images:


To download and unpack the data, run the following commands:

mv CLEVR_v1.0 CLEVR_v1
mkdir CLEVR_v1/data
mv CLEVR_v1/questions/* CLEVR_v1/data/
The final command moves the dataset questions into the
directory, where we will put all the data files we use during training.

Feature extraction

Extract ResNet-101 features for the CLEVR train, val, and test images with the following commands:

python --input_image_dir CLEVR_v1/images/train --output_h5_file CLEVR_v1/data/train.h5 --batch_size 32
python --input_image_dir CLEVR_v1/images/val --output_h5_file CLEVR_v1/data/val.h5 --batch_size 32
python --input_image_dir CLEVR_v1/images/test --output_h5_file CLEVR_v1/data/test.h5 --batch_size 32


To train the model, run the following command:

python --expName "clevrExperiment" --train --testedNum 10000 --epochs 25 --netLength 4 @configs/args.txt

First, the program preprocesses the CLEVR questions. It tokenizes them and maps them to integers to prepare them for the network. It then stores a JSON with that information about them as well as word-to-integer dictionaries in the


Then, the program trains the model. Weights are saved by default to

and statistics about the training are collected in
, where
is the name we choose to give to the current experiment.


  • The number of examples used for training and evaluation can be set by
  • You can use the
    flag to restore and continue training a previously pre-trained model.
  • We recommend you to try out varying the number of MAC cells used in the network through the
    option to explore different lengths of reasoning processes.
  • Good lengths for CLEVR are in the range of 4-16 (using more cells tends to converge faster and achieves a bit higher accuracy, while lower number of cells usually results in more easily interpretable attention maps).

Model variants

We have explored several variants of our model. We provide a few examples in

. For instance, you can run the first by:
python --expName "experiment1" --train --testedNum 10000 --epochs 40 --netLength 6 @configs/args2.txt
uses a non-recurrent variant of the control unit that converges faster. -
incorporates self-attention into the write unit. -
adds control-based gating over the memory.

for further available options (Note that some of them are still in an experimental stage).


To evaluate the trained model, and get predictions and attention maps, run the following:

python --expName "clevrExperiment" --finalTest --testedNum 10000 --netLength 16 -r --getPreds --getAtt @configs/args.txt
The command will restore the model we have trained, and evaluate it on the validation set. JSON files with predictions and the attention distributions resulted by running the model are saved by default to
  • In case you are interested in getting attention maps (
    ), and to avoid having large prediction files, we advise you to limit the number of examples evaluated to 5,000-20,000.


After we evaluate the model with the command above, we can visualize the attention maps generated by running:

python --expName "clevrExperiment" --tier val 
(Tier can be set to
as well). The script supports filtering of the visualized questions by various ways. See
for further details.

To get more interpretable visualizations, it is highly recommended to reduce the number of cells to 4-8 (

). Using more cells allows the network to learn more effective ways to approach the task but these tend to be less interpretable compared to a shorter networks (with less cells).

Optionally, to make the image attention maps look a little bit nicer, you can do the following (using imagemagick):

for x in preds/clevrExperiment/*Img*.png; do magick convert $x -brightness-contrast 20x35 $x; done;

Thank you for your interest in our model! Please contact me at [email protected] for any questions, comments, or suggestions! :-)

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.