diff --git a/ctr/train.py b/ctr/train.py index 235e6fa59be6416cfac6b1b5f2039c09889b9f76..421425e2a701e3c413e23ab81930e3c7e68d40ef 100644 --- a/ctr/train.py +++ b/ctr/train.py @@ -95,7 +95,7 @@ def train(): args.model_output_prefix, event.pass_id, event.batch_id, result.cost) with gzip.open(path, 'w') as f: - params.to_tar(f) + trainer.save_parameter_to_tar(f) trainer.train( reader=paddle.batch( diff --git a/deep_speech_2/model_utils/model.py b/deep_speech_2/model_utils/model.py index 123eed9b42e32aa0391fdac823d71bcc5b7710b4..5a0d8890dac3d6b4ce49ae3e3a0a945ef0de9132 100644 --- a/deep_speech_2/model_utils/model.py +++ b/deep_speech_2/model_utils/model.py @@ -111,7 +111,7 @@ class DeepSpeech2Model(object): output_model_path = os.path.join(output_model_dir, "params.latest.tar.gz") with gzip.open(output_model_path, 'w') as f: - self._parameters.to_tar(f) + trainer.save_parameter_to_tar(f) print("\nPass: %d, Batch: %d, TrainCost: %f" % (event.pass_id, event.batch_id + 1, cost_sum / cost_counter)) @@ -136,7 +136,7 @@ class DeepSpeech2Model(object): output_model_path = os.path.join( output_model_dir, "params.pass-%d.tar.gz" % event.pass_id) with gzip.open(output_model_path, 'w') as f: - self._parameters.to_tar(f) + trainer.save_parameter_to_tar(f) # run train trainer.train( diff --git a/dssm/train.py b/dssm/train.py index 98e7f321015c057dc41275677f159a7f806cc9eb..bc7685ab9632a7c34acdb2c0cacf7feec7699985 100644 --- a/dssm/train.py +++ b/dssm/train.py @@ -237,7 +237,7 @@ def train(train_data_path=None, with open("%sdssm_%s_pass_%05d.tar" % (args.model_output_prefix, model_desc, event.pass_id), "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) trainer.train( reader=train_reader, diff --git a/generate_chinese_poetry/train.py b/generate_chinese_poetry/train.py index a641efa59a82d9a8e2b3e37c393e347063568685..c6eb737b01da4a89d3d04d87a708fd41c17255c3 100755 --- a/generate_chinese_poetry/train.py +++ b/generate_chinese_poetry/train.py @@ -12,9 +12,9 @@ logger = logging.getLogger("paddle") logger.setLevel(logging.INFO) -def save_model(save_path, parameters): +def save_model(trainer, save_path, parameters): with gzip.open(save_path, "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) def load_initial_model(model_path, parameters): @@ -111,7 +111,7 @@ def train(num_passes, save_path = os.path.join(save_dir_path, "pass_%05d_batch_%05d.tar.gz" % (event.pass_id, event.batch_id)) - save_model(save_path, parameters) + save_model(trainer, save_path, parameters) if not event.batch_id % 5: logger.info("Pass %d, Batch %d, Cost %f, %s" % ( @@ -120,7 +120,7 @@ def train(num_passes, if isinstance(event, paddle.event.EndPass): save_path = os.path.join(save_dir_path, "pass_%05d.tar.gz" % event.pass_id) - save_model(save_path, parameters) + save_model(trainer, save_path, parameters) # start training trainer.train( diff --git a/generate_sequence_by_rnn_lm/train.py b/generate_sequence_by_rnn_lm/train.py index 00e8ffa742ffd1966ee6f39b2fb14394ac870fcf..ab6219ec68b08bf2cff8f7b277df06b8e8ae7f47 100644 --- a/generate_sequence_by_rnn_lm/train.py +++ b/generate_sequence_by_rnn_lm/train.py @@ -60,7 +60,7 @@ def train(topology, "rnn_lm_pass_%05d_batch_%03d.tar.gz" % (event.pass_id, event.batch_id)) with gzip.open(save_name, "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) if isinstance(event, paddle.event.EndPass): if test_reader is not None: @@ -70,7 +70,7 @@ def train(topology, save_name = os.path.join(model_save_dir, "rnn_lm_pass_%05d.tar.gz" % (event.pass_id)) with gzip.open(save_name, "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) logger.info("start training...") trainer.train( diff --git a/globally_normalized_reader/train.py b/globally_normalized_reader/train.py index b06b9720adfcbb2a30e32ba6bd61f7a0c04dde72..b0e6ca6d9fa79314ccea6f21848603961a97d5dd 100644 --- a/globally_normalized_reader/train.py +++ b/globally_normalized_reader/train.py @@ -43,7 +43,7 @@ def load_pretrained_parameters(path): return np.load(path) -def save_model(save_path, parameters): +def save_model(trainer, save_path, parameters): """ Save the trained parameters. Arguments: @@ -51,7 +51,7 @@ def save_model(save_path, parameters): - parameters: The trained model parameters. """ with gzip.open(save_path, "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) def show_parameter_init_info(parameters): @@ -161,7 +161,7 @@ def build_event_handler(config, parameters, trainer): if event.batch_id and not event.batch_id % config.checkpoint_period: save_path = os.path.join(config.save_dir, "checkpoint_param.latest.tar.gz") - save_model(save_path, parameters) + save_model(trainer, save_path, parameters) if not event.batch_id % config.log_period: logger.info("Pass %d, Batch %d, Cost %f" % @@ -170,7 +170,7 @@ def build_event_handler(config, parameters, trainer): if isinstance(event, paddle.event.EndPass): save_path = os.path.join(config.save_dir, "pass_%05d.tar.gz" % event.pass_id) - save_model(save_path, parameters) + save_model(trainer, save_path, parameters) return event_handler diff --git a/hsigmoid/train.py b/hsigmoid/train.py index 7d9775bdcf564bdc72997c49125cf5c2be27dc0c..75769c30ac015dfa3eb1438edf260c94ce981c09 100644 --- a/hsigmoid/train.py +++ b/hsigmoid/train.py @@ -22,7 +22,12 @@ def main(save_dir="models"): regularization=paddle.optimizer.L2Regularization(8e-4)) cost = ngram_lm(hidden_size=256, embed_size=32, dict_size=dict_size) + parameters = paddle.parameters.create(cost) + adam_optimizer = paddle.optimizer.Adam( + learning_rate=3e-3, + regularization=paddle.optimizer.L2Regularization(8e-4)) + trainer = paddle.trainer.SGD(cost, parameters, adam_optimizer) def event_handler(event): if isinstance(event, paddle.event.EndPass): @@ -30,7 +35,7 @@ def main(save_dir="models"): event.pass_id) logger.info("Save model into %s ..." % model_name) with gzip.open(model_name, "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) if isinstance(event, paddle.event.EndIteration): if event.batch_id and event.batch_id % 10 == 0: @@ -41,8 +46,6 @@ def main(save_dir="models"): "Pass %d, Batch %d, Cost %f, Test Cost %f" % (event.pass_id, event.batch_id, event.cost, result.cost)) - trainer = paddle.trainer.SGD(cost, parameters, adam_optimizer) - trainer.train( paddle.batch( paddle.reader.shuffle( diff --git a/image_classification/train.py b/image_classification/train.py index 37f5deec28418c3aa523b0969f99d583bd34f161..12a582db3a28f8e136a87d29ca09be4b8b4782cb 100644 --- a/image_classification/train.py +++ b/image_classification/train.py @@ -81,6 +81,13 @@ def main(): # reader.test_reader('val.list'), batch_size=BATCH_SIZE) + # Create trainer + trainer = paddle.trainer.SGD( + cost=cost, + parameters=parameters, + update_equation=optimizer, + extra_layers=extra_layers) + # End batch and end pass event handler def event_handler(event): if isinstance(event, paddle.event.EndIteration): @@ -89,21 +96,14 @@ def main(): event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) result = trainer.test(reader=test_reader) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) - # Create trainer - trainer = paddle.trainer.SGD( - cost=cost, - parameters=parameters, - update_equation=optimizer, - extra_layers=extra_layers) - trainer.train( reader=train_reader, num_passes=200, event_handler=event_handler) if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/ltr/lambda_rank.py b/ltr/lambda_rank.py index fb527ed3d099fca5b7a2da04f51d64a97c6a9c2f..fade0ac7bf17631345846c7769dd4e8325c5e915 100644 --- a/ltr/lambda_rank.py +++ b/ltr/lambda_rank.py @@ -82,7 +82,7 @@ def train_lambda_rank(num_passes): print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) with gzip.open("lambda_rank_params_%d.tar.gz" % (event.pass_id), "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) feeding = {"label": 0, "data": 1} trainer.train( diff --git a/ltr/ranknet.py b/ltr/ranknet.py index 7b45ca65608cefe89a58545bd47f6d118b422c83..339bb34cfbb17931359399128fe81110fa15301e 100644 --- a/ltr/ranknet.py +++ b/ltr/ranknet.py @@ -86,7 +86,7 @@ def train_ranknet(num_passes): print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) with gzip.open("ranknet_params_%d.tar.gz" % (event.pass_id), "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) trainer.train( reader=train_reader, diff --git a/mt_with_external_memory/train.py b/mt_with_external_memory/train.py index ecb6d18331f21a7eeec07c78fa856149e2056bfb..4f413564ff538837061afafbd7c33f2b1e5d9f89 100644 --- a/mt_with_external_memory/train.py +++ b/mt_with_external_memory/train.py @@ -129,7 +129,7 @@ def train(): print "Pass: %d, Batch: %d, TrainCost: %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) with gzip.open("checkpoints/params.latest.tar.gz", 'w') as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) else: sys.stdout.write('.') sys.stdout.flush() @@ -139,7 +139,7 @@ def train(): result.metrics) with gzip.open("checkpoints/params.pass-%d.tar.gz" % event.pass_id, 'w') as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) # run train if not os.path.exists('checkpoints'): diff --git a/nce_cost/train.py b/nce_cost/train.py index a3540ac87f818daf83d66a29d47edcde1da22292..e76df867d9091193b71281cc5f91ba27cee19e40 100644 --- a/nce_cost/train.py +++ b/nce_cost/train.py @@ -28,13 +28,7 @@ def train(model_save_dir): if event.batch_id and not event.batch_id % 10: logger.info("Pass %d, Batch %d, Cost %f" % (event.pass_id, event.batch_id, event.cost)) - save_path = os.path.join(model_save_dir, - "model_pass_%05d.tar.gz" % event.pass_id) - logger.info("Save model into %s ..." % save_path) - with gzip.open(save_path, "w") as f: - parameters.to_tar(f) - - if isinstance(event, paddle.event.EndPass): + elif isinstance(event, paddle.event.EndPass): result = trainer.test( paddle.batch(paddle.dataset.imikolov.test(word_dict, 5), 64)) logger.info("Test Pass %d, Cost %f" % (event.pass_id, result.cost)) @@ -43,7 +37,7 @@ def train(model_save_dir): "model_pass_%05d.tar.gz" % event.pass_id) logger.info("Save model into %s ..." % save_path) with gzip.open(save_path, "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) trainer.train( paddle.batch( diff --git a/nested_sequence/text_classification/train.py b/nested_sequence/text_classification/train.py index a0da1ad0c57fb4dc32f13fd57d92f4f925e7a67c..20ad59dd0d55d817c37fa415049d0a8d67e98bb0 100644 --- a/nested_sequence/text_classification/train.py +++ b/nested_sequence/text_classification/train.py @@ -179,7 +179,7 @@ def train(train_data_dir, test_data_dir, word_dict_path, label_dict_path, with gzip.open( os.path.join(model_save_dir, "params_pass_%05d.tar.gz" % event.pass_id), "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) # begin training network trainer.train( diff --git a/nmt_without_attention/train.py b/nmt_without_attention/train.py index 15585e18978080cac0d3536a6aea8825eb8f04cd..144d61fac554b7cbe62b4a31bffc866c7453ca5d 100644 --- a/nmt_without_attention/train.py +++ b/nmt_without_attention/train.py @@ -50,7 +50,7 @@ def train(save_dir_path, source_dict_dim, target_dict_dim): os.path.join(save_path, "nmt_without_att_%05d_batch_%05d.tar.gz" % event.pass_id, event.batch_id), "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) if event.batch_id and not event.batch_id % 10: logger.info("Pass %d, Batch %d, Cost %f, %s" % ( diff --git a/scheduled_sampling/scheduled_sampling.py b/scheduled_sampling/scheduled_sampling.py index e2e328ea6ab4a46a6d4d066d65302b557407e1d5..9e7017806d6007a1c77539d7c0bbba7194bd3f79 100644 --- a/scheduled_sampling/scheduled_sampling.py +++ b/scheduled_sampling/scheduled_sampling.py @@ -264,7 +264,7 @@ def main(): # save parameters with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) # start to train trainer.train( diff --git a/sequence_tagging_for_ner/train.py b/sequence_tagging_for_ner/train.py index 5facfeda0d7152bdc065e141d73392eb51e4c2f8..398f526e25877046e500cf36775547c8b40b7a11 100644 --- a/sequence_tagging_for_ner/train.py +++ b/sequence_tagging_for_ner/train.py @@ -87,7 +87,7 @@ def main(train_data_file, with gzip.open( os.path.join(model_save_dir, "params_pass_%d.tar.gz" % event.pass_id), "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) result = trainer.test(reader=test_reader, feeding=feeding) logger.info("\nTest with Pass %d, %s" % (event.pass_id, diff --git a/ssd/train.py b/ssd/train.py index 783944214b67d15af31267c8ba1ded3fa48e6cb0..602742b54cbdfecf2cb7cf5d2085134c12086953 100644 --- a/ssd/train.py +++ b/ssd/train.py @@ -55,7 +55,7 @@ def train(train_file_list, dev_file_list, data_args, init_model_path): if isinstance(event, paddle.event.EndPass): with gzip.open('checkpoints/params_pass_%05d.tar.gz' % \ event.pass_id, 'w') as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) result = trainer.test(reader=dev_reader, feeding=feeding) print "\nTest with Pass %d, TestCost: %f, Detection mAP=%g" % \ (event.pass_id, diff --git a/text_classification/train.py b/text_classification/train.py index 3d1a5819e3a9fafc0aec5971af8046f8c37b93c8..cda04bfc6a33ee9e39298910d724bc716f1b53df 100644 --- a/text_classification/train.py +++ b/text_classification/train.py @@ -141,7 +141,7 @@ def train(topology, with gzip.open( os.path.join(model_save_dir, "dnn_params_pass_%05d.tar.gz" % event.pass_id), "w") as f: - parameters.to_tar(f) + trainer.save_parameter_to_tar(f) trainer.train( reader=train_reader,