【问题标题】:print out the shape of each layer in the net architecture打印出网络架构中每一层的形状
【发布时间】:2017-04-05 15:55:26
【问题描述】:

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

【问题讨论】:

    标签: tensorflow deep-learning theano keras


    【解决方案1】:

    如果一个层有一个节点(即如果它不是一个共享层),你可以通过: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/

    这可能是最简单的方法:

    model.layers[layer_of_interest_index].output_shape
    

    【讨论】:

    • 您能否详细说明如何使用它?比如我想打印pool1的信息。我试过 config = pool1.get_config(),没用。
    【解决方案2】:

    要打印完整的模型及其所有依赖项,您还可以查看此处:https://keras.io/visualization/

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

    from keras.utils.visualize_util import plot
    plot(model, to_file='model.png')
    

    如果您只想打印图层形状,您可以执行以下操作:

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

    打印:(None, 1, 224, 224) # Nr.过滤器、通道、x_dim、y_dim

    【讨论】:

    • 谢谢。如果我只想打印一个特定的层,即pool1,该怎么做?
    • 最简单的方法是给图层命名,然后用model.get_layer('pool1')找到它。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      相关资源
      最近更新 更多