Deeplearning Tutorial (2) machine learning algorithm saves parameters during training

Source: Internet
Author: User
Tags gz file object serialization

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
    1. #以读取mnist. pkl.gz as an example
    2. Import Cpickle, gzip
    3. f = gzip.open (' mnist.pkl.gz ', ' RB ')
    4. Train_set, valid_set, test_set = Cpickle.load (f)
    5. 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
  1. a=[1,2,3]
  2. b={4:5,6:7}
  3. #保存, 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.
  4. Import Cpickle
  5. Write_file=open ('/home/wepon/ab ',' WB ')
  6. Cpickle.dump (a,write_file,-1)
  7. Cpickle.dump (b,write_file,-1)
  8. Write_file.close ()
  9. #读取, Cpickle.load function.
  10. Read_file=open ('/home/wepon/ab ',' RB ')
  11. A_1=cpickle.load (Read_file)
  12. B_1=cpickle.load (Read_file)
  13. Print A, b
  14. 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
  1. Import Cpickle
  2. #保存
  3. Write_file = open (' path ', ' WB ')
  4. Cpickle.dump (W.get_value (borrow=True), Write_file,-1)
  5. Cpickle.dump (V.get_value (borrow=True), Write_file,-1)
  6. Cpickle.dump (U.get_value (borrow=True), Write_file,-1)
  7. Write_file.close ()
  8. #读取
  9. Read_file = open (' path ')
  10. W.set_value (Cpickle.load (read_file), borrow=True)
  11. V.set_value (Cpickle.load (read_file), borrow=True)
  12. U.set_value (Cpickle.load (read_file), borrow=True)
  13. Read_file.close ()

Deeplearning Tutorial (2) machine learning algorithm saves parameters during training

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.