属性错误: 'numpy.ndarray'对象没有属性'history'



我正在尝试在 keras 中绘制训练图,但在运行代码时出错。

def modelx(X, y):
classifier = Sequential()
classifier.add(Dense(4, activation='relu', kernel_initializer='random_normal', input_dim=10))
classifier.add(Dense(4, activation='relu', kernel_initializer='random_normal'))
classifier.add(Dense(1, activation='sigmoid', kernel_initializer='random_normal'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics =['accuracy'])
out = classifier.fit(X, y, batch_size=10, epochs=1000, verbose=0)
return out, classifier
..............
..............
..............
predictions, model = modelx(X, y)
predictions = model.predict(test)
predictions = (predictions>0.5)
predictions = predictions.astype(int)
print(predictions)
results = ids.assign(Survived=predictions)
results.to_csv("/home/navaneeth/work/kaggle/titanic/gender_submission.csv", index=False)
scores = model.evaluate(test, predictions, verbose=0)
print(scores)
print(predictions.history.keys())
plt.plot(predictions.history['acc'])
plt.plot(predictions.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

错误似乎

print(predictions.history.keys()) 属性错误:"numpy.ndarray"对象没有属性"历史记录">

您不应该在预测中使用历史记录,而只应在之前训练的模型上使用历史记录。 所以在我看来,model.history.keys()正是你需要做的。

您可以在此处找到更多信息:

Keras 回调

这个问题很旧,但其他人可能会发现它很有用。只能提取已训练模型的历史记录详细信息,而不能在用于预测时提取。我不确定在解析 fmodel(X,y)函数后是否仍然可以检索数据。以下是两种管理方法:

  1. 将绘图函数添加到model(X, y)过程中;

  2. 使用np.save保存历史记录详细信息,稍后加载以显示它。

最新更新