I am little white, said not very good, please forgive
@author: Wepon
@blog: http://blog.csdn.net/u012162613/article/details/43169019
Reference: Pickle-python object serialization, Deeplearning Getting started
One, Python read "***.pkl.gz" file
Using the gzip and cpickle modules in Python, simply use the code below and refer to the links given above if you want to learn more about them.
[Python]View PlainCopy
- #以读取mnist. pkl.gz as an example
- Import Cpickle, gzip
- f = gzip.open (' mnist.pkl.gz ', ' RB ')
- Train_set, valid_set, test_set = Cpickle.load (f)
- F.close ()
In fact, it is a two-step, first read the GZ file, and then read the Pkl file. The application of the Pkl file is the following, which we use to preserve the parameters in the training process of the machine learning algorithm.
Second, how to save parameters in the training process of machine learning algorithm? We know that the machine learning algorithm is particularly computationally intensive, running the program to less than a few 10 minutes, more hours or even days, in the middle if there is any situation (such as computer overheating restart, the program appeared some small bugs ...) The program will be interrupted, if you do not save the parameters, the previous training is wasted, so it is necessary to add the function of the timing saving parameters in the program, so that the next training can be initialized to the last saved results, instead of starting from the beginning of random initialization.
So how do you save the model parameters? You can copy the parameters deep, or call Python data to permanently store the Cpickle module, the principle is not much to say, directly use the line. (Note: Python has cpickle and pickle,cpickle based on the C implementation, faster than pickle.) )
Use an example directly to illustrate how to work with:
[Python]View PlainCopy
- a=[1,2,3]
- b={4:5,6:7}
- #保存, Cpickle.dump function. /home/wepon/ab is the path, AB is the name of the saved file, if/home/wepon/is already there ab this file will be overwritten #, if not, then created. ' WB ' means to open in binary writable mode. The 1 in dump indicates the use of highest protocol.
- Import Cpickle
- Write_file=open ('/home/wepon/ab ',' WB ')
- Cpickle.dump (a,write_file,-1)
- Cpickle.dump (b,write_file,-1)
- Write_file.close ()
- #读取, Cpickle.load function.
- Read_file=open ('/home/wepon/ab ',' RB ')
- A_1=cpickle.load (Read_file)
- B_1=cpickle.load (Read_file)
- Print A, b
- Read_file.close ()
Number filtering software mobile phone number filter tool
In the deeplearning algorithm, because the GPU is used, the parameters are often declared as shared variables, so you must use the Get_value (), Set_value, for example, W, V, and U three shared variables, using the code as follows:
[Python]View PlainCopy
- Import Cpickle
- #保存
- 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_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 ()
Deeplearning Tutorial (2) machine learning algorithm saves parameters during training