【问题标题】:How do I compute one hot encoding using tf.one_hot?如何使用 tf.one_hot 计算一种热编码?
【发布时间】:2019-11-03 19:32:25
【问题描述】:

我正在尝试使用 tensorflowmnist 数据集的 y_train 构建一个热编码。我不明白怎么做?

# unique values 0 - 9
y_train = array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)

keras 我们会做类似的事情

# this converts it into one hot encoding
one hot_encoding = tf.keras.utils.to_categorical(y_train)

tf.one_hot 中,我对indicesdepth 参数的输入应该是什么?进行一次热编码后,如何将其从 2d-tensor 转换回 numpy 数组?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    我对 Tensorflow 不熟悉,但经过一些测试,这是我发现的:

    tf.one_hot() 采用 indicesdepthindices 是实际转换为 one-hot 编码的值。 depth 指的是要使用的最大值。

    以如下代码为例:

    y = [1, 2, 3, 2, 1]
    tf.keras.utils.to_categorical(y)
    sess = tf.Session();
    with sess.as_default():
        print(tf.one_hot(y, 2).eval())
        print(tf.one_hot(y, 4).eval())
        print(tf.one_hot(y, 6).eval())
    

    tf.keras.utils.to_categorical(y) 返回以下内容:

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

    相比之下,tf.one_hot() 选项(2、4 和 6)执行以下操作:

    [[0. 1.]
     [0. 0.]
     [0. 0.]
     [0. 0.]
     [0. 1.]]
    [[0. 1. 0. 0.]
     [0. 0. 1. 0.]
     [0. 0. 0. 1.]
     [0. 0. 1. 0.]
     [0. 1. 0. 0.]]
    [[0. 1. 0. 0. 0. 0.]
     [0. 0. 1. 0. 0. 0.]
     [0. 0. 0. 1. 0. 0.]
     [0. 0. 1. 0. 0. 0.]
     [0. 1. 0. 0. 0. 0.]]
    

    从这里可以看出,要使用tf.one_hot() 模拟tf.keras.utils.to_categorical()depth 参数应该等于数组中存在的最大值,+1 表示 0。在这种情况下,最大值为 3 ,因此编码中有四个可能的值 - 0、1、2 和 3。因此,在 one-hot 编码中表示所有这些值需要 4 的深度。

    至于转换为 numpy,如上所示,使用 Tensorflow 会话,在张量上运行 eval() 会将其转换为 numpy 数组。具体方法请参考How can I convert a tensor into a numpy array in TensorFlow?

    我不熟悉 Tensorflow,但我希望这会有所帮助。

    注意:对于 MNIST,深度为 10 就足够了。

    【讨论】:

      【解决方案2】:

      我想反驳@Andrew Fan 所说的话。首先,上面的 y 标签列表不是从索引 0 开始的,这是需要的。只需查看所有这些示例中的第一列(即索引 0):它们都是空的。这将在学习中创建一个冗余类,并可能导致问题。一个热创建一个简单的列表,该索引位置为 1,其他位置为 0。因此,您的深度必须与类数相同,但您也必须从索引 0 开始。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        • 2018-10-25
        • 2019-02-06
        • 2016-11-30
        • 2021-12-01
        • 2018-12-17
        相关资源
        最近更新 更多