1

I am trying to extract the layers from a sequential model to build an autoencoder. I trained the model on some data but when I try to get model.input from my model I get an error saying that it has never been called.

I set up and train the model here.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam

model = Sequential()
model.add(Input(shape=(X_train_scaled.shape[1],)))  # Input layer
model.add(Dense(128, activation='relu'))  # First hidden layer
model.add(Dense(64, activation='relu'))  # Second hidden layer
model.add(Dense(1))  # Output layer

optimiser = Adam(learning_rate = 0.001)
model.compile(optimizer = optimiser, loss='mse')

history = model.fit(X_train_scaled, y_train, validation_data=(X_test_scaled, y_test), epochs=100, batch_size=16)

And the following line causes the error.

from tensorflow.keras.models import Model

encoder = Model(inputs=model.input, outputs=model.layers[-2].output)  # -2 to exclude the last layer

I am very new to this, so any advice or direction would be appreciated.

1 Answer 1

3

You are creating initial model using Sequential API and trying to access the input attribute of the model on another model via functional API. As sequential model don't have an accessible input attribute. To resolve this you need to define the input implicitly by using model.layers[0].input or model.get_layer(index=0).input or else switch the sequential model to functional model. please refer this gist.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.