Convert from sequential to Functional API model

Viewed 154

I have the following code snippet:

model = Sequential()
model.add(Conv2D(16, (5, 5), input_shape=(256, 256, 1)))
x = model.layers[0].output
model.add(Lambda(lambda x: tf.abs(x)))
model.add(Activation(activation='tanh'))

My question, how to convert these steps into the Functional API Keras model. My confused idea is how to insert the ABS layer into the Functional API models.

1 Answers

Let's take a look at your model with the Sequential implementation and Functional API implementation :

Here are some imports:

import tensorflow as tf
from tensorflow.keras.layers import Lambda,Conv2D, Activation, Input
from tensorflow.keras import Model, Sequential

Here is your implementation using the Sequential Model:

model = Sequential()
model.add(Conv2D(16, (5, 5), input_shape=(256, 256, 1)))
x = model.layers[0].output
model.add(Lambda(lambda x: tf.abs(x)))
model.add(Activation(activation='tanh'))

model.summary()

The summary output:

Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_6 (Conv2D)            (None, 252, 252, 16)      416       
_________________________________________________________________
lambda_6 (Lambda)            (None, 252, 252, 16)      0         
_________________________________________________________________
activation_5 (Activation)    (None, 252, 252, 16)      0         
=================================================================
Total params: 416
Trainable params: 416
Non-trainable params: 0
_________________________________________________________________

Now the implementation with Functional API:

First, define your function:

def arbitrary_functionality(tensor):

  return tf.abs(tensor)

And:

input_layer = Input(shape=(256, 256, 1))
conv1 = Conv2D(16, (5, 5))(input_layer)
lambda_layer = Lambda(arbitrary_functionality)(conv1)
output_layer = Activation(activation='tanh')(lambda_layer)

model_2 = Model(inputs=input_layer, outputs=output_layer)
model_2 .summary()

The summary output:

Model: "model_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_7 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 252, 252, 16)      416       
_________________________________________________________________
lambda_9 (Lambda)            (None, 252, 252, 16)      0         
_________________________________________________________________
activation_8 (Activation)    (None, 252, 252, 16)      0         
=================================================================
Total params: 416
Trainable params: 416
Non-trainable params: 0
_________________________________________________________________

Note: according to the TensorFlow documentation, a better way is to subclass the Layer class. See an example here.

Related