How to get weights of layers in TensorFlow

This tutorial explains how to get weights of dense layers in keras Sequential model. We will build a Sequential model with tf.keras API.

Instantiate Sequential model with tf.keras

  
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(3, name="lastlayer"),
])
  

Build the model by providing input

  
input = tf.random.normal((1,4))
output = model(input)

  

Iterate over all the layers of model


for layer in model.layers:
  print(layer.name, layer)

Output


firstlayer <tensorflow.python.keras.layers.core.Dense object at 0x7fec2b045b38>
secondlayer <tensorflow.python.keras.layers.core.Dense object at 0x7fec2ad10fd0>
lastlayer <tensorflow.python.keras.layers.core.Dense object at 0x7fec2ad590f0>

  • Get weight,bias and bias initializer for the first layer
  • 
    print(model.layers[0].weights)
    print(model.layers[0].bias.numpy())
    print(model.layers[0].bias_initializer)
    

    Output

    
    [<tf.Variable 'sequential_4/firstlayer/kernel:0' shape=(4, 3) dtype=float32, numpy=
    array([[ 0.13008976,  0.04912078,  0.8281461 ],
           [ 0.14713573,  0.20518899,  0.89869845],
           [ 0.91662526, -0.12765765,  0.02967268],
           [ 0.775646  ,  0.5383214 ,  0.4608177 ]], dtype=float32)>, <tf.Variable 'sequential_4/firstlayer/bias:0' shape=(3,) dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>]
    [0. 0. 0.]
    <tensorflow.python.ops.init_ops_v2.Zeros object at 0x7fec2acebbe0>
    

  • Get weight,bias and bias initializer for the second layer
  • 
    print(model.layers[1].weights)
    print(model.layers[1].bias.numpy())
    print(model.layers[1].bias_initializer)
    

    Output

    
    [<tf.Variable 'sequential_4/secondlayer/kernel:0' shape=(3, 4) dtype=float32, numpy=
    array([[ 0.4453739 ,  0.5896597 , -0.7100645 ,  0.63668966],
           [ 0.5065664 ,  0.23734832, -0.9246994 ,  0.6262492 ],
           [ 0.5549624 , -0.41925687,  0.19855618, -0.2993641 ]],
          dtype=float32)>, <tf.Variable 'sequential_4/secondlayer/bias:0' shape=(4,) dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>]
    [0. 0. 0. 0.]
    <tensorflow.python.ops.init_ops_v2.Zeros object at 0x7fec2aceb780>
    

  • Get weight,bias and bias initializer for the third layer
  • 
    print(model.layers[2].weights)
    print(model.layers[2].bias.numpy())
    print(model.layers[2].bias_initializer)
    

    Output

    
    [<tf.Variable 'sequential_4/lastlayer/kernel:0' shape=(4, 3) dtype=float32, numpy=
    array([[-0.05207109,  0.29934072, -0.19738293],
           [ 0.88687694,  0.50912046,  0.67754614],
           [ 0.62408936,  0.5546762 ,  0.527491  ],
           [ 0.0999198 , -0.6864388 , -0.82465744]], dtype=float32)>, <tf.Variable 'sequential_4/lastlayer/bias:0' shape=(3,) dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>]
    [0. 0. 0.]
    <tensorflow.python.ops.init_ops_v2.Zeros object at 0x7fec2acebba8>
    

    Categories: TensorFlow