I have a Tensor that I initialize as follows (thank @Dennis for you help with this):
//let w = tf.rand([2,1], () => Math.random());
let w = tf.randomUniform([2,1]);
w.print(true);
Output:
Tensor
dtype: float32
rank: 2
shape: [2,1]
values:
[[0.8221924],
[0.7023968]]
What if I want to scale the values so that each one is in [0, 1]
and sum to 1 while maintaining their relative weights?
In regular JavaScript I would apply a map function and do something like this:
let sumOfWeights = arr.reduce((acc, w) => acc + w, 0);
arr.map((w) => (w /= sumOfWeights));
console.log(arr);
But how do I do this in TensorFlow?
Alternatively, how can I generate a tensor of random numbers, summing to 1, without going through the above transformations/operations?
EDIT: I think I found a solution below, unless there is a better way to do it.
let w = tf.randomUniform([2,1]);
let sum = tf.sum(w);
w = tf.div(w, sum);