Home > Mobile >  Index assignment to tensor objects
Index assignment to tensor objects

Time:10-03

I have four tensor objects, e.g., A, B, C, and D (see below).

A: <tf.Tensor 'mul_388:0' shape=(5,) dtype=float32>
B: <tf.Tensor 'mul_396:0' shape=(5,) dtype=float32>
C: <tf.Tensor 'clip_by_value_30:0' shape=(5,) dtype=float32>
D: <tf.Tensor 'clip_by_value_31:0' shape=(5,) dtype=float32>

I have to create new tensor object E based on the conditions given below:

E = C when A <= B
else
E = D

What is the best way to do it? I am using Tensorflow version 2.9.1.

I tried following but didn't work.

E = C[A <= B]
E = D[A > B]

CodePudding user response:

Use tf.cond:

E = tf.cond(A <= B, true_fn=lambda: C, false_fn=lambda: D)
  • Related