简体   繁体   English

使用中间层作为输入和输出的 keras 模型

[英]keras model using intermediate layers as inputs and outputs

I have a basic LSTM autoencoder in Keras (Tensoflow backend).我在 Keras(Tensoflow 后端)中有一个基本的 LSTM 自动编码器。 The structure of the model is as follows:该模型的结构如下:

l0 = Input(shape=(10, 2))
l1 = LSTM(16, activation='relu', return_sequences=True)(l0)
l2 = LSTM(8, activation='relu', return_sequences=False)(l1)
l3 = RepeatVector(10)(l2)
l4 = LSTM(8, activation='relu', return_sequences=True)(l3)
l5 = LSTM(16, activation='relu', return_sequences=True)(l4)
l6 = TimeDistributed(Dense(2))(l5)

I can extract and compile the encoder and the autoencoder as follows:我可以按如下方式提取和编译编码器和自动编码器:

encoder = Model(l0, l2)
auto_encoder = Model(l0, l6)
auto_encoder.compile(optimizer='rmsprop', loss='mse', metrics=['mse'])

However when I try to make a model out of intermediate layers such as:但是,当我尝试使用中间层制作模型时,例如:

decoder = Model(inputs=l3, outputs=l6)

I get the following error:我收到以下错误:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_12:0", shape=(?, 10, 2), dtype=float32) at layer "input_12". The following previous layers were accessed without issue: []

I don't understand how l3 and l6 are disconnected with respect to each other!我不明白l3l6是如何相互断开的! I also tried to make the decoder using get_layer(...).input and get_layer(...).output but it throws he same error.我还尝试使用get_layer(...).inputget_layer(...).output制作解码器,但它抛出了同样的错误。

An explanation would help me a lot.一个解释对我很有帮助。

The issue is that there is no input layer for the model you are trying to create with:问题是您尝试创建的模型没有输入层:

decoder = Model(inputs=l3, outputs=l6)

You could create one by producing a new Input() layer with the correct shape then accessing each of your existing layers.您可以通过生成一个具有正确形状的新Input()图层然后访问每个现有图层来创建一个。 Something like this:像这样的东西:

input_layer = Input(shape=(8,))
l3 = auto_encoder.layers[3](input_layer)
l4 = auto_encoder.layers[4](l3)
l5 = auto_encoder.layers[5](l4)
l6 = auto_encoder.layers[6](l5)

decoder = Model(input_layer, l6)
decoder.summary()
Model: "model_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_14 (InputLayer)        [(None, 8)]               0         
_________________________________________________________________
repeat_vector_2 (RepeatVecto (None, 10, 8)             0         
_________________________________________________________________
lstm_12 (LSTM)               (None, 10, 8)             544       
_________________________________________________________________
lstm_13 (LSTM)               (None, 10, 16)            1600      
_________________________________________________________________
time_distributed_1 (TimeDist (None, 10, 2)             34        
=================================================================
Total params: 2,178
Trainable params: 2,178
Non-trainable params: 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM