sebjwallace
sebjwallace

Reputation: 795

tensorflow - normalizing vectors

I have a vector that looks something like [0, 0.2, 0, 0.24, 0, 0, 0.4, 0.12]. Is there a single operator in tensorflow that will normalize this vector so that the values range between 0 and 1 (where 0.24 is 1).

I will not know the max value before hand (ie 0.24). The range is fairly arbitrary.

Upvotes: 1

Views: 1722

Answers (3)

Muhammad Umar Amanat
Muhammad Umar Amanat

Reputation: 917

Check tensorflow_transform.scale_to_0_1 function, tensorflow_transform is the api for applying different type of transformation during training and prediction. https://www.tensorflow.org/tfx/transform/api_docs/python/tft/scale_to_0_1

Upvotes: 1

Sharky
Sharky

Reputation: 4543

You can try tf.math.l2_normalize

arr = [0, 0.2, 0, 0.24, 0, 0, 0.4, 0.12]
norm = tf.math.l2_normalize(arr)

with tf.Session() as sess:
    print(sess.run(norm))

output

[0., 0.38348246, 0., 0.46017894, 0., 0., 0.7669649, 0.23008947]

https://www.tensorflow.org/api_docs/python/tf/math/l2_normalize

Upvotes: 1

TassosK
TassosK

Reputation: 293

You can check in tensorflow documentation about that but you can easily implement a min max normalization or z-score normalization using standard deviation and mean from numpy lib or scipy

This is for numpy arrays but you can get the idea

# min-max mormalization
def normalize(X):
    col_max = np.max(X, axis=0)
    col_min = np.min(X, axis=0)
    normX = np.divide(X - col_min, col_max - col_min)
    return normX

Upvotes: 0

Related Questions