如何使一层输出两层,一层连接到Keras的两层?

问题描述 投票:0回答:1

我想在Keras中建立一个模型,这样的层连接如下:

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

这种类型的连接是Segnet,在Caffe中很容易做到。但我不知道如何用keras实现。

有人可以帮帮我吗?

machine-learning deep-learning keras caffe
1个回答
3
投票

在Keras也很容易,但你需要使用Keras Functional API。

在这里你可以找到一个例子https://keras.io/getting-started/functional-api-guide/

enter image description here

和代码:

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)
© www.soinside.com 2019 - 2024. All rights reserved.