【问题标题】:Argmax on a tensor and ceiling in TensorflowTensorflow中张量和天花板上的Argmax
【发布时间】:2017-06-29 20:47:37
【问题描述】:

假设我在 Tensorflow 中有一个张量,它的值类似于:

A = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]]

如何将此张量更改为以下内容:

B = [[1, 0, 0],[0, 0, 1]] 

换句话说,我只想保留最大值并将其替换为 1。
任何帮助将不胜感激。

【问题讨论】:

    标签: tensorflow neural-network keras softmax argmax


    【解决方案1】:

    我认为你可以用一条线来解决它:

    import tensorflow as tf
    import numpy as np
    
    x_data = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]]
    # I am using hard-coded dimensions for simplicity
    x = tf.placeholder(dtype=tf.float32, name="x", shape=(2,3))
    
    session = tf.InteractiveSession()
    
    session.run(tf.one_hot(tf.argmax(x, 1), 3), {x: x_data})
    

    结果是你所期望的:

    Out[6]: 
    array([[ 1.,  0.,  0.],
           [ 0.,  0.,  1.]], dtype=float32)
    

    【讨论】:

    • 这很聪明!谢谢
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2016-02-11
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多