简体   繁体   English

使用Keras Python测试神经网络

[英]Test neural network using Keras Python

I have trained and tested a Feed Forward Neural Network using Keras in Python with a dataset. 我已经在Python中使用Keras在带有数据集的情况下训练并测试了前馈神经网络。 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. 现在,我将仅使用网络学到的知识来测试一个新的测试集,即“ new_test2.csv”,而无需再次进行重新训练。 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: 只需将数据从“ new_test2.csv”输入到您的预测函数即可:

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: 如果您需要实时预测,则可以使用Flask设置API:

http://flask.pocoo.org/ 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). 训练训练集 (所有你的数据的例如70%)。

You validate your training with a validation set (eg 15% of your data). 您可以使用验证集 (例如数据的15%)来验证您的训练。 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). 然后,您可以通过预测测试集中的数据(数据的15%)来评估模型的最终性能。 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): 如果要保存训练好的模型,请使用以下命令(来自Keras文档):

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 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. 然后,您可以调用model.predict等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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