-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Feature/tensor array add python binding #4616
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
Superjomn
merged 8 commits into
PaddlePaddle:develop
from
Superjomn:feature/tensor_array_add_python_binding
Oct 7, 2017
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81bf1bc
fix bug
Superjomn 7f9227d
Merge branch 'develop' of github.com:PaddlePaddle/Paddle into feature…
Superjomn a7833c3
add pack todo
Superjomn 2049609
add tensor array python unittest
Superjomn 4c169d6
clean code
Superjomn dacf6f8
Merge branch 'develop' of github.com:PaddlePaddle/Paddle into feature…
Superjomn bb5ed51
enable cuda LoD
Superjomn 28f9d10
change to const
Superjomn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| if(WITH_PYTHON) | ||
| cc_library(paddle_pybind SHARED | ||
| SRCS pybind.cc exception.cc protobuf.cc | ||
| DEPS pybind python backward proto_desc | ||
| DEPS pybind python backward proto_desc tensor_array | ||
| ${GLOB_OP_LIB}) | ||
| endif(WITH_PYTHON) |
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 |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import logging | ||
| import paddle.v2.framework.core as core | ||
| import unittest | ||
| import numpy as np | ||
|
|
||
|
|
||
| class TestTensorArray(unittest.TestCase): | ||
| def setUp(self): | ||
| self.ta = core.TensorArray() | ||
|
|
||
| self.batch_size = 10 | ||
| self.dim = 2 | ||
|
|
||
| # create a LoDTensor | ||
| self.scope = core.Scope() | ||
| var = self.scope.new_var("test_tensor") | ||
| self.place = core.CPUPlace() | ||
| tensor = var.get_tensor() | ||
| tensor.set_dims([self.batch_size, self.dim]) | ||
| tensor.alloc_float(self.place) | ||
| tensor_array = np.array(tensor) | ||
| tensor_array[0, 0] = 0 | ||
| tensor_array[1, 0] = 1 | ||
| tensor_array[2, 0] = 2 | ||
| tensor_array[3, 0] = 3 | ||
| tensor_array[4, 0] = 4 | ||
| tensor_array[5, 0] = 5 | ||
| tensor_array[6, 0] = 6 | ||
| tensor_array[7, 0] = 7 | ||
| tensor_array[8, 0] = 8 | ||
| tensor_array[9, 0] = 9 | ||
|
|
||
| lod_py = [[0, 2, 5, 10]] | ||
| lod_tensor = core.LoDTensor(lod_py) | ||
| lod_tensor.set(tensor_array, self.place) | ||
|
|
||
| self.py_seq_meta = [[5, 10, 2], [2, 5, 1], [0, 2, 0]] | ||
|
|
||
| self.tensor = lod_tensor | ||
|
|
||
| def test_unstack(self): | ||
| self.ta.unstack(self.tensor) | ||
| self.assertEqual(self.tensor.get_dims()[0], self.ta.size()) | ||
|
|
||
| def test_read(self): | ||
| self.ta.unstack(self.tensor) | ||
| for i in range(self.batch_size): | ||
| tensor = self.ta.read(i) | ||
|
|
||
| def test_write(self): | ||
| self.ta.unstack(self.tensor) | ||
|
|
||
| # create a tensor with shape of [1, self.dim] | ||
| var = self.scope.new_var("hell") | ||
| tensor = var.get_tensor() | ||
| tensor.set_dims([1, self.dim]) | ||
| tensor.alloc_float(self.place) | ||
| tensor_array = np.array(tensor) | ||
| for i in range(self.dim): | ||
| tensor_array[0, i] = i | ||
| tensor.set(tensor_array, self.place) | ||
|
|
||
| self.ta.write(2, tensor) | ||
|
|
||
| ta_tensor = self.ta.read(2) | ||
| ta_tensor_array = np.array(ta_tensor) | ||
| self.assertEqual(ta_tensor.get_dims(), [1, self.dim]) | ||
| self.assertTrue((tensor_array == ta_tensor_array).all()) | ||
|
|
||
| def test_write_shared(self): | ||
| self.ta.unstack(self.tensor) | ||
|
|
||
| # create a tensor with shape of [1, self.dim] | ||
| var = self.scope.new_var("hell") | ||
| tensor = var.get_tensor() | ||
| tensor.set_dims([1, self.dim]) | ||
| tensor.alloc_float(self.place) | ||
| tensor_array = np.array(tensor) | ||
| for i in range(self.dim): | ||
| tensor_array[0, i] = i | ||
| tensor.set(tensor_array, self.place) | ||
|
|
||
| self.ta.write_shared(2, tensor) | ||
|
|
||
| ta_tensor = self.ta.read(2) | ||
| ta_tensor_array = np.array(ta_tensor) | ||
| self.assertEqual(ta_tensor.get_dims(), [1, self.dim]) | ||
| self.assertTrue((tensor_array == ta_tensor_array).all()) | ||
|
|
||
| def test_unpack(self): | ||
| meta = self.ta.unpack(self.tensor, 0, True) | ||
| self.assertEqual(self.ta.size(), 5) | ||
| self.assertEqual(meta, self.py_seq_meta) | ||
|
|
||
| def test_pack(self): | ||
| meta = self.ta.unpack(self.tensor, 0, True) | ||
| print "meta", meta | ||
| tensor = self.ta.pack(0, meta, self.tensor.lod()) | ||
| print np.array(self.tensor) | ||
| print np.array(tensor) | ||
| self.assertTrue((np.array(self.tensor) == np.array(tensor)).all()) | ||
| self.assertTrue(tensor.lod(), self.tensor.lod()) | ||
|
|
||
|
|
||
| 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.
const?