GeezerVison is showing github.com — Merge pull request #92 from memimo/predict · starimpact/DeepLearningTutorials@55db5cd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: code/convolutional_mlp.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -110,6 +110,9 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)): | 110 | 110 | # store parameters of this layer | 111 | 111 | self.params = [self.W, self.b] | 112 | 112 |
| | 113 | + # keep track of model input | | 114 | + self.input = input | | 115 | + | 113 | 116 |
| 114 | 117 | def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 115 | 118 | dataset='mnist.pkl.gz', | |
Collapse file Copy file name to clipboardExpand all lines: code/logistic_cg.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -97,6 +97,9 @@ def __init__(self, input, n_in, n_out): | 97 | 97 | # symbolic form | 98 | 98 | self.y_pred = T.argmax(self.p_y_given_x, axis=1) | 99 | 99 |
| | 100 | + # keep track of model input | | 101 | + self.input = input | | 102 | + | 100 | 103 | def negative_log_likelihood(self, y): | 101 | 104 | """Return the negative log-likelihood of the prediction of this model | 102 | 105 | under a given target distribution. | |
Collapse file Copy file name to clipboardExpand all lines: code/logistic_sgd.py +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,6 +109,9 @@ def __init__(self, input, n_in, n_out): | 109 | 109 | # parameters of the model | 110 | 110 | self.params = [self.W, self.b] | 111 | 111 |
| | 112 | + # keep track of model input | | 113 | + self.input = input | | 114 | + | 112 | 115 | def negative_log_likelihood(self, y): | 113 | 116 | """Return the mean of the negative log-likelihood of the prediction | 114 | 117 | of this model under a given target distribution. | @@ -415,6 +418,10 @@ def sgd_optimization_mnist(learning_rate=0.13, n_epochs=1000, | 415 | 418 | ) | 416 | 419 | ) | 417 | 420 |
| | 421 | + # save the best model | | 422 | + with open('best_model.pkl', 'w') as f: | | 423 | + cPickle.dump(classifier, f) | | 424 | + | 418 | 425 | if patience <= iter: | 419 | 426 | done_looping = True | 420 | 427 | break | @@ -433,5 +440,31 @@ def sgd_optimization_mnist(learning_rate=0.13, n_epochs=1000, | 433 | 440 | os.path.split(__file__)[1] + | 434 | 441 | ' ran for %.1fs' % ((end_time - start_time))) | 435 | 442 |
| | 443 | + | | 444 | +def predict(): | | 445 | + """ | | 446 | + An example of how to load a trained model and use it | | 447 | + to predict labels. | | 448 | + """ | | 449 | + | | 450 | + # load the saved model | | 451 | + classifier = cPickle.load(open('best_model.pkl')) | | 452 | + | | 453 | + # compile a predictor function | | 454 | + predict_model = theano.function( | | 455 | + inputs=[classifier.input], | | 456 | + outputs=classifier.y_pred) | | 457 | + | | 458 | + # We can test it on some examples from test test | | 459 | + dataset='mnist.pkl.gz' | | 460 | + datasets = load_data(dataset) | | 461 | + test_set_x, test_set_y = datasets[2] | | 462 | + test_set_x = test_set_x.get_value() | | 463 | + | | 464 | + predicted_values = predict_model(test_set_x[:10]) | | 465 | + print ("Predicted values for the first 10 examples in test set:") | | 466 | + print predicted_values | | 467 | + | | 468 | + | 436 | 469 | if __name__ == '__main__': | 437 | 470 | sgd_optimization_mnist() |
Collapse file Copy file name to clipboardExpand all lines: code/mlp.py +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -191,6 +191,9 @@ def __init__(self, rng, input, n_in, n_hidden, n_out): | 191 | 191 | self.params = self.hiddenLayer.params + self.logRegressionLayer.params | 192 | 192 | # end-snippet-3 | 193 | 193 |
| | 194 | + # keep track of model input | | 195 | + self.input = input | | 196 | + | 194 | 197 |
| 195 | 198 | def test_mlp(learning_rate=0.01, L1_reg=0.00, L2_reg=0.0001, n_epochs=1000, | 196 | 199 | dataset='mnist.pkl.gz', batch_size=20, n_hidden=500): | |
Collapse file Copy file name to clipboardExpand all lines: doc/logreg.txt +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -264,6 +264,19 @@ approximately 1.936 epochs/sec and it took 75 epochs to reach a test | 264 | 264 | error of 7.489%. On the GPU the code does almost 10.0 epochs/sec. For this | 265 | 265 | instance we used a batch size of 600. | 266 | 266 |
| | 267 | + | | 268 | +Prediction Using a Trained Model | | 269 | ++++++++++++++++++++++++++++++++ | | 270 | + | | 271 | +``sgd_optimization_mnist`` serialize and pickle the model each time new | | 272 | +lowest validation error is reached. We can reload this model and predict | | 273 | +labels of new data. ``predict`` function shows an example of how | | 274 | +this could be done. | | 275 | + | | 276 | +.. literalinclude:: ../code/logistic_sgd.py | | 277 | + :pyobject: predict | | 278 | + | | 279 | + | 267 | 280 | .. rubric:: Footnotes | 268 | 281 |
| 269 | 282 | .. [#f1] For smaller datasets and simpler models, more sophisticated descent | |
|