by YuxianMeng

A Pytorch implementation of "Matrix Capsules with EM routing"

207 Stars 40 Forks Last release: Not found 53 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


This is a pytorch implementation of Matrix Capsules with EM routing

, there are two implemented classes:
. The ClassCapsules in the paper is actually a special case of
with whole receptive field, transformation matrix sharing and Coordinate Addition.

, I define a CapsNet in the paper using classes in
, and could be used to train a model for MNIST dataset.

Train a small CapsNet on MNIST

python -batch_size=64 -lr=2e-2 -num_epochs=5 -r=1 -print_freq=5

Note: * more args can be found in
, and if you want to change A,B,C,D, go to
line 62
* m and lambda schedule need to be changed if you want to train a capsnet with r=2 or 3. The default schedule make capsnet does not converge in those cases.


The test accuracy is around 97.6% after 1 epoch and 98.7% after 2 epochs of training with a small Capsule of A,B,C,D,r = 64,8,16,16,1. After 30 epochs of training, the best acc is around 99.3%. More results on different configurations are welcomed.


  • using more matrix operation rather than
    iteration in E-step of
  • make capsules work when heightin != widthin
  • find better lambda/m schedule to speed up the convergence.

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.