Issue
How can I do the L2 norm in Tensorflow? I'm looking for the equivalent of sklearn.preprocessing.normalize
in Tensorflow or in tfx
.
Solution
You can use tensorflow.keras.utils.normalize
for L2 norm as follows.
Using sklearn.preprocessing.normalize
X = [[ 1., -1., 2.],
[ 2., 0., 0.],
[ 0., 1., -1.]]
X_normalized = sklearn.preprocessing.normalize(X, norm='l2')
X_normalized
Output:
array([[ 0.40824829, -0.40824829, 0.81649658],
[ 1. , 0. , 0. ],
[ 0. , 0.70710678, -0.70710678]])
Using tf.keras.utils.normalize
gives the same output as above
X = [[ 1., -1., 2.],
[ 2., 0., 0.],
[ 0., 1., -1.]]
tf.keras.utils.normalize(
X, order=2
)
Output:
array([[ 0.40824829, -0.40824829, 0.81649658],
[ 1. , 0. , 0. ],
[ 0. , 0.70710678, -0.70710678]])
Answered By - Tfer3
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.