index 2 is out of bounds for axis 1 with size 2

版权声明:转载请声明 https://blog.csdn.net/weixin_42451919/article/details/84105984

在使用类似  keras.np_utils.to_categorical 的函数,对标签转换成one-hot编码的时候,要使标签从0开始。即如果是两类,设标签里面的内容为0和1,不能设置为1和2或其他数字。

比如:

from keras.utils import np_utils
x=[]
x.append(2)
x.append(1)
print(x)
x = np_utils.to_categorical(x, 2)
print(x)

这就是错误的

from keras.utils import np_utils
x=[]
x.append(0)
x.append(1)
print(x)
x = np_utils.to_categorical(x, 2)
print(x)

这样才正确

猜你喜欢

转载自blog.csdn.net/weixin_42451919/article/details/84105984