我想应用KerasCLassifier
来解决多类分类问题。y
的值是一个热编码,例如:
0 1 0
1 0 0
1 0 0
这是我的代码:
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
# Function to create model, required for KerasClassifier
def create_model(optimizer='rmsprop', init='glorot_uniform'):
# create model
model = Sequential()
model.add(Dense(2048, input_dim=X_train.shape[1], kernel_initializer=init, activation='relu'))
model.add(Dense(512, kernel_initializer=init, activation='relu'))
model.add(Dense(y_train_onehot.shape[1], kernel_initializer=init, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
# create model
model = KerasClassifier(build_fn=create_model, class_weight="balanced", verbose=2)
# grid search epochs, batch size and optimizer
optimizers = ['rmsprop', 'adam']
epochs = [10, 50]
batches = [5, 10, 20]
init = ['glorot_uniform', 'normal', 'uniform']
param_grid = dict(optimizer=optimizers, epochs=epochs, batch_size=batches, init=init)
grid = model_selection.GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy')
grid_result = grid.fit(X_train], y_train_onehot)
当我运行最后一行代码时,它在10个时期后抛出以下错误:
/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py在accurcy_score(y_true,y_pred,normalize,sample_weight)中174175#计算每个可能表示的准确性-->176 y_type,y_true,y_pred=检查目标(y_true,y_pred)177 check_consistent_length(y_true,y_pred,sample_weight)178如果y_type.startswitch("多标签"):
/opt/conda/lib/python3.6/site-packages/sklearn/metrics/classification.py在_check_targets(y_true,y_pred)中79如果len(y_type)>1:80引发ValueError("分类指标无法处理{0}的混合"--->81"和{1}个目标".format(type_true,type_pred))8283#我们不能在y_type上有一个以上的值=>该集合不再需要
ValueError:分类指标无法处理多标签指示器和二进制目标
当我写categorical_accuracy
或balanced_accuracy
而不是accuracy
时,我无法编译模型。
下面是一个工作演示:
import numpy as np
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
N = 100
X_train = np.random.rand(N, 4)
Y_train = np.random.choice([0,1,2], N, p=[.5, .3, .2])
# Function to create model, required for KerasClassifier
def create_model(optimizer='rmsprop', init='glorot_uniform'):
# create model
model = Sequential()
model.add(Dense(2048, input_dim=X_train.shape[1], kernel_initializer=init, activation='relu'))
model.add(Dense(512, kernel_initializer=init, activation='relu'))
model.add(Dense(len(np.unique(Y_train)), kernel_initializer=init, activation='softmax'))
# Compile model
model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['sparse_categorical_accuracy'])
return model
# create model
model = KerasClassifier(build_fn=create_model, class_weight="balanced", verbose=2)
# grid search epochs, batch size and optimizer
optimizers = ['rmsprop', 'adam']
epochs = [10, 50]
batches = [5, 10, 20]
init = ['glorot_uniform', 'normal', 'uniform']
param_grid = dict(optimizer=optimizers, epochs=epochs, batch_size=batches, init=init)
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='accuracy')
grid_result = grid.fit(X_train, Y_train)
PS请注意sparse_categorical_*
损失函数和度量的使用。