未验证 提交 14cdcde7 编写于 作者: W wangzhuang01 提交者: GitHub

修改了 示例代码错误 (#36041) (#36089)

上级 ba2a1bb4
...@@ -73,8 +73,10 @@ def data(name, shape, dtype='float32', lod_level=0): ...@@ -73,8 +73,10 @@ def data(name, shape, dtype='float32', lod_level=0):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import numpy as np import numpy as np
paddle.enable_static()
# Creates a variable with fixed size [3, 2, 1] # Creates a variable with fixed size [3, 2, 1]
# User can only feed data of the same shape to x # User can only feed data of the same shape to x
......
...@@ -727,6 +727,7 @@ class InMemoryDataset(DatasetBase): ...@@ -727,6 +727,7 @@ class InMemoryDataset(DatasetBase):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: skiptest
import paddle.fluid as fluid import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"] filelist = ["a.txt", "b.txt"]
...@@ -753,6 +754,7 @@ class InMemoryDataset(DatasetBase): ...@@ -753,6 +754,7 @@ class InMemoryDataset(DatasetBase):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: skiptest
import paddle.fluid as fluid import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"] filelist = ["a.txt", "b.txt"]
...@@ -777,6 +779,7 @@ class InMemoryDataset(DatasetBase): ...@@ -777,6 +779,7 @@ class InMemoryDataset(DatasetBase):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: skiptest
import paddle.fluid as fluid import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"] filelist = ["a.txt", "b.txt"]
...@@ -797,6 +800,7 @@ class InMemoryDataset(DatasetBase): ...@@ -797,6 +800,7 @@ class InMemoryDataset(DatasetBase):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: skiptest
import paddle.fluid as fluid import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"] filelist = ["a.txt", "b.txt"]
...@@ -819,6 +823,7 @@ class InMemoryDataset(DatasetBase): ...@@ -819,6 +823,7 @@ class InMemoryDataset(DatasetBase):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: skiptest
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
...@@ -866,6 +871,7 @@ class InMemoryDataset(DatasetBase): ...@@ -866,6 +871,7 @@ class InMemoryDataset(DatasetBase):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: skiptest
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
...@@ -925,6 +931,7 @@ class InMemoryDataset(DatasetBase): ...@@ -925,6 +931,7 @@ class InMemoryDataset(DatasetBase):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: skiptest
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
...@@ -965,6 +972,7 @@ class InMemoryDataset(DatasetBase): ...@@ -965,6 +972,7 @@ class InMemoryDataset(DatasetBase):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: skiptest
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
......
...@@ -98,7 +98,10 @@ def one_hot(input, depth, allow_out_of_range=False): ...@@ -98,7 +98,10 @@ def one_hot(input, depth, allow_out_of_range=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
# Correspond to the first example above, where label.shape is 4 and one_hot_label.shape is [4, 4]. # Correspond to the first example above, where label.shape is 4 and one_hot_label.shape is [4, 4].
label = fluid.data(name="label", shape=[4], dtype="int64") label = fluid.data(name="label", shape=[4], dtype="int64")
one_hot_label = fluid.one_hot(input=label, depth=4) one_hot_label = fluid.one_hot(input=label, depth=4)
......
...@@ -4521,7 +4521,10 @@ def reduce_mean(input, dim=None, keep_dim=False, name=None): ...@@ -4521,7 +4521,10 @@ def reduce_mean(input, dim=None, keep_dim=False, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
# x is a Tensor variable with following elements: # x is a Tensor variable with following elements:
# [[0.2, 0.3, 0.5, 0.9] # [[0.2, 0.3, 0.5, 0.9]
# [0.1, 0.2, 0.6, 0.7]] # [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): ...@@ -5160,7 +5163,10 @@ def matmul(x, y, transpose_x=False, transpose_y=False, alpha=1.0, name=None):
# x: [M], y: [N] # x: [M], y: [N]
# fluid.layers.matmul(x, y, True, True) # out: [M, N] # fluid.layers.matmul(x, y, True, True) # out: [M, N]
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
x = fluid.layers.data(name='x', shape=[2, 3], dtype='float32') x = fluid.layers.data(name='x', shape=[2, 3], dtype='float32')
y = fluid.layers.data(name='y', shape=[3, 2], dtype='float32') y = fluid.layers.data(name='y', shape=[3, 2], dtype='float32')
out = fluid.layers.matmul(x, y, True, True) out = fluid.layers.matmul(x, y, True, True)
...@@ -5999,7 +6005,10 @@ def one_hot(input, depth, allow_out_of_range=False): ...@@ -5999,7 +6005,10 @@ def one_hot(input, depth, allow_out_of_range=False):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
# Correspond to the first example above, where label.shape is [4, 1] and one_hot_label.shape is [4, 4]. # Correspond to the first example above, where label.shape is [4, 1] and one_hot_label.shape is [4, 4].
label = fluid.data(name="label", shape=[4, 1], dtype="int64") label = fluid.data(name="label", shape=[4, 1], dtype="int64")
one_hot_label = fluid.layers.one_hot(input=label, depth=4) one_hot_label = fluid.layers.one_hot(input=label, depth=4)
...@@ -8363,7 +8372,10 @@ def gather(input, index, overwrite=True): ...@@ -8363,7 +8372,10 @@ def gather(input, index, overwrite=True):
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
x = fluid.data(name='x', shape=[-1, 5], dtype='float32') x = fluid.data(name='x', shape=[-1, 5], dtype='float32')
index = fluid.data(name='index', shape=[-1, 1], dtype='int32') index = fluid.data(name='index', shape=[-1, 1], dtype='int32')
output = fluid.layers.gather(x, index) output = fluid.layers.gather(x, index)
...@@ -8453,7 +8465,10 @@ def gather_nd(input, index, name=None): ...@@ -8453,7 +8465,10 @@ def gather_nd(input, index, name=None):
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
x = fluid.data(name='x', shape=[3, 4, 5], dtype='float32') x = fluid.data(name='x', shape=[3, 4, 5], dtype='float32')
index = fluid.data(name='index', shape=[2, 2], dtype='int32') index = fluid.data(name='index', shape=[2, 2], dtype='int32')
output = fluid.layers.gather_nd(x, index) output = fluid.layers.gather_nd(x, index)
...@@ -8488,6 +8503,7 @@ def scatter(input, index, updates, name=None, overwrite=True): ...@@ -8488,6 +8503,7 @@ def scatter(input, index, updates, name=None, overwrite=True):
Output is obtained by updating the input on selected indices based on updates. Output is obtained by updating the input on selected indices based on updates.
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
#input: #input:
...@@ -8529,8 +8545,10 @@ def scatter(input, index, updates, name=None, overwrite=True): ...@@ -8529,8 +8545,10 @@ def scatter(input, index, updates, name=None, overwrite=True):
.. code-block:: python .. code-block:: python
import paddle
import numpy as np import numpy as np
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
input = fluid.layers.data(name='data', shape=[3, 2], dtype='float32', append_batch_size=False) input = fluid.layers.data(name='data', shape=[3, 2], dtype='float32', append_batch_size=False)
index = fluid.layers.data(name='index', shape=[4], dtype='int64', append_batch_size=False) 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): ...@@ -8871,8 +8889,10 @@ def selu(x, scale=None, alpha=None, name=None):
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import numpy as np import numpy as np
paddle.enable_static()
inputs = fluid.layers.data(name="x", shape=[2, 2], dtype="float32") inputs = fluid.layers.data(name="x", shape=[2, 2], dtype="float32")
output = fluid.layers.selu(inputs) output = fluid.layers.selu(inputs)
...@@ -10480,22 +10500,24 @@ def expand_as(x, target_tensor, name=None): ...@@ -10480,22 +10500,24 @@ def expand_as(x, target_tensor, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import numpy as np import paddle.fluid as fluid
import numpy as np
paddle.enable_static()
data = fluid.layers.data(name="data", shape=[-1,10], dtype='float64') data = fluid.layers.data(name="data", shape=[-1,10], dtype='float64')
target_tensor = fluid.layers.data( target_tensor = fluid.layers.data(
name="target_tensor", shape=[-1,20], dtype='float64') name="target_tensor", shape=[-1,20], dtype='float64')
result = fluid.layers.expand_as(x=data, target_tensor=target_tensor) result = fluid.layers.expand_as(x=data, target_tensor=target_tensor)
use_cuda = False use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
exe.run(fluid.default_startup_program()) exe.run(fluid.default_startup_program())
x = np.random.rand(3,10) x = np.random.rand(3,10)
y = np.random.rand(3,20) y = np.random.rand(3,20)
output= exe.run(feed={"data":x,"target_tensor":y},fetch_list=[result.name]) output= exe.run(feed={"data":x,"target_tensor":y},fetch_list=[result.name])
print(output[0].shape) print(output[0].shape)
#(3,20) #(3,20)
""" """
if in_dygraph_mode(): if in_dygraph_mode():
...@@ -10576,7 +10598,9 @@ def uniform_random_batch_size_like(input, ...@@ -10576,7 +10598,9 @@ def uniform_random_batch_size_like(input,
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
# example 1: # example 1:
input = fluid.data(name="input", shape=[1, 3], dtype='float32') input = fluid.data(name="input", shape=[1, 3], dtype='float32')
...@@ -10649,7 +10673,9 @@ def gaussian_random(shape, ...@@ -10649,7 +10673,9 @@ def gaussian_random(shape,
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
# example 1: # example 1:
# attr shape is a list which doesn't contain Tensor. # attr shape is a list which doesn't contain Tensor.
...@@ -10677,7 +10703,8 @@ def gaussian_random(shape, ...@@ -10677,7 +10703,8 @@ def gaussian_random(shape,
.. code-block:: python .. code-block:: python
# declarative mode # declarative mode
# required: skiptest
import numpy as np import numpy as np
from paddle import fluid from paddle import fluid
...@@ -10816,7 +10843,10 @@ def gaussian_random_batch_size_like(input, ...@@ -10816,7 +10843,10 @@ def gaussian_random_batch_size_like(input,
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
input = fluid.data(name="input", shape=[13, 11], dtype='float32') input = fluid.data(name="input", shape=[13, 11], dtype='float32')
out = fluid.layers.gaussian_random_batch_size_like( out = fluid.layers.gaussian_random_batch_size_like(
...@@ -11422,7 +11452,9 @@ def size(input): ...@@ -11422,7 +11452,9 @@ def size(input):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid.layers as layers import paddle.fluid.layers as layers
paddle.enable_static()
input = layers.data( input = layers.data(
name="input", shape=[3, 100], dtype="float32", append_batch_size=False) name="input", shape=[3, 100], dtype="float32", append_batch_size=False)
...@@ -12525,7 +12557,10 @@ def mean(x, name=None): ...@@ -12525,7 +12557,10 @@ def mean(x, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
input = fluid.layers.data( input = fluid.layers.data(
name='data', shape=[2, 3], dtype='float32') name='data', shape=[2, 3], dtype='float32')
mean = fluid.layers.mean(input) mean = fluid.layers.mean(input)
...@@ -15195,7 +15230,9 @@ def uniform_random(shape, dtype='float32', min=-1.0, max=1.0, seed=0, ...@@ -15195,7 +15230,9 @@ def uniform_random(shape, dtype='float32', min=-1.0, max=1.0, seed=0,
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
paddle.enable_static()
# example 1: # example 1:
# attr shape is a list which doesn't contain Tensor. # attr shape is a list which doesn't contain Tensor.
......
...@@ -2528,18 +2528,21 @@ def lstm(input, ...@@ -2528,18 +2528,21 @@ def lstm(input,
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.layers as layers import paddle.fluid.layers as layers
paddle.enable_static()
emb_dim = 256 emb_dim = 256
vocab_size = 10000 vocab_size = 10000
data = fluid.data(name='x', shape=[None, 100], dtype='int64') data = fluid.data(name='x', shape=[None, 100], dtype='int64')
emb = fluid.embedding(input=data, size=[vocab_size, emb_dim], is_sparse=True) emb = fluid.embedding(input=data, size=[vocab_size, emb_dim], is_sparse=True)
batch_size = 20 batch_size = 100
dropout_prob = 0.2 dropout_prob = 0.2
input_size = 100 input_size = 100
hidden_size = 150 hidden_size = 150
num_layers = 1 num_layers = 1
max_len = 12
init_h = layers.fill_constant( [num_layers, batch_size, hidden_size], 'float32', 0.0 ) init_h = layers.fill_constant( [num_layers, batch_size, hidden_size], 'float32', 0.0 )
init_c = layers.fill_constant( [num_layers, batch_size, hidden_size], 'float32', 0.0 ) init_c = layers.fill_constant( [num_layers, batch_size, hidden_size], 'float32', 0.0 )
rnn_out, last_h, last_c = layers.lstm( emb, init_h, init_c, \ rnn_out, last_h, last_c = layers.lstm( emb, init_h, init_c, \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册