我正在努力解决一个问题,我需要转换张量,以便在总值中,最大值为 1,其余为 0。
tf.Tensor(
[[0.05]
[0.1]
[0.5]
[0.35]],shape=(4,1),dtype = float32)
我试过了
out = tf.sparse_to_dense(tf.argmax(a),tf.cast(tf.shape(a), dtype=tf.int64), tf.reduce_max(a))
但不幸的是,出现错误
Input must be a SparseTensor.
我想要输出为
tf.Tensor(
[[0]
[0]
[1]
[0]],shape=(4,1),dtype = float32)
请帮我解决这个问题。 非常感谢
最佳答案
按这个方法试试
x = tf.constant(
[[0.05],
[0.1],
[0.5],
[0.35]])
top_values, top_indices = tf.nn.top_k(tf.reshape(x, (-1,)), 1)
tf.cast(tf.greater_equal(x, top_values), tf.float64)
输出
<tf.Tensor: shape=(4, 1), dtype=float64, numpy=
array([[0.],
[0.],
[1.],
[0.]])>
关于tensorflow - 转换张量,使得最大值为 1,其余为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009184/