Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Keras sequential model from functional API

I have a Keras model using functional API and it looks:

nn = keras.layers.Conv1D(300,19,strides=1,activation='relu')(inputs) 
nn = keras.layers.Conv1D(300,19,strides=1,activation='relu')(nn) 

nn = keras.layers.MaxPool1D(pool_size=3)(nn)

nn = keras.layers.Flatten()(nn)
nn = keras.layers.Dense(596,activation='relu')(nn)

logits = keras.layers.Dense(35, activation='linear')(nn)
outputs = keras.layers.Activation('sigmoid')(logits)

I want to convert it to sequential model however I am confused how logit and output layer would look like in sequential model. So what I have so far:

model.add(keras.layers.Conv1D(300,19,'relu',input_shape=dataset['x_train'].shape[1:])
model.add(keras.layers.Conv1D(300,19,'relu')
model.add(Flatten())
model.add(keras.layers.Dense(596,'relu'))

I am confused about the next two layers. Can someone guide me how to code for it in a sequential model. Help will be much appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use tf.keras.Model and pass inputs, outputs and get the model.summary() and create an exact model with tf.keras.Sequential() like the below: (You can see the Total params: 3,706,091 for both of models.)

Using functional API:

import tensorflow as tf
inputs = tf.keras.layers.Input((64, 64))
nn = tf.keras.layers.Conv1D(300,19,strides=1,activation='relu')(inputs) 
nn = tf.keras.layers.Conv1D(300,19,strides=1,activation='relu')(nn) 
nn = tf.keras.layers.MaxPool1D(pool_size=3)(nn)
nn = tf.keras.layers.Flatten()(nn)
nn = tf.keras.layers.Dense(596,activation='relu')(nn)
logits = tf.keras.layers.Dense(35, activation='linear')(nn)
outputs = tf.keras.layers.Activation('sigmoid')(logits)
model = tf.keras.Model(inputs, outputs)
model.summary()

Output:

Model: "model_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_2 (InputLayer)        [(None, 64, 64)]          0         
                                                                 
 conv1d_2 (Conv1D)           (None, 46, 300)           365100    
                                                                 
 conv1d_3 (Conv1D)           (None, 28, 300)           1710300   
                                                                 
 max_pooling1d_1 (MaxPooling  (None, 9, 300)           0         
 1D)                                                             
                                                                 
 flatten_1 (Flatten)         (None, 2700)              0         
                                                                 
 dense_2 (Dense)             (None, 596)               1609796   
                                                                 
 dense_3 (Dense)             (None, 35)                20895     
                                                                 
 activation_1 (Activation)   (None, 35)                0         
                                                                 
=================================================================
Total params: 3,706,091
Trainable params: 3,706,091
Non-trainable params: 0
_________________________________________________________________

Create an exact model with tf.keras.Sequential().

import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv1D(300,19,strides=1,activation='relu',input_shape=(64,64)))
model.add(tf.keras.layers.Conv1D(300,19,strides=1,activation='relu'))
model.add(tf.keras.layers.MaxPool1D(pool_size=3))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(596,'relu'))
model.add(tf.keras.layers.Dense(35, activation='linear'))
model.add(tf.keras.layers.Activation('sigmoid'))
model.summary()

Output:

Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d_2 (Conv1D)           (None, 46, 300)           365100    
                                                                 
 conv1d_3 (Conv1D)           (None, 28, 300)           1710300   
                                                                 
 max_pooling1d_1 (MaxPooling  (None, 9, 300)           0         
 1D)                                                             
                                                                 
 flatten_1 (Flatten)         (None, 2700)              0         
                                                                 
 dense_2 (Dense)             (None, 596)               1609796   
                                                                 
 dense_3 (Dense)             (None, 35)                20895     
                                                                 
 activation_1 (Activation)   (None, 35)                0         
                                                                 
=================================================================
Total params: 3,706,091
Trainable params: 3,706,091
Non-trainable params: 0
_________________________________________________________________
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading