Git Product home page Git Product logo

matrix-capsules-em-pytorch's Introduction

Matrix Capsules with EM Routing

A PyTorch implementation of Matrix Capsules with EM Routing

Usage

  1. Install PyTorch

  2. Start training (default: MNIST)

python train.py

Note that master is upgraded to be compatiable with PyTorch 0.4.0. If you want to use the old version of PyTorch, please

git checkout 0.3.1.post3

MNIST experiments

The experiments are conducted on TitanXP. Specific setting is lr=0.01, batch_size=128, weight_decay=0, Adam optimizer, without data augmentation. The paper does not mention the specific scheduler for inverse_temperature, it is fixed to 0.001 in our setting. As our experiments shown, \lambda between 1e-2 and 1e-4 achieves similar results. A large lambda may prevent the model from convergence.

Following is the result after 30 epochs training:

Arch Iters Coord Add Loss BN Test Accuracy
A=64, B=8, C=D=16 1 Y Spread Y 97.1
A=64, B=8, C=D=16 2 Y Spread Y 99.1
A=64, B=8, C=D=16 3 Y Spread Y 97.5
A=64, B=8, C=D=16 2 N Spread Y 99.0
A=64, B=8, C=D=16 2 Y Spread N 98.9
A=64, B=8, C=D=16 2 Y Cross-Ent Y 97.8
A=B=C=D=32 2 Y Spread Y 99.3

The training time of A=64, B=8, C=D=16 for a 128 batch is around 1.05s. The training time of A=B=C=D=32 for a 32 batch is around 1.45s.

smallNORB experiments

python train.py --dataset smallNORB --batch-size 32 --test-batch-size 256

As the paper suggests, the image is resized to 48x48, followed by randomly cropping a 32x32 patch and randomly changing brightness and contrast. Since BN is used after first normal conv layer, we do not normalize the input.

Following is the result after 50 epochs training:

Arch Iters Coord Add Loss BN Test Accuracy
A=64, B=8, C=D=16 1 Y Spread Y 74.81
A=64, B=8, C=D=16 2 Y Spread Y 89.52
A=64, B=8, C=D=16 3 Y Spread Y 82.55
A=B=C=D=32 2 Y Spread Y 90.03

A weird thing is that large batch size seems to result in poor result.

Reference

matrix-capsules-em-pytorch's People

Contributors

moeinh77 avatar yl-1993 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

Watchers

 avatar  avatar

matrix-capsules-em-pytorch's Issues

The smallNORB link

The smallNORB link is dead, can you please switch the link to some other server ?

class-capsule 层是否有一个错误?关于权重共享

这个 地方如果是class_caps 类型,这个地方的地方是否应该修改 为

修改代码

if w_shared:
     hw = int(B / w.size(1))
     w = w.repeat(1, hw, 1, 1, 1)
else:
    w = w.repeat(b, 1, 1, 1, 1)

原代码

    def transform_view(self, x, w, C, P, w_shared=False):
        """
            For conv_caps:
                Input:     (b*H*W, K*K*B, P*P)
                Output:    (b*H*W, K*K*B, C, P*P)
            For class_caps:
                Input:     (b, H*W*B, P*P)
                Output:    (b, H*W*B, C, P*P)
        """
        b, B, psize = x.shape
        assert psize == P*P

        x = x.view(b, B, 1, P, P)
        if w_shared:
            hw = int(B / w.size(1))
            w = w.repeat(1, hw, 1, 1, 1)

        w = w.repeat(b, 1, 1, 1, 1)
        x = x.repeat(1, 1, C, 1, 1)
        v = torch.matmul(x, w)
        v = v.view(b, B, C, P*P)
        return v

M步有个错误吧。

ConvCaps的m-step成员函数,第一行代码。

`r = r* a_in.

该函数的初值 r 是形状是 (b, B, C, 1), 而 a_in 的形状是 (b, C, 1). 不能广播,直接做哈达玛积会报错的。是不是需要对 a_in unsqueeze一下?

Out of memory

Even if I set the training batch size to be 1, it still showed out of memory. And my GPU is GTX1080Ti which is enough for it.

Traceback (most recent call last):
File "/home/hongjin/PycharmProjects/Matrix-Capsules-EM-PyTorch-master/train.py", line 223, in
main()
File "/home/hongjin/PycharmProjects/Matrix-Capsules-EM-PyTorch-master/train.py", line 210, in main
best_acc = test(test_loader, model, criterion, device)
File "/home/hongjin/PycharmProjects/Matrix-Capsules-EM-PyTorch-master/train.py", line 175, in test
output = model(data)
File "/home/hongjin/anaconda3/envs/python3.6/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in call
result = self.forward(*input, **kwargs)
File "/home/hongjin/PycharmProjects/Matrix-Capsules-EM-PyTorch-master/model/capsules.py", line 338, in forward
x = self.conv_caps1(x)
File "/home/hongjin/anaconda3/envs/python3.6/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in call
result = self.forward(*input, **kwargs)
File "/home/hongjin/PycharmProjects/Matrix-Capsules-EM-PyTorch-master/model/capsules.py", line 260, in forward
p_out, a_out = self.caps_em_routing(v, a_in, self.C, self.eps)
File "/home/hongjin/PycharmProjects/Matrix-Capsules-EM-PyTorch-master/model/capsules.py", line 182, in caps_em_routing
a_out, mu, sigma_sq = self.m_step(a_in, r, v, eps, b, B, C, psize)
File "/home/hongjin/PycharmProjects/Matrix-Capsules-EM-PyTorch-master/model/capsules.py", line 124, in m_step
sigma_sq = torch.sum(coeff * (v - mu)**2, dim=1, keepdim=True) + eps
RuntimeError: CUDA error: out of memory

Process finished with exit code 1

How many Gaussians are in the GMM in EM Routing?

Thanks for this great resource! I am trying to understand the original paper and am using this code as a template.

I am not sure how EM routing is a form of GMM as described in the original paper. For example, how many Gaussians are we fitting? To what data and in what ambient dimension?

It appears that the dimension of mu is 32x4x4=CxPxP. https://github.com/YuxianMeng/Matrix-Capsules-pytorch, which you cite, has mu of size CxWxWxPxP where W is the size of the convolutional kernel. Is this correct?

ConvCaps p_in a_in view

I think there is an issue in the way the input tensor x is reshaped in order to extract a_in and p_in.
It seems to me that the dimensions of a_in and p_in require a permutation before applying Tensor.view().

Note that I changed the training batch size to 16, also I am using A, B, C, D = 32, 32, 32, 32.

Transformation before view:

After this line:

p_in = x[:, :, :, :, :, :self.B*self.psize].contiguous()

I get this:

p_in.shape
Out[2]: torch.Size([16, 3, 3, 6, 6, 512])

View:

The view is done in the following way:

p_in = p_in.view(b*oh*ow, self.K*self.K*self.B, self.psize)

To do the view in this way, p_in.shape should be:
torch.Size([16, 6, 6, 3, 3, 512])

Do you agree? I am new to Pytorch, so I might misunderstand the way Tensor.view() works.

The accuracy of the best result

The paper reaches a much lower Err-rate(about 1.5%) for smallNORB, but it seems that no one here can achieve the level of state-of-art. i wonder why : )

This maybe a bug

In "add_coord(self, v, b, h, w, B, C, psize):" function,

coor = 1. * torch.arange(h) / h

always ouput zeros,and I think that should be changed as

coor = torch.arange(h).float()/h
.

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.