Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: The layer sequential_6 has never been called and thus has no defined input #20339

Open
dgcovell opened this issue Oct 11, 2024 · 1 comment

Comments

@dgcovell
Copy link

I am implementing the autoencoder code at https://jovian.ai/1potdish/autoencoder-tensorflow-pytorch
where I can successfully complete
optimizer = keras.optimizers.Adam()
loss = keras.losses.MeanSquaredError
model.compile(optimizer=optimizer, loss= loss, metrics=["accuracy"])
model.fit(x , x, epochs=20)
which ends with
<keras.src.callbacks.history.History object at 0x00000230CA119DF0>

However the next line produces an Error
mm = keras.Model(inputs=model.input, outputs=model.output)
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\covelld\AppData\Local\anaconda3\Lib\site-packages\keras\src\ops\operation.py", line 254, in input
return self._get_node_attribute_at_index(0, "input_tensors", "input")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\covelld\AppData\Local\anaconda3\Lib\site-packages\keras\src\ops\operation.py", line 285, in _get_node_attribute_at_index
raise ValueError(
ValueError: The layer sequential_6 has never been called and thus has no defined input.

Blogs indicate various ways to call to last sequential layer, non of which eliminate the Error.

If anyone on the Keras/Team could offer a suggestion, that would be appreciated. I do not know whether this is a keras
issue of a tensorflow issue. Regardless, I have made no progress on this issue, and I see that it is a common Error.
But the exact solution is unclear.

Thanks in advance
David Covell, Ph.D.
NIH-NCI

@mehtamansi29
Copy link
Collaborator

Hi @dgcovell -

Thanks for reporting the issue. Here CAE model is created using Squential keras API and later trying to creating model with Functional API here mm = keras.Model(inputs=model.input, outputs=model.output) using Sequential model inputs and output. But Squential model of keras doesn't have input and output.
By creating CAE model using Functional API will resolve the error.

def CAE(input_shape=(28, 28, 1), filters=[32, 64, 128, 10]):
    input_layer = keras.layers.Input(shape=input_shape)
    if input_shape[0] % 8 == 0:
        pad3 = 'same'
    else:
        pad3 = 'valid'
    x = keras.layers.Conv2D(filters[0], 5, strides=2, padding='same', activation='relu', name='conv1')(input_layer)
    x = keras.layers.Conv2D(filters[1], 5, strides=2, padding='same', activation='relu', name='conv2')(x)
    x = keras.layers.Conv2D(filters[2], 3, strides=2, padding=pad3, activation='relu', name='conv3')(x)   
    x = keras.layers.Flatten()(x)
    x = keras.layers.Dense(units=filters[3], name='embedding')(x)
    x = keras.layers.Dense(units=filters[2]*int(input_shape[0]/8)*int(input_shape[0]/8), activation='relu')(x)
    x = keras.layers.Reshape((int(input_shape[0]/8), int(input_shape[0]/8), filters[2]))(x)
    x = keras.layers.Conv2DTranspose(filters[1], 3, strides=2, padding=pad3, activation='relu', name='deconv3')(x)
    x = keras.layers.Conv2DTranspose(filters[0], 5, strides=2, padding='same', activation='relu', name='deconv2')(x)
    output_layer = keras.layers.Conv2DTranspose(input_shape[2], 5, strides=2, padding='same', name='deconv1')(x)
    model = keras.Model(inputs=input_layer, outputs=output_layer)
    model.summary()
    return model

Attached gist here for the reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants