Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix model saving bug #392

Merged
merged 2 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ctr/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __event_handler__(event):
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(
Expand Down
4 changes: 2 additions & 2 deletions deep_speech_2/model_utils/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def event_handler(event):
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))
Expand All @@ -136,7 +136,7 @@ def event_handler(event):
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(
Expand Down
2 changes: 1 addition & 1 deletion dssm/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def _event_handler(event):
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,
Expand Down
8 changes: 4 additions & 4 deletions generate_chinese_poetry/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
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):
Expand Down Expand Up @@ -111,7 +111,7 @@ def event_handler(event):
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" % (
Expand All @@ -120,7 +120,7 @@ def event_handler(event):
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(
Expand Down
4 changes: 2 additions & 2 deletions generate_sequence_by_rnn_lm/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def event_handler(event):
"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:
Expand All @@ -70,7 +70,7 @@ def event_handler(event):
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(
Expand Down
8 changes: 4 additions & 4 deletions globally_normalized_reader/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ 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:
- save_path: The path to save the trained 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):
Expand Down Expand Up @@ -161,7 +161,7 @@ def event_handler(event):
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" %
Expand All @@ -170,7 +170,7 @@ def event_handler(event):
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

Expand Down
9 changes: 6 additions & 3 deletions hsigmoid/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ 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):
model_name = os.path.join(save_dir, "hsigmoid_pass_%05d.tar.gz" %
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:
Expand All @@ -41,8 +46,6 @@ def event_handler(event):
"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(
Expand Down
18 changes: 9 additions & 9 deletions image_classification/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -89,21 +96,14 @@ def event_handler(event):
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()
main()
2 changes: 1 addition & 1 deletion ltr/lambda_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def event_handler(event):
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(
Expand Down
2 changes: 1 addition & 1 deletion ltr/ranknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def event_handler(event):
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,
Expand Down
4 changes: 2 additions & 2 deletions mt_with_external_memory/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def event_handler(event):
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()
Expand All @@ -139,7 +139,7 @@ def event_handler(event):
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'):
Expand Down
10 changes: 2 additions & 8 deletions nce_cost/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ def event_handler(event):
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))
Expand All @@ -43,7 +37,7 @@ def event_handler(event):
"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(
Expand Down
2 changes: 1 addition & 1 deletion nested_sequence/text_classification/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def _event_handler(event):
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(
Expand Down
2 changes: 1 addition & 1 deletion nmt_without_attention/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def event_handler(event):
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" % (
Expand Down
2 changes: 1 addition & 1 deletion scheduled_sampling/scheduled_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def event_handler(event):
# 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(
Expand Down
2 changes: 1 addition & 1 deletion sequence_tagging_for_ner/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def event_handler(event):
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,
Expand Down
2 changes: 1 addition & 1 deletion ssd/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def event_handler(event):
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,
Expand Down
2 changes: 1 addition & 1 deletion text_classification/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _event_handler(event):
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,
Expand Down