Home > front end >  How to create a vector in TensorFlow.js, where the i-th entry is a scalar raised to the i-th power?
How to create a vector in TensorFlow.js, where the i-th entry is a scalar raised to the i-th power?

Time:09-21

I want to create a vector in TensorFlow.js, which has the following structure:

vec

where a is a non-complex scalar, n is the length of the vector, and i is the i-th entry of the vector, starting to count at zero.

Is there a fast way of doing this or do I need to loop?

CodePudding user response:

IIUC, you could try maybe something like this:

x = tf.tensor1d([1, 2, 3, 4, 5, 6, 7, 8]).toInt();
y = tf.range(0, x.shape[0], 1).toInt();

x.pow(y).print();
"Tensor
    [1, 2, 9, 64, 625, 7776, 117649, 2097152]"
  • Related