我正在使用CIFAR进行TensorFlow训练,但我正在将MNIST代码改为我现在需要的,但我无法找到一种方法让tf.argmax
按照其原始设计完成任务。现在需要将类似于,
[2, 7, 1, 5, 3]
的数组转换为
[[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0]]
我该如何做?我正在使用CIFAR 100数据库,你可以在这里获取我的代码。
回答:
看起来你需要的是one-hot编码
。要获得one-hot编码,请将索引指定为Python列表,并将其传递给tf.one_hot
,同时指定所需的depth
:
# 也可以使用numpy数组In [85]: idx = [2, 7, 1, 5, 3]In [86]: one_hot_vec = tf.one_hot(idx, depth=8, dtype=tf.int32)In [87]: sess = tf.InteractiveSession()In [91]: one_hot_vec = tf.one_hot(idx, depth=8, dtype=tf.int32)In [92]: one_hot_vec.eval()Out[92]: array([[0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0]], dtype=int32)