How to get weights of layers in TensorFlow

This tutorial explains how to get weight, bias and bias initializer of dense layers in keras Sequential model by iterating over layers and by layer's name. First we will build a Sequential model with tf.keras.Sequential API and than will get weights of layer by iterating over model layers and by using layer name.

1. Instantiate Sequential model with tf.keras.Sequential

    
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"),
])
    
  

2. Build the model by providing input

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

3. 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>
  
4. 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>
  
5. 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>
  
6. 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>
  

Get weights of layers by name in with TensorFlow Keras API

1. Get weights of layer "firstlayer" by name
   
print((model.get_layer("firstlayer").weights))
 
2. Get weights of layer "secondlayer" by name
   
print((model.get_layer("secondlayer").weights))
 
3. Get weights of layer "lastlayer" by name
   
print((model.get_layer("lastlayer").weights))
 

Complete code snippet to get layer weights by name and by iterating over model layers

   
import tensorflow as tf

# Instantiate keras sequential model
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"),
])

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

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

# firstlayer
print(model.layers[0].weights)
print(model.layers[0].bias.numpy())
print(model.layers[0].bias_initializer)

# secondlayer
print(model.layers[1].weights)
print(model.layers[1].bias.numpy())
print(model.layers[1].bias_initializer)

# lastlayer
print(model.layers[2].weights)
print(model.layers[2].bias.numpy())
print(model.layers[2].bias_initializer)

# firstlayer by name
print((model.get_layer("firstlayer").weights))

# secondlayer by name
print((model.get_layer("secondlayer").weights))

# lastlayer by name
print((model.get_layer("lastlayer").weights))
    
 

Category: TensorFlow