Convert keras layers to tensorflow lite


feat_size = 8
num_ch = 1
x = tf.keras.layers.Input(shape=(feat_size, num_ch), name="encoder_input")

encoder_conv_layer1 = tf.keras.layers.Conv1D(filters=1, kernel_size=(3), padding="same", strides=1, name="encoder_conv_1")(x)
encoder_norm_layer1 = tf.keras.layers.BatchNormalization(name="encoder_norm_1")(encoder_conv_layer1)
encoder_activ_layer1 = tf.keras.layers.LeakyReLU(name="encoder_leakyrelu_1")(encoder_norm_layer1)

shape_before_flatten = tf.keras.backend.int_shape(encoder_activ_layer1)[1:]
encoder_flatten = tf.keras.layers.Flatten()(encoder_activ_layer1)
print(encoder_flatten.shape)

latent_space_dim = 1

encoder_mu = tf.keras.layers.Dense(units=latent_space_dim, name="encoder_mu")(encoder_flatten)
encoder_log_variance = tf.keras.layers.Dense(units=latent_space_dim, name="encoder_log_variance")(encoder_flatten)

encoder_output = tf.keras.layers.Lambda(sampling, name="encoder_output")([encoder_mu, encoder_log_variance])

encoder = tf.keras.models.Model(x, encoder_output, name="encoder_model")
encoder.summary()

Can you please tell me how to convert tf.keras.layers.Lambda into tensorflow lite? or please recommend any alternative step for this.

Thank you :slight_smile:

Hi @Pankhi_Kashyap, You can convert, your complete model to tflite model using tf.lite.TFLiteConverter( ).But cannot convert particular layer to lite. Please refer to this gist for converting a model to tflite. Thank You.