-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Update trainer api #10653
Closed
Closed
Update trainer api #10653
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9dff644
Inferencer take infer_func as parameter
jacquesqiao d94f673
update trainer and word2vector demo
jacquesqiao 52ac039
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao acc94dc
delete unused code
jacquesqiao 214dc6e
update test_fit_a_line
jacquesqiao 1a05ae0
update test_recognize_digits_conv.py
jacquesqiao e347198
update test_recognize_digits_mlp.py
jacquesqiao ac0836c
clean code
jacquesqiao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,48 +53,39 @@ def train_program(): | |
predict = inference_program() | ||
cost = fluid.layers.cross_entropy(input=predict, label=label) | ||
avg_cost = fluid.layers.mean(cost) | ||
# acc = fluid.layers.accuracy(input=predict, label=label) | ||
# return avg_cost, acc | ||
return avg_cost | ||
acc = fluid.layers.accuracy(input=predict, label=label) | ||
return [avg_cost, acc] | ||
|
||
|
||
def train(use_cuda, save_dirname): | ||
def train(use_cuda, train_program, save_dirname): | ||
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() | ||
optimizer = fluid.optimizer.Adam(learning_rate=0.001) | ||
|
||
trainer = fluid.Trainer( | ||
train_func=train_program, | ||
infer_func=inference_program, | ||
place=place, | ||
optimizer=optimizer) | ||
train_func=train_program, place=place, optimizer=optimizer) | ||
|
||
def event_handler(event): | ||
if isinstance(event, fluid.EndEpochEvent): | ||
# if (event.epoch + 1) % 10 == 0: | ||
# trainer.save_params(save_dirname) | ||
trainer.save_inference_model(save_dirname) | ||
|
||
# TODO: Uncomment this part once we are sure that .train is working | ||
# test_reader = paddle.batch( | ||
# paddle.dataset.mnist.test(), batch_size=BATCH_SIZE) | ||
# test_metrics = trainer.test(reader=test_reader) | ||
# avg_cost_set = test_metrics[0] | ||
# acc_set = test_metrics[1] | ||
# | ||
# # get test acc and loss | ||
# acc = numpy.array(acc_set).mean() | ||
# avg_cost = numpy.array(avg_cost_set).mean() | ||
# | ||
# print("avg_cost: %s" % avg_cost) | ||
# print("acc : %s" % acc) | ||
# | ||
# if float(acc) > 0.2: # Smaller value to increase CI speed | ||
# trainer.save_params(save_dirname) | ||
# else: | ||
# print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format( | ||
# event.epoch + 1, float(avg_cost), float(acc))) | ||
# if math.isnan(float(avg_cost)): | ||
# sys.exit("got NaN loss, training failed.") | ||
test_reader = paddle.batch( | ||
paddle.dataset.mnist.test(), batch_size=BATCH_SIZE) | ||
test_metrics = trainer.test(reader=test_reader) | ||
avg_cost_set = test_metrics[0] | ||
acc_set = test_metrics[1] | ||
|
||
# get test acc and loss | ||
acc = numpy.array(acc_set).mean() | ||
avg_cost = numpy.array(avg_cost_set).mean() | ||
|
||
print("avg_cost: %s" % avg_cost) | ||
print("acc : %s" % acc) | ||
|
||
if float(acc) > 0.2: # Smaller value to increase CI speed | ||
trainer.save_params(save_dirname) | ||
else: | ||
print('BatchID {0}, Test Loss {1:0.2}, Acc {2:0.2}'.format( | ||
event.epoch + 1, float(avg_cost), float(acc))) | ||
if math.isnan(float(avg_cost)): | ||
sys.exit("got NaN loss, training failed.") | ||
|
||
train_reader = paddle.batch( | ||
paddle.reader.shuffle( | ||
|
@@ -108,10 +99,11 @@ def event_handler(event): | |
feed_order=['img', 'label']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
def infer(use_cuda, save_dirname=None): | ||
def infer(use_cuda, inference_program, save_dirname=None): | ||
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() | ||
|
||
inferencer = fluid.Inferencer(param_path=save_dirname, place=place) | ||
inferencer = fluid.Inferencer( | ||
infer_func=inference_program, param_path=save_dirname, place=place) | ||
|
||
batch_size = 1 | ||
tensor_img = numpy.random.uniform(-1.0, 1.0, | ||
|
@@ -126,8 +118,14 @@ def main(use_cuda): | |
save_dirname = "recognize_digits_conv.inference.model" | ||
|
||
# call train() with is_local argument to run distributed train | ||
train(use_cuda=use_cuda, save_dirname=save_dirname) | ||
infer(use_cuda=use_cuda, save_dirname=save_dirname) | ||
train( | ||
use_cuda=use_cuda, | ||
train_program=train_program, | ||
save_dirname=save_dirname) | ||
infer( | ||
use_cuda=use_cuda, | ||
inference_program=inference_program, | ||
save_dirname=save_dirname) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed we switched from
trainer.save_inference_model(save_dirname)
back totrainer.save_params(save_dirname)
. What is the diff between them? We discussed this here. To me save_inference_model looks more reasonable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#10648
@wangkuiyi Think that we only need to save parameters and do not need to save inference program, inference program should be provided by a Python function.