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

Tho*_*ell 8 python machine-learning keras tensorflow

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

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

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

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

              ___________      _________      _________      _________     ________    ______
              | Conv    |     | Max    |     | Conv    |     | Max    |    |       |   |     |
    Image --> | Layer 1 | --> | Pool 1 | --> | Layer 2 | --> | Pool 2 | -->|       |   |     |
              |_________|     |________|     |_________|     |________|    | Dense |   | Out |
                                                                           | Layer |-->|_____|
   Other      ------------------------------------------------------------>|       |
   Data                                                                    |       |
                                                                           |_______|
Run Code Online (Sandbox Code Playgroud)

Nas*_*Ben 5

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

我建议你使用Functionnal API.如果你不熟悉它,请在这里阅读一些文档.

以下是使用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')
Run Code Online (Sandbox Code Playgroud)

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