caffe's python interface learning (2) generate solver files

 When caffe is training, it needs some parameter settings. We generally set these parameters in a file called solver.prototxt

There are some parameters that need to be calculated and are not set randomly.

Suppose we have 50,000 training samples and batch_size is 64, that is, 64 samples are processed in each batch, then it takes 50,000/64=782 iterations to process all the samples once. We process all the samples once and call it a generation, that is, an epoch. Therefore, the test_interval here is set to 782, that is, after all the training data is processed once, the test is performed. If we want to train for 100 generations, we need to set max_iter to 78200.

Similarly, if there are 10,000 test samples and batch_size is set to 32, then it takes 10,000/32=313 iterations to complete the test once, so set test_iter to 313.

 We set the learning rate change rule to gradually decrease as the number of iterations increases. A total of 78200 iterations, we will change lr_rate three times, so stepsize is set to 78200/3=26067, that is, every 26067 iterations, we reduce the learning rate once. 

The following is the python code to generate the solver file, which is relatively simple:

copy code
# -*- coding: utf-8 -*-
""" Created on Sun Jul 17 18:20:57 2016
 @author: root
""" path= ' /home/xxx/data/ ' solver_file=path+ ' solver.prototxt ' #solver file save location sp= {} sp[ ' train_net ']='"'+path+ ' train.prototxt" ' # Training profile sp[ ' test_net ']='"'+path+ ' val.prototxt" ' # Test profile sp[ ' test_iter ']= ' 313 ' # Test iterations sp[ ' test_interval ']= ' 782 ' # Test interval sp[ 'base_lr']='0.001' # base learning rate sp[ ' display ']= ' 782 ' # screen log display interval sp[ ' max_iter ']= ' 78200 ' # maximum number of iterations sp[ ' lr_policy ']= '" step" ' # learning rate change Law sp[ ' gamma ']= ' 0.1 ' # Learning rate change index sp[ ' momentum ']= ' 0.9 ' # Momentum sp[ ' weight_decay ']= ' 0.0005 ' # Weight decay sp[ 'stepsize']='26067 ' # learning Rate change frequency sp[ ' snapshot ']= ' 7820 ' # Save model interval sp[ ' snapshot_prefix raise TypeError( ' All solver parameters must be strings ' ) f.write( ' %s: %s\n ' % ​​(key,value)) if __name__ == ' ']=' " snapshot "' # Save model prefix sp[ ' solver_mode ']= ' GPU ' # Whether to use gpu sp[ ' solver_type ']= ' SGD ' # optimization algorithm def write_solver(): # write to file with open(solver_file, ' w ' ) as f: for key, value in sorted(sp.items()):if not(type(value) is str): __main__': write_solver()
copy code

 Execute the above file, we will get a solver.prototxt file, with this file, we can train in the next step.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325313153&siteId=291194637