1

I have the following code

aa = tf.zeros([10, 4, 240, 240, 1])
aa[:, :, 10:20, 5:15, :] = 1

However, this does not work.

I know that numpy arrays allow specific elements to be changed using the notation aa[:, :, 10:20, 5:15, :] = 1. How would I do this in tensorflow?

PiccolMan
  • 4,854
  • 12
  • 35
  • 53
  • Does this answer your question? [Adjust Single Value within Tensor -- TensorFlow](https://stackoverflow.com/questions/34685947/adjust-single-value-within-tensor-tensorflow) – pregenRobot Dec 25 '20 at 08:12

2 Answers2

1

Use tf.concat():

aa = tf.zeros([10, 4, 240, 240, 1])
aaa = tf.concat(
    (
        aa[..., :10, 5:15, :],
        tf.ones((10, 4, 10, 10, 1)),
        aa[..., 20:, 5:15, :],
    ),
    2
)
aa = tf.concat(
    (
        aa[..., :5, :],
        aaa,
        aa[..., 15:, :],
    ),
    3
)
Andrey
  • 5,932
  • 3
  • 17
  • 35
  • Hi, so this actually doesn't do what I want it to. If a 240x240 image is plotted it will contain a cross of 1s instead of a rectangle of 1s. Thank you for trying though. – PiccolMan Dec 25 '20 at 22:31
  • 1
    Thanks! Apologies for the late acceptance - I was swamped with unrelated work. – PiccolMan Jan 15 '21 at 04:28
0

I have tried with a simpler shape. I can see the changes but not sure if you are getting what you want here.

aa =  tf.Variable(tf.zeros([10, 4, 20, 20, 1]))

tensor = tf.constant(10,shape=(10,4,10,10,1))
tf.print(aa[:, :, 10:20, 5:15, :].assign(tf.ones_like(tensor, dtype=tf.float32)))

tf.print(aa[:, :, 10:20, 5:15, :],summarize=-1)
tf.print(aa,summarize=-1)
Mohan Radhakrishnan
  • 3,002
  • 5
  • 28
  • 42