Add docstrings for get/set layer weights.
PiperOrigin-RevId: 284065436
Change-Id: I11035ec0fbe39afc44cec0a3dcb45f087d6442a2
diff --git a/tensorflow/python/keras/engine/base_layer.py b/tensorflow/python/keras/engine/base_layer.py
index 276431b..b5d1d31 100644
--- a/tensorflow/python/keras/engine/base_layer.py
+++ b/tensorflow/python/keras/engine/base_layer.py
@@ -124,7 +124,7 @@
using Python control flow. If `False`, we assume that the layer can
safely be used to generate a static computation graph.
- Read-only properties:
+ Attributes (read-only properties):
name: The name of the layer (string).
dtype: The dtype of the layer's computations and weights. If mixed
precision is used with a `tf.keras.mixed_precision.experimental.Policy`,
@@ -1264,6 +1264,36 @@
def set_weights(self, weights):
"""Sets the weights of the layer, from Numpy arrays.
+ The weights of a layer represent the state of the layer. This function
+ sets the weight values from numpy arrays. The weight values should be
+ passed in the order they are created by the layer. Note that the layer's
+ weights must be instantiated before calling this function by calling
+ the layer.
+
+ For example, a Dense layer returns a list of two values-- per-output
+ weights and the bias value. These can be used to set the weights of another
+ Dense layer:
+
+ >>> a = tf.keras.layers.Dense(1,
+ ... kernel_initializer=tf.constant_initializer(1.))
+ >>> a_out = a(tf.convert_to_tensor([[1., 2., 3.]]))
+ >>> a.get_weights()
+ [array([[1.],
+ [1.],
+ [1.]], dtype=float32), array([0.], dtype=float32)]
+ >>> b = tf.keras.layers.Dense(1,
+ ... kernel_initializer=tf.constant_initializer(2.))
+ >>> b_out = b(tf.convert_to_tensor([[10., 20., 30.]]))
+ >>> b.get_weights()
+ [array([[2.],
+ [2.],
+ [2.]], dtype=float32), array([0.], dtype=float32)]
+ >>> b.set_weights(a.get_weights())
+ >>> b.get_weights()
+ [array([[1.],
+ [1.],
+ [1.]], dtype=float32), array([0.], dtype=float32)]
+
Arguments:
weights: a list of Numpy arrays. The number
of arrays and their shape must match
@@ -1314,6 +1344,35 @@
def get_weights(self):
"""Returns the current weights of the layer.
+ The weights of a layer represent the state of the layer. This function
+ returns both trainable and non-trainable weight values associated with this
+ layer as a list of Numpy arrays, which can in turn be used to load state
+ into similarly parameterized layers.
+
+ For example, a Dense layer returns a list of two values-- per-output
+ weights and the bias value. These can be used to set the weights of another
+ Dense layer:
+
+ >>> a = tf.keras.layers.Dense(1,
+ ... kernel_initializer=tf.constant_initializer(1.))
+ >>> a_out = a(tf.convert_to_tensor([[1., 2., 3.]]))
+ >>> a.get_weights()
+ [array([[1.],
+ [1.],
+ [1.]], dtype=float32), array([0.], dtype=float32)]
+ >>> b = tf.keras.layers.Dense(1,
+ ... kernel_initializer=tf.constant_initializer(2.))
+ >>> b_out = b(tf.convert_to_tensor([[10., 20., 30.]]))
+ >>> b.get_weights()
+ [array([[2.],
+ [2.],
+ [2.]], dtype=float32), array([0.], dtype=float32)]
+ >>> b.set_weights(a.get_weights())
+ >>> b.get_weights()
+ [array([[1.],
+ [1.],
+ [1.]], dtype=float32), array([0.], dtype=float32)]
+
Returns:
Weights values as a list of numpy arrays.
"""