在Keras/TensorFlow CNN密集层中添加变量 [英] Adding a variable into Keras/TensorFlow CNN dense layer

查看:287
本文介绍了在Keras/TensorFlow CNN密集层中添加变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在卷积神经网络的密集层中添加一个变量(以及以前的卷积层的连接,是否还有其他功能集可用于区分目的)?如果可以的话,谁能指出我的示例/文档,说明如何执行此操作?

I was wondering if it is possible to add a variable into a convolutional neural network's dense layer (so as well as the connections from the previous convolutional layers, there would be an additional feature set that could be used for discriminatory purposes)? If this is possible, can anyone point me to an example/documentation explaining how to do so?

我希望使用Keras,但如果Keras的限制过于严格,我很乐意使用TensorFlow.

I am hoping to use Keras, but am happy to use TensorFlow if Keras is too restrictive.

在这种情况下,我认为应该起作用的方式是,我向神经网络提供了一个包含图像和相关特征集的列表(在训练相关分类期间).

In this case, the way that I would think that this should work is that I provide a list containing images and associated feature sets to the neural network (and during training the associated classifications).

我想要的架构看起来像:

The architecture that I want looks something like:

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

推荐答案

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

Indeed, as @Marcin said, you can use a merge layer.

我建议您为此使用Functionnal API.如果您不熟悉它,请阅读此处的一些文档.

I advise you to use the Functionnal API for this. If you're not familiar with it, read some doc here.

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

Here is your doodled network model using the 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对象时在列表中指定它们即可.当您适应它时,还应在列表中分别填充它们.

There you go :) It is quite easy in the end, define how many inputs and outputs you want, just specify them in a list when you create the Model object. When you fit it, also feed them separately, in a list.

这篇关于在Keras/TensorFlow CNN密集层中添加变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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