【问题标题】:tf.argsort and np.argsort gave different resultstf.argsort 和 np.argsort 给出了不同的结果
【发布时间】:2019-10-02 17:44:59
【问题描述】:

所以我想argmax y0,我在numpy和tensorflow 2中测试过,结果不一样。 不知道为什么。

maxy0 = np.amax(y0)
e0 = np.exp(y0 - maxy0)
p0 = e0 / np.sum(e0)
y0 = np.log(1e-20 + p0)
print(y0)
[[-46.0517 -46.0517 -46.0517 ... -46.0517 -46.0517 -46.0517]]
np.argsort(-y0)
array([[   11,     0, 26664, ..., 13337, 13330, 40001]])
tf.argsort(-y0)
array([[   11,     0,     1, ..., 39999, 40000, 40001]], dtype=int32)

为什么它们不同?

【问题讨论】:

  • 你有一个相等数字的数组......因为默认情况下使用的合并算法不稳定,所以不能保证它们会产生相同的结果
  • 就像@rafaelc 所说,您可以在np.argsorttf.argsort 中设置稳定参数。默认为不稳定排序。
  • @rafaelc 并非所有数字都相等,但大多数数字都是相等的,但我认为您的回答是有道理的。谢谢!

标签: python numpy tensorflow tensorflow2.0


【解决方案1】:

尝试将 stable 参数更改为 True,因为在 tf.argsort 中默认为 False。 请参考https://www.tensorflow.org/api_docs/python/tf/argsort

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2021-09-25
    • 2023-03-30
    相关资源
    最近更新 更多