0

i'm trying to plot the training diagram in keras but error occured while running the code.

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

the error seems

print(predictions.history.keys()) AttributeError: 'numpy.ndarray' object has no attribute 'history'

Dr Sheldon
  • 176
  • 1
  • 14

2 Answers2

0

You should not use history on predictions, just on model which you previously train. So model.history.keys() in my opinion is exactly what you need to do.

You can find more information here:

Keras callbacks

s3nh
  • 546
  • 2
  • 11
0

This question is old but others may find it useful. You can only extract the history details on trained model not when used for prediction. I am not sure if you could still retrieve the data after parsing out f the model(X,y) function. Here are two ways to manage it:

  1. add the plot function into the model(X, y) procedure;

  2. save the history details with np.save and load it later on to display it.

j__carlson
  • 1,346
  • 3
  • 12
  • 20
Olatunji
  • 59
  • 5