如何在 Chainer 模型中打印图层?
How do I print layers in a Chainer model?
我有一个 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),
我想打印模型中的图层。谷歌搜索 "chainer print layers" 是徒劳的。
有人知道如何在 chainer 中打印图层吗?
抱歉,这很简单,详细信息 here
提到的答案显示计算图。当您以 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()
或者,
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),
)
并且,
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),
)
我有一个 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),
我想打印模型中的图层。谷歌搜索 "chainer print layers" 是徒劳的。
有人知道如何在 chainer 中打印图层吗?
抱歉,这很简单,详细信息 here
提到的答案显示计算图。当您以 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()
或者,
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),
)
并且,
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),
)