How to implement Sequential model with tk.keras

TensorFlow provides implemention of Sequential model with tk.keras.Sequential API. Sequential model is used when each layer has only one input tensor and one output tensor.

In this tutorial we will learn how to build Sequential model with tf.keras from scratch and will analyze model's layers.

Instantiate Sequential model with three layers

  
import tensorflow as tf

model = tf.keras.Sequential([
      tf.keras.layers.Dense(3, activation="relu", name="firstLayer"),
      tf.keras.layers.Dense(4, activation="tanh", name="secondLayer"),
      tf.keras.layers.Dense(2, name="lastLayer"),
])

  

With the above snippet we have instantiated a Sequential model with three layers.

  • "firstlayer" is having 3 units or nodes and "relu" activatation function
  • "secondLayer" is having 4 units or nodes and "tanh" activation function
  • "lastLayer" is having 2 units or nodes and no activation function specified, so by default "linear" activation function would be applied for this layer
  • Till now we have just instantiated the model, we have not actually "build" the model, if we try to run model.weights now, it will thorw an error , lets try it with below snippet

    
    print(model.weights)
    
    

    Output

    
    ValueError: Weights for model sequential_13 have not yet been created.
    Weights are created when the Model is first called on inputs or `build()`
    is called with an `input_shape`.
    
    

    Lets build the model now, we will do it by two methods

    Method 1 : Build the model by providng explicit input

      
    import tensorflow as tf
    
    model = tf.keras.Sequential([
          tf.keras.layers.Dense(3, activation="relu", name="firstLayer"),
          tf.keras.layers.Dense(4, activation="tanh", name="secondLayer"),
          tf.keras.layers.Dense(2, name="lastLayer"),
    ])
    
    # Create a input and pass it to model
    input = tf.random.normal((3,4))
    output = model(input)
    
      
    

    Execute model.weights again and this time it should run without any errors

      
    print(model.weights)
      
    

    You should see output similar to as shown below

    
    [<tf.Variable 'sequential/firstlayer/kernel:0' shape=(4, 3) dtype=float32, numpy=
      array([[ 0.05237347, -0.5885333 ,  0.8455621 ],
             [-0.23029417, -0.42183632,  0.85601246],
             [-0.0988304 ,  0.13164008,  0.46770597],
             ......
    
    

    Method 2 : Build the model by instantiating with input shape

      
    import tensorflow as tf
    
    model = tf.keras.Sequential([
          tf.keras.Input((12,)),
          tf.keras.layers.Dense(3, activation="relu", name="firstLayer"),
          tf.keras.layers.Dense(4, activation="tanh", name="secondLayer"),
          tf.keras.layers.Dense(2, name="lastLayer"),
    ])
    
    
      
    

    As we added one extra layer for input shape(tf.keras.Input((12,))), so in this case explicitly passing input to the model is not required, Execute model.weights again and it should run without any errors

      
    print(model.weights)
      
    

    You should see output similar to as shown below

    
      [<tf.Variable 'firstlayer/kernel:0' shape=(12, 3) dtype=float32, numpy=
       array([[ 0.23396462,  0.16387159,  0.41509408],
              [ 0.1252827 , -0.59924555, -0.11882699],
              [ 0.3434235 ,  0.58942086,  0.3280686 ],
              [ 0.15097857,  0.49042028, -0.02682376],
              [ 0.3386299 , -0.44764125,  0.17891586],
              [ 0.11055166,  0.5378074 , -0.41883433],
              ..................
    
    

    Let's analyze the layers of the model

    Details for input shape, output shape, bias and activation function for first layer

      
      print(model.layers[0].input_shape)
      print(model.layers[0].output_shape)
      print(model.layers[0].bias.numpy())
      print(model.layers[0].activation)
      
    

    Output

      
        (None, 12)
        (None, 3)
        [0. 0. 0.]
        <function relu at 0x7fec36f522f0>
      
    

    Details for input shape, output shape, bias and activation function for second layer

      
        print(model.layers[1].input_shape)
        print(model.layers[1].output_shape)
        print(model.layers[1].bias.numpy())
        print(model.layers[1].activation)
      
    

    Output

      
        (None, 3)
        (None, 4)
        [0. 0. 0. 0.]
      <function tanh at 0x7fec36f52378>
      
    

    Details for input shape, output shape, bias and activation function for last layer

      
        print(model.layers[2].input_shape)
        print(model.layers[2].output_shape)
        print(model.layers[2].bias.numpy())
        print(model.layers[2].activation)
      
    

    Output

      
        (None, 4)
        (None, 2)
        [0. 0.]
      <function linear at 0x7fec36f52598>
      
    

    Once the model is built , we can use built-in APIs like model.compile(), model.fit(), model.evaluate(), model.predict() for training, evaluation, and predictions.


    Category: TensorFlow