Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras : Why does Sequential and Model give different outputs?

I am using Keras for computing a simple sequence classification neural network. I played with the different module and I found that there are two way to create Sequential neural network.

The first way is to use Sequential API. This is the most common way which I found in a lot of tutorial/documentation. Here is the code :

# Sequential Neural Network using Sequential()
model = Sequential()
model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=(27 , 300,)))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(100))
model.add(Dense(len(7, activation='softmax'))
model.summary()

The second ways is to build de sequential neural network from "scratch" with the Model API. Here is the code.

# Sequential neural network using Model()   
inputs = Input(shape=(27 , 300))
x = Conv1D(filters=32, kernel_size=3, padding='same', activation='relu')(inputs)
x = MaxPooling1D(pool_size=2)(x)
x = LSTM(100)(x)
predictions = Dense(7, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.summary()

I trained it both with a fixed seed (np.random.seed(1337)), with the same training data and my output are different... Knowing that the only difference in the summary is the first layer of inputs with the Model API.

Is there anyone that knows why this neural network are different ? And if there are not, why did i get different results ?

Thanks

like image 948
Clement Viricel Avatar asked Feb 01 '18 11:02

Clement Viricel


People also ask

What is the difference between sequential and model in Keras?

Sequential class : Sequential groups a linear stack of layers into a tf. keras. Model . Model class : Model group's layers into an object with training and inference features.

What is the difference between sequential model and functional model?

For example, in sequential model you can only stack one layer after another, while in functional model you can connect a layer to literally any other layer.

Why do we use sequential model in Keras?

A Sequential model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor. A Sequential model is not appropriate when: Your model has multiple inputs or multiple outputs. Any of your layers has multiple inputs or multiple outputs.

How is functional different from sequential and why is it considered more flexible?

The functional API offers more flexibility and control over the layers than the sequential API. It can be used to predict multiple outputs(i.e output layers) with multiple inputs(i.e input layers))


1 Answers

You setup the random seed only in numpy and not in tensorflow (in case it's the backend of keras in your case). Try to add this in your code:

from numpy.random import seed
seed(1337)
from tensorflow import set_random_seed
set_random_seed(1337)

the detailed article about this topic here

like image 71
Andrey Kite Gorin Avatar answered Sep 30 '22 03:09

Andrey Kite Gorin