13

I have a Tensorflow tensor A of size (64, 2, 82, 1), and I want to replace its (:, :, 80:82, :) part with the corresponding part of the tensor B (also (64, 2, 82, 1) size).

How would I do that?

P.S.: To be precise, I mean the operation that would look like this in the numpy:

A[:, :, 80:82, :] = B[:, :, 80:82, :]
Massyanya
  • 2,844
  • 8
  • 28
  • 37
  • give a look at the answer here https://stackoverflow.com/questions/44648788/how-to-assign-values-to-specified-location-in-tensorflow – Pietro Tortella Jun 20 '17 at 16:47
  • there is also this discussion on github, https://github.com/tensorflow/tensorflow/issues/206, that i did not understand if and how it was solved... – Pietro Tortella Jun 20 '17 at 16:49

2 Answers2

7

the following code might help you to get some idea,

a = tf.constant([[11,0,13,14],
                 [21,22,23,0]])
condition = tf.equal(a, 0)
case_true = tf.reshape(tf.multiply(tf.ones([8], tf.int32), -9999), [2, 4])
case_false = a
a_m = tf.where(condition, case_true, case_false)
sess = tf.Session()
sess.run(a_m)

here i am accessing individual element of a tensor!

drsbhattac
  • 109
  • 2
  • 7
  • I'm trying to use your code in my activation function like this: ` def ScoreActivationFromSigmoid(x, target_min=1, target_max=9) : condition = K.tf.logical_and(K.tf.less(x, 1), K.tf.greater(x, -1)) case_true = K.tf.reshape(K.tf.multiply(tf.ones([x.shape[1] * x.shape[2]]), 0.0), x.shape) case_false = a changed_x = K.tf.where(condition, case_true, case_false) activated_x = K.sigmoid(changed_x) score = activated_x * (target_max - target_min) + target_min return score` But it does not find the correct shape, how am I supposed to reshape correctly? – Isaac Sim Feb 01 '19 at 07:34
0

tf.assign should work: (not tested)

 tf.assign(A[:, :, 80:82, :], B[:, :, 80:82, :])
gdelab
  • 6,124
  • 2
  • 26
  • 59
  • Unfortunately it doesn't: `a = tf.constant([[1,2,3], [4,5,6]])`, `b = tf.constant([[7,8,9], [10,11,12]])`, `tf.assign(a[0,:], b[0, :])` gives the following error: `TypeError: assign() got an unexpected keyword argument 'name'` – Massyanya Jun 20 '17 at 16:27
  • You can't use assign with constants. – redfiloux Dec 21 '18 at 09:24