user2543622
user2543622

Reputation: 6756

keras tensorflow2 get results for the training data

In keras we could train model using fit command and then use predict.

Dcnn=model.fit(x_train, y_train, epochs=5, batch_size=32)
model.predict(test_dataset,verbose=True)

when we use fit method we get accuracy results as below. Lets say after 5 epochs we got accuracy of 98.62% on the training data. Now if we use model.predict(x_train,verbose=True) would we get the exact same accuracy and exactly same predictions for each observation as shown in the outcome of fit method? if not, why?

Epoch 5/5
61/61 - 11s - loss: 0.0320 - tp: 1602.0000 - fp: 18.0000 - tn: 321.0000 - fn: 9.0000 - accuracy: 0.9862 

update1

I updated commands as below Dcnn.fit(train_dataset, epochs=NB_EPOCHS, verbose=2,validation_data=test_dataset)

and i got below results

Epoch 5/5

61/61 - 11s - loss: 0.0320 - tp: 1602.0000 - fp: 18.0000 - tn: 321.0000 - fn: 9.0000 - accuracy: 0.9862 - precision: 0.9889 - recall: 0.9944 - auc: 0.9990 - val_loss: 0.9760 - val_tp: 161.0000 - val_fp: 22.0000 - val_tn: 9.0000 - val_fn: 0.0000e+00 - val_accuracy: 0.8854 - val_precision: 0.8798 - val_recall: 1.0000 - val_auc: 0.7169

Now if i try model.predict(test_dataset,verbose=True) I get 88.54% accuracy - same as output of the fit method.

If i run model.predict(train_dataset,verbose=True), would i get accuracy 98.62%? if no, then why?

Upvotes: 2

Views: 423

Answers (1)

Igna
Igna

Reputation: 1127

When calling fit() the model is still training and the results are not accurate. Calling predict() is accurate since the model is done training.

You can't see the probabilities for the classes using fit. That is due to the weights being changed after each batch based on the probabilities. Meaning that from a batch to another you have different weights, and as such would give different answers even on the same data.

In order to get the real probabilities, you can use predict(). Predict and fit wouldn't return the same probabilities since predict returns for the whole set while fit is an average over all of the batches (with varying weights between batches).

Also, you might be confused, calling model.fit() returns a History object that contains the values for the loss and accuracy and such. Calling Dcnn.predict() would give an error since History objects don't have a "predict" attribute. model.predict() should be used to get the predictions which in turn can be used to calculate probabilities.

The weights of a model are saved in the instance itself, such that after calling fit() the weights are changed and saved automatically by the model.

Upvotes: 1

Related Questions