如何在Chainer模型中打印图层?

时间:2019-12-15 23:48:49

标签: neural-network chainer

我有一个chainer模型。例如这样的东西:

导入chainer.links为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),

我想打印模型中的图层。谷歌搜索“链式打印层”是徒劳的。

有人知道如何在链接器中打印图层吗?

2 个答案:

答案 0 :(得分:1)

提到的答案显示计算图。当您以pythonic或顺序方式编写模型时,请在变量中初始化模型,例如

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)
)

然后

print(model)

这将给出以下内容:

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),
)

答案 1 :(得分:0)

对不起,这很简单,详细信息here