Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'Tensor' object is not callable | Keras Autoencoder

I'm creating an autoencoder in Keras following this tutorial and I keep getting the following error.

decoder = tf.keras.Model(encoded_input, decoded(input_img))
TypeError: 'Tensor' object is not callable

I believe it's something to do with not being able to use tensors in this way because of the nature of this type of object but I have some gaps in understanding of why and how to go about solving this.

Here is a minimal working example of my code:

# input_img input placeholder
input_img = tf.keras.layers.Input(shape=(16, 16, 1), name ="input")
encoded = tf.keras.layers.Dense(encoding_dim, activation='relu')(input_img)
decoded = tf.keras.layers.Dense(256, activation='sigmoid')(encoded)
autoencoder = tf.keras.Model(input_img, decoded)
encoder = tf.keras.Model(input_img, encoded)
encoded_input = tf.keras.layers.Input(shape=(encoding_dim,))
decoder = tf.keras.Model(input_img, decoded(encoded_input))
like image 249
iiooii Avatar asked May 12 '26 12:05

iiooii


1 Answers

Keras Model expects input & output arguments as layers, not tensors. The tutorial is correct - you appear to have missed a line right before decoder =: -- decoder_layer = autoencoder.layers[-1]

All together, with decoder = changed appropriately:

input_img = tf.keras.layers.Input(shape=(16, 16, 256), name ="input")
encoded   = tf.keras.layers.Dense(encoding_dim, activation='relu')(input_img)
decoded   = tf.keras.layers.Dense(256, activation='sigmoid')(encoded)

autoencoder = tf.keras.Model(input_img, decoded)
encoder     = tf.keras.Model(input_img, encoded)

encoded_input = tf.keras.layers.Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1] # gets 'decoded' = last layer of 'autoencoder'

decoder = tf.keras.Model(encoded_input, decoder_layer(encoded_input))
like image 180
OverLordGoldDragon Avatar answered May 15 '26 01:05

OverLordGoldDragon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!