pythontensorflownormalizationragged

Is there a way to normalize a ragged tensor?


I have tried tf.linalg.normalize and it gave me a value error:

ValueError: TypeError: object of type 'RaggedTensor' has no len().

Also I could not make tf.keras.layers.experimental.preprocessing.Normalization() method work. Thank you,


Solution

  • I have recreated the Error you were facing and found the solution to fix it. Below is how you can normalize the ragged tensor.

    Using tf.linalg.normalize:

    import tensorflow as tf
    import keras
    import numpy as np
    
    # Create a Ragged Tensor
    rt = tf.ragged.constant([[9.0, 8.0, 7.0], [], [6.0, 5.0], [4.0]])
    print("Ragged Tensor:","\n",rt,"\n")
    
    # Convert to Tensor to have same length
    rt = rt.to_tensor()
    print("Tensor of same length:","\n",rt,"\n")
    
    # Normalize
    rt = tf.linalg.normalize(rt, axis = None)
    print("Normalized and Norm Tensor:","\n",rt,"\n")
    # Get the normalized part
    rt = tf.convert_to_tensor(rt[0])
    print("Normalized Tensor:","\n",rt,"\n")
    
    # Convert to Ragged Tensor
    rt = tf.RaggedTensor.from_tensor(rt, padding=0.0)
    print("Normalized Ragged Tensor:","\n",rt)
    

    Output -

    Ragged Tensor: 
     <tf.RaggedTensor [[9.0, 8.0, 7.0], [], [6.0, 5.0], [4.0]]> 
    
    Tensor of same length: 
     tf.Tensor(
    [[9. 8. 7.]
     [0. 0. 0.]
     [6. 5. 0.]
     [4. 0. 0.]], shape=(4, 3), dtype=float32) 
    
    Normalized and Norm Tensor: 
     (<tf.Tensor: shape=(4, 3), dtype=float32, numpy=
    array([[0.546711  , 0.48596537, 0.4252197 ],
           [0.        , 0.        , 0.        ],
           [0.36447403, 0.30372837, 0.        ],
           [0.24298269, 0.        , 0.        ]], dtype=float32)>, <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[16.462078]], dtype=float32)>) 
    
    Normalized Tensor: 
     tf.Tensor(
    [[0.546711   0.48596537 0.4252197 ]
     [0.         0.         0.        ]
     [0.36447403 0.30372837 0.        ]
     [0.24298269 0.         0.        ]], shape=(4, 3), dtype=float32) 
    
    Normalized Ragged Tensor: 
     <tf.RaggedTensor [[0.5467110276222229, 0.485965371131897, 0.42521971464157104], [], [0.36447402834892273, 0.3037283718585968], [0.2429826855659485]]>
    

    Using math.l2_normalize:

    import tensorflow as tf
    import keras
    import numpy as np
    
    # Create a Ragged Tensor
    rt = tf.ragged.constant([[9.0, 8.0, 7.0], [], [6.0, 5.0], [4.0]])
    print("Ragged Tensor:","\n",rt,"\n")
    
    # Convert to Tensor to have same length
    rt = rt.to_tensor()
    print("Tensor of same length:","\n",rt,"\n")
    
    # Normalize
    rt = tf.math.l2_normalize(rt, axis = None)
    print("Normalized Tensor:","\n",rt,"\n")
    
    # Convert to Ragged Tensor
    rt = tf.RaggedTensor.from_tensor(rt, padding=0.0)
    print("Normalized Ragged Tensor:","\n",rt)
    

    Output -

    Ragged Tensor: 
     <tf.RaggedTensor [[9.0, 8.0, 7.0], [], [6.0, 5.0], [4.0]]> 
    
    Tensor of same length: 
     tf.Tensor(
    [[9. 8. 7.]
     [0. 0. 0.]
     [6. 5. 0.]
     [4. 0. 0.]], shape=(4, 3), dtype=float32) 
    
    Normalized Tensor: 
     tf.Tensor(
    [[0.546711   0.48596537 0.4252197 ]
     [0.         0.         0.        ]
     [0.36447403 0.30372834 0.        ]
     [0.24298269 0.         0.        ]], shape=(4, 3), dtype=float32) 
    
    Normalized Ragged Tensor: 
     <tf.RaggedTensor [[0.5467110276222229, 0.485965371131897, 0.42521971464157104], [], [0.36447402834892273, 0.3037283420562744], [0.2429826855659485]]>