4

I have 2 build_model functions as below:

def build_model01():
    X_input = Input(shape=(784,))
    Y = Dense(1, activation='sigmoid')(X_input)
    model = Model(inputs = X_input, outputs = Y, name='build_model')
    return model

def build_model02():
    model = Sequential()
    model.add(Dense(input_dim=784,units=1,activation='sigmoid'))
    return model

What are the differences between build_model01 and build_model02? Are they practically the same? Will the differences affect other layers?

today
  • 32,602
  • 8
  • 95
  • 115
H42
  • 725
  • 2
  • 9
  • 28

1 Answers1

4

Actually, there is no difference between the models created using the functional API (i.e. build_model01) and the same model created as a Sequential model (i.e. build_model02). You can further confirm this by checking the Sequential class source code; as you can see, it is a subclass of Model class. Of course, Keras functional API gives you more flexibility and it lets you create models with complex architectures (e.g. models with multiple inputs/outputs or multiple branches).

today
  • 32,602
  • 8
  • 95
  • 115
  • 1
    @H42 That's right. Since models created using functional API, unlike Sequential models, may have multiple outputs, each with different output configuration, [it does not make sense](https://github.com/keras-team/keras/issues/2524#issuecomment-218590463) to have `predict_classes` or `predict_proba` methods. As an alternative, you can use the solution suggested in this [answer](https://stackoverflow.com/a/45176824/2099607). – today Aug 22 '18 at 19:22