Skip to content

Commit 69e0af5

Browse files
committed
do this to new_api example
1 parent dbc6102 commit 69e0af5

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

python/paddle/fluid/tests/book/high-level-api/label_semantic_roles/test_label_semantic_roles_newapi.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,24 +202,35 @@ def infer(use_cuda, inference_program, save_path):
202202
inferencer = fluid.Inferencer(
203203
inference_program, param_path=save_path, place=place)
204204

205-
def create_random_lodtensor(lod, place, low, high):
206-
data = np.random.random_integers(low, high,
207-
[lod[-1], 1]).astype("int64")
208-
res = fluid.LoDTensor()
209-
res.set(data, place)
210-
res.set_lod([lod])
211-
return res
212-
213-
# Create an input example
214-
lod = [0, 4, 10]
215-
word = create_random_lodtensor(lod, place, low=0, high=WORD_DICT_LEN - 1)
216-
pred = create_random_lodtensor(lod, place, low=0, high=PRED_DICT_LEN - 1)
217-
ctx_n2 = create_random_lodtensor(lod, place, low=0, high=WORD_DICT_LEN - 1)
218-
ctx_n1 = create_random_lodtensor(lod, place, low=0, high=WORD_DICT_LEN - 1)
219-
ctx_0 = create_random_lodtensor(lod, place, low=0, high=WORD_DICT_LEN - 1)
220-
ctx_p1 = create_random_lodtensor(lod, place, low=0, high=WORD_DICT_LEN - 1)
221-
ctx_p2 = create_random_lodtensor(lod, place, low=0, high=WORD_DICT_LEN - 1)
222-
mark = create_random_lodtensor(lod, place, low=0, high=MARK_DICT_LEN - 1)
205+
# Setup inputs by creating LoDTensors to represent sequences of words.
206+
# Here each word is the basic element of these LoDTensors and the shape of
207+
# each word (base_shape) should be [1] since it is simply an index to
208+
# look up for the corresponding word vector.
209+
# Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]],
210+
# which has only one lod level. Then the created LoDTensors will have only
211+
# one higher level structure (sequence of words, or sentence) than the basic
212+
# element (word). Hence the LoDTensor will hold data for three sentences of
213+
# length 3, 4 and 2, respectively.
214+
# Note that lod info should be a list of lists.
215+
lod = [[3, 4, 2]]
216+
base_shape = [1]
217+
# The range of random integers is [low, high]
218+
word = fluid.create_random_lodtensor(
219+
lod, base_shape, place, low=0, high=WORD_DICT_LEN - 1)
220+
pred = fluid.create_random_lodtensor(
221+
lod, base_shape, place, low=0, high=PRED_DICT_LEN - 1)
222+
ctx_n2 = fluid.create_random_lodtensor(
223+
lod, base_shape, place, low=0, high=WORD_DICT_LEN - 1)
224+
ctx_n1 = fluid.create_random_lodtensor(
225+
lod, base_shape, place, low=0, high=WORD_DICT_LEN - 1)
226+
ctx_0 = fluid.create_random_lodtensor(
227+
lod, base_shape, place, low=0, high=WORD_DICT_LEN - 1)
228+
ctx_p1 = fluid.create_random_lodtensor(
229+
lod, base_shape, place, low=0, high=WORD_DICT_LEN - 1)
230+
ctx_p2 = fluid.create_random_lodtensor(
231+
lod, base_shape, place, low=0, high=WORD_DICT_LEN - 1)
232+
mark = fluid.create_random_lodtensor(
233+
lod, base_shape, place, low=0, high=MARK_DICT_LEN - 1)
223234

224235
results = inferencer.infer(
225236
{

python/paddle/fluid/tests/book/test_label_semantic_roles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,10 @@ def infer(use_cuda, save_dirname=None):
257257
# one higher level structure (sequence of words, or sentence) than the basic
258258
# element (word). Hence the LoDTensor will hold data for three sentences of
259259
# length 3, 4 and 2, respectively.
260+
# Note that lod info should be a list of lists.
260261
lod = [[3, 4, 2]]
261262
base_shape = [1]
263+
# The range of random integers is [low, high]
262264
word = fluid.create_random_lodtensor(
263265
lod, base_shape, place, low=0, high=word_dict_len - 1)
264266
pred = fluid.create_random_lodtensor(

0 commit comments

Comments
 (0)