I am trying to debug my model_builder function in Keras Functional API by printing the shapes of intermediate tensors. However, none of the methods I have tried so far seem to work as expected.
Here's the code snippet for my model_builder function:
def model_builder(hp, vocab_size, max_token_size):
text_input = tf.keras.Input(shape=(max_token_size,), dtype="int32", name="text_input")
# Embedding layer
embedding_layer = tf.keras.layers.Embedding(
input_dim=vocab_size,
output_dim=128,
input_length=max_token_size
)(text_input)
# Attempt 1: Using Python's print
print("Embedding Layer Shape (Attempt 1):", embedding_layer.getOutput(0)) # This throws an error
# Attempt 2: Using a Lambda layer with tf.print
embedding_layer = tf.keras.layers.Lambda(
lambda x: tf.print("Embedding Layer Output Shape (Attempt 2):", tf.shape(x)) or x
)(embedding_layer)
# Attempt 3: Using tf.print directly
tf.print("Embedding Layer Output Shape (Attempt 3):", tf.shape(embedding_layer))
I understand that Keras Functional API constructs a symbolic graph and print or tf.print might not work directly in graph construction. How can I print intermediate tensor shapes while building the model? This is necessary because I have multiple inputs and am getting unmatched matrix size errors. I need to understand at which layer this occurs. Best Regards, Ferda
