-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add Inference example and unit test for rnn_encoder_decoder #8176
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
Changes from 6 commits
dc8390d
c3d27b1
6f0e630
dc68e7c
d5686f5
450c39a
ea89bf3
01c2697
64800cf
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 |
|---|---|---|
|
|
@@ -145,7 +145,7 @@ def seq_to_seq_net(): | |
| cost = fluid.layers.cross_entropy(input=prediction, label=label) | ||
| avg_cost = fluid.layers.mean(x=cost) | ||
|
|
||
| return avg_cost | ||
| return avg_cost, prediction | ||
|
|
||
|
|
||
| def to_lodtensor(data, place): | ||
|
|
@@ -163,8 +163,8 @@ def to_lodtensor(data, place): | |
| return res | ||
|
|
||
|
|
||
| def main(): | ||
| avg_cost = seq_to_seq_net() | ||
| def train(save_dirname=None): | ||
| [avg_cost, prediction] = seq_to_seq_net() | ||
|
|
||
| optimizer = fluid.optimizer.Adagrad(learning_rate=1e-4) | ||
| optimizer.minimize(avg_cost) | ||
|
|
@@ -196,9 +196,51 @@ def main(): | |
| print('pass_id=' + str(pass_id) + ' batch=' + str(batch_id) + | ||
| " avg_cost=" + str(avg_cost_val)) | ||
| if batch_id > 3: | ||
| if save_dirname is not None: | ||
| fluid.io.save_inference_model( | ||
| save_dirname, ['source_sequence', | ||
| 'target_sequence'], [prediction], exe) | ||
|
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. I think we need to rethink about the decoding for inference. In this example, we take the
Contributor
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. I agree. We can refine the inference example in future PR. |
||
| return | ||
| exit(0) | ||
| batch_id += 1 | ||
|
|
||
|
|
||
| def infer(save_dirname=None): | ||
| if save_dirname is None: | ||
| return | ||
|
|
||
| place = fluid.CPUPlace() | ||
| exe = fluid.Executor(place) | ||
|
|
||
| # Use fluid.io.load_inference_model to obtain the inference program desc, | ||
| # the feed_target_names (the names of variables that will be feeded | ||
| # data using feed operators), and the fetch_targets (variables that | ||
| # we want to obtain data from using fetch operators). | ||
| [inference_program, feed_target_names, | ||
| fetch_targets] = fluid.io.load_inference_model(save_dirname, exe) | ||
|
|
||
| data = [[0, 1, 0, 1], [0, 1, 1, 0, 0, 1]] | ||
| word_data = to_lodtensor(data, place) | ||
| trg_word = to_lodtensor(data, place) | ||
|
|
||
| # Construct feed as a dictionary of {feed_target_name: feed_target_data} | ||
| # and results will contain a list of data corresponding to fetch_targets. | ||
| assert feed_target_names[0] == 'source_sequence' | ||
| assert feed_target_names[1] == 'target_sequence' | ||
| results = exe.run(inference_program, | ||
| feed={ | ||
| feed_target_names[0]: word_data, | ||
| feed_target_names[1]: trg_word, | ||
| }, | ||
| fetch_list=fetch_targets, | ||
| return_numpy=False) | ||
| print(results[0].lod()) | ||
| np_data = np.array(results[0]) | ||
| print("Inference shape: ", np_data.shape) | ||
| print("Inference results: ", np_data) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| save_dirname = "rnn_encoder_decoder.inference.model" | ||
| train(save_dirname) | ||
| infer(save_dirname) | ||
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.
minor: "hasn't be" -> "hasn't been"