-
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
trainer.test() #10453
Changes from all commits
a2167a6
5ff4446
bbb7c92
3d441f8
3a42aa4
6aec3a7
13dcb21
e81baf2
21be154
7068b9d
9e71cd1
69f489e
18a69cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,11 +75,15 @@ 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") | ||
|
|
||
| # The fisrt element of program_func_outs is loss. | ||
| loss = self.test_outputs[0] | ||
| optimize_ops, params_grads = optimizer.minimize(loss) | ||
|
|
||
| self.place = Trainer._check_and_get_place(place) | ||
|
|
@@ -168,8 +172,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) | ||
|
Member
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. Why put all code into a separate function?
Collaborator
Author
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. Maybe in the future, we need to support |
||
|
|
||
| def save_params(self, param_path): | ||
| # reference: save_persistables in io.py | ||
|
|
@@ -225,26 +238,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 = len(fetch_list) * [0] | ||
| count = 0 | ||
| for data in reader(): | ||
| outs = exe.run(program=self.test_program, | ||
| feed=feeder.feed(data), | ||
| fetch_list=fetch_list) | ||
| accumulated = [x[0] + x[1][0] for x in zip(accumulated, outs)] | ||
| count += 1 | ||
|
|
||
| return [x / count for x in accumulated] | ||
|
|
||
|
|
||
| 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 | ||
Uh oh!
There was an error while loading. Please reload this page.
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 think we can also specify the feed order in the
trainer.train(....., feed_order=['firstw', 'secondw', 'thirdw', 'forthw', 'next_word'])