Issue
What would be the simplest solution to a convert the tensor:
tensor = tf.constant([4.0])
to a 5x5 tensor, where the main diagonal and the antidiagonal have the scalar 4.0, but the rest of the tensor has the value 0.0 as shown below:
tf.Tensor(
[[4. 0. 0. 0. 4.]
[0. 4. 0. 4. 0.]
[0. 0. 4. 0. 0.]
[0. 4. 0. 4. 0.]
[4. 0. 0. 0. 4.]], shape=(5, 5), dtype=float32)
Solution
Simply use tf.eye
:
import tensorflow as tf
tensor = tf.where(tf.greater(tf.reverse(tf.eye(5), axis=[1]) + tf.eye(5), 0.0), 1.0, 0.0) * tf.constant([4.0])
tf.Tensor(
[[4. 0. 0. 0. 4.]
[0. 4. 0. 4. 0.]
[0. 0. 4. 0. 0.]
[0. 4. 0. 4. 0.]
[4. 0. 0. 0. 4.]], shape=(5, 5), dtype=float32)
Answered By - AloneTogether
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.