fork download
  1. import tensorflow as tf
  2. from tensorflow.keras import layers
  3. from tensorflow.keras import initializers
  4.  
  5. # Create a zeros initializer
  6. initializer = tf.keras.initializers.Zeros()
  7.  
  8. # Create a Dense layer with 3 units and the zero initializer for the kernel weights
  9. layer = tf.keras.layers.Dense(3, kernel_initializer=initializer)
  10.  
  11. # Build the layer to initialize it (requires specifying input shape)
  12. layer.build((None, 4)) # Example input shape, batch size is None (unknown), 4 features
  13.  
  14. # Print the kernel (weights) of the Dense layer
  15. print("Kernel Weights (Layer's weights initialized to Zeros):")
  16. print(layer.get_weights()[0]) # The weights are the first element in the list returned by get_weights()
  17.  
  18. # Optionally, you can also print the biases (second element in get_weights())
  19. print("Bias Weights (initialized by default):")
  20. print(layer.get_weights()[1]) # The biases are the second element in the list
  21.  
Success #stdin #stdout #stderr 1.31s 205484KB
stdin
Standard input is empty
stdout
Kernel Weights (Layer's weights initialized to Zeros):
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
Bias Weights (initialized by default):
[0. 0. 0.]
stderr
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.