Как сделать так, чтобы один слой выводил два слоя, а один слой был связан с двумя слоями в Keras?

Я хочу сделать модель в Керасе, некоторые соединения слоев вроде этого:

    MaxPooling
      /\
     /  \  
 pooled poolmask    convLayer    
              \      /
               \    /
               upsample

Этот тип подключения - как Segnet, и это легко сделать в Caffe. Но я не знаю, как реализовать с keras.

Кто-нибудь мог мне помочь?


person spider    schedule 07.09.2017    source источник


Ответы (1)


В Keras это тоже легко, но вам нужно использовать Keras Functional API.

Здесь вы можете найти пример https://keras.io/getting-started/functional-api-guide/

введите здесь описание изображения

И код:

from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model

# Headline input: meant to receive sequences of 100 integers, between 1 and 10000.
# Note that we can name any layer by passing it a "name" argument.
main_input = Input(shape=(100,), dtype='int32', name='main_input')

# This embedding layer will encode the input sequence
# into a sequence of dense 512-dimensional vectors.
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)



# A LSTM will transform the vector sequence into a single vector,
# containing information about the entire sequence
lstm_out = LSTM(32)(x)


auxiliary_input = Input(shape=(5,), name='aux_input')
x = keras.layers.concatenate([lstm_out, auxiliary_input])

auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)

# We stack a deep densely-connected network on top
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

# And finally we add the main logistic regression layer
main_output = Dense(1, activation='sigmoid', name='main_output')(x)

model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])

model.compile(optimizer='rmsprop', loss='binary_crossentropy',
              loss_weights=[1., 0.2])

model.fit([headline_data, additional_data], [labels, labels],
          epochs=50, batch_size=32)
person Paddy    schedule 07.09.2017
comment
Очень интересное объяснение! Один вопрос: где определяется вспомогательный_выход? Генерируется ли он автоматически на основе входной / выходной части имени? - person sdgaw erzswer; 26.02.2018
comment
@sdgawerzswer и да, вы должны это определить. - person Paddy; 26.02.2018