I want to create a mask with iterating over the tensor. I have this code:
import tensorflow as tf
out = tf.Variable(tf.zeros_like(alp, dtype=tf.int32))
rows_tf = tf.constant (
[[1, 2, 5],
[1, 2, 5],
[1, 2, 5],
[1, 4, 6],
[1, 4, 6],
[2, 3, 6],
[2, 3, 6],
[2, 4, 7]])
columns_tf = tf.constant(
[[1],
[2],
[3],
[2],
[3],
[2],
[3],
[2]])
I want to iterate through rows_tf and accordingly columns_tf to create a mask over the out.
for example, it will mask the index at [1,1] [2,1] and [5,1] in the out tensor equals 1.
for the second row in rows_tf indexes at [1,2] [2,2] [5,2] in the out tensor will be set to 1 and so on for the total 8 rows
So far I have done this, though it does not run successfully:
body = lambda k, i: (tf.add(out[rows_tf[i][k]][columns_tf[i][i]], 1)) # find the corresponding element in out tensor and add 1 to it (0+1=1)
k = 0
n2, m2 = rows_tf.shape
for i in tf.range(0,n2): # loop through rows in rows_tf
cond = lambda k, _: tf.less(k, m2) #this check to go over the columns in rows_tf
tf.while_loop(cond, body, (k, i))
it raises this error:
TypeError: Cannot iterate over a scalar tensor.
in this while cond(*loop_vars):
I have gone through several links namely here to make sure Im following the instruction, but could not fix this one.
Thanks for the help
tf.while_loopnot working. but if you can use atf.SparseTensorto represent your mask (can apply the mask with other sparse ops in moduletf.sparse), you can dorows_tf = tf.reshape(rows_tf, shape=[-1, 1]) columns_tf = tf.reshape( tf.tile(columns_tf, multiples=[1, 3]), shape=[-1, 1]) mask_indices = tf.reshape( tf.concat([rows_tf, columns_tf], axis=-1), shape=[-1, 2]). The values of the mask would be all ones.