简体   繁体   中英

Test neural network using Keras Python

I have trained and tested a Feed Forward Neural Network using Keras in Python with a dataset. But each time, in order to recognize a new test set with external data (external since the data are not included within the dataset), I have to re-train the Feed Forward Neural Network to compute the test set. For instance each time I have to do:

 model.fit (data, output_data)
 prediction=model.predict_classes(new_test)
 print "Prediction : " prediction

Obtaining correct output:

  Prediction: [1 2 3 4 5 1 2 3 1 2 3]
  Acc: 100%

Now I would test a new test set, namely "new_test2.csv" without re-training again, just using what the network has learned. I am also thinking about a sort of real time recognition.

How I should do that?

Thanks in advance

With a well trained model you can make predictions on any new data. You don´t have to retrain anything because (hopefully) your model can generalize it´s learning to unseen data and will achieve comparable accuracy.

Just feed in the data from "new_test2.csv" to your predict function:

prediction=model.predict_classes(content_of_new_test2)

Obviously you need data of the same type and classes. In addition to that you need to apply any transformations to the new data in the same way you may have transformed the data you trained your model on.

If you want realtime predictions you could setup an API with Flask:

http://flask.pocoo.org/

Regarding terminology and correct method of training:

You train on a training set (eg 70% of all the data you have).

You validate your training with a validation set (eg 15% of your data). You use the accuracy and loss values from your training to tune your hyperparameters.

You then evaluate your models final performance by predicting data from your test set (again 15% of your data). That has to be data, your network hasn´t seen before at all and hasn´t been used by you to optimize training parameters.

After that you can predict on production data.

If you want to save your trained model use this (taken from Keras documentation):

from keras.models import load_model

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
del model  # deletes the existing model

# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5') 

https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model

In your training file, you can save the model using

model.save('my_model.h5')

Later, whenever you want to test, you can load it with

from keras.models import load_model
model = load_model('my_model.h5')

Then you can call model.predict and whatnot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM