2017-03-02 6 views
4

畳み込みニューラルネットワークの高密度層に変数を追加することが可能かどうか疑問に思っていました(これまでの畳み込み層からの接続だけでなく、差別目的のために使用することができます)?これが可能ならば、どのようにして行うのか説明しているサンプル/ドキュメントを誰かに教えてもらえますか?Keras/TensorFlow CNN高密度層に変数を追加する

私はKerasを使用したいと考えていますが、Kerasがあまりにも制限的な場合は、TensorFlowを使用してください。

EDIT:この場合、私はこれがうまくいかなければならないと思いますが、画像と関連するフィーチャセットを含むリストをニューラルネットワークに提供します(関連する分類をトレーニング中に)。

EDIT2:あなたは次のようにこれを行うことがconvoluton_modelを持っていると仮定して、

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

ビルドしようとしているアーキテクチャは何ですか? –

+0

@NassimBen私は今すぐ希望のアーキテクチャを追加しました! :) –

答えて

3

実際、@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オブジェクトを作成するときに発生します。あなたがそれに合ったときには、それらを別々にリストにも送ります。

+0

ありがとうございました。これは非常に便利です! –

+0

'.fit'を使ってこのモデルを訓練するとき、損失はすべてのエポックにわたって一定のままですか?なぜこのことが起こり得るのか考えていますか? –

+0

私は編集するでしょう、間違いは最後の層の活性化から来ます。 Sigmoidを1出力に、または「softmax」を2出力に使用してください。編集を参照してください:) –

1

OK::

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あなたは見つけることができる私が欲しいアーキテクチャはようになりますさまざまな入力をマージする方法の例。

関連する問題