2

以下代码工作正常,但使用 eval() 我认为效率低下。有没有更好的方法来实现同样的目标?

import tensorflow as tf
import numpy as np
sess = tf.Session()
t = tf.constant([[4,5.1,6.3,5,6.5,7.2,9.3,7,1,1.4],[4,5.1,9.3,5,6.5,7.2,1.3,7,1,1.4],[4,3.1,6.3,5,6.5,3.2,5.3,7,1,1.4]])
print t
a = tf.argmax(t,1).eval(session=sess)
z = [ k==np.arange(14) for k in a]
z1 = tf.convert_to_tensor(np.asarray(z).astype('int32'))
print z1
print sess.run(z1)

输出

Tensor("Const_25:0", shape=TensorShape([Dimension(3), Dimension(10)]), dtype=float32)
Tensor("Const_26:0", shape=TensorShape([Dimension(3), Dimension(14)]), dtype=int32)
[[0 0 0 0 0 0 1 0 0 0 0 0 0 0]
 [0 0 1 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1 0 0 0 0 0 0]]
4

1 回答 1

3

实现它的一种方法是计算每一行的最大值,然后将每个元素与该值进行比较。我没有在这台机器上安装张量流,所以不能为你提供确切的代码,但它会沿着这条线:

z1 = tf.equal(t, tf.reduce_max(t, reduction_indices=[1], keep_dims=True))
于 2015-12-14T05:10:50.780 回答