Skip to content

Commit 61b9192

Browse files
authored
Cherrypick add import to examples lack of it (#18440)
* test=develop * test=develop
1 parent 83e799a commit 61b9192

21 files changed

+390
-188
lines changed

paddle/fluid/API.spec

Lines changed: 180 additions & 179 deletions
Large diffs are not rendered by default.

paddle/fluid/pybind/pybind.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ All parameter, weight, gradient are variables in Paddle.
625625
Examples:
626626
.. code-block:: python
627627
628+
import paddle.fluid as fluid
628629
# create tensor from a scope and set value to it.
629630
param = scope.var('Param').get_tensor()
630631
param_array = np.full((height, row_numel), 5.0).astype("float32")
@@ -781,6 +782,7 @@ All parameter, weight, gradient are variables in Paddle.
781782
Examples:
782783
.. code-block:: python
783784
785+
import paddle.fluid as fluid
784786
gpu_place = fluid.CUDAPlace(0)
785787
786788
)DOC")
@@ -839,6 +841,7 @@ All parameter, weight, gradient are variables in Paddle.
839841
Examples:
840842
.. code-block:: python
841843
844+
import paddle.fluid as fluid
842845
cpu_place = fluid.CPUPlace()
843846
844847
)DOC")
@@ -858,6 +861,7 @@ All parameter, weight, gradient are variables in Paddle.
858861
Examples:
859862
.. code-block:: python
860863
864+
import paddle.fluid as fluid
861865
place = fluid.CUDAPinnedPlace()
862866
863867
)DOC")
@@ -1156,6 +1160,7 @@ All parameter, weight, gradient are variables in Paddle.
11561160
Examples:
11571161
.. code-block:: python
11581162
1163+
import paddle.fluid as fluid
11591164
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
11601165
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
11611166
y_predict = fluid.layers.fc(input=x, size=1, act=None)

python/paddle/fluid/average.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class WeightedAverage(object):
4949
Examples:
5050
.. code-block:: python
5151
52+
import paddle.fluid as fluid
5253
avg = fluid.average.WeightedAverage()
5354
avg.add(value=2.0, weight=1)
5455
avg.add(value=4.0, weight=2)

python/paddle/fluid/backward.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ def append_backward(loss, parameter_list=None, no_grad_set=None,
489489
490490
# network configuration code
491491
# loss from ...
492+
import paddle.fluid as fluid
492493
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
493494
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
494495

python/paddle/fluid/data_feed_desc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class DataFeedDesc(object):
3131
3232
.. code-block:: python
3333
34+
import paddle.fluid as fluid
3435
f = open("data.proto", "w")
3536
print >> f, 'name: "MultiSlotDataFeed"'
3637
print >> f, 'batch_size: 2'
@@ -61,6 +62,7 @@ class DataFeedDesc(object):
6162
6263
.. code-block:: python
6364
65+
import paddle.fluid as fluid
6466
data_feed = fluid.DataFeedDesc('data.proto')
6567
data_feed.set_batch_size(128)
6668
data_feed.set_dense_slots('wd') # The slot named 'wd' will be dense
@@ -95,6 +97,7 @@ def set_batch_size(self, batch_size):
9597
Example:
9698
.. code-block:: python
9799
100+
import paddle.fluid as fluid
98101
f = open("data.proto", "w")
99102
print >> f, 'name: "MultiSlotDataFeed"'
100103
print >> f, 'batch_size: 2'
@@ -131,6 +134,7 @@ def set_dense_slots(self, dense_slots_name):
131134
Example:
132135
.. code-block:: python
133136
137+
import paddle.fluid as fluid
134138
f = open("data.proto", "w")
135139
print >> f, 'name: "MultiSlotDataFeed"'
136140
print >> f, 'batch_size: 2'
@@ -175,6 +179,7 @@ def set_use_slots(self, use_slots_name):
175179
Example:
176180
.. code-block:: python
177181
182+
import paddle.fluid as fluid
178183
f = open("data.proto", "w")
179184
print >> f, 'name: "MultiSlotDataFeed"'
180185
print >> f, 'batch_size: 2'
@@ -217,6 +222,7 @@ def desc(self):
217222
Example:
218223
.. code-block:: python
219224
225+
import paddle.fluid as fluid
220226
f = open("data.proto", "w")
221227
print >> f, 'name: "MultiSlotDataFeed"'
222228
print >> f, 'batch_size: 2'

python/paddle/fluid/executor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def scope_guard(scope):
7272
Examples:
7373
.. code-block:: python
7474
75+
import paddle.fluid as fluid
7576
import numpy
7677
7778
new_scope = fluid.Scope()

python/paddle/fluid/framework.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def in_dygraph_mode():
6767
Examples:
6868
.. code-block:: python
6969
70+
import paddle.fluid as fluid
7071
if fluid.in_dygraph_mode():
7172
pass
7273
@@ -143,6 +144,7 @@ def cuda_places(device_ids=None):
143144
Examples:
144145
.. code-block:: python
145146
147+
import paddle.fluid as fluid
146148
cuda_places = fluid.cuda_places()
147149
148150
"""
@@ -173,6 +175,7 @@ def cpu_places(device_count=None):
173175
Examples:
174176
.. code-block:: python
175177
178+
import paddle.fluid as fluid
176179
cpu_places = fluid.cpu_places()
177180
"""
178181

@@ -199,6 +202,7 @@ def cuda_pinned_places(device_count=None):
199202
Examples:
200203
.. code-block:: python
201204
205+
import paddle.fluid as fluid
202206
cuda_pinned_places_cpu_num = fluid.cuda_pinned_places()
203207
# or
204208
cuda_pinned_places = fluid.cuda_pinned_places(1)
@@ -251,6 +255,7 @@ def name_scope(prefix=None):
251255
Examples:
252256
.. code-block:: python
253257
258+
import paddle.fluid as fluid
254259
with fluid.name_scope("s1"):
255260
a = fluid.layers.data(name='data', shape=[1], dtype='int32')
256261
b = a + 1
@@ -412,6 +417,7 @@ class Variable(object):
412417
Examples:
413418
.. code-block:: python
414419
420+
import paddle.fluid as fluid
415421
cur_program = Program()
416422
cur_block = cur_program.current_block()
417423
new_variable = cur_block.create_var(name="X",
@@ -1011,6 +1017,7 @@ class Operator(object):
10111017
Examples:
10121018
.. code-block:: python
10131019
1020+
import paddle.fluid as fluid
10141021
cur_program = Program()
10151022
cur_block = cur_program.current_block()
10161023
# var1 += var2 + var3
@@ -2907,6 +2914,7 @@ def _optimized_guard(self, param_and_grads):
29072914
29082915
Examples:
29092916
2917+
>>> import paddle.fluid as fluid
29102918
>>> p, g = backward(...)
29112919
>>> with program._optimized_guard([p,g]):
29122920
>>> p = p - 0.001 * g
@@ -2940,6 +2948,7 @@ def _lr_schedule_guard(self, is_with_opt=False):
29402948
29412949
Examples:
29422950
2951+
>>> import paddle.fluid as fluid
29432952
>>> p, g = backward(...)
29442953
>>> with program.lr_schedule_guard():
29452954
>>> lr = lr * decay

python/paddle/fluid/initializer.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def force_init_on_cpu():
4242
4343
.. code-block:: python
4444
45-
if fluid.initializer.force_init_on_cpu():
45+
import paddle.fluid as fluid
46+
if fluid.initializer.force_init_on_cpu():
4647
step = fluid.layers.create_global_var(
4748
shape=[2,3], value=1.0, dtype='float32')
4849
@@ -58,7 +59,8 @@ def init_on_cpu():
5859
Examples:
5960
.. code-block:: python
6061
61-
with fluid.initializer.init_on_cpu():
62+
import paddle.fluid as fluid
63+
with fluid.initializer.init_on_cpu():
6264
step = fluid.layers.create_global_var(
6365
shape=[2,3], value=1.0, dtype='float32')
6466
@@ -133,7 +135,8 @@ class ConstantInitializer(Initializer):
133135
Examples:
134136
.. code-block:: python
135137
136-
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
138+
import paddle.fluid as fluid
139+
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
137140
fc = fluid.layers.fc(input=x, size=10,
138141
param_attr=fluid.initializer.Constant(value=2.0))
139142
@@ -292,7 +295,8 @@ class NormalInitializer(Initializer):
292295
Examples:
293296
.. code-block:: python
294297
295-
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
298+
import paddle.fluid as fluid
299+
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
296300
fc = fluid.layers.fc(input=x, size=10,
297301
param_attr=fluid.initializer.Normal(loc=0.0, scale=2.0))
298302
@@ -608,7 +612,8 @@ class MSRAInitializer(Initializer):
608612
Examples:
609613
.. code-block:: python
610614
611-
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
615+
import paddle.fluid as fluid
616+
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
612617
fc = fluid.layers.fc(input=x, size=10,
613618
param_attr=fluid.initializer.MSRA(uniform=False))
614619
@@ -710,7 +715,8 @@ class BilinearInitializer(Initializer):
710715
711716
.. code-block:: python
712717
713-
factor = 2
718+
import paddle.fluid as fluid
719+
factor = 2
714720
C = 2
715721
w_attr = fluid.initializer.ParamAttr(
716722
learning_rate=0.,
@@ -836,6 +842,7 @@ class NumpyArrayInitializer(Initializer):
836842
Examples:
837843
.. code-block:: python
838844
845+
import paddle.fluid as fluid
839846
x = fluid.layers.data(name="x", shape=[5], dtype='float32')
840847
fc = fluid.layers.fc(input=x, size=10,
841848
param_attr=fluid.initializer.NumpyArrayInitializer(numpy.array([1,2])))

python/paddle/fluid/io.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def is_parameter(var):
5454
Examples:
5555
.. code-block:: python
5656
57+
import paddle.fluid as fluid
5758
param = fluid.default_main_program().global_block().var('fc.w')
5859
res = fluid.io.is_parameter(param)
5960
"""
@@ -74,6 +75,7 @@ def is_persistable(var):
7475
Examples:
7576
.. code-block:: python
7677
78+
import paddle.fluid as fluid
7779
param = fluid.default_main_program().global_block().var('fc.b')
7880
res = fluid.io.is_persistable(param)
7981
"""
@@ -301,6 +303,7 @@ def _save_distributed_persistables(executor, dirname, main_program):
301303
Examples:
302304
.. code-block:: python
303305
306+
import paddle.fluid as fluid
304307
exe = fluid.Executor(fluid.CPUPlace())
305308
param_path = "./my_paddle_model"
306309
t = distribute_transpiler.DistributeTranspiler()
@@ -681,6 +684,7 @@ def load_params(executor, dirname, main_program=None, filename=None):
681684
Examples:
682685
.. code-block:: python
683686
687+
import paddle.fluid as fluid
684688
exe = fluid.Executor(fluid.CPUPlace())
685689
param_path = "./my_paddle_model"
686690
prog = fluid.default_main_program()
@@ -723,6 +727,7 @@ def load_persistables(executor, dirname, main_program=None, filename=None):
723727
Examples:
724728
.. code-block:: python
725729
730+
import paddle.fluid as fluid
726731
exe = fluid.Executor(fluid.CPUPlace())
727732
param_path = "./my_paddle_model"
728733
prog = fluid.default_main_program()
@@ -760,6 +765,7 @@ def _load_distributed_persistables(executor, dirname, main_program=None):
760765
Examples:
761766
.. code-block:: python
762767
768+
import paddle.fluid as fluid
763769
exe = fluid.Executor(fluid.CPUPlace())
764770
param_path = "./my_paddle_model"
765771
t = distribute_transpiler.DistributeTranspiler()
@@ -1230,6 +1236,7 @@ def get_parameter_value(para, executor):
12301236
Examples:
12311237
.. code-block:: python
12321238
1239+
import paddle.fluid as fluid
12331240
exe = fluid.Executor(fluid.CPUPlace())
12341241
param = fluid.default_main_program().global_block().var('fc.w')
12351242
p = fluid.io.get_parameter_value(param, exe)
@@ -1267,6 +1274,7 @@ def get_parameter_value_by_name(name, executor, program=None):
12671274
Examples:
12681275
.. code-block:: python
12691276
1277+
import paddle.fluid as fluid
12701278
exe = fluid.Executor(fluid.CPUPlace())
12711279
p = fluid.io.get_parameter_value('fc.w', exe)
12721280
"""

python/paddle/fluid/layers/control_flow.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def split_lod_tensor(input, mask, level=0):
5858
Examples:
5959
.. code-block:: python
6060
61+
import paddle.fluid as fluid
6162
x = fluid.layers.data(name='x', shape=[1])
6263
x.persistable = True
6364
@@ -107,6 +108,7 @@ def merge_lod_tensor(in_true, in_false, x, mask, level=0):
107108
Examples:
108109
.. code-block:: python
109110
111+
import paddle.fluid as fluid
110112
x = layers.data(
111113
name='x', shape=[1], dtype='float32', stop_gradient=False)
112114
y = layers.data(
@@ -756,6 +758,7 @@ def lod_rank_table(x, level=0):
756758
Examples:
757759
.. code-block:: python
758760
761+
import paddle.fluid as fluid
759762
x = fluid.layers.data(name='x', shape=[10],
760763
dtype='float32', lod_level=1)
761764
out = layers.lod_rank_table(x=x, level=0)
@@ -822,6 +825,7 @@ def lod_tensor_to_array(x, table):
822825
Examples:
823826
.. code-block:: python
824827
828+
import paddle.fluid as fluid
825829
x = fluid.layers.data(name='x', shape=[10])
826830
table = fluid.layers.lod_rank_table(x, level=0)
827831
array = fluid.layers.lod_tensor_to_array(x, table)
@@ -855,6 +859,7 @@ def array_to_lod_tensor(x, table):
855859
Examples:
856860
.. code-block:: python
857861
862+
import paddle.fluid as fluid
858863
x = fluid.layers.data(name='x', shape=[10])
859864
table = fluid.layers.lod_rank_table(x, level=0)
860865
array = fluid.layers.lod_tensor_to_array(x, table)
@@ -964,6 +969,7 @@ def create_array(dtype):
964969
Examples:
965970
.. code-block:: python
966971
972+
import paddle.fluid as fluid
967973
data = fluid.layers.create_array(dtype='float32')
968974
969975
"""
@@ -991,6 +997,7 @@ def less_than(x, y, force_cpu=None, cond=None):
991997
Examples:
992998
.. code-block:: python
993999
1000+
import paddle.fluid as fluid
9941001
label = fluid.layers.data(name='y', shape=[1], dtype='int64')
9951002
limit = fluid.layers.fill_constant(shape=[1], dtype='int64', value=5)
9961003
cond = fluid.layers.less_than(x=label, y=limit)
@@ -1339,6 +1346,7 @@ class ConditionalBlock(object):
13391346
Examples:
13401347
.. code-block:: python
13411348
1349+
import paddle.fluid as fluid
13421350
cond = layers.less_than(x=label, y=limit)
13431351
true_image, false_image = layers.split_lod_tensor(
13441352
input=image, mask=cond)

0 commit comments

Comments
 (0)