DeepLearning tutorial (2) machine learning algorithm stores parameters during training
DeepLearning tutorial (2) machine learning algorithm stores parameters during training
@ Author: wepon
@ Blog: www.bkjia.com
See pickle-Python object serialization and DeepLearning Getting started.
1. python reads the *** .pkl.gz File
The gzip and cPickle modules in python are used. The simple code is as follows. For details, refer to the above link.
Example: import cPickle, gzipf = gzip.open('mnist.pkl.gz ', 'rb') train_set, valid_set, test_set = cPickle. load (f) f. close ()
In fact, there are two steps: Read the gz file first and then read the pkl file. The application of the pkl file is described below. We use it to save the parameters in the machine learning algorithm training process.
Ii. How does a machine learning algorithm save parameters during training?
We know that machine learning algorithms have a very large amount of computing power, and the number of running programs is dozens of minutes, the number of hours, or even days, if there is any problem in the middle (for example, the computer is overheated and restarted, and the program has some bugs ...) the program will be interrupted. If you do not save the parameters on a regular basis, the previous training will be wasted. Therefore, it is necessary to add the timed parameter saving function to the program, in this way, the parameter can be initialized to the result saved last time, instead of random initialization from the beginning.
So how do I save model parameters? You can copy parameters in depth or call the python data to permanently store the cPickle module. The principle is not to mention, just use it directly. (Note: cPickle and pickle are available in python. cPickle is implemented based on c and is faster than pickle .)
An example is used to illustrate how to use it:
A = [, 3] B = {,} # Save, cPickle. dump function. /Home/wepon/AB is the path and AB is the name of the saved file. If/home/wepon/already has the AB file, it will be overwritten #. If no,. 'Wb 'indicates that it is opened in binary writable mode. -1 in dump indicates that highest protocol is used. Import cPicklewrite_file = open ('/home/wepon/AB', 'wb') cPickle. dump (a, write_file,-1) cPickle. dump (B, write_file,-1) write_file.close () # Read, cPickle. load function. Read_file = open ('/home/wepon/AB', 'rb') a_1 = cPickle. load (read_file) B _1 = cPickle. load (read_file) print a, bread_file.close ()
In the deeplearning algorithm, because GPU is used, parameters are often declared as shared variables. Therefore, get_value () and set_value must be used. For example, three shared variables, w, v, and u, must be used, the Code is as follows:
Import cPickle # Save write_file = open ('path', 'wb ') cPickle. dump (w. get_value (borrow = True), write_file,-1) cPickle. dump (v. get_value (borrow = True), write_file,-1) cPickle. dump (u. get_value (borrow = True), write_file,-1) write_file.close () # Read read_file = open ('path') w. set_value (cPickle. load (read_file), borrow = True) v. set_value (cPickle. load (read_file), borrow = True) u. set_value (cPickle. load (read_file), borrow = True) read_file.close ()
One instance
The following example shows how to add the function of saving parameters to a program. Take logical regression on deeplearnig.net as an example. Its Code address is logistic_sgd.py. This program uses logistic regression for MNIST classification. parameters are not saved during the program running, or even at the end of the program running. How can we save parameters?
In the last sgd_optimization_mnist () function in the logistic_sgd.py code, there is a while loop with the following code:
if this_validation_loss < best_validation_loss:
This code is used to determine whether the current verification loss is less than the optimal verification loss. If yes, best_validation_loss will be updated. That is to say, the model is optimized under the current parameter, therefore, we can add the code for saving parameters after this if statement:
save_params(classifier.W,classifier.b)
The save_params function is defined as follows:
def save_params(param1,param2):import cPicklewrite_file = open('params', 'wb') cPickle.dump(param1.get_value(borrow=True), write_file, -1)cPickle.dump(param2.get_value(borrow=True), write_file, -1)write_file.close()
Of course, the number of parameters is defined as needed. In logistic_sgd.py, the parameter only includes classifier. W and classifier. B. Therefore, it is defined as save_params (param1, param2 ).
In logistic_sgd.py, I added save_params (classifier. w, classifier. b) Run epoch three times and interrupt the program. In the folder where the code is located, there is an extra params file. Let's take a look at what is in this file:
Import cPicklef = open ('params') w = cPickle. load (f) B = cPickle. load (f) # w is (n_in, n_out), B is (n_out,), and B has the following values, because MINST has 10 categories, n_out = 10, the following is a 10-digit array ([-0.0888151, 0.16875755,-0.03238435,-0.06493175, 0.05245609, 0.1754718,-0.0155049,-0.11216578]).
That is to say, the params file does save the parameters during training.
How can we use the saved parameters to initialize the parameters of our model?
Under class LogisticRegression (object) in logistic_sgd.py, self. W and self. bben is initialized to 0. We can add a few lines of code below, so that we can use the saved params file to initialize the parameter:
Class LogisticRegression (object): def _ init _ (self, input, n_in, n_out): self. W = theano. shared (value = numpy. zeros (n_in, n_out), dtype = theano. config. floatX), name = 'w', borrow = True) self. B = theano. shared (value = numpy. zeros (n_out,), dtype = theano. config. floatX), name = 'B', borrow = True )#!!! # The added code is here. When the program runs here, it will judge whether there is any params file in the current path. If yes, it will be used to initialize W and bif OS. path. exists ('params'): f = open ('params') self. w. set_value (cPickle. load (f), borrow = True) self. b. set_value (cPickle. load (f), borrow = True)