Tuesday, March 15, 2016

How to load saved theano parameters.

In almost all of my theano project, I saved intermediate results as well as parameters of models into files in order to keep track the changes of the models. One of problem I confused for a long time comes when I tried to reuse these presaved parameters when I rebuild my model.

Following steps shows how to correctly use these presaved parameters.


  1. Save the parameters using cPickle.dump().
  2. When reuse these parameters. Recompile your model to build computation graph (this step is very important, theano treat entire model as symbolic computation in computation graph. This step sort of pre-allocate space for all parameters)
  3. Load presaved parameters, say, we load them to a variable called "param". Be careful here, since all intermediate result including parameters are coded as CudaNdarraySharedVariable, so here when "param" is loaded, it is "another" instance of CudaNdarraySharedVariable which is "NOT" compiled into computation graph. Because of these, you can not directly assign it to target variable which saved all parameters using "g_params=params". Because if so, "g_params" becomes a CudaNdarraySharedVariable that is not compiled into computation graph. The correct way to do use set_value() method:

     params=cPickle.load(open('./params.pkl', 'rb'))
     coutner=0     for p in g_params:
        p.set_value(params[paramscoutner].get_value(), borrow=True)
        coutner+=1

No comments:

Post a Comment