3

Good afternoon. I continue to have issues with updating random elements in tensorflow by index. I want to randomly choose indices (half of all, for instance), and then set to zero elements correspond to that indices. Here's the problematic part:

with tf.variable_scope("foo", reuse=True):
    temp_var = tf.get_variable("W")
    size_2a = tf.get_variable("b")
    s1 = tf.shape(temp_var).eval()[0]
    s2 = tf.shape(size_2a).eval()[0]


    row_indices = tf.random_uniform(dtype=tf.int32, minval=0, maxval = s1 - 1, shape=[s1]).eval()
    col_indices = tf.random_uniform(dtype=tf.int32, minval=0, maxval = s2 - 1, shape=[s2]).eval()

    ones_mask = tf.ones([s1,s2])

    # turn 'ones_mask' into 1d variable since "scatter_update" supports linear indexing only
    ones_flat = tf.Variable(tf.reshape(ones_mask, [-1]))

    # no automatic promotion, so make updates float32 to match ones_mask
    updates = tf.zeros(shape=(s1,), dtype=tf.float32)


    # get linear indices
    linear_indices = row_indices*s2 + tf.reshape(col_indices,s1*s2)
    ones_flat = tf.scatter_update(ones_flat, linear_indices/2, updates) 
    #I want to set to zero only half of all elements,that's why linear_indices/2

    # convert back into original shape
    ones_mask = tf.reshape(ones_flat, ones_mask.get_shape())

It gives me ValueError: Cannot reshape a tensor with 10 elements to shape [784,10] (7840 elements) for 'foo_1/Reshape_1' (op: 'Reshape') with input shapes: [10], [2]., but I don't know how to be here without reshaping (I tried to reshape to both s1 and s2, no use)

I have already read these topics:Update values of a matrix variable in tensorflow, advanced indexing (feed_dict doesn't seem to work in my case), python numpy ValueError: operands could not be broadcast together with shapes and practically everything on the subject on stackoverflow =(

Community
  • 1
  • 1
TheDoctor
  • 105
  • 1
  • 9
  • can't you just kind of sonicate it? - sorry couldn't resist; more seriously: the reshape in the last but five line looks suspicious to me (size doesn't seem to match the one you use when creating `col_indices`; strangely, the error message doesn't look right for that. perhaps you could give the reshape ops in that code block names, then the errors would be easier to map – Paul Panzer Feb 10 '17 at 01:17
  • @PaulPanzer, it shouldn't match, `col_indices` already has shape s2, and by logic it seems, if row_indices has shape s1 and multiplied to s2, then I should reshape col_indices accordingly. But if I write `tf.reshape(col_indices,[s1])`, I get ValueError: Cannot reshape a tensor with 10 elements to shape [784] (784 elements) for 'foo_1/Reshape_1' (op: 'Reshape') with input shapes: [10], [1]. Aggr I really don't understand how to calculate this linear indices, then. – TheDoctor Feb 10 '17 at 01:46
  • If I'm not totally mistaken then the shape logic of tf is closely modelled on that of numpy and at least in numpy you cannot change the total number of elements (= product over shape) with a reshape. For example x has shape (n1, n2) legal: x.reshape (n2, n1), x.reshape (n1*n2) but: won't work: x.reshape(n2) x.reshape(n1+n2) etc. What exactly do you want to achieve? Grid-like random coords like (r1y, r1x) (r1y, r2x) (r2y,r1x) (r2y,r2x) or scattered like (r1y, r1x) (r2y,r2x) (r3y, r3x) (r4y, r4x)? – Paul Panzer Feb 10 '17 at 02:07
  • @PaulPanzer, thanks. I want the scatter_update() to work properly, so I guess, the latter. Waaait, the linear indices mean there is 1d and not 2d Tensor. If you mean before all this flattening-yes, scattered. – TheDoctor Feb 10 '17 at 02:10
  • Ok, if you want uniform distribution over the rectangle and you ravel the rectangle into one long vector you'll get uniform distribution on tnhe vector therefore its not necessary to do it coordinate by coordinate and then combine them. do it directly `maxval = s1*s2` (you are coming from matlab, aren't you? In python the convention is lower bound included, upper bound excluded, hence no minus one) and shape (int(s1*s2*fraction),) (note the comma) where fraction is the fraction of positions you want to update – Paul Panzer Feb 10 '17 at 02:32
  • @PaulPanzer, There were Tensorflow and python only. Thank you, but could you please elaborate about the fraction? For the same instance: I want to update half of all the indices, so I write: `tf.reshape(col_indices,(int(s1*s2*0.5),))`? – TheDoctor Feb 10 '17 at 02:47
  • Yes, there is, however a potential problem: you'll probably get some of the offsets multiple times. I don't know scatterupdate well enough to know whether it can handle that. Well, just try it, and see. – Paul Panzer Feb 10 '17 at 02:57
  • @PaulPanzer, it gives ValueError: Cannot reshape a tensor with 10 elements to shape [3920] (3920 elements) for 'foo_1/Reshape_1' (op: 'Reshape') with input shapes: [10], [1]. Will continue to think... – TheDoctor Feb 10 '17 at 03:10
  • You must give `updates` the same shape as `linear_indices` and don't divide `linear_indices` by two. – Paul Panzer Feb 10 '17 at 03:19

0 Answers0