我是一名初学者,致力于CNN图像分类,并且具有波纹管回调功能;
class Metrics(Callback):
def on_train_begin(self, logs = {}):
self.val_kappas = []
def on_epoch_end(self, epoch, logs = {}):
X_val, y_val = self.validation_data[:2]
y_val = y_val.sum(axis = 1) - 1
y_pred = self.model.predict(X_val) > 0.5
y_pred = y_pred.astype(int).sum(axis = 1) - 1
_val_kappa = cohen_kappa_score(
y_val,
y_pred,
weights = 'quadratic'
)
self.val_kappas.append(_val_kappa)
print(f"val_kappa: {_val_kappa:.4f}")
if _val_kappa == max(self.val_kappas):
print("Validation Kappa has improved. Saving model.")
self.model.save('/path_to/model.h5')
return
当我训练模型时;
kappa_metrics = Metrics()
history = model.fit(
data_generator,
steps_per_epoch = x_train.shape[0] / BATCH_SIZE,
epochs = 15,
validation_data = (x_val, y_val),
callbacks = [kappa_metrics]
)
不幸的是,我不明白我犯了什么错误。请注意,我是CNN和Python的初学者。
答案 0 :(得分:1)
我使用以下链接解决了问题。如果有人对此感兴趣,我会在这里发布。
https://github.com/keras-team/keras/issues/10472