cyrux
cyrux

Reputation: 243

Tensorflow:using results of one operation in other

I am trying to replace a piece of numpy in my code. I have something like this

value = some_const
unique_values = np.unique(<ndarray>)
eq_tensors = [tf.equal(<ndarray>, x) for x in unique_values]

I would like to use tf.unqiue, but the result of returning tensor wont be available until I evaluate the graph. I want to build one single graph so I can evaluate all ops together. Is it possible to do something like this in TensorFlow. If not, is this the advantage dynamically generated graphs like pyTorch and others provide ?

Upvotes: 0

Views: 48

Answers (1)

kww
kww

Reputation: 549

If you knew the number of unique values, then something like this could work:

array = <ndarray>
num_unique = <# of unique values>
y, idx = tf.unique(array)
idx = tf.reshape(idx, (-1, 1))
eq_tensors = tf.transpose(tf.equal(idx, tf.range(num_unique)))

Upvotes: 1

Related Questions