提交 95621788 编写于 作者: Q qiaolongfei

support save parameter in trainer

上级 cf86ca04
...@@ -69,6 +69,7 @@ def main(): ...@@ -69,6 +69,7 @@ def main():
def event_handler(event): def event_handler(event):
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0: if event.batch_id % 100 == 0:
trainer.save_parameter("output", "batch-" + str(event.batch_id))
result = trainer.test( result = trainer.test(
paddle.batch( paddle.batch(
paddle.dataset.imikolov.test(word_dict, N), 32)) paddle.dataset.imikolov.test(word_dict, N), 32))
......
import collections import collections
import gzip
import os
import py_paddle.swig_paddle as api import py_paddle.swig_paddle as api
...@@ -96,6 +98,18 @@ class SGD(object): ...@@ -96,6 +98,18 @@ class SGD(object):
self.__gradient_machine__.prefetch(in_args) self.__gradient_machine__.prefetch(in_args)
self.__parameter_updater__.getParametersRemote() self.__parameter_updater__.getParametersRemote()
def save_parameter(self, dir_name, file_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
param_file_name = dir_name + "/" + file_name + '.tar.gz'
assert not os.path.exists(param_file_name)
self.__parameter_updater__.catchUpWith()
self.__parameter_updater__.apply()
self.__parameter_updater__.getParametersRemote(True, True)
with gzip.open(param_file_name, 'w') as f:
self.__parameters__.to_tar(f)
self.__parameter_updater__.restore()
def train(self, reader, num_passes=1, event_handler=None, feeding=None): def train(self, reader, num_passes=1, event_handler=None, feeding=None):
""" """
Training method. Will train num_passes of input data. Training method. Will train num_passes of input data.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册