|
19 | 19 |
|
20 | 20 |
|
21 | 21 | 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. |
23 | 24 |
|
24 | 25 | Create a lod tensor by doing the following:
|
| 26 | +
|
25 | 27 | 1. Check that the length-based input lod is valid.
|
| 28 | +
|
26 | 29 | 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 |
28 | 32 | CPU or GPU device (based on input place).
|
| 33 | +
|
29 | 34 | 4. Set the level of detail (LoD) using the offset-based LoD.
|
30 | 35 |
|
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: |
35 | 37 |
|
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. |
40 | 41 |
|
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. |
44 | 49 |
|
45 | 50 | 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. |
49 | 57 |
|
50 | 58 | Returns:
|
51 | 59 | A fluid LoDTensor object with tensor data and lod info.
|
@@ -77,31 +85,38 @@ def create_lod_tensor(data, lod, place):
|
77 | 85 |
|
78 | 86 |
|
79 | 87 | 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. |
81 | 90 |
|
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. |
85 | 94 |
|
86 | 95 | 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 | +
|
89 | 101 | 2. Create a numpy array of this shape.
|
| 102 | +
|
90 | 103 | 3. Create the LoDTensor using create_lod_tensor API.
|
91 | 104 |
|
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. |
97 | 110 |
|
98 | 111 | 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. |
105 | 120 |
|
106 | 121 | Returns:
|
107 | 122 | A fluid LoDTensor object with tensor data and lod info.
|
|
0 commit comments