将 Tensorflow 中除 max 之外的所有值归零 [英] Zero all values except max in Tensorflow

查看:30
本文介绍了将 Tensorflow 中除 max 之外的所有值归零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组[0.3, 0.5, 0.79, 0.2, 0.11].

我想将除最大值以外的所有值都转换为零.所以结果数组将是:[0, 0, 0.79, 0, 0]

I want to convert all values to zero except the max value. So the resulting array would be: [0, 0, 0.79, 0, 0]

在 Tensorflow 图中最好的方法是什么?

What would be the best way to do this in a Tensorflow graph?

推荐答案

如果你想要 tf.argmax() 的行为并且只想要 one max关系,你可以这样做:

If you want the behavior of tf.argmax() and want only one max in case of ties, you can do:

tf.sparse_to_dense(tf.argmax(a),tf.cast(tf.shape(a), dtype=tf.int64),tf.reduce_max(a))

tf.sparse_to_dense(tf.argmax(a),tf.cast(tf.shape(a), dtype=tf.int64), tf.reduce_max(a))

a = tf.constant([0.3, 0.5, 0.79, 0.79, 0.11])

out = tf.sparse_to_dense(tf.argmax(a),tf.cast(tf.shape(a), dtype=tf.int64), tf.reduce_max(a))

输出:

[0.   0.   0.79 0.   0.  ]

这篇关于将 Tensorflow 中除 max 之外的所有值归零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆