我有4个需要预测的类,正在使用keras的to_categorical
来实现,我希望得到4个one-hot-encoded
数组,但似乎我得到了5个值,另外一个[0]
值出现在所有行中
dict = {'word': 1, 'feature_name': 2, 'feature_value': 3, 'part_number': 4}
Y = dataset['class'].apply(lambda label: dict[label])
print(Y.unique()) #prints [1 4 2 3]
train_x, test_x, train_y, test_y = model_selection.train_test_split(X, Y, test_size=0.2, random_state=0)
train_y = to_categorical(train_y)
print(train_y[0])# prints [0. 0. 1. 0. 0.]
要构建的模型如下
model = Sequential()
model.add(Dense(10, input_dim=input_dim, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(4, activation='softmax'))
但随后不断抛出
ValueError: Error when checking target: expected dense_5 to have shape (4,) but got array with shape (5,)
答案 0 :(得分:2)
您需要从0开始编号类别,例如:
dict = {'word': 0, 'feature_name': 1, 'feature_value': 2, 'part_number': 3}
您可以使用help()命令获取功能说明
help(np_utils.to_categorical)
:
Help on function to_categorical in module keras.utils.np_utils:
to_categorical(y, num_classes=None, dtype='float32')
Converts a class vector (integers) to binary class matrix.
E.g. for use with categorical_crossentropy.
# Arguments
y: class vector to be converted into a matrix
(integers from 0 to num_classes).
num_classes: total number of classes.
dtype: The data type expected by the input, as a string
(`float32`, `float64`, `int32`...)
# Returns
A binary matrix representation of the input. The classes axis
is placed last.
答案 1 :(得分:1)
可能是keras版本错误。尝试更新它,因为它对我有用:
dict = {'word': 1, 'feature_name': 2, 'feature_value': 3, 'part_number': 4}
Y = np.random.randint(4, size=10)
print(np.unique(Y)) #prints [0 1 2 3]
train_y = np_utils.to_categorical(Y, num_classes=4)
print(train_y[0]) # prints [0. 0. 1. 0.]
请尝试从0开始创建字典,因为Keras读取数据时,以0为参考。
dict = {'word': 0, 'feature_name': 1, 'feature_value': 2, 'part_number': 3}
如果它不起作用,请尝试强制增加类数:
train_y = to_categorical(train_y, num_classes = 4)