Skip to content

Commit 7747e01

Browse files
committed
Polish LoDTensor API
1 parent ab21092 commit 7747e01

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

python/paddle/fluid/lod_tensor.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,41 @@
1919

2020

2121
def create_lod_tensor(data, lod, place):
22-
"""Create a lod tensor from a numpy array, a list, or an existing lod tensor.
22+
"""
23+
Create a lod tensor from a numpy array, a list, or an existing lod tensor.
2324
2425
Create a lod tensor by doing the following:
26+
2527
1. Check that the length-based input lod is valid.
28+
2629
2. Convert the length-based lod to a offset-based LoD.
27-
3. Copy the data from a numpy array, a list or a existing lod tensor to
30+
31+
3. Copy the data from a numpy array, a list or a existing lod tensor to
2832
CPU or GPU device (based on input place).
33+
2934
4. Set the level of detail (LoD) using the offset-based LoD.
3035
31-
Use example:
32-
Suppose we want LoDTensor to hold data for sequences of word, where each word is
33-
represented by an integer. If we want to create a LoDTensor to represent two
34-
sentences, one of 2 words, and one of 3 words.
36+
Examples:
3537
36-
Then 'data' can be a numpy array of integers with shape (5, 1).
37-
'lod' will be [[2, 3]], indicating the length(# of words) in each sentence.
38-
This length-based input lod [[2, 3]] will be converted to offset-based lod [[0, 2, 5]]
39-
inside the function call.
38+
Suppose we want LoDTensor to hold data for sequences of word, where each
39+
word is represented by an integer. If we want to create a LoDTensor to
40+
represent two sentences, one of 2 words, and one of 3 words.
4041
41-
Please refer to
42-
github.com/PaddlePaddle/Paddle/blob/develop/doc/fluid/design/concepts/lod_tensor.md
43-
for more details regarding LoD.
42+
Then :code:`data` can be a numpy array of integers with shape (5, 1).
43+
:code:`lod` will be [[2, 3]], indicating the length(# of words) in each
44+
sentence. This length-based input lod [[2, 3]] will be converted to
45+
offset-based lod [[0, 2, 5]] inside the function call.
46+
47+
Please reference :ref:`api_guide_low_level_lod_tensor` for more details
48+
regarding LoD.
4449
4550
Args:
46-
data: a numpy array or a LoDTensor or a list holding the data to be copied.
47-
lod: a list of lists indicating the length-based LoD info specified by the user.
48-
place: CPU or GPU place indicating where the data in the new LoDTensor will be stored.
51+
data(numpy.ndarray|list|LoDTensor): a numpy array or a LoDTensor or a
52+
list holding the data to be copied.
53+
lod(list): a list of lists indicating the length-based LoD info
54+
specified by the user.
55+
place(Place): CPU or GPU place indicating where the data in the new
56+
LoDTensor will be stored.
4957
5058
Returns:
5159
A fluid LoDTensor object with tensor data and lod info.
@@ -77,31 +85,38 @@ def create_lod_tensor(data, lod, place):
7785

7886

7987
def create_random_int_lodtensor(lod, base_shape, place, low, high):
80-
"""Create a LoDTensor containing random integers.
88+
"""
89+
Create a LoDTensor containing random integers.
8190
82-
This function is frequently used in the book examples. So we revised it based on
83-
the new create_lod_tensor API and put it here in the lod_tensor module to simplify
84-
the code.
91+
This function is frequently used in the book examples. So we revised it
92+
based on the new create_lod_tensor API and put it here in the lod_tensor
93+
module to simplify the code.
8594
8695
The function does the following:
87-
1. Calculate the overall shape of the LoDTensor based on the length-based 'lod' input
88-
and the shape of the basic element in 'base_shape'.
96+
97+
1. Calculate the overall shape of the LoDTensor based on the length-based
98+
:code:`lod` input and the shape of the basic element in
99+
:code:`base_shape`.
100+
89101
2. Create a numpy array of this shape.
102+
90103
3. Create the LoDTensor using create_lod_tensor API.
91104
92-
Suppose we want LoDTensor to hold data for sequences of word, where each word is
93-
represented by an integer. If we want to create a LoDTensor to represent two
94-
sentences, one of 2 words, and one of 3 words. Then 'base_shape' is [1], input
95-
length-based 'lod' is [[2, 3]]. Then the overall shape of the LoDTensor would be
96-
[5, 1], holding 5 words for two sentences.
105+
Suppose we want LoDTensor to hold data for sequences of word, where each
106+
word is represented by an integer. If we want to create a LoDTensor to
107+
represent two sentences, one of 2 words, and one of 3 words. Then
108+
'base_shape' is [1], input length-based 'lod' is [[2, 3]]. Then the overall
109+
shape of the LoDTensor would be [5, 1], holding 5 words for two sentences.
97110
98111
Args:
99-
data: a numpy array or a LoDTensor holding the data to be copied.
100-
lod: a list of lists indicating the length-based LoD info specified by the user.
101-
base_shape: the shape of the basic element to be held by the LoDTensor.
102-
place: CPU or GPU place indicating where the data in the new LoDTensor will be stored.
103-
low: the lower bound of the random integers.
104-
high: the upper bound of the random integers.
112+
lod(list): a list of lists indicating the length-based LoD info
113+
specified by the user.
114+
base_shape(list): the shape of the basic element to be held by the
115+
LoDTensor.
116+
place(Place): CPU or GPU place indicating where the data in the new
117+
LoDTensor will be stored.
118+
low(int): the lower bound of the random integers.
119+
high(int): the upper bound of the random integers.
105120
106121
Returns:
107122
A fluid LoDTensor object with tensor data and lod info.

0 commit comments

Comments
 (0)