Index assignment to tensor objects

Issue

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]

Solution

Use tf.cond:

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

Answered By – AndrzejO

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published