Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 354
Souradeep Nanda Avatar asked Jul 10 '19 03:07

Souradeep Nanda


People also ask

What is model in Keras?

Keras is a neural network Application Programming Interface (API) for Python that is tightly integrated with TensorFlow, which is used to build machine learning models. Keras' models offer a simple, user-friendly way to define a neural network, which will then be built for you by TensorFlow.

What are layers in Keras?

Layers are the basic building blocks of neural networks in Keras. A layer consists of a tensor-in tensor-out computation function (the layer's call method) and some state, held in TensorFlow variables (the layer's weights).

What is difference between Keras model and sequential?

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.

When should you create a custom layer versus a custom model?

If you are building a new model architecture using existing keras/tf layers then build a custom model. If you are implementing your own custom tensor operations with in a layer, then build a custom layer.


2 Answers

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.

like image 192
NPE Avatar answered Oct 11 '22 16:10

NPE


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.

like image 31
Dr. Snoopy Avatar answered Oct 11 '22 17:10

Dr. Snoopy