python - How best to implement a matrix mask operation in tensorflow? -
i had case needed fill holes (missing data) in image processing application in tensorflow. 'holes' easy locate zeros , data not zeros. wanted fill holes random data. quite easy using python numpy doing in tensorflow requires work. came solution , wanted see if there better or more efficient way same thing. understand tensorflow not yet support more advanced numpy type indexing yet there function tf.gather_nd() seems promising this. however, not tell documentation how wanted do. appreciate answers improve on did or if can show me how using tf.gather_nd(). also, tf.boolean_mask() not work trying because not allow use output index. in python trying do:
a = np.ones((2,2)) a[0,0]=a[0,1] = 0 mask = == 0 a[mask] = np.random.random_sample(a.shape)[mask] print('new = ', a)
what ended doing in tensorflow achieve same thing (skipping filling array steps)
zeros = tf.zeros(tf.shape(a)) mask = tf.greater(a,zeros) mask_n = tf.equal(a,zeros) mask = tf.cast(mask,tf.float32) mask_n = tf.cast(mask_n,tf.float32 r = tf.random_uniform(tf.shape(a),minval = 0.0,maxval=1.0,dtype=tf.float32) r_add = tf.multiply(mask_n,r) targets = tf.add(tf.multiply(mask,a),r_add)
i think these 3 lines might want. first, make mask. then, create random data. finally, fill in masked values random data.
mask = tf.equal(a, 0.0) r = tf.random_uniform(tf.shape(a), minval = 0.0,maxval=1.0,dtype=tf.float32) targets = tf.where(mask, r, a)
Comments
Post a Comment