Having a model instance as input, I would like to create a function that prints the input shape of the model layers. I am aware of model.layers() but this is not general enough for what I want. Layers in my models might not have a sequential order, and a layer might receive its input from a layer different than the one preceding it. I would also to have tensorops included in case they are present in the model.
Hi @Nada-Nada, You can iterate through the layers in the model and can get the input shapes of the layers. For example,
input1 = keras.Input(shape=(128,))
input2 = keras.Input(shape=(64,))
dense1 = keras.layers.Dense(64)(input1)
dense2 = keras.layers.Dense(32)(input2)
concat = keras.layers.Concatenate()([dense1, dense2])
output = keras.layers.Dense(10)(concat)
model = keras.Model(inputs=[input1, input2], outputs=output)
for layer in model.layers:
print(layer.input)
Thank You.
Hi @Kiran_Sai_Ramineni , Thank you for your answer. My model has this architecture:
class NeuralNetwork(tf.keras.Model):
def __init__(self):
super().__init__()
self.l1 = layers.Embedding(input_dim=5000, output_dim=50)
self.l2 = layers.Dropout(rate=0.5)
self.l3 = layers.Conv1D(filters=200, kernel_size=4, strides=1, padding='valid', activation='relu')
self.l4 = layers.MaxPool1D(pool_size=2, strides=2, padding='valid')
self.l5 = layers.Conv1D(filters=200, kernel_size=5, strides=1, padding='valid', activation='relu')
self.l6 = layers.MaxPool1D(pool_size=2, strides=2, padding='valid')
self.l7 = layers.Dropout(rate=0.15)
self.l8 = layers.GRU(units=100, activation=None, dropout=0.0)
self.l9 = layers.Dense(units=400, activation='relu')
self.l10 = layers.Dropout(rate=0.1)
self.l11 = layers.Dense(units=1, activation='sigmoid')
def call(self, x):
x = self.l1(x)
x = self.l2(x)
x_1 = self.l3(x)
x_1 = self.l4(x_1)
x_2 = self.l5(x)
x_2 = self.l6(x_2)
x_2 = tf.concat([x_1, x_2], axis=1)
x_2 = self.l7(x_2)
x_2 = self.l8(x_2)
x_2 = self.l9(x_2)
x_2 = self.l10(x_2)
x_2 = self.l11(x_2)
return x_2
x = tf.random.uniform((1, 299))
model = NeuralNetwork()
out = model(x)
Unfortunately, model.layers
is not giving me the correct input shapes.
I also cannot change how the model is defined.
Thank you
Hi @Nada-Nada, I have tried in an alternative way by creating a dictionary in the class by adding the layer name and input and able to get those. please refer to this gist for working code example. Thank You.
Thank you @Kiran_Sai_Ramineni, much appreciated.