Setup
When to use a Sequential modelA Show
Schematically, the following
is equivalent to this function:
A Sequential model is not appropriate when:
Creating a Sequential modelYou can create a Sequential model by passing a list of layers to the Sequential constructor:
Its layers are accessible via the
[<keras.layers.core.Dense at 0x7fdc784478d0>, <keras.layers.core.Dense at 0x7fdbbc3c4650>, <keras.layers.core.Dense at 0x7fdbbc3c4a10>] You can also create a Sequential model incrementally via the
Note that there's also a corresponding
2 Also note that the Sequential constructor accepts a
Specifying the input shape in advanceGenerally, all layers in Keras need to know the shape of their inputs in order to be able to create their weights. So when you create a layer like this, initially, it has no weights:
[] It creates its weights the first time it is called on an input, since the shape of the weights depends on the shape of the inputs:
[<tf.Variable 'dense_6/kernel:0' shape=(4, 3) dtype=float32, numpy= array([[ 0.5319189 , -0.8767905 , -0.63919735], [-0.6276014 , 0.1689707 , -0.57695866], [ 0.6710613 , 0.5354214 , -0.00893992], [ 0.15670097, -0.15280598, 0.8865864 ]], dtype=float32)>, <tf.Variable 'dense_6/bias:0' shape=(3,) dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>] Naturally, this also applies to Sequential models. When you instantiate a Sequential model without an input shape, it isn't "built": it has no weights (and calling
Number of weights after calling the model: 6 Once a model is "built", you can call its
Model: "sequential_3" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_7 (Dense) (1, 2) 10 _________________________________________________________________ dense_8 (Dense) (1, 3) 9 _________________________________________________________________ dense_9 (Dense) (1, 4) 16 ================================================================= Total params: 35 Trainable params: 35 Non-trainable params: 0 _________________________________________________________________ However, it can be very useful when building a Sequential model
incrementally to be able to display the summary of the model so far, including the current output shape. In this case, you should start your model by passing an
Model: "sequential_4" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_10 (Dense) (None, 2) 10 ================================================================= Total params: 10 Trainable params: 10 Non-trainable params: 0 _________________________________________________________________ Note that the
[<keras.layers.core.Dense at 0x7fdbbc37c390>] A simple alternative is to just pass an
Model: "sequential_5" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_11 (Dense) (None, 2) 10 ================================================================= Total params: 10 Trainable params: 10 Non-trainable params: 0 _________________________________________________________________ Models built with a predefined input shape like this always have weights (even before seeing any data) and always have a defined output shape. In general, it's a recommended best practice to always specify the input shape of a Sequential model in advance if you know what it is. A common debugging workflow: add() + summary()When building a new Sequential architecture, it's useful to
incrementally stack layers with
Model: "sequential_6" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 123, 123, 32) 2432 _________________________________________________________________ conv2d_1 (Conv2D) (None, 121, 121, 32) 9248 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 40, 40, 32) 0 ================================================================= Total params: 11,680 Trainable params: 11,680 Non-trainable params: 0 _________________________________________________________________ Model: "sequential_6" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 123, 123, 32) 2432 _________________________________________________________________ conv2d_1 (Conv2D) (None, 121, 121, 32) 9248 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 40, 40, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 38, 38, 32) 9248 _________________________________________________________________ conv2d_3 (Conv2D) (None, 36, 36, 32) 9248 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 12, 12, 32) 0 _________________________________________________________________ conv2d_4 (Conv2D) (None, 10, 10, 32) 9248 _________________________________________________________________ conv2d_5 (Conv2D) (None, 8, 8, 32) 9248 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 4, 4, 32) 0 ================================================================= Total params: 48,672 Trainable params: 48,672 Non-trainable params: 0 _________________________________________________________________ Very practical, right? What to do once you have a modelOnce your model architecture is ready, you will want to:
Once a Sequential model has been built, it behaves like a Functional API model. This means that every layer has an
Here's a similar example that only extract features from one layer:
Transfer learning with a Sequential modelTransfer learning consists of freezing the bottom layers in a model and only training the top layers. If you aren't familiar with it, make sure to read our guide to transfer learning. Here are two common transfer learning blueprint involving Sequential models. First, let's say that you have a Sequential model, and you want to freeze all layers except the last one. In this case, you would simply iterate over
Another common blueprint is to use a Sequential model to stack a pre-trained model and some freshly initialized classification layers. Like this:
If you do transfer learning, you will probably find yourself frequently using these two patterns. That's about all you need to know about Sequential models! To find out more about building models in Keras, see:
In which form of interdependence does the output of one unit become the input for another in a successive fashion?In sequential interdependence, your team members rely on each other in predictable ways for the flow of information, work and decisions. Each person's output becomes the input for the next person in the sequence.
When the output of one department becomes the input of the other department and vice versa it is classified as?Reciprocal interdependence is similar to sequential interdependence in that the output of one department becomes the input of another, with the addition of being cyclical. In this model, an organization's departments are at their highest intensity of interaction.
During which stage in group development group members come to accept fellow members and develop a unity of purpose that binds them?Norming. Over time, the group begins to develop a sense of oneness. Here, group norms emerge (norming) to guide individual behavior. Group members come to accept fellow members and develop a unity of purpose that binds them.
What exists when activities flow both ways between units?Reciprocal interdependence exists when activities flow both ways between units.
|