From e95dc8efc9bd350db79f81a4208033c5f7685196 Mon Sep 17 00:00:00 2001 From: Francois Chollet Date: Fri, 19 Mar 2021 10:50:09 -0700 Subject: [PATCH] Improve docstring of Keras `normalize` utility. PiperOrigin-RevId: 363924762 --- keras/utils/np_utils.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/keras/utils/np_utils.py b/keras/utils/np_utils.py index 569f8a6c35e..de724edd26d 100644 --- a/keras/utils/np_utils.py +++ b/keras/utils/np_utils.py @@ -83,15 +83,24 @@ def to_categorical(y, num_classes=None, dtype='float32'): @keras_export('keras.utils.normalize') def normalize(x, axis=-1, order=2): - """Normalizes a Numpy array. + """Normalizes a NumPy array. Args: - x: Numpy array to normalize. - axis: axis along which to normalize. - order: Normalization order (e.g. `order=2` for L2 norm). + x: NumPy array to normalize. + axis: Axis along which to normalize. For instance, `axis=-1` corresponds + to feature-wise normalization. + order: Normalization order (e.g. `order=2` for the L2 norm). Returns: A normalized copy of the array. + + Example: + + >>> array = np.random.random(size=(32, 3)) + >>> normalized_array = tf.keras.utils.normalize(array, axis=-1) + >>> # Every element in the batch has now a unit norm + >>> for i in range(32): + >>> np.testing.assert_allclose(np.square(normalized_array[i, :]).sum(), 1) """ l2 = np.atleast_1d(np.linalg.norm(x, order, axis)) l2[l2 == 0] = 1