我正在尝试在Keras中构建自定义回调,以跟踪精度并在每个时期结束时进行调用。
class Precision(Callback):
"""
Keras Callback. Calculates precision metrics at the end of each epoch.
"""
def __init__(self):
super().__init__()
self.precisions = []
def on_train_begin(self, logs={}):
self.precisions = []
def on_epoch_end(self, epoch, logs={}):
y_pred = (np.asarray(self.model.predict(self.validation_data[0]))).round()
y_true = self.validation_data[1]
precision = precision_score(y_true, y_pred)
self.precisions.append(precision)
print("validation set precision at epoch {}: {}".format(epoch, precision))
return
# ------------
model.fit_generator(training_generator,
steps_per_epoch=(TRAIN_SIZE / BATCH_SIZE), # number of samples in the dataset
epochs=EPOCHS, # number of epochs, training cycles
validation_data=validation_generator, # performance eval on test set
validation_steps=(TEST_SIZE / BATCH_SIZE),
callbacks=[history,
precision])
我收到以下错误:
File "/Users/user/Desktop/cnn_toolkit.py", line 68, in on_epoch_end
y_pred = (np.asarray(self.model.predict(self.validation_data[0]))).round()
TypeError: 'NoneType' object is not subscriptable
我正在做迁移学习(Inception V3),并使用功能性Keras Model对象。我尝试更新tensorflow,从源代码构建它,还尝试了self.model.validation_data
仍然不起作用。到处都在寻找答案,却找不到能解决我问题的任何东西。我有Keras 2.2.4。