summaryrefslogtreecommitdiff
path: root/paramsaveload.py
blob: 7181e9aa04898ebd26fb91742647c8c4b94cf56c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import logging

import numpy

import cPickle

from blocks.extensions import SimpleExtension

logging.basicConfig(level='INFO')
logger = logging.getLogger('extensions.SaveLoadParams')

class SaveLoadParams(SimpleExtension):
	def __init__(self, path, model, **kwargs):
		super(SaveLoadParams, self).__init__(**kwargs)

		self.path = path
		self.model = model
	
	def do_save(self):
		with open(self.path, 'w') as f:
			logger.info('Saving parameters to %s...'%self.path)
			cPickle.dump(model.get_param_values(), f, protocol=cPickle.HIGHEST_PROTOCOL)
	
	def do_load(self):
		try:
			with open(self.path, 'r') as f:
				logger.info('Loading parameters from %s...'%self.path)
				model.set_parma_values(cPickle.load(f))
		except IOError:
			pass

	def do(self, which_callback, *args):
		if which_callback == 'before_training':
			self.do_load()
		else:
			self.do_save()