-
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
Enhance look_up_table op #8932
Merged
chengduoZH
merged 6 commits into
PaddlePaddle:develop
from
chengduoZH:feature/add_concat_rows
Mar 14, 2018
Merged
Enhance look_up_table op #8932
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1509ce6
enhancement look_up_table
chengduoZH f1c3ecb
add concat rows
chengduoZH b9397b2
remove concat_rows
chengduoZH ff09b21
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chengduoZH 92e2207
refine doc
chengduoZH a43eee4
follow comments
chengduoZH 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
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 |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
import unittest | ||
import numpy as np | ||
from op_test import OpTest | ||
import paddle.fluid.core as core | ||
from paddle.fluid.op import Operator | ||
|
||
|
||
class TestLookupTableOp(OpTest): | ||
|
@@ -47,5 +49,53 @@ def test_check_grad(self): | |
pass | ||
|
||
|
||
class TestLookupTableIdsIsSelectedRows(OpTest): | ||
def check_with_place(self, place): | ||
scope = core.Scope() | ||
|
||
# create and initialize Grad Variable | ||
height = 10 | ||
rows = [0, 4, 4, 7] | ||
row_numel = 12 | ||
|
||
ids_selected_rows = scope.var('Ids').get_selected_rows() | ||
ids_selected_rows.set_height(height) | ||
ids_selected_rows.set_rows(rows) | ||
np_array = np.ones((len(rows), row_numel)).astype("float32") | ||
ids_tensor = ids_selected_rows.get_tensor() | ||
ids_tensor.set(np_array, place) | ||
|
||
# create and initialize W Variable | ||
W = scope.var('W').get_tensor() | ||
W_array = np.full((height, row_numel), 1.0).astype("float32") | ||
for i in range(height): | ||
W_array[i] *= i | ||
W.set(W_array, place) | ||
|
||
Out = scope.var('Out').get_selected_rows() | ||
Out_array = np.full((len(rows), row_numel), -1.0).astype("float32") | ||
Out.set_height(height) | ||
Out.set_rows(rows) | ||
Out_tensor = Out.get_tensor() | ||
Out_tensor.set(Out_array, place) | ||
|
||
# create and run concat_rows_op operator | ||
concat_rows_op = Operator("lookup_table", W='W', Ids='Ids', Out='Out') | ||
concat_rows_op.run(scope, place) | ||
|
||
# get and compare result | ||
result_array = np.array(Out_tensor) | ||
|
||
for idx, row in enumerate(rows): | ||
assert (row == result_array[idx]).all() | ||
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. please add a comments about this usage of numpy.array 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. Done |
||
|
||
def test_concat_rows(self): | ||
places = [core.CPUPlace()] | ||
if core.is_compiled_with_cuda(): | ||
places.append(core.CUDAPlace(0)) | ||
for place in places: | ||
self.check_with_place(place) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
remote these lines
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.
Done