打印出网络体系结构中每一层的形状 [英] print out the shape of each layer in the net architecture

查看:226
本文介绍了打印出网络体系结构中每一层的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Keras, we can define the network as follows. Are there any way to output the shape after each layer. For instance, I want to print out the shape of inputs after the line defining inputs, then print out the shape of conv1 after the line defining conv1, etc.

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)

推荐答案

如果层具有单个节点(即,如果它不是共享层),则可以获取其输入张量,输出张量,输入形状和输出形状通过:layer.input_shape

If a layer has a single node (i.e. if it isn't a shared layer), you can get its input tensor, output tensor, input shape and output shape via: layer.input_shape

from keras.utils.layer_utils import layer_from_config

config = layer.get_config()
layer = layer_from_config(config)

来源: https://keras.io/layers/about-keras-layers/

这可能是最简单的方法:

May be this the easiest way to do:

model.layers[layer_of_interest_index].output_shape

这篇关于打印出网络体系结构中每一层的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆