-
Notifications
You must be signed in to change notification settings - Fork 5.9k
trainer.test() #10453
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
Merged
Merged
trainer.test() #10453
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a2167a6
a draft of trainer.test()
JiayiFeng 5ff4446
polish trainer.test()
JiayiFeng bbb7c92
polish trainer.test()
JiayiFeng 3d441f8
update code format
JiayiFeng 3a42aa4
update
JiayiFeng 6aec3a7
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiayiFeng 13dcb21
polish code
JiayiFeng e81baf2
polish code
JiayiFeng 21be154
polish code
JiayiFeng 7068b9d
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiayiFeng 9e71cd1
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiayiFeng 69f489e
Make trainer.test follow the rule of returning [loss, metric, metric,…
JiayiFeng 18a69cb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
JiayiFeng 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -75,11 +75,16 @@ def __init__(self, program_func, optimizer, param_path=None, place=None): | |
| self.train_program = framework.Program() | ||
|
|
||
| with framework.program_guard(self.train_program, self.startup_program): | ||
| loss = program_func() | ||
| program_func_outs = program_func() | ||
| self.test_outputs = program_func_outs if isinstance( | ||
| program_func_outs, list) else [program_func_outs] | ||
| self.test_program = self.train_program.clone() | ||
| if not isinstance(optimizer, opt_module.Optimizer): | ||
| raise TypeError( | ||
| "The optimizer should be an instance of Optimizer") | ||
|
|
||
| # Here we assume that the fisrt one of program_func_outs is loss. | ||
| # It may be incorrect! | ||
| loss = self.test_outputs[0] | ||
| optimize_ops, params_grads = optimizer.minimize(loss) | ||
|
|
||
| self.place = Trainer._check_and_get_place(place) | ||
|
|
@@ -168,8 +173,17 @@ def train(self, | |
|
|
||
| self._train_by_executor(num_epochs, event_handler, reader, feed_order) | ||
|
|
||
| def test(self, reader): | ||
| pass | ||
| def test(self, reader, feed_order=None): | ||
| """ | ||
| Test the model on given test data | ||
|
|
||
| Args: | ||
| reader: The reader that yields test data. | ||
| feed_order: Feeding order of reader. None will following the defining | ||
| order in program | ||
| """ | ||
|
|
||
| return self._test_by_executor(reader, feed_order, self.test_outputs) | ||
|
|
||
| def save_params(self, param_path): | ||
| # reference: save_persistables in io.py | ||
|
|
@@ -225,26 +239,59 @@ def _train_by_executor(self, num_epochs, event_handler, reader, feed_order): | |
|
|
||
| """ | ||
| with self._prog_and_scope_guard(): | ||
| exe = executor.Executor(self.place) | ||
| if feed_order is None: | ||
| feed_var_list = [ | ||
| var | ||
| for var in self.train_program.global_block( | ||
| ).vars.itervalues() | ||
| if hasattr(var, 'is_data') and var.is_data | ||
| ] | ||
| else: | ||
| feed_var_list = [ | ||
| self.train_program.global_block().var(var_name) | ||
| for var_name in feed_order | ||
| ] | ||
|
|
||
| feed_var_list = build_feed_var_list(self.train_program, feed_order) | ||
| feeder = data_feeder.DataFeeder( | ||
| feed_list=feed_var_list, place=self.place) | ||
| exe = executor.Executor(self.place) | ||
| for epoch_id in range(num_epochs): | ||
| event_handler(BeginEpochEvent(epoch_id)) | ||
| for step_id, data in enumerate(reader()): | ||
| event_handler(BeginStepEvent(epoch_id, step_id)) | ||
| exe.run(feed=feeder.feed(data), fetch_list=[]) | ||
| event_handler(EndStepEvent(epoch_id, step_id)) | ||
| event_handler(EndEpochEvent(epoch_id)) | ||
|
|
||
| def _test_by_executor(self, reader, feed_order, fetch_list): | ||
| with executor.scope_guard(self.scope): | ||
| feed_var_list = build_feed_var_list(self.test_program, feed_order) | ||
|
Contributor
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. Trainer. train takes in |
||
| feeder = data_feeder.DataFeeder( | ||
| feed_list=feed_var_list, place=self.place) | ||
| exe = executor.Executor(self.place) | ||
| accumulated_loss = 0 | ||
| count = 0 | ||
| for data in reader(): | ||
| loss, = exe.run(program=self.test_program, | ||
| feed=feeder.feed(data), | ||
| fetch_list=fetch_list) | ||
| accumulated_loss += loss[0] | ||
| count += 1 | ||
|
|
||
| return accumulated_loss / count | ||
|
||
|
|
||
|
|
||
| def build_feed_var_list(program, feed_order): | ||
| if not isinstance(program, framework.Program): | ||
| raise TypeError("The 'program' should be an object of Program") | ||
|
|
||
| if feed_order is None: | ||
| feed_var_list = [ | ||
| var for var in program.global_block().vars.itervalues() | ||
| if var.is_data | ||
| ] | ||
| elif isinstance(feed_order, list): | ||
| feed_var_list = [ | ||
| program.global_block().var(var_name) for var_name in feed_order | ||
| ] | ||
| else: | ||
| if not isinstance(feed_order, dict): | ||
| raise TypeError( | ||
| "The 'feed_order' should be either None, list or dict.") | ||
| if not sorted(feed_order.values()) == range(len(feed_order)): | ||
| raise ValueError( | ||
| "The values of 'feed_order' should be a permutation of [0, len(feed_order))" | ||
| ) | ||
| sorted_pair_list = sorted(feed_order.items(), key=lambda item: item[1]) | ||
| feed_var_list = [ | ||
| program.global_block().var(pair[0]) for pair in sorted_pair_list | ||
| ] | ||
| return feed_var_list | ||
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.
Why put all code into a separate function?
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.
Maybe in the future, we need to support
_test_by_parralle_executor. Here is a reserved interface for switching between multiple executors.