Keras models and layers
Quick answer
Keras layers implement transformations and own weights; a Model groups layers into a trainable prediction function. Sequential represents a straight stack, Functional builds a graph of symbolic inputs and outputs, and subclassing expresses custom behavior in Python. Native TensorFlow examples are unexecuted; local Keras uses Torch. ByteMind execution is unverified.
Sequential parameter count
import osos.environ["KERAS_BACKEND"]="torch"import kerasm=keras.Sequential([keras.Input((2,)),keras.layers.Dense(4,activation="tanh"),keras.layers.Dense(1)])print(m.count_params())Expected output (locally tested): 17. The layers contain 24+4 and 41+1 parameters.
How it works
Dense(H) learns a kernel of shape (D,H) and optionally H biases. Sequential is simplest for one chain. Functional uses keras.Input, then calls layers on symbolic tensors; it can express branches and shared layers. For subclassing, inherit keras.Model or keras.layers.Layer, call super().init, create child layers in init or shape-dependent state in build using add_weight, and implement call. Use keras.ops for portable tensor math, not arbitrary backend calls on symbolic KerasTensor. Reusing one layer object shares weights; creating two layers creates independent state. Pass training to layers whose behavior changes between modes, such as dropout. A built model can report count_params; subclass serialization may require configuration and registered custom objects.
Functional and subclassed models
import osos.environ["KERAS_BACKEND"]="torch"import kerasfrom keras import opsx=keras.Input((2,))y=keras.layers.Dense(1,kernel_initializer="ones",bias_initializer="zeros")(x)f=keras.Model(x,y)class SumModel(keras.Model): def __init__(self): super().__init__() self.d=keras.layers.Dense(1,kernel_initializer="ones",bias_initializer="zeros") def call(self,x): return self.d(x)a=ops.convert_to_tensor([[2.,3.]])print(ops.convert_to_numpy(f(a)).tolist(),ops.convert_to_numpy(SumModel()(a)).tolist())Expected output (locally tested): [[5.0]] [[5.0]]. Both models use the same explicitly chosen weights, so their simple outputs agree.
Common mistakes
Creating new weights inside every call breaks persistent state and may fail graph compilation. Do not assume all custom Python control flow is portable. Use an explicit Input to build Sequential before inspecting weights.
Practice and next steps
Construct a 2→4→1 model in both Sequential and Functional forms and count parameters before training. Then configure a loss and optimizer.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Keras models and layers — knowledge check
Apply keras models and layers to small examples and debugging scenarios.
Which API expresses a branched graph of symbolic layers?
How many parameters does Dense(4) on 2 features with bias have?
What happens when a layer instance is reused?
Where create shape-dependent custom weights?
Which arithmetic API aids Keras backend portability?