简体   繁体   中英

TypeError: 'NoneType' object is not subscriptable when I train a CNN model

I am a beginner, working on CNN for image classification and I have a callback function as bellow;

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

When I trained the model;

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]
)

I get the following error; 在此处输入图像描述

Unfortunately I don't understand whats the mistake I do. Please note that I am beginner for CNN and Python.

I solved the problem using the following link. I post it here if someone is interested in it.

https://github.com/keras-team/keras/issues/10472

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM