入坑tensorflow2.0之keras中to_categorical方法

简言之,to_categorical方法就是将类别向量转换为独热编码,所谓独热编码简单来说就是用一个二进制编码唯一表示一个类别。
这类似于在数字电路的十进制BCD编码,下面以此为例:

from tensorflow import keras
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a = keras.utils.to_categorical(a, 10)
print(a)

运行结果:

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

你可能感兴趣的:(tensorflow2.0,细小知识点)