首页 文章

将变量添加到Keras / TensorFlow CNN密集层中

提问于
浏览
5

我想知道是否有可能将一个变量添加到卷积神经网络的密集层中(以及来自先前卷积层的连接,还有一个可用于歧视目的的附加功能集)?如果这是可能的,有人可以给我一个示例/文档解释如何这样做吗?

我希望使用Keras,但如果Keras限制太多,我很乐意使用TensorFlow .

编辑:在这种情况下,我认为这应该工作的方式是我提供一个包含图像和相关功能集的列表到神经网络(并在训练期间相关的分类) .

EDIT2:我想要的架构看起来像:

___________      _________      _________      _________     ________    ______
              | Conv    |     | Max    |     | Conv    |     | Max    |    |       |   |     |
    Image --> | Layer 1 | --> | Pool 1 | --> | Layer 2 | --> | Pool 2 | -->|       |   |     |
              |_________|     |________|     |_________|     |________|    | Dense |   | Out |
                                                                           | Layer |-->|_____|
   Other      ------------------------------------------------------------>|       |
   Data                                                                    |       |
                                                                           |_______|

2 回答

  • 1

    实际上,正如@Marcin所说,你可以使用合并层 .

    我建议你使用Functionnal API . 如果您不熟悉它,请阅读some doc here .

    以下是使用keras API的涂鸦网络模型:

    from keras.layers.core import *
    from keras.models import Model
    
    # this is your image input definition. You have to specify a shape. 
    image_input = Input(shape=(32,32,3))
    # Some more data input with 10 features (eg.)
    other_data_input = Input(shape=(10,))    
    
    # First convolution filled with random parameters for the example
    conv1 = Convolution2D(nb_filter = nb_filter1, nb_row = nb_row1, nb_col=_nb_col1, padding = "same", activation = "tanh")(image_input)
    # MaxPool it 
    conv1 = MaxPooling2D(pool_size=(pool_1,pool_2))(conv1)
    # Second Convolution
    conv2 = Convolution2D(nb_filter = nb_filter2, nb_row = nb_row2, nb_col=_nb_col2, padding = "same", activation = "tanh")(conv1)
    # MaxPool it
    conv2  = MaxPooling2D(pool_size=(pool_1,pool_2))(conv2)
    # Flatten the output to enable the merge to happen with the other input
    first_part_output = Flatten()(conv2)
    
    # Merge the output of the convNet with your added features by concatenation
    merged_model = keras.layers.concatenate([first_part_output, other_data_input])
    
    # Predict on the output (say you want a binary classification)
    predictions = Dense(1, activation ='sigmoid')(merged_model)
    
    # Now create the model
    model = Model(inputs=[image_input, other_data_input], outputs=predictions)
    # see your model 
    model.summary()
    
    # compile it
    model.compile(optimizer='adamax', loss='binary_crossentropy')
    

    在那里你去:)最后很容易定义你想要的输入和输出数量,只需在创建 Model 对象时在列表中指定它们 . 如果适合它,也可以在列表中单独提供它们 .

  • 4

    好的,假设您有 convoluton_model ,您可以通过以下方式执行此操作:

    convolution_model = Flatten()(convolution_model) # if it wasn't flattened before
    static_features_input = Input(shape=(static_features_size,))
    blended_features = merge([convolution_model, static_features_input], mode='concat')
    ... here you are defining a blending model with blended features as input
    

    Here您可以找到有关如何合并不同输入的示例 .

相关问题