[英]converting numerical output labels to categorical vectors in keras
I'm creating a Deep learning multi-classification model in Keras and I have converted my outputlabel training set y_train from numerical values ranging from 1 to 14 to output vectors looking like this [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0] => representing the number 2
.我正在 Keras 中创建一个深度学习多分类 model,我已经将我的输出标签训练集 y_train 从 1 到 14 的数值转换为 output 向量,看起来像这样[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0] => representing the number 2
。 This is the code I used in python (keras):这是我在 python (keras) 中使用的代码:
from keras.utils import to_categorical
y_train = to_categorical(y_train)
However, because it converts these outputlabels into a vector of length 15 instead of 14 because it adds zero as a potential output as well.但是,因为它将这些输出标签转换为长度为 15 而不是 14 的向量,因为它也将零添加为潜在的 output。 My original numpy array y_train looked like this: [1,8,9,7,2,2,8...]
and it should be converted to a vector of length 14 instead of 15 to avoid extra loss when training the model. Is there a simpel way to avoid using zero as a potential output class?我原来的 numpy 数组 y_train 看起来像这样: [1,8,9,7,2,2,8...]
应该将其转换为长度为 14 而不是 15 的向量,以避免在训练 model 时造成额外损失。有没有一种简单的方法可以避免使用零作为潜在的 output class?
( num_classes = 14
as a parameter of to_categorical gives an error message) ( num_classes = 14
作为 to_categorical 的参数给出错误信息)
if your labels are from 1 to 14, try this simple trick:如果你的标签是从 1 到 14,试试这个简单的技巧:
y_train = to_categorical(np.asarray(y_train)-1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.