Tensorflow:使用其他

时间:2018-01-06 22:59:20

标签: python numpy tensorflow

我正试图在我的代码中替换一块numpy。我有类似的东西

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

我想使用tf.unqiue,但是在我评估图表之前,返回张量的结果不可用。我想构建一个单独的图,以便我可以一起评估所有操作。是否有可能在TensorFlow中做这样的事情。如果没有,这是像pyTorch和其他人提供的动态生成图形的优势吗?

1 个答案:

答案 0 :(得分:1)

如果你知道唯一值的数量,那么这样的东西可以起作用:

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)))