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

时间:2017-03-02 13:33:49

标签: python machine-learning tensorflow keras

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

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

编辑:在这种情况下,我认为这应该有效的方式是我提供一个包含图像和相关特征集的列表到神经网络(以及在训练期间相关的分类)。

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

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

2 个答案:

答案 0 :(得分:5)

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

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

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

url(r'^ex/(?P<example>[\w ()]+)$', views.example, name='example'),

你去了:)最后很容易定义你想要的输入和输出数量,只需在创建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') 对象时在列表中指定它们。当你适合它时,也要在列表中单独喂它们。

答案 1 :(得分:1)

好的,假设您拥有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您可以找到有关如何合并不同输入的示例。