-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add create LoDTensor from list option and simplify recommender book example #10946
Merged
kexinzhao
merged 2 commits into
PaddlePaddle:develop
from
kexinzhao:recommender_system_lod_fix
May 25, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,43 +197,30 @@ def event_handler(event): | |
num_epochs=1, | ||
event_handler=event_handler, | ||
reader=train_reader, | ||
feed_order=[ | ||
'user_id', 'gender_id', 'age_id', 'job_id', 'movie_id', | ||
'category_id', 'movie_title', 'score' | ||
]) | ||
feed_order=feed_order) | ||
|
||
|
||
def infer(use_cuda, inference_program, save_path): | ||
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() | ||
inferencer = fluid.Inferencer( | ||
inference_program, param_path=save_path, place=place) | ||
|
||
def create_lod_tensor(data, lod=None): | ||
tensor = fluid.LoDTensor() | ||
if lod is None: | ||
# Tensor, the shape is [batch_size, 1] | ||
index = 0 | ||
lod_0 = [index] | ||
for l in range(len(data)): | ||
index += 1 | ||
lod_0.append(index) | ||
lod = [lod_0] | ||
tensor.set_lod(lod) | ||
|
||
flattened_data = np.concatenate(data, axis=0).astype("int64") | ||
flattened_data = flattened_data.reshape([len(flattened_data), 1]) | ||
tensor.set(flattened_data, place) | ||
return tensor | ||
|
||
# Generate a random input for inference | ||
user_id = create_lod_tensor([[1]]) | ||
gender_id = create_lod_tensor([[1]]) | ||
age_id = create_lod_tensor([[0]]) | ||
job_id = create_lod_tensor([[10]]) | ||
movie_id = create_lod_tensor([[783]]) | ||
category_id = create_lod_tensor([[10], [8], [9]], [[0, 3]]) | ||
movie_title = create_lod_tensor([[1069], [4140], [2923], [710], [988]], | ||
[[0, 5]]) | ||
# Use the first data from paddle.dataset.movielens.test() as input. | ||
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. Since we are touching this inference, I raised an issue here, but it is not related to this PR 🤣 |
||
# Use create_lod_tensor(data, lod, place) API to generate LoD Tensor, | ||
# where `data` is a list of sequences of index numbers, `lod` is | ||
# the level of detail (lod) info associated with `data`. | ||
# For example, data = [[10, 2, 3], [2, 3]] means that it contains | ||
# two sequences of indexes, of length 3 and 2, respectively. | ||
# Correspondingly, lod = [[3, 2]] contains one level of detail info, | ||
# indicating that `data` consists of two sequences of length 3 and 2. | ||
user_id = fluid.create_lod_tensor([[1]], [[1]], place) | ||
gender_id = fluid.create_lod_tensor([[1]], [[1]], place) | ||
age_id = fluid.create_lod_tensor([[0]], [[1]], place) | ||
job_id = fluid.create_lod_tensor([[10]], [[1]], place) | ||
movie_id = fluid.create_lod_tensor([[783]], [[1]], place) | ||
category_id = fluid.create_lod_tensor([[10, 8, 9]], [[3]], place) | ||
movie_title = fluid.create_lod_tensor([[1069, 4140, 2923, 710, 988]], [[5]], | ||
place) | ||
|
||
results = inferencer.infer( | ||
{ | ||
|
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
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.
deals