提交 9a601dd2 编写于 作者: R RaindragonD 提交者: Cheerego

update api_cn (#954)

* update api_cn; fix the error of reverting layers_cn to 1.4; include previous changes

* update layers_cn; add code example to elementwise_mod

* delete recordio_writer_cn; revert index_cn

* update api_cn after review

* fix io_cn.rst

* update layers_cn

* update io_cn
上级 0009c8e3
......@@ -35,57 +35,49 @@ append_backward
.. code-block:: python
# 网络配置
# ...
avg_loss = fluid.layers.mean(loss)
param_grad_list = fluid.backward.append_backward(loss=avg_loss)
# 损失计算
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, act=None)
loss = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_loss = fluid.layers.mean(loss)
param_grad_list = fluid.backward.append_backward(loss=avg_loss)
.. _cn_api_fluid_backward_gradients:
gradients
-------------------------------
.. py:function:: paddle.fluid.backward.gradients(targets, inputs, target_gradients=None, no_grad_set=None)
将目标梯度反向传播到输入。
参数:
- **targets** (Variable|list[Variable]) – 目标变量
- **inputs** (Variable|list[Variable]) – 输入变量
- **target_gradients** (Variable|list[Variable]|None) – 目标的梯度变量,应与目标变量形状相同;如果设置为None,则以1初始化所有梯度变量
- **no_grad_sethread** (set[string]) – 在Block 0中不具有梯度的变量,所有block中被设置 ``stop_gradient=True`` 的变量将被自动加入该set
返回:数组,包含与输入对应的梯度。如果一个输入不影响目标函数,则对应的梯度变量为None
返回类型:(list[Variable])
**示例代码**
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[2,8,8], dtype='float32')
x.stop_gradient=False
y = fluid.layers.conv2d(x, 4, 1, bias_attr=False)
y = fluid.layers.relu(y)
y = fluid.layers.conv2d(y, 4, 1, bias_attr=False)
y = fluid.layers.relu(y)
z = fluid.gradients([y], x)
print(z)
\ No newline at end of file
......@@ -28,8 +28,23 @@ ErrorClipByValue
.. code-block:: python
var = fluid.framework.Variable(..., error_clip=ErrorClipByValue(max=5.0), ...)
import paddle.fluid as fluid
BATCH_SIZE = 128
CLIP_MAX = 2e-6
CLIP_MIN = -1e-6
prog = fluid.framework.Program()
with fluid.program_guard(main_program=prog):
image = fluid.layers.data(name='x', shape=[784], dtype='float32')
hidden1 = fluid.layers.fc(input=image, size=128, act='relu')
hidden2 = fluid.layers.fc(input=hidden1, size=64, act='relu')
predict = fluid.layers.fc(input=hidden2, size=10, act='softmax')
label = fluid.layers.data(name='y', shape=[1], dtype='int64')
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(cost)
prog_clip = prog.clone()
prog_clip.block(0).var(hidden1.name)._set_error_clip(
fluid.clip.ErrorClipByValue(
max=CLIP_MAX, min=CLIP_MIN)
......@@ -70,6 +85,20 @@ GradientClipByGlobalNorm
.. code-block:: python
import paddle.fluid as fluid
prog = fluid.framework.Program()
startup_program = fluid.framework.Program()
with fluid.program_guard(
main_program=prog, startup_program=startup_program):
image = fluid.layers.data(name='x', shape=[784], dtype='float32')
label = fluid.layers.data(name='y', shape=[1], dtype='int64')
hidden1 = fluid.layers.fc(input=image, size=128, act='relu')
hidden2 = fluid.layers.fc(input=hidden1, size=64, act='relu')
predict = fluid.layers.fc(input=hidden2, size=10, act='softmax')
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(cost)
prog_clip = prog.clone()
avg_cost_clip = prog_clip.block(0).var(avg_cost.name)
p_g_clip = fluid.backward.append_backward(loss=avg_cost_clip)
with fluid.program_guard(main_program=prog_clip):
......@@ -83,6 +112,7 @@ GradientClipByGlobalNorm
.. _cn_api_fluid_clip_GradientClipByNorm:
GradientClipByNorm
......@@ -108,12 +138,14 @@ GradientClipByNorm
.. code-block:: python
import paddle.fluid as fluid
w_param_attrs = fluid.ParamAttr(name=None,
initializer=fluid.initializer.UniformInitializer(low=-1.0, high=1.0, seed=0),
learning_rate=1.0,
regularizer=fluid.regularizer.L1Decay(1.0),
trainable=True,
clip=fluid.clip.GradientClipByNorm(clip_norm=2.0))
gradient_clip=fluid.clip.GradientClipByNorm(clip_norm=2.0))
x = fluid.layers.data(name='x', shape=[10], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, param_attr=w_param_attrs)
......@@ -147,12 +179,14 @@ GradientClipByValue
.. code-block:: python
import paddle.fluid as fluid
w_param_attrs = fluid.ParamAttr(name=None,
initializer=fluid.initializer.UniformInitializer(low=-1.0, high=1.0, seed=0),
learning_rate=1.0,
regularizer=fluid.regualrizer.L1Decay(1.0),
trainable=True,
clip=fluid.clip.GradientClipByValue(-1.0, 1.0))
gradient_clip=fluid.clip.GradientClipByValue(-1.0, 1.0))
x = fluid.layers.data(name='x', shape=[10], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, param_attr=w_param_attrs)
......
......@@ -19,6 +19,7 @@ DataFeeder将reader返回的数据转换为可以输入Executor和ParallelExecut
.. code-block:: python
import paddle.fluid as fluid
place = fluid.CPUPlace()
img = fluid.layers.data(name='image', shape=[1, 28, 28])
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
......@@ -33,10 +34,16 @@ DataFeeder将reader返回的数据转换为可以输入Executor和ParallelExecut
.. code-block:: python
import paddle
import paddle.fluid as fluid
place=fluid.CUDAPlace(0)
data = fluid.layers.data(name='data', shape=[3, 224, 224], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
feeder = fluid.DataFeeder(place=place, feed_list=[data, label])
reader = feeder.decorate_reader(
paddle.batch(flowers.train(), batch_size=16))
paddle.batch(paddle.dataset.flowers.train(), batch_size=16), multi_devices=False)
参数:
......@@ -51,16 +58,32 @@ DataFeeder将reader返回的数据转换为可以输入Executor和ParallelExecut
.. code-block:: python
# ...
import numpy as np
import paddle
import paddle.fluid as fluid
place = fluid.CPUPlace()
feed_list = [
main_program.global_block().var(var_name) for var_name in feed_vars_name
] # feed_vars_name is a list of variables' name.
feeder = fluid.DataFeeder(feed_list, place)
def reader():
yield [np.random.random([4]).astype('float32'), np.random.random([3]).astype('float32')],
main_program = fluid.Program()
startup_program = fluid.Program()
with fluid.program_guard(main_program, startup_program):
data_1 = fluid.layers.data(name='data_1', shape=[1, 2, 2])
data_2 = fluid.layers.data(name='data_2', shape=[1, 1, 3])
out = fluid.layers.fc(input=[data_1, data_2], size=2)
# ...
feeder = fluid.DataFeeder([data_1, data_2], place)
exe = fluid.Executor(place)
exe.run(startup_program)
for data in reader():
outs = exe.run(program=main_program,
feed=feeder.feed(data))
feed=feeder.feed(data),
fetch_list=[out])
.. py:method:: feed(iterable)
......@@ -74,6 +97,24 @@ DataFeeder将reader返回的数据转换为可以输入Executor和ParallelExecut
返回类型: dict
**代码示例**
.. code-block:: python
import numpy.random as random
import paddle.fluid as fluid
def reader(limit=5):
for i in range(limit):
yield random.random([784]).astype('float32'), random.random([1]).astype('int64'), random.random([256]).astype('float32')
data_1 = fluid.layers.data(name='data_1', shape=[1, 28, 28])
data_2 = fluid.layers.data(name='data_2', shape=[1], dtype='int64')
data_3 = fluid.layers.data(name='data_3', shape=[16, 16], dtype='float32')
feeder = fluid.DataFeeder(['data_1','data_2', 'data_3'], fluid.CPUPlace())
result = feeder.feed(reader())
.. py:method:: feed_parallel(iterable, num_places=None)
......@@ -94,6 +135,34 @@ DataFeeder将reader返回的数据转换为可以输入Executor和ParallelExecut
设备数量和mini-batches数量必须一致。
**代码示例**
.. code-block:: python
import numpy.random as random
import paddle.fluid as fluid
def reader(limit=10):
for i in range(limit):
yield [random.random([784]).astype('float32'), random.randint(10)],
x = fluid.layers.data(name='x', shape=[1, 28, 28])
y = fluid.layers.data(name='y', shape=[1], dtype='int64')
feeder = fluid.DataFeeder(['x','y'], fluid.CPUPlace())
place_num = 2
places = [fluid.CPUPlace() for x in range(place_num)]
data = []
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
program = fluid.CompiledProgram(fluid.default_main_program()).with_data_parallel(places=places)
for item in reader():
data.append(item)
if place_num == len(data):
exe.run(program=program, feed=list(feeder.feed_parallel(data, place_num)), fetch_list=[])
data = []
.. py:method:: decorate_reader(reader, multi_devices, num_places=None, drop_last=True)
将输入数据转换成reader返回的多个mini-batches。每个mini-batch分别送入各设备中。
......@@ -110,6 +179,31 @@ DataFeeder将reader返回的数据转换为可以输入Executor和ParallelExecut
抛出异常: ``ValueError`` – 如果drop_last为False并且数据batch和设备数目不匹配。
**代码示例**
.. code-block:: python
import numpy.random as random
import paddle
import paddle.fluid as fluid
def reader(limit=5):
for i in range(limit):
yield (random.random([784]).astype('float32'), random.random([1]).astype('int64')),
place=fluid.CUDAPlace(0)
data = fluid.layers.data(name='data', shape=[1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
feeder = fluid.DataFeeder(place=place, feed_list=[data, label])
reader = feeder.decorate_reader(reader, multi_devices=False)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
for data in reader():
exe.run(feed=data)
.. _cn_api_paddle_data_reader_reader:
......@@ -374,4 +468,4 @@ Creator包包含一些简单的reader creator,可以在用户Program中使用
路径:recordio文件的路径,可以是字符串或字符串列表。
返回: recordio文件的数据读取器
返回:recordio文件的数据读取器
......@@ -18,7 +18,7 @@ MNIST数据集。
MNIST训练数据集的creator。
它返回一个reader creator, reader中的每个样本的图像像素范围是[0,1],标签范围是[0,9]。
它返回一个reader creator, reader中的每个样本的图像像素范围是[-1,1],标签范围是[0,9]。
返回: 训练数据的reader creator
......@@ -30,7 +30,7 @@ MNIST训练数据集的creator。
MNIST测试数据集的creator。
它返回一个reader creator, reader中的每个样本的图像像素范围是[0,1],标签范围是[0,9]。
它返回一个reader creator, reader中的每个样本的图像像素范围是[-1,1],标签范围是[0,9]。
返回: 测试数据集的reader creator
......
......@@ -14,13 +14,14 @@ DataFeeder
``DataFeeder`` 负责将reader(读取器)返回的数据转成一种特殊的数据结构,使它们可以输入到 ``Executor`` 和 ``ParallelExecutor`` 中。
reader通常返回一个minibatch条目列表。在列表中每一条目都是一个样本(sample),它是由具有一至多个特征的列表或元组组成的。
reader通常返回一个minibatch条目列表。在列表中每一条目都是一个样本(sample)它是由具有一至多个特征的列表或元组组成的。
以下是简单用法:
.. code-block:: python
import paddle.fluid as fluid
place = fluid.CPUPlace()
img = fluid.layers.data(name='image', shape=[1, 28, 28])
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
......@@ -31,10 +32,16 @@ reader通常返回一个minibatch条目列表。在列表中每一条目都是
.. code-block:: python
import paddle
import paddle.fluid as fluid
place=fluid.CUDAPlace(0)
data = fluid.layers.data(name='data', shape=[3, 224, 224], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
feeder = fluid.DataFeeder(place=place, feed_list=[data, label])
reader = feeder.decorate_reader(
paddle.batch(flowers.train(), batch_size=16))
paddle.batch(paddle.dataset.flowers.train(), batch_size=16), multi_devices=False)
......@@ -51,15 +58,32 @@ reader通常返回一个minibatch条目列表。在列表中每一条目都是
.. code-block:: python
# ...
import numpy as np
import paddle
import paddle.fluid as fluid
place = fluid.CPUPlace()
feed_list = [
main_program.global_block().var(var_name) for var_name in feed_vars_name
] # feed_vars_name 是一个由变量名组成的列表
feeder = fluid.DataFeeder(feed_list, place)
def reader():
yield [np.random.random([4]).astype('float32'), np.random.random([3]).astype('float32')],
main_program = fluid.Program()
startup_program = fluid.Program()
with fluid.program_guard(main_program, startup_program):
data_1 = fluid.layers.data(name='data_1', shape=[1, 2, 2])
data_2 = fluid.layers.data(name='data_2', shape=[1, 1, 3])
out = fluid.layers.fc(input=[data_1, data_2], size=2)
# ...
feeder = fluid.DataFeeder([data_1, data_2], place)
exe = fluid.Executor(place)
exe.run(startup_program)
for data in reader():
outs = exe.run(program=main_program,
feed=feeder.feed(data))
feed=feeder.feed(data),
fetch_list=[out])
.. py:method:: feed(iterable)
......@@ -74,6 +98,24 @@ reader通常返回一个minibatch条目列表。在列表中每一条目都是
返回类型: dict
**代码示例**
.. code-block:: python
import numpy.random as random
import paddle.fluid as fluid
def reader(limit=5):
for i in range(limit):
yield random.random([784]).astype('float32'), random.random([1]).astype('int64'), random.random([256]).astype('float32')
data_1 = fluid.layers.data(name='data_1', shape=[1, 28, 28])
data_2 = fluid.layers.data(name='data_2', shape=[1], dtype='int64')
data_3 = fluid.layers.data(name='data_3', shape=[16, 16], dtype='float32')
feeder = fluid.DataFeeder(['data_1','data_2', 'data_3'], fluid.CPUPlace())
result = feeder.feed(reader())
.. py:method:: feed_parallel(iterable, num_places=None)
......@@ -91,10 +133,36 @@ reader通常返回一个minibatch条目列表。在列表中每一条目都是
.. note::
设备(CPU或GPU)的数目必须等于minibatch的数目
**代码示例**
.. code-block:: python
.. py:method:: decorate_reader(reader, multi_devices, num_places=None, drop_last=True)
import numpy.random as random
import paddle.fluid as fluid
def reader(limit=10):
for i in range(limit):
yield [random.random([784]).astype('float32'), random.randint(10)],
x = fluid.layers.data(name='x', shape=[1, 28, 28])
y = fluid.layers.data(name='y', shape=[1], dtype='int64')
feeder = fluid.DataFeeder(['x','y'], fluid.CPUPlace())
place_num = 2
places = [fluid.CPUPlace() for x in range(place_num)]
data = []
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
program = fluid.CompiledProgram(fluid.default_main_program()).with_data_parallel(places=places)
for item in reader():
data.append(item)
if place_num == len(data):
exe.run(program=program, feed=list(feeder.feed_parallel(data, place_num)), fetch_list=[])
data = []
.. py:method:: decorate_reader(reader, multi_devices, num_places=None, drop_last=True)
将reader返回的输入数据batch转换为多个mini-batch,之后每个mini-batch都会被输入进各个设备(CPU或GPU)中。
......@@ -111,9 +179,29 @@ reader通常返回一个minibatch条目列表。在列表中每一条目都是
弹出异常: ValueError – 如果 ``drop_last`` 值为False并且reader返回的minibatch数目与设备数目不相等时,产生此异常
**代码示例**
.. code-block:: python
import numpy.random as random
import paddle
import paddle.fluid as fluid
def reader(limit=5):
for i in range(limit):
yield (random.random([784]).astype('float32'), random.random([1]).astype('int64')),
place=fluid.CUDAPlace(0)
data = fluid.layers.data(name='data', shape=[1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
feeder = fluid.DataFeeder(place=place, feed_list=[data, label])
reader = feeder.decorate_reader(reader, multi_devices=False)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
for data in reader():
exe.run(feed=data)
......
......@@ -14,28 +14,221 @@ DatasetFactory
.. py:class:: paddle.fluid.dataset.DatasetFactory
DatasetFactory是一个按数据集名称创建数据集的 "工厂" ,可以创建“QueueDataset”或“InMemoryDataset”,默认为“QueueDataset”。
DatasetFactory是一个按数据集名称创建数据集的 "工厂",可以创建“QueueDataset”,“InMemoryDataset”或“FileInstantDataset”,默认为“QueueDataset”。
**代码示例**
.. code-block:: python
dataset = paddle.fluid.DatasetFactory.create_dataset(“InMemoryDataset”)
import paddle.fluid as fluid
dataset = paddle.fluid.DatasetFactory().create_dataset("InMemoryDataset")
.. py:method:: create_dataset(datafeed_class='QueueDataset')
创建“QueueDataset”或“InMemoryDataset”,默认为“QueueDataset”。
创建“QueueDataset”,“InMemoryDataset” 或 “FileInstantDataset”,默认为“QueueDataset”。
参数:
- **datafeed_class** (str) – datafeed类名,为QueueDataset或InMemoryDataset。默认为QueueDataset。
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset()
.. _cn_api_fluid_dataset_InMemoryDataset:
InMemoryDataset
-------------------------------
.. py:class:: paddle.fluid.dataset.InMemoryDataset
InMemoryDataset会向内存中加载数据并在训练前缓冲数据。此类由DatasetFactory创建。
**代码示例**:
.. code-block:: python
dataset = paddle.fluid.DatasetFactory().create_dataset(“InMemoryDataset”)
.. py:method:: load_into_memory()
向内存中加载数据。
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"]
dataset.set_filelist(filelist)
dataset.load_into_memory()
.. py:method:: local_shuffle()
局域shuffle。
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"]
dataset.set_filelist(filelist)
dataset.load_into_memory()
dataset.local_shuffle()
.. py:method:: global_shuffle(fleet=None)
全局shuffle。
只能用在分布式模式(单机多进程或多机多进程)中。您如果在分布式模式中运行,应当传递fleet而非None。
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"]
dataset.set_filelist(filelist)
dataset.load_into_memory()
dataset.global_shuffle(fleet)
参数:
- **fleet** (Fleet) – fleet单例。默认为None。
.. py:method:: release_memory()
当数据不再使用时,释放InMemoryDataset内存数据。
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"]
dataset.set_filelist(filelist)
dataset.load_into_memory()
dataset.global_shuffle(fleet)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
exe.train_from_dataset(fluid.default_main_program(), dataset)dataset.release_memory()
dataset.release_memory()
.. py:method:: get_memory_data_size(fleet=None)
用户可以调用此函数以了解加载进内存后所有workers中的ins数量。
.. note::
该函数可能会导致性能不佳,因为它具有barrier。
参数:
- **fleet** (Fleet) – fleet对象。
返回:内存数据的大小。
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"]
dataset.set_filelist(filelist)
dataset.load_into_memory()
print dataset.get_memory_data_size(fleet)
.. py:method:: get_shuffle_data_size(fleet=None)
获取shuffle数据大小,用户可以调用此函数以了解局域/全局shuffle后所有workers中的ins数量。
.. note::
该函数可能会导致局域shuffle性能不佳,因为它具有barrier。但其不影响局域shuffle。
参数:
- **fleet** (Fleet) – fleet对象。
返回:shuffle数据的大小。
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset")
filelist = ["a.txt", "b.txt"]
dataset.set_filelist(filelist)
dataset.load_into_memory()
dataset.global_shuffle(fleet)
print dataset.get_shuffle_data_size(fleet)
.. _cn_api_fluid_dataset_QueueDataset:
QueueDataset
-------------------------------
.. py:class:: paddle.fluid.dataset.QueueDataset
流式处理数据。
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset("QueueDataset")
.. py:method:: local_shuffle()
局域shuffle数据
QueueDataset中不支持局域shuffle,可能抛出NotImplementedError
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
dataset = fluid.DatasetFactory().create_dataset("QueueDataset")
dataset.local_shuffle()
.. py:method:: global_shuffle(fleet=None)
全局shuffle数据
QueueDataset中不支持全局shuffle,可能抛出NotImplementedError
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
from paddle.fluid.incubate.fleet.parameter_server.pslib import fleet
dataset = fluid.DatasetFactory().create_dataset("QueueDataset")
dataset.global_shuffle(fleet)
......@@ -15,9 +15,7 @@ Executor
执行引擎(Executor)使用python脚本驱动,仅支持在单GPU环境下运行。多卡环境下请参考 ``ParallelExecutor`` 。
执行引擎(Executor)使用python脚本驱动,支持在单/多GPU、单/多CPU环境下运行。
Python Executor可以接收传入的program,并根据feed map(输入映射表)和fetch_list(结果获取表)
向program中添加feed operators(数据输入算子)和fetch operators(结果获取算子)。
feed map为该program提供输入数据。fetch_list提供program训练结束后用户预期的变量(或识别类场景中的命名)。
......@@ -28,54 +26,77 @@ Executor将全局变量存储到全局作用域中,并为临时变量创建局
当每一mini-batch上的前向/反向运算完成后,局部作用域的内容将被废弃,
但全局作用域中的变量将在Executor的不同执行过程中一直存在。
program中所有的算子会按顺序执行。
**示例代码**
.. code-block:: python
# 新建一个执行引擎Executor名为exe。
import paddle.fluid as fluid
import paddle.fluid.compiler as compiler
import numpy
import os
use_cuda = True
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
# 仅运行一次startup program.
# 不需要优化/编译这个startup program.
exe.run(fluid.default_startup_program())
train_program = fluid.Program()
startup_program = fluid.Program()
with fluid.program_guard(train_program, startup_program):
data = fluid.layers.data(name='X', shape=[1], dtype='float32')
hidden = fluid.layers.fc(input=data, size=10)
loss = fluid.layers.mean(hidden)
fluid.optimizer.SGD(learning_rate=0.01).minimize(loss)
# 仅运行一次startup program
# 不需要优化/编译这个startup program
startup_program.random_seed=1
exe.run(startup_program)
# 无需编译,直接运行main program
loss, = exe.run(fluid.default_main_program(),
feed=feed_dict,
x = numpy.random.random(size=(10, 1)).astype('float32')
loss_data, = exe.run(train_program,
feed={"X": x},
fetch_list=[loss.name])
# 另一种方法是,编译这个main program然后运行. 参考CompiledProgram
# 另一种方法是,编译这个main program然后运行。
# 参考CompiledProgram以获取更多信息。
# 注意:如果你使用CPU运行程序,需要具体设置CPU_NUM,
# 否则fluid会把逻辑核的所有数目设为CPU_NUM,
# 在这种情况下,输入的batch size应大于CPU_NUM,
# 否则程序会异常中断。
if not use_cuda:
os.environ['CPU_NUM'] = str(2)
compiled_prog = compiler.CompiledProgram(
fluid.default_main_program()).with_data_parallel(
train_program).with_data_parallel(
loss_name=loss.name)
loss, = exe.run(compiled_prog,
feed=feed_dict,
loss_data, = exe.run(compiled_prog,
feed={"X": x},
fetch_list=[loss.name])
参数:
- **place** (core.CPUPlace|core.CUDAPlace(n)) – 指明了 ``Executor`` 的执行场所
- **place** (fluid.CPUPlace|fluid.CUDAPlace(n)) – 指明了 ``Executor`` 的执行场所
提示:你可以用 ``Executor`` 来调试基于并行GPU实现的复杂网络,他们有完全一样的参数也会产生相同的结果。
.. py:method:: close()
关闭这个执行器(Executor)。调用这个方法后不可以再使用这个执行器。 对于分布式训练, 该函数会释放在PServers上涉及到目前训练器的资源。
关闭这个执行器(Executor)。
调用这个方法后不可以再使用这个执行器。 对于分布式训练, 该函数会释放在PServers上和目前Trainer有关联的资源。
**示例代码**
.. code-block:: python
cpu = core.CPUPlace()
exe = Executor(cpu)
...
import paddle.fluid as fluid
cpu = fluid.CPUPlace()
exe = fluid.Executor(cpu)
# 执行训练或测试过程
exe.close()
......@@ -89,54 +110,115 @@ feed map为该program提供输入数据。fetch_list提供program训练结束后
应注意,执行器会执行program中的所有算子而不仅仅是依赖于fetch_list的那部分。
**示例代码**
.. code-block:: python
import paddle.fluid as fluid
import numpy
#首先创建执行引擎
place = fluid.CPUPlace() # fluid.CUDAPlace(0)
exe = fluid.Executor(place)
data = fluid.layers.data(name='X', shape=[1], dtype='float32')
hidden = fluid.layers.fc(input=data, size=10)
loss = fluid.layers.mean(hidden)
adam = fluid.optimizer.Adam()
adam.minimize(loss)
#仅运行startup程序一次
exe.run(fluid.default_startup_program())
x = numpy.random.random(size=(10, 1)).astype('float32')
outs = exe.run(feed={'X': x},
fetch_list=[loss.name])
参数:
- **program** (Program|CompiledProgram) – 需要执行的program,如果没有给定那么默认使用default_main_program (未编译的)
- **feed** (dict) – 前向输入的变量,数据,词典dict类型, 例如 {“image”: ImageData, “label”: LabelData}
- **fetch_list** (list) – 用户想得到的变量或者命名的列表, run会根据这个列表给与结果
- **fetch_list** (list) – 用户想得到的变量或者命名的列表, 该方法会根据这个列表给出结果
- **feed_var_name** (str) – 前向算子(feed operator)变量的名称
- **fetch_var_name** (str) – 结果获取算子(fetch operator)的输出变量名称
- **scope** (Scope) – 执行这个program的域,用户可以指定不同的域。缺省为全局域
- **return_numpy** (bool) – 如果为True,则将结果张量(fetched tensor)转化为numpy
- **use_program_cache** (bool) – 当program较上次比没有改动则将其置为True
- **use_program_cache** (bool) – 是否跨批使用缓存程序设置。设置为True时,只有当(1)程序没有用数据并行编译,并且(2)program、 feed变量名和fetch_list变量名与上一步相比没有更改时,运行速度才会更快。
返回: 根据fetch_list来获取结果
返回类型: list(numpy.array)
**示例代码**
.. py:method:: infer_from_dataset(program=None, dataset=None, scope=None, thread=0, debug=False, fetch_list=None, fetch_info=None, print_period=100)
.. code-block:: python
infer_from_dataset的文档与train_from_dataset几乎完全相同,只是在分布式训练中,推进梯度将在infer_from_dataset中禁用。 infer_from_dataset()可以非常容易地用于多线程中的评估。
data = fluid.layers.data(name='X', shape=[1], dtype='float32')
out = fluid.layers.create_tensor(dtype='float32')
hidden = fluid.layers.fc(input=data, size=10)
fluid.layers.assign(hidden,out)
loss = fluid.layers.mean(out)
adam = fluid.optimizer.Adam()
# adam.minimize(loss)
参数:
- **program** (Program|CompiledProgram) – 需要执行的program,如果没有给定那么默认使用default_main_program (未编译的)
- **dataset** (paddle.fluid.Dataset) – 在此函数外创建的数据集,用户应当在调用函数前提供完整定义的数据集。必要时请检查Dataset文件。默认为None
- **scope** (Scope) – 执行这个program的域,用户可以指定不同的域。默认为全局域
- **thread** (int) – 用户想要在这个函数中运行的线程数量。线程的实际数量为min(Dataset.thread_num, thread),如果thread > 0,默认为0
- **debug** (bool) – 是否开启debug模式,默认为False
- **fetch_list** (Variable List) – 返回变量列表,每个变量都会在训练过程中被打印出来,默认为None
- **fetch_info** (String List) – 每个变量的打印信息,默认为None
- **print_period** (int) – 每两次打印之间间隔的mini-batches的数量,默认为100
返回: None
**示例代码**
.. code-block:: python
cpu = core.CPUPlace()
exe = fluid.Executor(cpu)
import paddle.fluid as fluid
place = fluid.CPUPlace() # 使用GPU时可设置place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
x = fluid.layers.data(name="x", shape=[10, 10], dtype="int64")
y = fluid.layers.data(name="y", shape=[1], dtype="int64", lod_level=1)
dataset = fluid.DatasetFactory().create_dataset()
dataset.set_use_var([x, y])
dataset.set_thread(1)
filelist = [] # 您可以设置您自己的filelist,如filelist = ["dataA.txt"]
dataset.set_filelist(filelist)
exe.run(fluid.default_startup_program())
exe.infer_from_dataset(program=fluid.default_main_program(),dataset=dataset)
.. code-block:: python
x = numpy.random.random(size=(10, 1)).astype('float32')
outs = exe.run(
feed={'X': x},
fetch_list=[loss.name])
.. py:method:: train_from_dataset(program=None, dataset=None, scope=None, thread=0, debug=False, fetch_list=None, fetch_info=None, print_period=100)
从预定义的数据集中训练。 数据集在paddle.fluid.dataset中定义。 给定程序(或编译程序),train_from_dataset将使用数据集中的所有数据样本。 输入范围可由用户给出。 默认情况下,范围是global_scope()。训练中的线程总数是thread。 训练中使用的线程数将是数据集中threadnum的最小值,同时也是此接口中线程的值。 可以设置debug,以便执行器显示所有算子的运行时间和当前训练任务的吞吐量。
注意:train_from_dataset将销毁每次运行在executor中创建的所有资源。
参数:
- **program** (Program|CompiledProgram) – 需要执行的program,如果没有给定那么默认使用default_main_program (未编译的)
- **dataset** (paddle.fluid.Dataset) – 在此函数外创建的数据集,用户应当在调用函数前提供完整定义的数据集。必要时请检查Dataset文件。默认为None
- **scope** (Scope) – 执行这个program的域,用户可以指定不同的域。默认为全局域
- **thread** (int) – 用户想要在这个函数中运行的线程数量。线程的实际数量为min(Dataset.thread_num, thread),如果thread > 0,默认为0
- **debug** (bool) – 是否开启debug模式,默认为False
- **fetch_list** (Variable List) – 返回变量列表,每个变量都会在训练过程中被打印出来,默认为None
- **fetch_info** (String List) – 每个变量的打印信息,默认为None
- **print_period** (int) – 每两次打印之间间隔的mini-batches的数量,默认为100
返回: None
**示例代码**
.. code-block:: python
import paddle.fluid as fluid
place = fluid.CPUPlace() # 通过设置place = fluid.CUDAPlace(0)使用GPU
exe = fluid.Executor(place)
x = fluid.layers.data(name="x", shape=[10, 10], dtype="int64")
y = fluid.layers.data(name="y", shape=[1], dtype="int64", lod_level=1)
dataset = fluid.DatasetFactory().create_dataset()
dataset.set_use_var([x, y])
dataset.set_thread(1)
filelist = [] # 您可以设置您自己的filelist,如filelist = ["dataA.txt"]
dataset.set_filelist(filelist)
exe.run(fluid.default_startup_program())
exe.infer_from_dataset(program=fluid.default_main_program(),
dataset=dataset)
.. _cn_api_fluid_executor_global_scope:
......@@ -144,11 +226,21 @@ feed map为该program提供输入数据。fetch_list提供program训练结束后
global_scope
-------------------------------
.. py:function:: paddle.fluid.executor.global_scope ()
.. py:function:: paddle.fluid.global_scope()
获取全局/默认作用域实例。很多api使用默认 ``global_scope`` ,例如 ``Executor.run`` 。
**示例代码**
.. code-block:: python
import paddle.fluid as fluid
import numpy
fluid.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), fluid.CPUPlace())
numpy.array(fluid.global_scope().find_var("data").get_tensor())
返回:全局/默认作用域实例
返回类型:Scope
......@@ -175,12 +267,12 @@ scope_guard
.. code-block:: python
import paddle.fluid as fluid
import numpy
new_scope = fluid.Scope()
with fluid.scope_guard(new_scope):
...
fluid.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), fluid.CPUPlace())
numpy.array(new_scope.find_var("data").get_tensor())
......
此差异已折叠。
......@@ -27,14 +27,19 @@ BilinearInitializer
.. code-block:: python
factor = 2
w_attr = ParamAttr(learning_rate=0., regularizer=L2Decay(0.),
initializer=Bilinear())
C = 2
w_attr = fluid.initializer.ParamAttr(
learning_rate=0.,
regularizer=fluid.regularizer.L2Decay(0.),
initializer=fluid.initializer.Bilinear())
x = fluid.layers.data(name="data", shape=[3, 32, 32],
dtype="float32")
conv_up = fluid.layers.conv2d_transpose(
input,
input=x,
num_filters=C,
output_size=None,
filter_size=2 * factor - factor % 2,
padding=ceil((factor - 1) / 2.),
padding=int(math.ceil((factor - 1) / 2.)),
stride=factor,
groups=C,
param_attr=w_attr,
......@@ -72,6 +77,7 @@ ConstantInitializer
.. code-block:: python
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
fc = fluid.layers.fc(input=x, size=10,
param_attr=fluid.initializer.Constant(value=2.0))
......@@ -98,8 +104,8 @@ force_init_on_cpu
.. code-block:: python
if force_init_on_cpu():
create_op('force_cpu': force_init_on_cpu())
if fluid.initializer.force_init_on_cpu():
step = fluid.layers.create_global_var(shape=[2,3], value=1.0, dtype='float32')
......@@ -124,8 +130,8 @@ init_on_cpu
.. code-block:: python
with init_on_cpu():
step = fluid.layers.create_global_var()
with fluid.initializer.init_on_cpu():
step = fluid.layers.create_global_var(shape=[2,3], value=1.0, dtype='float32')
......@@ -177,9 +183,8 @@ MSRAInitializer
.. code-block:: python
fc = fluid.layers.fc(
input=queries, size=10,
param_attr=fluid.initializer.MSRA(uniform=False))
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
fc = fluid.layers.fc(input=x, size=10, param_attr=fluid.initializer.MSRA(uniform=False))
......@@ -214,6 +219,7 @@ NormalInitializer
.. code-block:: python
x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
fc = fluid.layers.fc(input=x, size=10,
param_attr=fluid.initializer.Normal(loc=0.0, scale=2.0)
......@@ -234,6 +240,7 @@ NumpyArrayInitializer
.. code-block:: python
x = fluid.layers.data(name="x", shape=[5], dtype='float32')
fc = fluid.layers.fc(input=x, size=10,
param_attr=fluid.initializer.NumpyArrayInitializer(numpy.array([1,2])))
......@@ -266,6 +273,8 @@ Random Truncated Normal(高斯)分布初始化器
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[1], dtype='float32')
fc = fluid.layers.fc(input=x, size=10,
param_attr=fluid.initializer.TruncatedNormal(loc=0.0, scale=2.0))
......@@ -306,6 +315,8 @@ UniformInitializer
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[1], dtype='float32')
fc = fluid.layers.fc(input=x, size=10,
param_attr=fluid.initializer.Uniform(low=-0.5, high=0.5))
......@@ -368,6 +379,8 @@ XavierInitializer
.. code-block:: python
import paddle.fluid as fluid
queries = fluid.layers.data(name='x', shape=[1], dtype='float32')
fc = fluid.layers.fc(
input=queries, size=10,
param_attr=fluid.initializer.Xavier(uniform=False))
......
此差异已折叠。
此差异已折叠。
......@@ -11,7 +11,8 @@ Accuracy
.. py:class:: paddle.fluid.metrics.Accuracy(name=None)
累加mini-batch正确率,计算每次pass的平均准确率。https://en.wikipedia.org/wiki/Accuracy_and_precision
计算多批次的平均准确率。
https://en.wikipedia.org/wiki/Accuracy_and_precision
参数:
- **name** — 度量标准的名称
......@@ -20,32 +21,39 @@ Accuracy
.. code-block:: python
labels = fluid.layers.data(name="data", shape=[1], dtype="int32")
data = fluid.layers.data(name="data", shape=[32, 32], dtype="int32")
pred = fluid.layers.fc(input=data, size=1000, act="tanh")
minibatch_accuracy = fluid.layers.accuracy(pred, label)
accuracy_evaluator = fluid.metrics.Accuracy()
for pass in range(PASSES):
accuracy_evaluator.reset()
for data in train_reader():
batch_size = data[0]
loss = exe.run(fetch_list=[cost, minibatch_accuracy])
accuracy_evaluator.update(value=minibatch_accuracy, weight=batch_size)
numpy_acc = accuracy_evaluator.eval()
# 假设有batch_size = 128
batch_size=128
accuracy_manager = fluid.metrics.Accuracy()
# 假设第一个batch的准确率为0.9
batch1_acc = 0.9
accuracy_manager.update(value = batch1_acc, weight = batch_size)
print("expect accuracy: %.2f, get accuracy: %.2f" % (batch1_acc, accuracy_manager.eval()))
# 假设第二个batch的准确率为0.8
batch2_acc = 0.8
accuracy_manager.update(value = batch2_acc, weight = batch_size)
#batch1和batch2的联合准确率为(batch1_acc * batch_size + batch2_acc * batch_size) / batch_size / 2
print("expect accuracy: %.2f, get accuracy: %.2f" % ((batch1_acc * batch_size + batch2_acc * batch_size) / batch_size / 2, accuracy_manager.eval()))
#重置accuracy_manager
accuracy_manager.reset()
#假设第三个batch的准确率为0.8
batch3_acc = 0.8
accuracy_manager.update(value = batch3_acc, weight = batch_size)
print("expect accuracy: %.2f, get accuracy: %.2f" % (batch3_acc, accuracy_manager.eval()))
.. py:method:: update(value, weight)
更新mini batch的状态.
更新mini batch的状态
参数:
- **value** (float|numpy.array) – 每个mini batch的正确率
- **weight** (int|float) – batch 大小
.. py:method:: eval()
返回所有累计batches的平均准确率(float或numpy.array)。
.. _cn_api_fluid_metrics_Auc:
......@@ -55,7 +63,7 @@ Auc
.. py:class:: paddle.fluid.metrics.Auc(name, curve='ROC', num_thresholds=4095)
Auc度量适用于二分类。参考 https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve 。需要注意auc度量本身是用Python计算值。如果关心速度,请用fluid.layers.auc。
Auc度量用于二分类。参考 https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve 。请注意auc度量是用Python实现的,可能速度略慢。
auc函数创建四个局部变量true_positives, true_negatives, false_positives和false_negatives,用于计算AUC。对于离散化AUC曲线,临界值线性间隔设置以便计算召回率和准确率的值,用false positive率的召回值高度计算ROC曲线面积,用recall的准确值高度计算PR曲线面积。
......@@ -69,15 +77,39 @@ auc函数创建四个局部变量true_positives, true_negatives, false_positives
.. code-block:: python
pred = fluid.layers.fc(input=data, size=1000, act="tanh")
metric = fluid.metrics.Auc()
for data in train_reader():
loss, preds, labels = exe.run(fetch_list=[cost, preds, labels])
metric.update(preds, labels)
numpy_auc = metric.eval()
import numpy as np
# 初始化auc度量
auc_metric = fluid.metrics.Auc("ROC")
# 假设batch_size为128
batch_num = 100
batch_size = 128
for batch_id in range(batch_num):
class0_preds = np.random.random(size = (batch_size, 1))
class1_preds = 1 - class0_preds
preds = np.concatenate((class0_preds, class1_preds), axis=1)
labels = np.random.randint(2, size = (batch_size, 1))
auc_metric.update(preds = preds, labels = labels)
# 应为一个接近0.5的值,因为preds是随机指定的
print("auc for iteration %d is %.2f" % (batch_id, auc_metric.eval()))
.. py:method:: update(preds, labels)
用给定的预测值和标签更新auc曲线。
参数:
- **preds** – 形状为(batch_size, 2)的numpy数组,preds[i][j]表示将实例i划分为类别j的概率。
- **labels** – 形状为(batch_size, 1)的numpy数组,labels[i]为0或1,代表实例i的标签。
.. py:method:: eval()
返回auc曲线下的区域(一个float值)。
......@@ -102,18 +134,29 @@ ChunkEvaluator
.. code-block:: python
labels = fluid.layers.data(name="data", shape=[1], dtype="int32")
data = fluid.layers.data(name="data", shape=[32, 32], dtype="int32")
pred = fluid.layers.fc(input=data, size=1000, act="tanh")
precision, recall, f1_score, num_infer_chunks, num_label_chunks, num_correct_chunks = fluid.layers.chunk_eval(
input=pred,
label=label)
# 初始化chunck-level的评价管理。
metric = fluid.metrics.ChunkEvaluator()
for data in train_reader():
loss, preds, labels = exe.run(fetch_list=[cost, preds, labels])
# 假设模型预测10个chuncks,其中8个为正确,且真值有9个chuncks。
num_infer_chunks = 10
num_label_chunks = 9
num_correct_chunks = 8
metric.update(num_infer_chunks, num_label_chunks, num_correct_chunks)
numpy_precision, numpy_recall, numpy_f1 = metric.eval()
print("precision: %.2f, recall: %.2f, f1: %.2f" % (numpy_precision, numpy_recall, numpy_f1))
# 下一个batch,完美地预测了3个正确的chuncks。
num_infer_chunks = 3
num_label_chunks = 3
num_correct_chunks = 3
metric.update(num_infer_chunks, num_label_chunks, num_correct_chunks)
numpy_precision, numpy_recall, numpy_f1 = metric.eval()
print("precision: %.2f, recall: %.2f, f1: %.2f" % (numpy_precision, numpy_recall, numpy_f1))
.. py:method:: update(num_infer_chunks, num_label_chunks, num_correct_chunks)
基于layers.chunk_eval()输出更新状态(state)输出
......@@ -121,7 +164,7 @@ ChunkEvaluator
参数:
- **num_infer_chunks** (int|numpy.array): 给定minibatch的Interface块数。
- **num_label_chunks** (int|numpy.array): 给定minibatch的Label块数。
- **num_correct_chunks** (int|numpy.array): 给定minibatch的Interface和Label的块数
- **num_correct_chunks** (int|float|numpy.array): 给定minibatch的Interface和Label的块数
......@@ -142,20 +185,24 @@ CompositeMetric
.. code-block:: python
labels = fluid.layers.data(name="data", shape=[1], dtype="int32")
data = fluid.layers.data(name="data", shape=[32, 32], dtype="int32")
pred = fluid.layers.fc(input=data, size=1000, act="tanh")
import numpy as np
preds = [[0.1], [0.7], [0.8], [0.9], [0.2],
[0.2], [0.3], [0.5], [0.8], [0.6]]
labels = [[0], [1], [1], [1], [1],
[0], [0], [0], [0], [0]]
preds = np.array(preds)
labels = np.array(labels)
comp = fluid.metrics.CompositeMetric()
acc = fluid.metrics.Precision()
precision = fluid.metrics.Precision()
recall = fluid.metrics.Recall()
comp.add_metric(acc)
comp.add_metric(precision)
comp.add_metric(recall)
for pass in range(PASSES):
comp.reset()
for data in train_reader():
loss, preds, labels = exe.run(fetch_list=[cost, preds, labels])
comp.update(preds=preds, labels=labels)
numpy_acc, numpy_recall = comp.eval()
numpy_precision, numpy_recall = comp.eval()
print("expect precision: %.2f, got %.2f" % ( 3. / 5, numpy_precision ) )
print("expect recall: %.2f, got %.2f" % (3. / 4, numpy_recall ) )
.. py:method:: add_metric(metric)
......@@ -198,17 +245,17 @@ DetectionMAP
.. py:class:: paddle.fluid.metrics.DetectionMAP(input, gt_label, gt_box, gt_difficult=None, class_num=None, background_label=0, overlap_threshold=0.5, evaluate_difficult=True, ap_version='integral')
计算 detection 平均精度(mAP)。 mAP是衡量object detectors精度的指标,比如 Faster R-CNN,SSD等。它不同于召回率,它是最大精度的平均值。 请从以下文章中获取更多信息:
https://sanchom.wordpress.com/tag/average-precision/
https://arxiv.org/abs/1512.02325
计算 detection 平均精度(mAP)。 mAP是衡量object detectors精度的指标,比如 Faster R-CNN,SSD等。它不同于召回率,它是最大精度的平均值。 5
通常步骤如下:
1. 根据detectors中的输入和label,计算 true positive 和 false positive
2. 计算map,支持 ‘11 point’ and ‘integral’
请从以下文章中获取更多信息:
- https://sanchom.wordpress.com/tag/average-precision/
- https://arxiv.org/abs/1512.0232
参数:
- **input** (Variable) – detection的结果,一个 shape=[M, 6] 的 lodtensor。布局为[label, confidence, xmin, ymin, xmax, ymax]
- **gt_label** (Variable) – ground truth label 的索引,它是一个形状为[N, 1]的lodtensor
......@@ -224,23 +271,39 @@ https://arxiv.org/abs/1512.02325
.. code-block:: python
exe = fluid.Executor(place)
map_evaluator = fluid.Evaluator.DetectionMAP(input,
gt_label, gt_box, gt_difficult)
cur_map, accum_map = map_evaluator.get_map_var()
fetch = [cost, cur_map, accum_map]
for epoch in PASS_NUM:
map_evaluator.reset(exe)
for data in batches:
loss, cur_map_v, accum_map_v = exe.run(fetch_list=fetch)
import paddle.fluid.layers as layers
batch_size = -1 # 可以为任意大小
image_boxs_num = 10
bounding_bboxes_num = 21
pb = layers.data(name='prior_box', shape=[image_boxs_num, 4],
append_batch_size=False, dtype='float32')
pbv = layers.data(name='prior_box_var', shape=[image_boxs_num, 4],
append_batch_size=False, dtype='float32')
在上述例子中:
loc = layers.data(name='target_box', shape=[batch_size, bounding_bboxes_num, 4],
append_batch_size=False, dtype='float32')
scores = layers.data(name='scores', shape=[batch_size, bounding_bboxes_num, image_boxs_num],
append_batch_size=False, dtype='float32')
nmsed_outs = fluid.layers.detection_output(scores=scores,
loc=loc, prior_box=pb, prior_box_var=pbv)
gt_box = fluid.layers.data(name="gt_box", shape=[batch_size, 4], dtype="float32")
gt_label = fluid.layers.data(name="gt_label", shape=[batch_size, 1], dtype="float32")
difficult = fluid.layers.data(name="difficult", shape=[batch_size, 1], dtype="float32")
exe = fluid.Executor(fluid.CUDAPlace(0))
map_evaluator = fluid.metrics.DetectionMAP(nmsed_outs, gt_label, gt_box, difficult, class_num = 3)
cur_map, accum_map = map_evaluator.get_map_var()
# 更详细的例子请参见
# https://github.com/PaddlePaddle/models/blob/43cdafbb97e52e6d93cc5bbdc6e7486f27665fc8/PaddleCV/object_detection
"cur_map_v" 是当前 mini-batch 的 mAP
"accum_map_v" 是一个 pass 的 mAP累加和
.. py:method:: get_map_var()
......@@ -263,8 +326,11 @@ EditDistance
.. py:class:: paddle.fluid.metrics.EditDistance(name)
编辑距离是通过计算将一个字符串转换为另一个字符串所需的最小操作数来量化两个字符串(例如单词)之间的差异的一种方法。参考 https://en.wikipedia.org/wiki/Edit_distance
从mini batch中累计编辑距离和序列号,计算所有batch的平均编辑距离和实例错误。
编辑距离是通过计算将一个字符串转换为另一个字符串所需的最小编辑操作数(添加、删除或替换)来量化两个字符串(例如单词)彼此不相似的程度一种方法。
参考 https://en.wikipedia.org/wiki/Edit_distance。
此EditDistance类使用更新函数获取两个输入:
1. distance:一个形状为(batch_size, 1)的numpy.array,每个元素表示两个序列之间的编辑距离;
2. seq_num:一个整型/浮点型数,代表序列对的数目,并返回多个序列对的整体编辑距离。
参数:
- **name** - 度量标准名称
......@@ -273,18 +339,54 @@ EditDistance
.. code-block:: python
distances, seq_num = fluid.layers.edit_distance(input, label)
distance_evaluator = fluid.metrics.EditDistance()
for epoch in PASS_NUM:
distance_evaluator.reset()
for data in batches:
loss = exe.run(fetch_list=[cost] + list(edit_distance_metrics))
distance_evaluator.update(distances, seq_num)
import numpy as np
# 假设batch_size为128
batch_size = 128
# 初始化编辑距离管理器
distances_evaluator = fluid.metrics.EditDistance("EditDistance")
# 生成128个序列对间的编辑距离,此处的最大距离是10
edit_distances_batch0 = np.random.randint(low = 0, high = 10, size = (batch_size, 1))
seq_num_batch0 = batch_size
distance_evaluator.update(edit_distances_batch0, seq_num_batch0)
distance, instance_error = distance_evaluator.eval()
avg_distance, wrong_instance_ratio = distance_evaluator.eval()
print("the average edit distance for batch0 is %.2f and the wrong instance ratio is %.2f " % (avg_distance, wrong_instance_ratio))
edit_distances_batch1 = np.random.randint(low = 0, high = 10, size = (batch_size, 1))
seq_num_batch1 = batch_size
distance_evaluator.update(edit_distances_batch1, seq_num_batch1)
avg_distance, wrong_instance_ratio = distance_evaluator.eval()
print("the average edit distance for batch0 and batch1 is %.2f and the wrong instance ratio is %.2f " % (avg_distance, wrong_instance_ratio))
.. py:method:: distance_evaluator.reset()
.. code-block:: python
edit_distances_batch2 = np.random.randint(low = 0, high = 10, size = (batch_size, 1))
seq_num_batch2 = batch_size
distance_evaluator.update(edit_distances_batch2, seq_num_batch2)
avg_distance, wrong_instance_ratio = distance_evaluator.eval()
print("the average edit distance for batch2 is %.2f and the wrong instance ratio is %.2f " % (avg_distance, wrong_instance_ratio))
.. py:method:: update(distances, seq_num)
更新整体的编辑距离。
参数:
- **distances** – 一个形状为(batch_size, 1)的numpy.array,每个元素代表两个序列间的距离。(edit) –
- **seq_num** – 一个整型/浮点型值,代表序列对的数量。
在上面的例子中:'distance'是一个pass中的编辑距离的平均值。 'instance_error'是一个pass中的实例的错误率。
.. py:method:: eval()
返回两个浮点数:
avg_distance:使用更新函数更新的所有序列对的平均距离。
avg_instance_error:编辑距离不为零的序列对的比例。
......@@ -310,8 +412,6 @@ MetricBase
获取度量(metric)状态和当前状态。状态(state)包含没有 ``_`` 前缀的成员。
参数:**None**
返回:metric对应到state的字典
返回类型:字典(dict)
......@@ -346,27 +446,36 @@ Precision
.. py:class:: paddle.fluid.metrics.Precision(name=None)
Precision(也称为 positive predictive value,正预测值)是被预测为正样例中实际为正的比例。https://en.wikipedia.org/wiki/Evaluation_of_binary_classifiers
注:二分类中,Precision与Accuracy不同,
Precision(也称为 positive predictive value,正预测值)是被预测为正样例中实际为正的比例。
https://en.wikipedia.org/wiki/Evaluation_of_binary_classifiers
该类管理二分类任务的precision分数。
.. math::
Accuracy & = \frac{true \quad positive}{total \quad instances(所有样例)} \\\\
Precision & = \frac{true \quad positive}{all \quad positive \quad instances(所有正样例)}
**代码示例**
.. code-block:: python
import numpy as np
metric = fluid.metrics.Precision()
for pass in range(PASSES):
metric.reset()
for data in train_reader():
loss, preds, labels = exe.run(fetch_list=[cost, preds, labels])
# 生成预测值和标签
preds = [[0.1], [0.7], [0.8], [0.9], [0.2],
[0.2], [0.3], [0.5], [0.8], [0.6]]
labels = [[0], [1], [1], [1], [1],
[0], [0], [0], [0], [0]]
preds = np.array(preds)
labels = np.array(labels)
metric.update(preds=preds, labels=labels)
numpy_precision = metric.eval()
print("expct precision: %.2f and got %.2f" % ( 3.0 / 5.0, numpy_precision))
......@@ -384,18 +493,29 @@ Recall
https://en.wikipedia.org/wiki/Precision_and_recall
该类管理二分类任务的召回率。
**代码示例**
.. code-block:: python
import numpy as np
metric = fluid.metrics.Recall()
# 生成预测值和标签
preds = [[0.1], [0.7], [0.8], [0.9], [0.2],
[0.2], [0.3], [0.5], [0.8], [0.6]]
labels = [[0], [1], [1], [1], [1],
[0], [0], [0], [0], [0]]
preds = np.array(preds)
labels = np.array(labels)
for pass in range(PASSES):
metric.reset()
for data in train_reader():
loss, preds, labels = exe.run(fetch_list=[cost, preds, labels])
metric.update(preds=preds, labels=labels)
numpy_recall = metric.eval()
numpy_precision = metric.eval()
print("expct precision: %.2f and got %.2f" % ( 3.0 / 4.0, numpy_precision))
......
......@@ -30,8 +30,10 @@ he Gated Linear Units(GLU)由切分(split),sigmoid激活函数和按元素
.. code-block:: python
data = fluid.layers.data(name="words", shape=[3, 6, 9], dtype="float32")
output = fluid.nets.glu(input=data, dim=1) # shape of output: [3, 3, 9]
data = fluid.layers.data(
name="words", shape=[-1, 6, 3, 9], dtype="float32")
# 输出的形状为[-1, 3, 3, 9]
output = fluid.nets.glu(input=data, dim=1)
......@@ -74,7 +76,6 @@ Image Convolution Group由Convolution2d,BatchNorm,DropOut和Pool2d组成。
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
conv_pool = fluid.nets.img_conv_group(input=img,
num_channels=3,
conv_padding=1,
conv_num_filter=[3, 3],
conv_filter_size=3,
......@@ -128,6 +129,8 @@ attention运算机制可以被视为将查询和一组键值对映射到输出
.. code-block:: python
import paddle.fluid as fluid
queries = fluid.layers.data(name="queries",
shape=[3, 5, 9],
dtype="float32",
......@@ -181,10 +184,11 @@ sequence_conv_pool由序列卷积和池化组成
.. code-block:: python
input_dim = len(word_dict)
import paddle.fluid as fluid
input_dim = 100 #len(word_dict)
emb_dim = 128
hid_dim = 512
data = fluid.layers.data( ame="words", shape=[1], dtype="int64", lod_level=1)
data = fluid.layers.data( name="words", shape=[1], dtype="int64", lod_level=1)
emb = fluid.layers.embedding(input=data, size=[input_dim, emb_dim], is_sparse=True)
seq_conv = fluid.nets.sequence_conv_pool(input=emb,
num_filters=hid_dim,
......@@ -234,6 +238,7 @@ simple_img_conv_pool
.. code-block:: python
import paddle.fluid as fluid
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
conv_pool = fluid.nets.simple_img_conv_pool(input=img,
filter_size=5,
......
此差异已折叠。
......@@ -31,6 +31,7 @@ CUDA分析器。通过CUDA运行时应用程序编程接口对CUDA程序进行
import paddle.fluid as fluid
import paddle.fluid.profiler as profiler
import numpy as np
epoc = 8
dshape = [4, 3, 28, 28]
......@@ -82,15 +83,21 @@ profile interface 。与cuda_profiler不同,此profiler可用于分析CPU和GP
.. code-block:: python
import paddle.fluid.profiler as profiler
import numpy as np
with profiler.profiler('All', 'total', '/tmp/profile') as prof:
for pass_id in range(pass_num):
for batch_id, data in enumerate(train_reader()):
exe.run(fluid.default_main_program(),
feed=feeder.feed(data),
fetch_list=[],
use_program_cache=True)
# ...
epoc = 8
dshape = [4, 3, 28, 28]
data = fluid.layers.data(name='data', shape=[3, 28, 28], dtype='float32')
conv = fluid.layers.conv2d(data, 20, 3, stride=[1, 1], padding=[1, 1])
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
with profiler.profiler('CPU', 'total', '/tmp/profile') as prof:
for i in range(epoc):
input = np.random.random(dshape).astype('float32')
exe.run(fluid.default_main_program(), feed={'data': input})
......@@ -112,7 +119,7 @@ reset_profiler
.. code-block:: python
import paddle.fluid.profiler as profiler
with profiler.profiler(state, 'total', '/tmp/profile'):
with profiler.profiler('CPU', 'total', '/tmp/profile'):
for iter in range(10):
if iter == 2:
profiler.reset_profiler()
......
#######################
fluid.recordio_writer
#######################
.. _cn_api_fluid_recordio_writer_convert_reader_to_recordio_file:
convert_reader_to_recordio_file
-------------------------------
.. py:function:: paddle.fluid.recordio_writer.convert_reader_to_recordio_file(filename, reader_creator, feeder, compressor=Compressor.Snappy, max_num_records=1000, feed_order=None)
将 Python reader 转换为recordio文件
**代码示例:**
.. code-block:: python
import paddle.fluid as fluid
import paddle.dataset.mnist as mnist
import paddle
tmp_program = fluid.Program()
with fluid.program_guard(tmp_program):
img = fluid.layers.data(name='img', shape=[784])
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
feeder = fluid.DataFeeder(feed_list=[img, label], place=fluid.CPUPlace())
# mnist.recordio 会在当前目录生成
fluid.recordio_writer.convert_reader_to_recordio_file(
filename="mnist.recordio",
reader_creator=paddle.batch(mnist.train(), batch_size=32),
feeder=feeder)
参数:
- **filename** (str) - recordio文件名
- **reader_creator** (callable) - Python reader的创造器。可参考 :ref:`api_guide_python_reader`
- **feeder** (DataFeeder) - 数据处理实例。用于将 :code:`reader_creator` 转换为 :code:`lod_tensor`
- **compressor** – 必须在 :code:`fluid.core.RecordIOWriter.Compressor.Snappy` 或 :code:` fluid.core.RecordIOWriter.Compressor.NoCompress` 中, 默认情况下使用 :code:`Snappy`
- **max_num_records** (int) – 一个 chuck 中 records 的最大数量。每个 records 都是 reader 函数的返回值
- **feed_order** (list) - reader 返回的变量名的顺序
返回: 保存的 record 的数目
返回类型: int
.. _cn_api_fluid_recordio_writer_convert_reader_to_recordio_files:
convert_reader_to_recordio_files
-------------------------------
.. py:function:: paddle.fluid.recordio_writer.convert_reader_to_recordio_files(filename, batch_per_file, reader_creator, feeder, compressor=Compressor.Snappy, max_num_records=1000, feed_order=None)
该函数可以将一个python驱动的reader(数据读取器)转变为多个recodio文件。
该API实现的功能和 ``convert_reader_to_recordio_file`` 基本相同,只不过本函数会生成多个recordio文件。
每个文件最多存储 ``batch_per_file`` 条记录。
请参照 :ref:`cn_api_fluid_recordio_writer_convert_reader_to_recordio_file` 获取更详细的介绍。
......@@ -35,11 +35,21 @@ L1正则将会稀疏化权重矩阵。
.. code-block:: python
ioptimizer = fluid.optimizer.Adagrad(
import paddle.fluid as fluid
main_prog = fluid.Program()
startup_prog = fluid.Program()
with fluid.program_guard(main_prog, startup_prog):
data = fluid.layers.data(name='image', shape=[3, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
hidden = fluid.layers.fc(input=data, size=128, act='relu')
prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label)
avg_loss = fluid.layers.mean(loss)
optimizer = fluid.optimizer.Adagrad(
learning_rate=1e-4,
regularization=fluid.regularizer.L1DecayRegularizer(
regularization_coeff=0.1))
optimizer.minimize(avg_cost)
optimizer.minimize(avg_loss)
......@@ -82,11 +92,21 @@ L2DecayRegularizer
.. code-block:: python
import paddle.fluid as fluid
main_prog = fluid.Program()
startup_prog = fluid.Program()
with fluid.program_guard(main_prog, startup_prog):
data = fluid.layers.data(name='image', shape=[3, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
hidden = fluid.layers.fc(input=data, size=128, act='relu')
prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
loss = fluid.layers.cross_entropy(input=prediction, label=label)
avg_loss = fluid.layers.mean(loss)
optimizer = fluid.optimizer.Adagrad(
learning_rate=1e-4,
regularization=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=0.1))
optimizer.minimize(avg_cost)
optimizer.minimize(avg_loss)
......
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册