Tensorflow 打印出网络架构中各层的形状

Tensorflow 打印出网络架构中各层的形状,tensorflow,deep-learning,theano,keras,Tensorflow,Deep Learning,Theano,Keras,在Keras中,我们可以如下定义网络。是否有任何方法可以在每层之后输出形状。例如,我想在定义inputs行之后打印inputs的形状,然后在定义conv1行之后打印conv1的形状,等等 inputs = Input((1, img_rows, img_cols)) conv1 = Convolution2D(64, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same'

在Keras中,我们可以如下定义网络。是否有任何方法可以在每层之后输出形状。例如,我想在定义
inputs
行之后打印
inputs
的形状,然后在定义
conv1
行之后打印
conv1
的形状,等等

inputs = Input((1, img_rows, img_cols))
conv1 = Convolution2D(64, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(inputs)
conv1 = Convolution2D(64, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Convolution2D(128, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(pool1)
conv2 = Convolution2D(128, 3, 3, activation='relu', init='lecun_uniform', W_constraint=maxnorm(3), border_mode='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

只需使用
model.summary()
,这将为您提供漂亮的打印效果。

如果一个层有一个节点(即,如果它不是共享层),您可以通过:
layer.input\u shape

from keras.utils.layer_utils import layer_from_config

config = layer.get_config()
layer = layer_from_config(config)
资料来源:

这可能是最简单的方法:

model.layers[layer_of_interest_index].output_shape

要打印完整的模型及其所有依赖项,还可以查看以下内容:

我使用此命令将模型可视化保存为png:

from keras.utils.visualize_util import plot
plot(model, to_file='model.png')
如果只想打印图层形状,可以执行以下操作:

layer = model.layers[-1]
print(layer.output._keras_shape)

打印:(无、1224224)#个滤镜、频道、x_dim、y_dim

您能详细介绍一下如何使用它吗?例如,我想打印pool1的信息。我尝试了config=pool1.get_config(),但没有成功。谢谢。如果我只想打印出一个特定的层,即pool1,如何实现?最简单的方法是命名该层,然后使用model.get_layer('pool1')找到它。