tachyon
tachyon

Reputation: 479

keras model using intermediate layers as inputs and outputs

I have a basic LSTM autoencoder in Keras (Tensoflow backend). 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! I also tried to make the decoder using get_layer(...).input and get_layer(...).output but it throws he same error.

An explanation would help me a lot.

Upvotes: 3

Views: 438

Answers (1)

Stewart_R
Stewart_R

Reputation: 14515

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. 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

Upvotes: 2

Related Questions