多类KerasClassifier的网格搜索
我正在尝试使用Keras对多类分类进行网格搜索。以下是代码的一部分: 数据的某些属性如下所示:多类KerasClassifier的网格搜索,keras,grid-search,hyperparameters,multiclass-classification,Keras,Grid Search,Hyperparameters,Multiclass Classification,我正在尝试使用Keras对多类分类进行网格搜索。以下是代码的一部分: 数据的某些属性如下所示: y_ array(['fast', 'immobile', 'immobile', ..., 'slow', 'immobile', 'slow'], dtype='<U17') y_onehot = pd.get_dummies(y_).values y_onehot array([[1, 0, 0], [0, 0, 1], [0, 0
y_
array(['fast', 'immobile', 'immobile', ..., 'slow',
'immobile', 'slow'],
dtype='<U17')
y_onehot = pd.get_dummies(y_).values
y_onehot
array([[1, 0, 0],
[0, 0, 1],
[0, 0, 1],
...
[0, 1, 0],
[0, 0, 1],
[0, 1, 0]], dtype=uint8)
#Do train-test split
y_train.shape
(1904,)
y_train_onehot.shape
(1904, 3)
下面是错误:
--> grid_result = grid.fit(X_train, y_train_onehot)
ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets
这段代码是针对二进制模型的,但我希望能针对多类数据集对其进行修改。请帮忙。谢谢 错误在softmax层中 我想你指的是
y\u-train\u-onehot.shape[1]
而不是y\u-train\u-onehot[1]
更新1:这很奇怪,但您的第二个问题似乎是您的培训问题,您是否介意尝试两种方法:
sparse\u categorical\u crossentropy
另外,请确保将
y\u-train\u-onehot.shape[1]
更改为softmax层中的类数是否可以显示您的y-train\u-onehot
和原始y
的一些示例?您将原始的y
转换为y\u onehot
@VivekKumar的代码,我已经按照您的建议进行了添加。它看起来像一个简单的多类问题。请不要对y.
进行热编码。直接在fit()
:grid.fit(X\u-train,y\u-train)
中使用它。我有一个类似的问题,你能更明确地说明你的尝试吗?恢复到非热编码似乎会产生各种其他问题,这些问题也不起作用(例如必须将最后一个softmax设置为layer,将其设置为dimension 1,等等)。谢谢
--> grid_result = grid.fit(X_train, y_train_onehot)
ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets