How do I print layers in a Chainer model?

139 views Asked by At

I have a chainer model. For example something like this:

import chainer.links as L

c0=L.Convolution2D(3, 32, 3, 1, 1),
c1=L.Convolution2D(32, 64, 4, 2, 1),
c2=L.Convolution2D(64, 64, 3, 1, 1),

I want to print the layers in the model. Googling "chainer print layers" has been futile.

Does anybody know how to print the layers in chainer?

2

There are 2 answers

0
Lars Ericson On BEST ANSWER

Sorry this was an easy one, details here

0
TulakHord On

The answer mentioned displays the computational graph. When you are writing the model in pythonic way or in Sequential once you initialize the model in a variable for e.g

class MLP(Chain):

    def __init__(self, n_mid_units=100, n_out=10):
        super(MLP, self).__init__()
        with self.init_scope():
            self.l1 = L.Linear(None, n_mid_units)
            self.l2 = L.Linear(None, n_mid_units)
            self.l3 = L.Linear(None, n_out)

    def forward(self, x):
        h1 = F.relu(self.l1(x))
        h2 = F.relu(self.l2(h1))
        return self.l3(h2)
model = MLP()

OR,

model = Sequential(
L.Linear(10, 100),
F.relu,
L.Linear(100, 100),
F.relu,
L.Linear(100, 10)
)

then

print(model)

which will give something like this:

MLP(
(l1): Linear(in_size=None, out_size=100, nobias=False),
(l2): Linear(in_size=None, out_size=100, nobias=False),
(l3): Linear(in_size=None, out_size=10, nobias=False),
)

AND,

Sequential(
(0): Linear(in_size=10, out_size=100, nobias=False),
(1): <function relu at 0x7f2fc2227378>,
(2): Linear(in_size=100, out_size=100, nobias=False),
(3): <function relu at 0x7f2fc2227378>,
(4): Linear(in_size=100, out_size=10, nobias=False),
)