Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
c27554ac
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
c27554ac
编写于
11月 12, 2018
作者:
Q
Qiao Longfei
提交者:
GitHub
11月 12, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14336 from jacquesqiao/add_bilinear_tensor_product_layer
add bilinear_tensor_product layer
上级
4d546f60
4d6f7515
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
81 addition
and
0 deletion
+81
-0
paddle/fluid/API.spec
paddle/fluid/API.spec
+1
-0
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+70
-0
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+10
-0
未找到文件。
paddle/fluid/API.spec
浏览文件 @
c27554ac
...
...
@@ -184,6 +184,7 @@ paddle.fluid.layers.hash ArgSpec(args=['input', 'hash_size', 'num_hash', 'name']
paddle.fluid.layers.grid_sampler ArgSpec(args=['x', 'grid', 'name'], varargs=None, keywords=None, defaults=(None,))
paddle.fluid.layers.log_loss ArgSpec(args=['input', 'label', 'epsilon', 'name'], varargs=None, keywords=None, defaults=(0.0001, None))
paddle.fluid.layers.add_position_encoding ArgSpec(args=['input', 'alpha', 'beta', 'name'], varargs=None, keywords=None, defaults=(None,))
paddle.fluid.layers.bilinear_tensor_product ArgSpec(args=['x', 'y', 'size', 'act', 'name', 'param_attr', 'bias_attr'], varargs=None, keywords=None, defaults=(None, None, None, None))
paddle.fluid.layers.data ArgSpec(args=['name', 'shape', 'append_batch_size', 'dtype', 'lod_level', 'type', 'stop_gradient'], varargs=None, keywords=None, defaults=(True, 'float32', 0, VarType.LOD_TENSOR, True))
paddle.fluid.layers.open_files ArgSpec(args=['filenames', 'shapes', 'lod_levels', 'dtypes', 'thread_num', 'buffer_size', 'pass_num', 'is_test'], varargs=None, keywords=None, defaults=(None, None, 1, None))
paddle.fluid.layers.read_file ArgSpec(args=['reader'], varargs=None, keywords=None, defaults=None)
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
c27554ac
...
...
@@ -165,6 +165,7 @@ __all__ = [
'grid_sampler'
,
'log_loss'
,
'add_position_encoding'
,
'bilinear_tensor_product'
,
]
...
...
@@ -8289,3 +8290,72 @@ def add_position_encoding(input, alpha, beta, name=None):
attrs
=
{
"alpha"
:
alpha
,
"beta"
:
beta
})
return
out
def
bilinear_tensor_product
(
x
,
y
,
size
,
act
=
None
,
name
=
None
,
param_attr
=
None
,
bias_attr
=
None
):
"""
**Add Bilinear Tensor Product Layer**
This layer performs bilinear tensor product on two inputs.
For example:
.. math::
out{i} = x * W_{i} * {y^\mathrm{T}}, i=0,1,...,size-1
In this formula:
- :math:`x`: the first input contains M elements, shape is [batch_size, M].
- :math:`y`: the second input contains N elements, shape is [batch_size, N].
- :math:`W_{i}`: the i-th learned weight, shape is [M, N]
- :math:`out{i}`: the i-th element of out, shape is [batch_size, size].
- :math:`y^\mathrm{T}`: the transpose of :math:`y_{2}`.
Args:
x (Variable): 2-D input tensor with shape [batch_size, M]
y (Variable): 2-D input tensor with shape [batch_size, N]
size (int): The dimension of this layer.
act (str, default None): Activation to be applied to the output of this layer.
name (str, default None): The name of this layer.
param_attr (ParamAttr, default None): The parameter attribute for the learnable w.
parameters/weights of this layer.
bias_attr (ParamAttr, default None): The parameter attribute for the bias
of this layer. If it is set to False, no bias will be added to the output units.
If it is set to None, the bias is initialized zero. Default: None.
Returns:
Variable: A 2-D Tensor of shape [batch_size, size].
Examples:
.. code-block:: python
tensor = bilinear_tensor_product(x=layer1, y=layer2, size=1000)
"""
helper
=
LayerHelper
(
'bilinear_tensor_product'
,
**
locals
())
dtype
=
helper
.
input_dtype
(
'x'
)
param_shape
=
[
size
,
x
.
shape
[
1
],
y
.
shape
[
1
]]
w
=
helper
.
create_parameter
(
attr
=
helper
.
param_attr
,
shape
=
param_shape
,
dtype
=
dtype
,
is_bias
=
False
)
if
name
is
None
:
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
dtype
)
else
:
out
=
helper
.
create_variable
(
name
=
name
,
dtype
=
dtype
,
persistable
=
False
)
inputs
=
{
"X"
:
x
,
"Y"
:
y
,
"Weight"
:
w
}
if
helper
.
bias_attr
:
bias_size
=
[
1
,
size
]
bias
=
helper
.
create_parameter
(
attr
=
helper
.
bias_attr
,
shape
=
bias_size
,
dtype
=
dtype
,
is_bias
=
True
)
inputs
[
"Bias"
]
=
bias
helper
.
append_op
(
type
=
"bilinear_tensor_product"
,
inputs
=
inputs
,
outputs
=
{
"Out"
:
out
})
# add activation
return
helper
.
append_activation
(
out
)
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
c27554ac
...
...
@@ -911,6 +911,16 @@ class TestBook(unittest.TestCase):
self
.
assertIsNotNone
(
data_1
)
print
(
str
(
program
))
def
test_bilinear_tensor_product_layer
(
self
):
program
=
Program
()
with
program_guard
(
program
):
data
=
layers
.
data
(
name
=
'data'
,
shape
=
[
4
],
dtype
=
"float32"
)
theta
=
layers
.
data
(
name
=
"theta"
,
shape
=
[
5
],
dtype
=
"float32"
)
out
=
layers
.
bilinear_tensor_product
(
data
,
theta
,
6
)
print
(
str
(
program
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录