Skip to content

Commit 14cdcde

Browse files
修改了 示例代码错误 (#36041) (#36089)
1 parent ba2a1bb commit 14cdcde

File tree

5 files changed

+70
-17
lines changed

5 files changed

+70
-17
lines changed

python/paddle/fluid/data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ def data(name, shape, dtype='float32', lod_level=0):
7373
Examples:
7474
.. code-block:: python
7575
76+
import paddle
7677
import paddle.fluid as fluid
7778
import numpy as np
79+
paddle.enable_static()
7880
7981
# Creates a variable with fixed size [3, 2, 1]
8082
# User can only feed data of the same shape to x

python/paddle/fluid/dataset.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ def load_into_memory(self, is_shuffle=False):
727727
Examples:
728728
.. code-block:: python
729729
730+
# required: skiptest
730731
import paddle.fluid as fluid
731732
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
732733
filelist = ["a.txt", "b.txt"]
@@ -753,6 +754,7 @@ def preload_into_memory(self, thread_num=None):
753754
Examples:
754755
.. code-block:: python
755756
757+
# required: skiptest
756758
import paddle.fluid as fluid
757759
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
758760
filelist = ["a.txt", "b.txt"]
@@ -777,6 +779,7 @@ def wait_preload_done(self):
777779
Examples:
778780
.. code-block:: python
779781
782+
# required: skiptest
780783
import paddle.fluid as fluid
781784
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
782785
filelist = ["a.txt", "b.txt"]
@@ -797,6 +800,7 @@ def local_shuffle(self):
797800
Examples:
798801
.. code-block:: python
799802
803+
# required: skiptest
800804
import paddle.fluid as fluid
801805
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
802806
filelist = ["a.txt", "b.txt"]
@@ -819,6 +823,7 @@ def global_shuffle(self, fleet=None, thread_num=12):
819823
Examples:
820824
.. code-block:: python
821825
826+
# required: skiptest
822827
import paddle.fluid as fluid
823828
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
824829
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
@@ -866,6 +871,7 @@ def release_memory(self):
866871
Examples:
867872
.. code-block:: python
868873
874+
# required: skiptest
869875
import paddle.fluid as fluid
870876
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
871877
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
@@ -925,6 +931,7 @@ def get_memory_data_size(self, fleet=None):
925931
Examples:
926932
.. code-block:: python
927933
934+
# required: skiptest
928935
import paddle.fluid as fluid
929936
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
930937
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
@@ -965,6 +972,7 @@ def get_shuffle_data_size(self, fleet=None):
965972
Examples:
966973
.. code-block:: python
967974
975+
# required: skiptest
968976
import paddle.fluid as fluid
969977
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
970978
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")

python/paddle/fluid/input.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ def one_hot(input, depth, allow_out_of_range=False):
9898
Examples:
9999
.. code-block:: python
100100
101+
import paddle
101102
import paddle.fluid as fluid
103+
paddle.enable_static()
104+
102105
# Correspond to the first example above, where label.shape is 4 and one_hot_label.shape is [4, 4].
103106
label = fluid.data(name="label", shape=[4], dtype="int64")
104107
one_hot_label = fluid.one_hot(input=label, depth=4)

python/paddle/fluid/layers/nn.py

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4521,7 +4521,10 @@ def reduce_mean(input, dim=None, keep_dim=False, name=None):
45214521
Examples:
45224522
.. code-block:: python
45234523

4524+
import paddle
45244525
import paddle.fluid as fluid
4526+
paddle.enable_static()
4527+
45254528
# x is a Tensor variable with following elements:
45264529
# [[0.2, 0.3, 0.5, 0.9]
45274530
# [0.1, 0.2, 0.6, 0.7]]
@@ -5160,7 +5163,10 @@ def matmul(x, y, transpose_x=False, transpose_y=False, alpha=1.0, name=None):
51605163
# x: [M], y: [N]
51615164
# fluid.layers.matmul(x, y, True, True) # out: [M, N]
51625165

5166+
import paddle
51635167
import paddle.fluid as fluid
5168+
paddle.enable_static()
5169+
51645170
x = fluid.layers.data(name='x', shape=[2, 3], dtype='float32')
51655171
y = fluid.layers.data(name='y', shape=[3, 2], dtype='float32')
51665172
out = fluid.layers.matmul(x, y, True, True)
@@ -5999,7 +6005,10 @@ def one_hot(input, depth, allow_out_of_range=False):
59996005
Examples:
60006006
.. code-block:: python
60016007

6008+
import paddle
60026009
import paddle.fluid as fluid
6010+
paddle.enable_static()
6011+
60036012
# Correspond to the first example above, where label.shape is [4, 1] and one_hot_label.shape is [4, 4].
60046013
label = fluid.data(name="label", shape=[4, 1], dtype="int64")
60056014
one_hot_label = fluid.layers.one_hot(input=label, depth=4)
@@ -8363,7 +8372,10 @@ def gather(input, index, overwrite=True):
83638372

83648373
.. code-block:: python
83658374

8375+
import paddle
83668376
import paddle.fluid as fluid
8377+
paddle.enable_static()
8378+
83678379
x = fluid.data(name='x', shape=[-1, 5], dtype='float32')
83688380
index = fluid.data(name='index', shape=[-1, 1], dtype='int32')
83698381
output = fluid.layers.gather(x, index)
@@ -8453,7 +8465,10 @@ def gather_nd(input, index, name=None):
84538465

84548466
.. code-block:: python
84558467

8468+
import paddle
84568469
import paddle.fluid as fluid
8470+
paddle.enable_static()
8471+
84578472
x = fluid.data(name='x', shape=[3, 4, 5], dtype='float32')
84588473
index = fluid.data(name='index', shape=[2, 2], dtype='int32')
84598474
output = fluid.layers.gather_nd(x, index)
@@ -8488,6 +8503,7 @@ def scatter(input, index, updates, name=None, overwrite=True):
84888503
Output is obtained by updating the input on selected indices based on updates.
84898504

84908505
.. code-block:: python
8506+
84918507
import numpy as np
84928508

84938509
#input:
@@ -8529,8 +8545,10 @@ def scatter(input, index, updates, name=None, overwrite=True):
85298545

85308546
.. code-block:: python
85318547

8548+
import paddle
85328549
import numpy as np
85338550
import paddle.fluid as fluid
8551+
paddle.enable_static()
85348552

85358553
input = fluid.layers.data(name='data', shape=[3, 2], dtype='float32', append_batch_size=False)
85368554
index = fluid.layers.data(name='index', shape=[4], dtype='int64', append_batch_size=False)
@@ -8871,8 +8889,10 @@ def selu(x, scale=None, alpha=None, name=None):
88718889

88728890
.. code-block:: python
88738891

8892+
import paddle
88748893
import paddle.fluid as fluid
88758894
import numpy as np
8895+
paddle.enable_static()
88768896

88778897
inputs = fluid.layers.data(name="x", shape=[2, 2], dtype="float32")
88788898
output = fluid.layers.selu(inputs)
@@ -10480,22 +10500,24 @@ def expand_as(x, target_tensor, name=None):
1048010500
Examples:
1048110501
.. code-block:: python
1048210502

10483-
import paddle.fluid as fluid
10484-
import numpy as np
10503+
import paddle
10504+
import paddle.fluid as fluid
10505+
import numpy as np
10506+
paddle.enable_static()
1048510507

10486-
data = fluid.layers.data(name="data", shape=[-1,10], dtype='float64')
10487-
target_tensor = fluid.layers.data(
10488-
name="target_tensor", shape=[-1,20], dtype='float64')
10489-
result = fluid.layers.expand_as(x=data, target_tensor=target_tensor)
10490-
use_cuda = False
10491-
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
10492-
exe = fluid.Executor(place)
10493-
exe.run(fluid.default_startup_program())
10494-
x = np.random.rand(3,10)
10495-
y = np.random.rand(3,20)
10496-
output= exe.run(feed={"data":x,"target_tensor":y},fetch_list=[result.name])
10497-
print(output[0].shape)
10498-
#(3,20)
10508+
data = fluid.layers.data(name="data", shape=[-1,10], dtype='float64')
10509+
target_tensor = fluid.layers.data(
10510+
name="target_tensor", shape=[-1,20], dtype='float64')
10511+
result = fluid.layers.expand_as(x=data, target_tensor=target_tensor)
10512+
use_cuda = False
10513+
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
10514+
exe = fluid.Executor(place)
10515+
exe.run(fluid.default_startup_program())
10516+
x = np.random.rand(3,10)
10517+
y = np.random.rand(3,20)
10518+
output= exe.run(feed={"data":x,"target_tensor":y},fetch_list=[result.name])
10519+
print(output[0].shape)
10520+
#(3,20)
1049910521

1050010522
"""
1050110523
if in_dygraph_mode():
@@ -10576,7 +10598,9 @@ def uniform_random_batch_size_like(input,
1057610598
Examples:
1057710599
.. code-block:: python
1057810600

10601+
import paddle
1057910602
import paddle.fluid as fluid
10603+
paddle.enable_static()
1058010604

1058110605
# example 1:
1058210606
input = fluid.data(name="input", shape=[1, 3], dtype='float32')
@@ -10649,7 +10673,9 @@ def gaussian_random(shape,
1064910673
Examples:
1065010674
.. code-block:: python
1065110675

10676+
import paddle
1065210677
import paddle.fluid as fluid
10678+
paddle.enable_static()
1065310679

1065410680
# example 1:
1065510681
# attr shape is a list which doesn't contain Tensor.
@@ -10677,7 +10703,8 @@ def gaussian_random(shape,
1067710703

1067810704
.. code-block:: python
1067910705

10680-
# declarative mode
10706+
# declarative mode
10707+
# required: skiptest
1068110708
import numpy as np
1068210709
from paddle import fluid
1068310710

@@ -10816,7 +10843,10 @@ def gaussian_random_batch_size_like(input,
1081610843
Examples:
1081710844
.. code-block:: python
1081810845

10846+
import paddle
1081910847
import paddle.fluid as fluid
10848+
paddle.enable_static()
10849+
1082010850
input = fluid.data(name="input", shape=[13, 11], dtype='float32')
1082110851

1082210852
out = fluid.layers.gaussian_random_batch_size_like(
@@ -11422,7 +11452,9 @@ def size(input):
1142211452
Examples:
1142311453
.. code-block:: python
1142411454

11455+
import paddle
1142511456
import paddle.fluid.layers as layers
11457+
paddle.enable_static()
1142611458

1142711459
input = layers.data(
1142811460
name="input", shape=[3, 100], dtype="float32", append_batch_size=False)
@@ -12525,7 +12557,10 @@ def mean(x, name=None):
1252512557
Examples:
1252612558
.. code-block:: python
1252712559

12560+
import paddle
1252812561
import paddle.fluid as fluid
12562+
paddle.enable_static()
12563+
1252912564
input = fluid.layers.data(
1253012565
name='data', shape=[2, 3], dtype='float32')
1253112566
mean = fluid.layers.mean(input)
@@ -15195,7 +15230,9 @@ def uniform_random(shape, dtype='float32', min=-1.0, max=1.0, seed=0,
1519515230
Examples:
1519615231
.. code-block:: python
1519715232

15233+
import paddle
1519815234
import paddle.fluid as fluid
15235+
paddle.enable_static()
1519915236

1520015237
# example 1:
1520115238
# attr shape is a list which doesn't contain Tensor.

python/paddle/fluid/layers/rnn.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2528,18 +2528,21 @@ def lstm(input,
25282528
Examples:
25292529
.. code-block:: python
25302530
2531+
import paddle
25312532
import paddle.fluid as fluid
25322533
import paddle.fluid.layers as layers
2534+
paddle.enable_static()
25332535
25342536
emb_dim = 256
25352537
vocab_size = 10000
25362538
data = fluid.data(name='x', shape=[None, 100], dtype='int64')
25372539
emb = fluid.embedding(input=data, size=[vocab_size, emb_dim], is_sparse=True)
2538-
batch_size = 20
2540+
batch_size = 100
25392541
dropout_prob = 0.2
25402542
input_size = 100
25412543
hidden_size = 150
25422544
num_layers = 1
2545+
max_len = 12
25432546
init_h = layers.fill_constant( [num_layers, batch_size, hidden_size], 'float32', 0.0 )
25442547
init_c = layers.fill_constant( [num_layers, batch_size, hidden_size], 'float32', 0.0 )
25452548
rnn_out, last_h, last_c = layers.lstm( emb, init_h, init_c, \

0 commit comments

Comments
 (0)