2017-03-02 5 views
4

길쌈 신경 네트워크의 고밀도 레이어에 변수를 추가 할 수 있는지 궁금합니다 (이전의 길쌈 레이어의 연결뿐 아니라 추가 기능이있을 것입니다). 차별적 목적으로 사용될 수있는 세트)? 이것이 가능하다면 누구나 그렇게하는 방법을 설명하는 예제/문서를 가르쳐 줄 수 있습니까?Keras/TensorFlow CNN 고밀도 레이어에 변수 추가

Keras를 사용하기를 희망하지만 Keras가 너무 제한적일 경우 TensorFlow를 사용해도 행복합니다.

EDIT :이 경우 작동해야한다고 생각하는 방식은 신경망에 이미지와 관련 기능 세트가 포함 된 목록을 제공하는 것입니다 (관련 분류를 학습하는 동안).

EDIT2 :

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 찾을 수있다 : 당신은이 convoluton_model 당신은 다음과 같은 방식으로이 작업을 수행 할 수 있다고 가정

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

당신이 구축을 위해 노력하고있는 아키텍처는 무엇입니까? 내가 원하는 구조를 추가 한 @NassimBen 지금 당신의 질문 –

+0

을 이해하기 모르겠어요! :) –

답변

3

실제로 @Marcin이 말했듯이 병합 레이어를 사용할 수 있습니다.

나는이에 대한 Functionnal API를 사용하는 것이 좋습니다. 익숙하지 않은 경우 some doc here을 읽어보십시오. 여기

인 keras API 사용하여 한 doodled 네트워크 모델 :

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

확인 : 내가 원하는 구조는 같은 형태 서로 다른 입력을 병합하는 방법에 대한 예입니다.

관련 문제