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.
CodePudding user response:
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
_________________________________________________________________