提交 f5ade81b 编写于 作者: Z zq19 提交者: xsrobin

Synchronize api cn develop 0823 (#1126)

* modify fluid.rst and layers.rst and gen_doc.py

* synchronize api_cn of develop 0823,test=document_preview

* update io_cn.rst

* delete ComposeNotAligned_cn.rst
上级 2da0e56d
.. _cn_api_fluid_is_compiled_with_cuda:
is_compiled_with_cuda
-------------------------------
.. py:function:: paddle.fluid.is_compiled_with_cuda()
检查 ``whl`` 包是否可以被用来在GPU上运行模型
返回:支持gpu则为True,否则为False。
返回类型:out(boolean)
**示例代码**
.. code-block:: python
import paddle.fluid as fluid
support_gpu = fluid.is_compiled_with_cuda()
......@@ -9,13 +9,25 @@ fluid.io
.. toctree::
:maxdepth: 1
io_cn/batch_cn.rst
io_cn/buffered_cn.rst
io_cn/cache_cn.rst
io_cn/chain_cn.rst
io_cn/compose_cn.rst
io_cn/Fake_cn.rst
io_cn/firstn_cn.rst
io_cn/load_inference_model_cn.rst
io_cn/load_params_cn.rst
io_cn/load_persistables_cn.rst
io_cn/load_vars_cn.rst
io_cn/map_readers_cn.rst
io_cn/multiprocess_reader_cn.rst
io_cn/PipeReader_cn.rst
io_cn/PyReader_cn.rst
io_cn/save_inference_model_cn.rst
io_cn/save_params_cn.rst
io_cn/save_persistables_cn.rst
io_cn/save_vars_cn.rst
io_cn/shuffle_cn.rst
io_cn/xmap_readers_cn.rst
.. _cn_api_fluid_io_Fake:
Fake
-------------------------------
.. py:class:: paddle.fluid.io.Fake
Fakereader将缓存它读取的第一个数据,并将其输出data_num次。它用于缓存来自真实reader的数据,并将其用于速度测试。
参数:
- **reader** – 原始读取器。
- **data_num** – reader产生数据的次数 。
返回: 一个Fake读取器
**代码示例**
.. code-block:: python
def reader():
for i in range(10):
yield i
fake_reader = Fake()(reader, 100)
\ No newline at end of file
.. _cn_api_fluid_io_PipeReader:
PipeReader
-------------------------------
.. py:class:: paddle.fluid.io.PipeReader
PipeReader从命令中通过流来读取数据,把数据存在一个pipe缓存中,并重定向到解析器中解析,返回预先设计格式的数据。
你可以使用标准的Linux命令或者调用其他程序来读取数据,从HDFS, Ceph, URL, AWS S3等等。
.. code-block:: python
cmd = "hadoop fs -cat /path/to/some/file"
cmd = "cat sample_file.tar.gz"
cmd = "curl http://someurl"
cmd = "python print_s3_bucket.py"
**代码示例**
.. code-block:: python
def example_reader():
for f in myfiles:
pr = PipeReader("cat %s"%f)
for l in pr.get_line():
sample = l.split(" ")
yield sample
.. py:method:: get_line(cut_lines=True,line_break='\n')
参数:
- **cut_lines** (bool) - 给行分割缓存
- **line_break** (string) - 行分隔符,比如'\n'或者'\r'
返回: 行或者字节缓存
.. _cn_api_fluid_io_batch:
batch
-------------------------------
.. py:function:: paddle.fluid.io.batch(reader, batch_size, drop_last=False)
该层是一个batched reader。
参数:
- **reader** (Variable)-读取数据的数据reader
- **batch_size** (int)-批尺寸
- **drop_last** (bool) - 如果最后一个batch不等于batch_size,则drop最后一个batch。
返回:batched reader
返回类型:callable
.. _cn_api_fluid_io_buffered:
buffered
-------------------------------
.. py:function:: paddle.fluid.io.buffered(reader, size)
创建一个缓存数据读取器,它读取数据并且存储进缓存区,从缓存区读取数据将会加速,只要缓存不是空的。
参数:
- **reader** (callable) – 读取数据的reader
- **size** (int) – 最大buffer的大小
返回:缓存的reader(读取器)
\ No newline at end of file
.. _cn_api_fluid_io_cache:
cache
-------------------------------
.. py:function:: paddle.fluid.io.cache(reader)
缓存reader数据到内存中,小心此方法可能会花长时间来处理数据,并且会占用大量内存。 ``reader()`` 只能被调用一次。
参数:
- **reader** (callable) – 读取数据的reader,每次都会yields数据。
返回:每次都会从内存中yields数据的一个装饰reader。
返回类型:生成器
\ No newline at end of file
.. _cn_api_fluid_io_chain:
chain
-------------------------------
.. py:function:: paddle.fluid.io.chain(*readers)
创建一个数据读取器,输出为输入数据读取器链接到一起的结果,如果输入如下:
[0, 0, 0]
[1, 1, 1]
[2, 2, 2]
输出将会为[0, 0, 0, 1, 1, 1, 2, 2, 2].
参数:
- **readers** – 输入reader
返回:新的数据reader。
返回类型:callable
\ No newline at end of file
.. _cn_api_fluid_io_compose:
composs
-------------------------------
.. py:function:: paddle.fluid.io.compose(*readers, **kwargs)
创建一个数据读取器,输出为输入数据读取器组合到一起的结果,如果输入如下:
(1,2) 3 (4,5)
输出将会为(1,2,3,4,5)。
参数:
- **readers** – 要组合的输入reader
- **check_alignment** (bool) - 若为True,将会检查输入readers是否正确的对准,若为False,将不会检查是否对准并且不会跟踪输出,默认为True。
返回:新的数据reader。
Raises:ComposeNotAligned - 输出readers没有对齐,当check_alignment设置为False时将不会raise。
\ No newline at end of file
.. _cn_api_fluid_io_firstn:
firstn
-------------------------------
.. py:function:: paddle.fluid.io.firstn(reader, n)
限制reader可以返回的最大样本数。
参数:
- **reader** (callable) – 要读取的数据读取器。
- **n** (int) – 返回的最大样本数 。
返回: 装饰reader
返回类型: callable
\ No newline at end of file
.. _cn_api_fluid_io_map_readers:
map_readers
-------------------------------
.. py:function:: paddle.fluid.io.map_readers(func, *readers)
创建使用每个数据读取器的输出作为参数输出函数返回值的数据读取器。
参数:
- **func** - 使用的函数. 函数类型应为(Sample) => Sample
- **readers** - 其输出将用作func参数的reader。
类型:callable
返回: 被创建数据的读取器
.. _cn_api_fluid_io_multiprocess_reader:
multiprocess_reader
-------------------------------
.. py:function:: paddle.fluid.io.multiprocess_reader(readers, use_pipe=True, queue_size=1000)
多进程reader使用python多进程从reader中读取数据,然后使用multi process.queue或multi process.pipe合并所有数据。进程号等于输入reader的编号,每个进程调用一个reader。
multiprocess.queue需要/dev/shm的rw访问权限,某些平台不支持。
您需要首先创建多个reader,这些reader应该相互独立,这样每个进程都可以独立工作。
**代码示例**
.. code-block:: python
reader0 = reader(["file01", "file02"])
reader1 = reader(["file11", "file12"])
reader1 = reader(["file21", "file22"])
reader = multiprocess_reader([reader0, reader1, reader2],
queue_size=100, use_pipe=False)
\ No newline at end of file
.. _cn_api_fluid_io_shuffle:
shuffle
-------------------------------
.. py:function:: paddle.fluid.io.shuffle(reader, buffer_size)
创建一个特殊的数据读取器,它的输出数据会被重洗(shuffle)。由原始读取器创建的迭代器得到的输出将会被暂存到shuffle缓存区,其后
会对其进行重洗运算。shuffle缓存区的大小由参数 ``buffer_size`` 决定。
参数:
- **reader** (callable) – 输出会被shuffle的原始reader
- **buffer_size** (int) – 进行shuffle的buffer的大小
返回:其输出会被shuffle的一个reader(读取器)
返回类型:callable
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
raw_reader = fluid.layers.io.open_files(filenames=['./data1.recordio',
'./data2.recordio'],
shapes=[(3,224,224), (1,)],
lod_levels=[0, 0],
dtypes=['float32', 'int64'],
thread_num=2,
buffer_size=2)
batch_reader = fluid.layers.batch(reader=raw_reader, batch_size=5)
shuffle_reader = fluid.io.shuffle(reader=batch_reader, buffer_size=5000)
.. _cn_api_fluid_io_xmap_readers:
xmap_readers
-------------------------------
.. py:function:: paddle.fluid.io.xmap_readers(mapper, reader, process_num, buffer_size, order=False)
通过多线程方式,通过用户自定义的映射器mapper来映射reader返回的样本(到输出队列)。
参数:
- **mapper** (callable) - 一种映射reader数据的函数。
- **reader** (callable) - 产生数据的reader。
- **process_num** (int) - 用于处理样本的线程数目。
- **buffer_size** (int) - 存有待读取数据的队列的大小。
- **order** (bool) - 是否保持原始reader的数据顺序。 默认为False。
返回:一个将原数据进行映射后的decorated reader。
返回类型: callable
\ No newline at end of file
.. _cn_api_fluid_layers_Normal:
Normal
-------------------------------
.. py:class:: paddle.fluid.layers.Normal(loc, scale)
正态分布
数学公式:
.. math::
pdf(x; \mu, \sigma) = \frac{1}{Z}e^{\frac {-0.5 (x - \mu)^2} {\sigma^2} }
Z = (2 \pi \sigma^2)^{0.5}
上面的数学公式中:
:math:`loc = \mu` : 平均值。
:math:`scale = \sigma` : 标准差。
:math:`Z`: 正态分布常量。
参数:
- **loc** (float|list|numpy.ndarray|Variable) - 正态分布平均值。
- **scale** (float|list|numpy.ndarray|Variable) - 正态分布标准差。
**代码示例**:
.. code-block:: python
from paddle.fluid import layers
from paddle.fluid.layers import Normal
# 定义单个数的正态分布。
dist = Normal(loc=0., scale=3.)
# 定义一组有两个数的正态分布。
# 第一组为均值1,标准差11,第二组为均值2,标准差22。
dist = Normal(loc=[1, 2.], scale=[11, 22.])
# 得到3个样本, 返回一个 3 x 2 向量。
dist.sample([3])
# 定义一个为两个参数的正态分布。
# 均值都是1,标准差不同。
dist = Normal(loc=1., scale=[11, 22.])
# 输入变量
dims = 3
loc = layers.data(name='loc', shape=[dims], dtype='float32')
scale = layers.data(name='scale', shape=[dims], dtype='float32')
other_loc = layers.data(
name='other_loc', shape=[dims], dtype='float32')
other_scale = layers.data(
name='other_scale', shape=[dims], dtype='float32')
values = layers.data(name='values', shape=[dims], dtype='float32')
normal = Normal(loc, scale)
other_normal = Normal(other_loc, other_scale)
sample = normal.sample([2, 3])
entropy = normal.entropy()
lp = normal.log_prob(values)
kl = normal.kl_divergence(other_normal)
.. py:function:: sample(shape, seed=0)
生成指定形状的样本
参数:
- **shape** (list) - int32的1维列表,指定生成样本的shape。
- **seed** (int) - 长整型数
返回:预备好维度shape的向量
返回类型:变量(Variable)
.. py:function:: entropy()
信息熵
返回:正态分布的信息熵
返回类型:变量(Variable)
.. py:function:: log_prob(value)
Log概率密度函数
参数:
- **value** (Variable) - 输入向量。
返回:log概率
返回类型:变量(Variable)
.. py:function:: kl_divergence(other)
两个正态分布之间的KL-divergence。
参数:
- **other** (Normal) - Normal实例。
返回:两个正态分布之间的KL-divergence
返回类型:变量(Variable)
.. _cn_api_fluid_layers_Uniform:
Uniform
-------------------------------
.. py:class:: paddle.fluid.layers.Uniform(low, high)
均匀分布
概率密度函数(pdf)为:
.. math::
pdf(x; a, b) = \frac{1}{Z}, a <=x < b
Z = b - a
上面的数学公式中:
:math:`low = a` 。
:math:`high = b` 。
:math:`Z`: 正态分布常量。
参数low和high应该为可支持广播的shape。
参数:
- **low** (float|list|numpy.ndarray|Variable) - 均匀分布的较低边界。
- **high** (float|list|numpy.ndarray|Variable) - 均匀分布的较高边界。
**代码示例**:
.. code-block:: python
from paddle.fluid import layers
from paddle.fluid.layers import Uniform
# 一个未广播的单独的均匀分布 [3, 4]:
u1 = Uniform(low=3.0, high=4.0)
# 两个分布 [1, 3], [2, 4]
u2 = Uniform(low=[1.0, 2.0],
high=[3.0, 4.0])
# 4个分布
u3 = Uniform(low=[[1.0, 2.0],
[3.0, 4.0]],
high=[[1.5, 2.5],
[3.5, 4.5]])
# 广播:
u4 = Uniform(low=3.0, high=[5.0, 6.0, 7.0])
# 作为输入的变量
dims = 3
low = layers.data(name='low', shape=[dims], dtype='float32')
high = layers.data(name='high', shape=[dims], dtype='float32')
values = layers.data(name='values', shape=[dims], dtype='float32')
uniform = Uniform(low, high)
sample = uniform.sample([2, 3])
entropy = uniform.entropy()
lp = uniform.log_prob(values)
.. py:function:: sample(shape, seed=0)
生成指定形状的样本
参数:
- **shape** (list) - int32的1维列表,指定生成样本的shape。
- **seed** (int) - 长整型数
返回:预备好维度shape的向量
返回类型:变量(Variable)
.. py:function:: entropy()
信息熵
返回:正态分布的信息熵
返回类型:变量(Variable)
.. py:function:: log_prob(value)
Log概率密度函数
参数:
- **value** (Variable) - 输入向量。
返回:log概率
返回类型:变量(Variable)
.. py:function:: kl_divergence(other)
两个正态分布之间的KL-divergence。
参数:
- **other** (Normal) - Normal实例。
返回:两个正态分布之间的KL-divergence
返回类型:变量(Variable)
.. _cn_api_fluid_layers_center_loss:
center_loss
-------------------------------
.. py:function:: paddle.fluid.layers.center_loss(input, label, num_classes, alpha, param_attr, update_center=True)
center_loss层
该层接收一个来自于最后一个隐藏层的输出和目标标签作为输入,返回损失值。
对于输入,\(X\)和标签\(Y\),计算公式为:
.. math::
out = \frac{1}{2}(X - Y)^2
参数:
- **input** (Variable) - 形为[N x M]的2维张量
- **label** (Variable) - groud truth,一个形为[N x 1]的2维张量,N表示batch size
- **num_class** (int) - 表示类别的数
- **alpha** (float|Variable) - 学习率
- **param_attr** (ParamAttr) - 参数初始化
- **update_center** (bool) - 是否更新损失值
返回:形为[N x 1]的2维张量。
返回类型:Variable
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
input = fluid.layers.data(name='x',shape=[20,30],dtype='float32')
label = fluid.layers.data(name='y',shape=[20,1],dtype='int64')
num_classes = 1000
alpha = 0.01
param_attr = fluid.initializer.Xavier(uniform=False)
center_loss=fluid.layers.center_loss(input=input,
label=label,
num_classes=1000,
alpha=alpha,
param_attr=fluid.initializer.Xavier(uniform=False),
update_center=True)
.. _cn_api_fluid_layers_eye:
eye
-------------------------------
.. py:function:: paddle.fluid.layers.eye(num_rows, num_columns=None, batch_shape=None, dtype='float32')
这个函数用来构建一个元素一样的向量,或一个向量batch。
参数:
- **num_rows** (int) - 每一个batch向量的行数。
- **num_columns** (int) - 每一个batch向量的列数,若为None,则等于num_rows。
- **batch_shape** (list(int)) - 如若提供,则返回向量将会有一个此shape的主要的batch size。
- **dtype** (string) - 'float32'|'int32'|...,返回向量的数据类型
返回:一个元素一样的向量,shape为batch_shape + [num_rows, num_columns]。
返回类型:变量(Variable)
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
data = fluid.layers.eye(3, dtype='int32')
# [[1, 0, 0]
# [0, 1, 0]
# [0, 0, 1]]
data = fluid.layers.eye(2, 3, dtype='int32')
# [[1, 0, 0]
# [0, 1, 0]]
data = fluid.layers.eye(2, batch_shape=[3])
# Construct a batch of 3 identity tensors, each 2 x 2.
# data[i, :, :] is a 2 x 2 identity tensor, i = 0, 1, 2.
.. _cn_api_fluid_layers_filter_by_instag:
filter_by_instag
-------------------------------
.. py:function:: paddle.fluid.layers.filter_by_instag(ins, ins_tag, filter_tag, is_lod)
此函数通过instag来过滤ins batch,大量属于同样的tags的样本,我们可以指定我们想要的一些tags,属于这些tags的样本将会被保留在输出中,其余的将会移除。比如,一个batch有4个样本,每个样本都有自己的tag表。
Ins | Ins_Tag |
|:—–:|:——:|
| 0 | 0, 1 |
| 1 | 1, 3 |
| 2 | 0, 3 |
| 3 | 2, 6 |
Lod为[1,1,1,1],filter tags为[1],从上面的定义中,带有标签[1]的样本将会通过filter,所以,样本0和1将会通过并且出现在输出中。准确来说,如果 ``is_lod`` 为false,它是一个等于值全为1的lod_tensor的普通的tensor,和上面的例子很相似。
参数:
- **ins** (Variable) - 输入变量(LoDTensor),通常为2D向量,第一个维度可以有lod info,也可以没有。
- **ins_tag** (Variable) - 输入变量(LoDTensor),通常为1维列表,通过lod info来分割。
- **filter_tag** (Variable) - 输入变量(1D Tensor/List),通常为持有tags的列表。
- **is_lod** (Bool) – 指定样本是否为lod tensor的布尔值。
返回:过滤之后的样本(LoDTensor)和 损失权重(Tensor)。
返回类型:变量(Variable)
**代码示例**:
.. code-block:: python
import paddle.fluid.layers as layers
ins = layers.data(name='Ins', shape=[-1,32], lod_level=0, dtype='float64')
ins_tag = layers.data(name='Ins_tag', shape=[-1,16], lod_level=0, dtype='int64')
filter_tag = layers.data(name='Filter_tag', shape=[-1,16], dtype='int64')
out, loss_weight = layers.filter_by_instag(ins, ins_tag, filter_tag, True)
.. _cn_api_fluid_layers_hard_swish:
hard_swish
-------------------------------
.. py:function:: paddle.fluid.layers.hard_swish(x, threshold=6.0, scale=6.0, offset=3.0, name=None)
hard_swish激活函数,swish的hard version(https://arxiv.org/pdf/1905.02244.pdf)。
:math:`out = \frac{x * (min(max(0, x+offset), threshold))}{scale}`
`` threshold`` 和 ``scale`` 应该为正, ``offset`` 正负均可,默认参数如上,建议使用默认参数。
参数:
- **x** (Variable) - 要做激活操作的输入变量。
- **threshold** (float) - 做激活操作的threshold,默认为6.0。
- **scale** (float) - 激活操作的scale,默认为6.0。
- **offset** (float) - 激活操作的offset,默认为3.0。
- **name** (str|None) - 层名,设置为None,此层将被自动命名。
返回:与输入有着同样shape的输出变量
返回类型:变量(Variable)
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name="x", shape=[3,10,32,32], dtype="float32")
y = fluid.layers.hard_swish(x)
.. _cn_api_fluid_layers_lod_append:
lod_append
-------------------------------
.. py:function:: paddle.fluid.layers.lod_append(x, level)
给 ``x`` 的LoD添加 ``level`` 。
简单示例:
.. code-block:: python
give a 1-level LodTensor x:
x.lod = [[2, 3, 1]]
x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
x.dims = [6, 1]
level:[1, 1, 1, 1, 1, 1]
Then we get a 2-level LodTensor:
x.lod = [[2, 3, 1], [1, 1, 1, 1, 1, 1]
x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
x.dims = [6, 1]
参数:
- **x** (Variable)-输入变量,可以是LoDTensor或tensor。
- **level** (list|tuple|Variable)-预添加到x的LoD里的LoD level。
返回:一个有着新的LoD level的输出变量
返回类型:Variable
Raise: ``ValueError`` - 如果y为None或者level不可迭代。
**代码示例:**
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[6, 10], lod_level=1)
out = fluid.layers.lod_append(x, [1,1,1,1,1,1])
.. _cn_api_fluid_layers_match_matrix_tensor:
match_matrix_tensor
-------------------------------
.. py:function:: paddle.fluid.layers.match_matrix_tensor(x, y, channel_num, act=None, param_attr=None, dtype='float32', name=None)
计算两个长度可变词序列的语义匹配矩阵,给一个长度为n的问题A,和一个长度为m的标题B,输入形状为[n, h]和[m, h],h为hidden_size。如果channel_num设置为3,将会生成一个形为[h, 3, h]的参数可学习的矩阵W。接着语义匹配矩阵将会通过A * W * B.T = [n, h]*[h, 3, h]*[h, m] = [n, 3, m]来计算A和B。可学习参数矩阵W在这个过程中相当于一个全链接层。如果提供了激活函数,相关激活函数将会被用到输出中。x和y应当为LodTensor并且仅支持一个level LoD。
给一个1-level LoDTensor x:
x.lod = [[2, 3, ]]
x.data = [[0.3, 0.1], [0.2, 0.3], [0.5, 0.6], [0.7, 0.1], [0.3, 0.4]]
x.dims = [5, 2]
y是一个Tensor:
y.lod = [[3, 1, ]]
y.data = [[0.1, 0.2], [0.3, 0.7], [0.9, 0.2], [0.4, 0.1]]
y.dims = [4, 2]
channel_num设为2,我们就可以得到一个 1-level LoDTensor:
out.lod = [[12, 6]] # where 12 = channel_num * x.lod[0][0] * y.lod[0][0]
out.dims = [18, 1] # where 18 = 12 + 6
参数:
- **x** (Variable) - 1-level的输入LoDTensor。
- **y** (Variable) - 1-level的输入LoDTensor。
- **channel_num** (int) - 可学习参数W的通道数。
- **act** (str,默认为None) - 激活函数。
- **param_attr** (ParamAttr|ParamAttr的列表,默认为None) - 此层可学习参数的属性。
- **dtype** ('float32') - w数据的数据类型。
- **name** (str|None) - 层名,若为None,则自动设置。
返回:由此层指定LoD的输出
返回类型:变量(Variable)
**代码示例**:
.. code-block:: python
import numpy as np
from paddle.fluid import layers
x_lod_tensor = layers.data(name='x', shape=[10], lod_level=1)
y_lod_tensor = layers.data(name='y', shape=[10], lod_level=1)
out, out_tmp = layers.match_matrix_tensor(x=x_lod_tensor, y=y_lod_tensor, channel_num=3)
.. _cn_api_fluid_layers_ones_like:
ones_like
-------------------------------
.. py:function:: paddle.fluid.layers.ones_like(x, out=None)
ones_like
该功能创建一个形状与类型与x相似的张量,初始值为1。
参数:
- **x** (Variable) - 指定形状与数据类型的输入张量
- **out** (Variable)-输出张量
返回:输出张量
返回类型:变量(Variable)
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name='x', dtype='float32', shape=[3], append_batch_size=False)
data = fluid.layers.ones_like(x) # [1.0, 1.0, 1.0]
.. _cn_api_fluid_layers_resize_trilinear:
resize_trilinear
-------------------------------
.. py:function:: paddle.fluid.layers.resize_trilinear(input, out_shape=None, scale=None, name=None, actual_shape=None, align_corners=True, align_mode=1)
该层对输入进行放缩,基于给定的由 ``actual_shape`` , ``out_shape`` , ``scale`` 确定的输出shape,进行三线插值。三线插值是包含三个参数的线性插值方程(D方向,H方向, W方向),在一个3D格子上进行三个方向的线性插值。更多细节,请参考维基百科:https://en.wikipedia.org/wiki/Trilinear_interpolation
Align_corners和align_mode都是可选参数,可以用来设置插值的计算方法,如下:
::
Example:
For scale:
if align_corners = True && out_size > 1 :
scale_factor = (in_size-1.0)/(out_size-1.0)
else:
scale_factor = float(in_size/out_size)
Bilinear interpolation:
if:
align_corners = False , align_mode = 0
input : (N,C,D_in,H_in,W_in)
output: (N,C,D_out,H_out,W_out) where:
D_out = (D_{in}+0.5) * scale_{factor} - 0.5
H_out = (H_{in}+0.5) * scale_{factor} - 0.5
W_out = (W_{in}+0.5) * scale_{factor} - 0.5
else:
input : (N,C,D_in,H_in,W_in)
output: (N,C,D_out,H_out,W_out) where:
D_out = D_{in} * scale_{factor}
H_out = H_{in} * scale_{factor}
W_out = W_{in} * scale_{factor}
参数:
- **input** (Variable) – 输入的四维张量
- **out_shape** (list|tuple|Variable|None) – 调整最近邻层的输出形状,形式为(out_h, out_w)。默认值:None。
- **scale** (float|None) – 输入高、宽的乘法器。 ``out_shape`` 和 ``scale`` 二者至少设置其一。 ``out_shape`` 具有比 ``scale`` 更高的优先级。 默认: None
- **name** (str|None) – 输出变量的命名
- **actual_shape** (Variable) – 可选输入, 动态设置输出张量的形状。 如果提供该值, 图片放缩会依据此形状进行, 而非依据 ``out_shape`` 和 ``scale`` 。 即为, ``actual_shape`` 具有最高的优先级。 如果想动态指明输出形状,推荐使用 ``actual_shape`` 取代 ``out_shape`` 。 当使用 ``actual_shape`` 来指明输出形状, ``out_shape`` 和 ``scale`` 也应该进行设置, 否则在图形生成阶段将会报错。默认: None
- **align_corners** (bool)- 一个可选的bool型参数,如果为True,则将输入和输出张量的4个角落像素的中心对齐,并保留角点像素的值。 默认值:True
- **align_mode** (bool) - (int,默认为'1'),双线性插值选项,src_idx = scale*(dst_index+0.5)-0.5时取'0',src_idx = scale*dst_index时取'1'。
返回:形为(num_batches, channels, out_d, out_h, out_w)的5-D张量
**代码示例**
.. code-block:: python
import paddle.fluid as fluid
input = fluid.layers.data(name="input", shape=[3,6,9,11], dtype="float32")
out = fluid.layers.resize_trilinear(input, out_shape=[12, 12, 12])
.. _cn_api_fluid_layers_size:
size
-------------------------------
.. py:function:: paddle.fluid.layers.size(input)
返回张量的单元数量,是一个shape为[1]的int64的张量。
参数:
- **input** (Variable)- 输入变量
返回:(Variable)。
**代码示例**:
.. code-block:: python
import paddle.fluid.layers as layers
input = layers.data(
name="input", shape=[3, 100], dtype="float32", append_batch_size=False)
rank = layers.size(input) # 300
.. _cn_api_fluid_layers_unfold:
unfold
-------------------------------
.. py:function:: paddle.fluid.layers.unfold(x, kernel_size, strides=1, paddings=0, dilation=1, name=None)
该函数会将在输入 ``x`` 上滑动的filter block转换为一列缓存数据。对于每一个卷积过滤器下的block,下面的元素都会被重新排成一列,当滑动的卷积过滤器走过整个特征图时,将会形成一系列的列。
对于每一个输入形为[N, C, H, W]的 ``x`` ,都将会按照下面公式计算出一个形为[N, Cout, Lout]的输出。
.. math::
dkernel[0] &= dilations[0] * (kernel\_sizes[0] - 1) + 1
dkernel[1] &= dilations[1] * (kernel\_sizes[1] - 1) + 1
hout &= \frac{H + paddings[0] + paddings[2] - dkernel[0]}{strides[0]} + 1
wout &= \frac{W + paddings[1] + paddings[3] - dkernel[1]}{strides[1]} + 1
Cout &= C * kernel\_sizes[0] * kernel\_sizes[1]
Lout &= hout * wout
参数:
- **x** (Variable) – 格式为[N, C, H, W]的输入张量
- **kernel_size** (int|list) – 卷积核的尺寸,应该为[k_h, k_w],或者为一个整型k,处理为[k, k]
- **strides** (int|list) – 卷积步长,应该为[stride_h, stride_w],或者为一个整型stride,处理为[stride, stride],默认为[1, 1]
- **paddings** (int|list) – 每个维度的扩展, 应该为[padding_top, padding_left,padding_bottom, padding_right]或者[padding_h, padding_w]或者一个整型padding。如果给了[padding_h, padding_w],则应该被扩展为[padding_h, padding_w, padding_h, padding_w]. 如果给了一个整型的padding,则会使用[padding, padding, padding, padding],默认为[0, 0, 0, 0]
- **dilations** (int|list) – 卷积膨胀,应当为[dilation_h, dilation_w],或者一个整型的dilation处理为[dilation, dilation]。默认为[1, 1]。
返回:
滑动block的输出张量,形状如上面所描述的[N, Cout, Lout],Cout每一个滑动block里面值的总数,Lout是滑动block的总数.
返回类型:(Variable)
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name = 'data', shape = [3, 224, 224], dtype = 'float32')
y = fluid.layers.unfold(x, [3, 3], 1, 1, 1)
.. _cn_api_fluid_layers_unique:
unique
-------------------------------
.. py:function:: paddle.fluid.layers.unique(x, dtype='int32')
unique为 ``x`` 返回一个unique张量和一个指向该unique张量的索引。
参数:
- **x** (Variable) - 一个1维输入张量
- **dtype** (np.dtype|core.VarDesc.VarType|str) – 索引张量的类型,int32,int64。
返回:元组(out, index)。 ``out`` 为 ``x`` 的指定dtype的unique张量, ``index`` 是一个指向 ``out`` 的索引张量, 用户可以通过该函数来转换原始的 ``x`` 张量的索引。
返回类型:元组(tuple)
**代码示例**:
.. code-block:: python
import numpy as np
import paddle.fluid as fluid
x = fluid.assign(np.array([2, 3, 3, 1, 5, 3], dtype='int32'))
out, index = fluid.layers.unique(x) # out is [2, 3, 1, 5]; index is [0, 1, 1, 2, 3, 1]
.. _cn_api_fluid_layers_unique_with_counts:
unique_with_counts
-------------------------------
.. py:function:: paddle.fluid.layers.unique_with_counts(x, dtype='int32')
unique_with_count为 ``x`` 返回一个unique张量和一个指向该unique张量的索引以及 ``x`` 中unique元素的数量。
参数:
- **x** (Variable) - 一个1维输入张量
- **dtype** (np.dtype|core.VarDesc.VarType|str) – 索引张量的类型,int32,int64。
返回:元组(out, index, count)。 ``out`` 为 ``x`` 的指定dtype的unique张量, ``index`` 是一个指向 ``out`` 的索引张量, 用户可以通过该函数来转换原始的 ``x`` 张量的索引, ``count`` 是 ``x`` 中unique元素的数量。
返回类型:元组(tuple)
**代码示例**:
.. code-block:: python
import numpy as np
import paddle.fluid as fluid
x = fluid.assign(np.array([2, 3, 3, 1, 5, 3], dtype='int32'))
out, index, count = fluid.layers.unique_with_counts(x) # out is [2, 3, 1, 5];
# index is [0, 1, 1, 2, 3, 1];
# count is [1, 3, 1, 1]
.. _cn_api_fluid_layers_var_conv_2d:
var_conv_2d
-------------------------------
.. py:function:: paddle.fluid.layers.var_conv_2d(input, row, col, input_channel, output_channel, filter_size, stride=1, param_attr=None, act=None, dtype='float32', name=None)
var_conv_2d层依据给定的参数来计算输出, ``input`` 、 ``row`` 和 ``col`` 都是1-level的 ``LodTensor`` 卷积操作与普通的conv2d卷积层一样,值得注意的是,输入数据的第二个维度即input.dim[1]应该为1。
如果 ``input_channel`` 是2,并且给了如下的row lodTensor 和 col lodTensor:
.. code-block:: text
row.lod = [[5, 4]]
col.lod = [[6, 7]]
输入是一个lodTensor:
input.lod = [[60, 56]] # where 60 = input_channel * 5 * 6
input.dims = [116, 1] # where 116 = 60 + 56
如果设置 output_channel 为3, filter_size 为 [3, 3], stride 为 [1, 1]:
output.lod = [[90, 84]] # where 90 = output_channel * [(5-1)/stride + 1] * [(6-1)/stride + 1]
output.dims = [174, 1] # where 174 = 90 + 84
参数:
- **input** (Variable) – dims[1]等于1的1-level的LodTensor。
- **row** (Variable) – 1-level的LodTensor提供height。
- **col** (Variable) – 1-level的LodTensor提供width。
- **input_channel** (int) – 输入通道的数目。
- **output_channel** (int) – 输出通道的数目。
- **filter_size** (int|tuple|None) – 过滤器尺寸。 如果是元组,则应当为两个整型数字(filter_size_H, filter_size_W)。否则,过滤器会变为正方形。
- **stride** (int|tuple) – 步长。 如果是元组,则应当为两个整型数字(stride_H, stride_W)。否则,stride_H = stride_W = stride。默认: stride = 1.
- **param_attr** (ParamAttr|None) – 为var_conv2d可学习的权重分配参数属性如果设置为None,或者ParamAttr的一个属性, var_conv2d将会创建ParamAttr做为param_attr。如果param_attr的初始化没有设定,参数将会以 \(Normal(0.0, std)\),进行初始化,\(std\) 为 \((\frac{2.0 }{filter\_elem\_num})^{0.5}\). 默认: None。
- **act** (str) – 激活类型,如果设置为None,则不会激活。默认:None
- **dtype** ('float32') – 输出与参数的数据类型
- **name** (str|None) – 层名。如果没有设置,将会被自动命名。默认: None。
返回: 由该层指定LoD的输出变量
返回类型: 变量(Variable)
**代码示例**:
.. code-block:: python
import numpy as np
from paddle.fluid import layers
x_lod_tensor = layers.data(name='x', shape=[1], lod_level=1)
row_lod_tensor = layers.data(name='row', shape=[6], lod_level=1)
col_lod_tensor = layers.data(name='col', shape=[6], lod_level=1)
out = layers.var_conv_2d(input=x_lod_tensor,
row=row_lod_tensor,
col=col_lod_tensor,
input_channel=3,
output_channel=5,
filter_size=[3, 3],
stride=1)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册