Why does the Keras (Tensorflow 2.0) model does not include the variables of matrix multiplication, when plotted?

Viewed 77

I am using the following code to build a deep learning model in Keras (Tensorflow 2.0).

import tensorflow as tf
keras = tf.keras
from keras.layers import Input, Dense
from keras.models import Model

a = Input(shape=(138,7), name='inputP')
b = Input(shape=(138,7), name='inputQ')
c = tf.transpose(b, [0,2,1])
d = tf.matmul(c,a)
e = Dense(15,activation = 'relu')(d)
model = Model([a,b],e)
keras.utils.plot_model(model)

Following output is generated:

enter image description here

Here, the inputP is not included in the plotted graph. What could be the reason for it?

1 Answers

To make plot_model works properly, replace all those tensorflow operations like tf.transpose and tf.matmul with lambda layers such that every node in the functional API is a keras layer, i.e.,

import tensorflow as tf

a = tf.keras.Input((138,7), name='inputP')
b = tf.keras.Input((138,7), name='inputQ')
c = tf.keras.layers.Lambda(lambda x: tf.transpose(x, [0,2,1]),name='transpose')(b)
d = tf.keras.layers.Lambda(lambda x: tf.matmul(x[0],x[1]),name='matmul')([c,a])
e = tf.keras.layers.Dense(15,activation = 'relu')(d)
model = tf.keras.Model([a,b],e)
tf.keras.utils.plot_model(model)
Related