如何使用 tf.one_hot 计算一种热编码?

Sai*_*mar 5 python tensorflow

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

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

我们keras会做类似的事情

# this converts it into one hot encoding
one hot_encoding = tf.keras.utils.to_categorical(y_train)
Run Code Online (Sandbox Code Playgroud)

我应该在哪里tf.one_hot输入indices&depth参数?完成一次热编码后,如何将其从2d-tensor转换回numpy数组?

And*_*Fan 6

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

tf.one_hot()需要 anindices和 a depth。这些indices是实际转换为 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())
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

相反,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.]]
Run Code Online (Sandbox Code Playgroud)

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

至于转换为 numpy,如上所示,使用 Tensorflow 会话,eval()在张量上运行将其转换为 numpy 数组。有关执行此操作的方法,请参阅如何在 TensorFlow 中将张量转换为 numpy 数组?

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

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