Souradeep Nanda
Souradeep Nanda

Reputation: 3298

Keras: What is the difference between model and layers?

EDIT: This video by Franchois Chollet says that Layer + training eval methods = Model


In keras documentation it says that models are made up of layers. However in this section it shows that a model can be made up of models.

from keras.layers import Conv2D, MaxPooling2D, Input, Dense, Flatten
from keras.models import Model

# First, define the vision modules
digit_input = Input(shape=(27, 27, 1))
x = Conv2D(64, (3, 3))(digit_input)
x = Conv2D(64, (3, 3))(x)
x = MaxPooling2D((2, 2))(x)
out = Flatten()(x)

vision_model = Model(digit_input, out)

# Then define the tell-digits-apart model
digit_a = Input(shape=(27, 27, 1))
digit_b = Input(shape=(27, 27, 1))

# The vision model will be shared, weights and all
out_a = vision_model(digit_a)
out_b = vision_model(digit_b)

concatenated = keras.layers.concatenate([out_a, out_b])
out = Dense(1, activation='sigmoid')(concatenated)

classification_model = Model([digit_a, digit_b], out)

So, what is the effective difference between Model and layers? Is it just for code readability or does it serve some function?

Upvotes: 6

Views: 1590

Answers (2)

Dr. Snoopy
Dr. Snoopy

Reputation: 56397

The difference is that models can be trained (they have a fit method), while layers do not have such method and need to be part of a Model instance so you can train them. Generally speaking, layers in isolation aren't useful.

The idea of the Functional API to use models inside models is that you can define one model, and reuse its weights as part of another model, in a way that weights are shared. This is not possible with layers alone.

Upvotes: 1

NPE
NPE

Reputation: 500883

In Keras, a network is a directed acyclic graph (DAG) of layers. A model is a network with added training and evaluation routines.

The framework allows you to build network DAGs out of both individual layers and other DAGs. The latter is what you're seeing in the example and what seems to be causing the confusion.

Upvotes: 4

Related Questions