18
18
__all__ = ['create_lod_tensor' , 'create_random_int_lodtensor' ]
19
19
20
20
21
- def create_lod_tensor (data , lod , place ):
21
+ def create_lod_tensor (data , recursive_seq_lens , place ):
22
22
"""
23
23
Create a lod tensor from a numpy array, a list, or an existing lod tensor.
24
24
25
25
Create a lod tensor by doing the following:
26
26
27
- 1. Check that the length-based input lod is valid.
27
+ 1. Check that the length-based level of detail (LoD) also known as
28
+ recursive_sequence_lengths of the input is valid.
28
29
29
- 2. Convert the length-based lod to a offset-based LoD.
30
+ 2. Convert recursive_sequence_lengths to a offset-based LoD.
30
31
31
32
3. Copy the data from a numpy array, a list or a existing lod tensor to
32
33
CPU or GPU device (based on input place).
@@ -37,45 +38,47 @@ def create_lod_tensor(data, lod, place):
37
38
38
39
Suppose we want LoDTensor to hold data for sequences of word, where each
39
40
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.
41
+ represent two sentences, one of 2 words, and one of 3 words.
41
42
42
43
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.
44
+ :code:`recursive_seq_lens ` will be [[2, 3]], indicating the length(# of words) in each
45
+ sentence. This length-based :code:`recursive_seq_lens` [[2, 3]] will be converted to
46
+ offset-based LoD [[0, 2, 5]] inside the function call.
46
47
47
48
Please reference :ref:`api_guide_low_level_lod_tensor` for more details
48
49
regarding LoD.
49
50
50
51
Args:
51
52
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.
53
+ list holding the data to be copied.
54
+ recursive_seq_lens (list): a list of lists indicating the length-based level of detail
55
+ info specified by the user.
55
56
place(Place): CPU or GPU place indicating where the data in the new
56
57
LoDTensor will be stored.
57
58
58
59
Returns:
59
- A fluid LoDTensor object with tensor data and lod info.
60
+ A fluid LoDTensor object with tensor data and recursive_seq_lens info.
60
61
"""
61
62
if isinstance (data , core .LoDTensor ):
62
- return create_lod_tensor (np .array (data ), lod , place )
63
+ return create_lod_tensor (np .array (data ), recursive_seq_lens , place )
63
64
elif isinstance (data , list ):
64
65
# When input data is a list, it only deal with the case where the base element
65
66
# is an index of shape [1] and dtype int64 (e.g., word id). Hence, the generated
66
67
# LoDTensor will be of shape [n, 1] and dtype int64, where `n` is the total number
67
68
# of words or other indexes in the sequence.
68
- new_lod = []
69
+ new_recursive_seq_lens = []
69
70
for seq in data :
70
- new_lod .append (len (seq ))
71
- assert [new_lod ] == lod , "data and lod do not match"
71
+ new_recursive_seq_lens .append (len (seq ))
72
+ assert [
73
+ new_recursive_seq_lens
74
+ ] == recursive_seq_lens , "data and recursive_seq_lens do not match"
72
75
flattened_data = np .concatenate (data , axis = 0 ).astype ("int64" )
73
76
flattened_data = flattened_data .reshape ([len (flattened_data ), 1 ])
74
- return create_lod_tensor (flattened_data , lod , place )
77
+ return create_lod_tensor (flattened_data , recursive_seq_lens , place )
75
78
elif isinstance (data , np .ndarray ):
76
79
tensor = core .LoDTensor ()
77
80
tensor .set (data , place )
78
- tensor .set_recursive_sequence_lengths (lod )
81
+ tensor .set_recursive_sequence_lengths (recursive_seq_lens )
79
82
assert tensor .has_valid_recursive_sequence_lengths (
80
83
), "the provided lod info is invalid"
81
84
return tensor
@@ -84,7 +87,8 @@ def create_lod_tensor(data, lod, place):
84
87
"data should be either a LoDTensor, a Numpy array or a list" )
85
88
86
89
87
- def create_random_int_lodtensor (lod , base_shape , place , low , high ):
90
+ def create_random_int_lodtensor (recursive_seq_lens , base_shape , place , low ,
91
+ high ):
88
92
"""
89
93
Create a LoDTensor containing random integers.
90
94
@@ -95,7 +99,7 @@ def create_random_int_lodtensor(lod, base_shape, place, low, high):
95
99
The function does the following:
96
100
97
101
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
102
+ :code:`recursive_seq_lens ` input and the shape of the basic element in
99
103
:code:`base_shape`.
100
104
101
105
2. Create a numpy array of this shape.
@@ -105,12 +109,13 @@ def create_random_int_lodtensor(lod, base_shape, place, low, high):
105
109
Suppose we want LoDTensor to hold data for sequences of word, where each
106
110
word is represented by an integer. If we want to create a LoDTensor to
107
111
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.
112
+ 'base_shape' is [1], input length-based 'recursive_seq_lens' is [[2, 3]].
113
+ Then the overall shape of the LoDTensor would be [5, 1], holding 5 words
114
+ for two sentences.
110
115
111
116
Args:
112
- lod (list): a list of lists indicating the length-based LoD info
113
- specified by the user.
117
+ recursive_seq_lens (list): a list of lists indicating the length-based
118
+ level of detail info specified by the user.
114
119
base_shape(list): the shape of the basic element to be held by the
115
120
LoDTensor.
116
121
place(Place): CPU or GPU place indicating where the data in the new
@@ -119,11 +124,11 @@ def create_random_int_lodtensor(lod, base_shape, place, low, high):
119
124
high(int): the upper bound of the random integers.
120
125
121
126
Returns:
122
- A fluid LoDTensor object with tensor data and lod info.
127
+ A fluid LoDTensor object with tensor data and recursive_seq_lens info.
123
128
"""
124
129
assert isinstance (base_shape , list ), "base_shape should be a list"
125
130
# append the total number of basic elements to the front of its shape
126
- overall_shape = [sum (lod [- 1 ])] + base_shape
131
+ overall_shape = [sum (recursive_seq_lens [- 1 ])] + base_shape
127
132
# the range of integer data elements is [low, high]
128
133
data = np .random .random_integers (low , high , overall_shape ).astype ("int64" )
129
- return create_lod_tensor (data , lod , place )
134
+ return create_lod_tensor (data , recursive_seq_lens , place )
0 commit comments