AttributeError:“ numpy.ndarray”对象没有属性“ history”

时间:2019-07-26 04:40:40

标签: python machine-learning keras

我试图在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()) AttributeError:“ numpy.ndarray”对象没有属性“ history”

1 个答案:

答案 0 :(得分:0)

您不应在预测中使用历史记录,而应在先前训练的模型上使用历史记录。 因此,我认为model.history.keys()正是您需要做的。

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

Keras callbacks