如何用keras功能api查看神经网络的摘要

时间:2018-01-15 15:56:12

标签: python-2.7 keras keras-layer

我有一个非常大的神经网络,我正在使用keras功能api。我想监控模型中添加的图层的参数和形状,这些参数和形状尚未定义为input_img = Input(shape=(256, 256, 3)) tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img) tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1) #stage1 tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img) tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2) #stage2 tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img) tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3) #stage3 output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)

如果我有这样的模型

summary()

我希望在这些不同阶段的model.summary()这个进步的模型。我知道我们可以通过定义model=Model(input,output)key=value,但是我们可以在逐层进展时这样做吗?

3 个答案:

答案 0 :(得分:3)

您可以使用_keras_shape成员变量轻松获得任何Keras张量的编译时形状,例如:

input_img = Input(shape=(256, 256, 3))

tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
#stage1

tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
#stage2

tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)
#stage3

output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)

print("Output shape is: {}".format(output._keras_shape))

只要您有TensorVariable(图层的输出),您就可以在计算中的任何位置执行此操作。它与完整摘要不同,但它对调试有很大帮助。

答案 1 :(得分:1)

一个丑陋的回答(因为我没有漂亮的回答)

您可以根据需要定义任意数量的模型并获取摘要:

input_img = Input(shape=(256, 256, 3))

tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)

model = Model(input_img, [tower1,tower2,tower3])
model.summary()


tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)

model = Model(input_img, [tower1,tower2,tower3])
model.summary()

output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)

model = Model(input_img, output)
model.summary()

答案 2 :(得分:1)

没有很多声誉可以评论。

您必须参考TENSORBOARD以获取与深度学习相关的各种可视化,并且也易于使用。