An example of how to load a trained model and use it to predict labels.
()
| 447 | |
| 448 | |
| 449 | def predict(): |
| 450 | """ |
| 451 | An example of how to load a trained model and use it |
| 452 | to predict labels. |
| 453 | """ |
| 454 | |
| 455 | # load the saved model |
| 456 | classifier = pickle.load(open('best_model.pkl')) |
| 457 | |
| 458 | # compile a predictor function |
| 459 | predict_model = theano.function( |
| 460 | inputs=[classifier.input], |
| 461 | outputs=classifier.y_pred) |
| 462 | |
| 463 | # We can test it on some examples from test test |
| 464 | dataset='mnist.pkl.gz' |
| 465 | datasets = load_data(dataset) |
| 466 | test_set_x, test_set_y = datasets[2] |
| 467 | test_set_x = test_set_x.get_value() |
| 468 | |
| 469 | predicted_values = predict_model(test_set_x[:10]) |
| 470 | print("Predicted values for the first 10 examples in test set:") |
| 471 | print(predicted_values) |
| 472 | |
| 473 | |
| 474 | if __name__ == '__main__': |