Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
csdn_franckjun
Paddle
提交
1bfff020
P
Paddle
项目概览
csdn_franckjun
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1bfff020
编写于
5月 10, 2019
作者:
Z
zhoukunsheng
提交者:
Tao Luo
5月 10, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Diag Op(#17027)
上级
8a2caacd
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
223 addition
and
1 deletion
+223
-1
paddle/fluid/API.spec
paddle/fluid/API.spec
+1
-0
paddle/fluid/operators/diag_op.cc
paddle/fluid/operators/diag_op.cc
+60
-0
paddle/fluid/operators/diag_op.cu
paddle/fluid/operators/diag_op.cu
+23
-0
paddle/fluid/operators/diag_op.h
paddle/fluid/operators/diag_op.h
+59
-0
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+37
-1
python/paddle/fluid/tests/unittests/test_diag.py
python/paddle/fluid/tests/unittests/test_diag.py
+43
-0
未找到文件。
paddle/fluid/API.spec
浏览文件 @
1bfff020
...
...
@@ -270,6 +270,7 @@ paddle.fluid.layers.isfinite (ArgSpec(args=['x'], varargs=None, keywords=None, d
paddle.fluid.layers.range (ArgSpec(args=['start', 'end', 'step', 'dtype'], varargs=None, keywords=None, defaults=None), ('document', '2ec937ede953ded2fdff2675883900bb'))
paddle.fluid.layers.linspace (ArgSpec(args=['start', 'stop', 'num', 'dtype'], varargs=None, keywords=None, defaults=None), ('document', '495e21e9a848c2d075a102802fc67756'))
paddle.fluid.layers.zeros_like (ArgSpec(args=['x', 'out'], varargs=None, keywords=None, defaults=(None,)), ('document', 'c7e4cfffc93ae89c8f6f53b6d650f923'))
paddle.fluid.layers.diag (ArgSpec(args=['diagonal'], varargs=None, keywords=None, defaults=None), ('document', '2964d07340e32e47efb6e5db619875c7'))
paddle.fluid.layers.While.__init__ (ArgSpec(args=['self', 'cond', 'is_test', 'name'], varargs=None, keywords=None, defaults=(False, None)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.layers.While.block (ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.layers.Switch.__init__ (ArgSpec(args=['self', 'name'], varargs=None, keywords=None, defaults=(None,)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
...
...
paddle/fluid/operators/diag_op.cc
0 → 100644
浏览文件 @
1bfff020
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/operators/diag_op.h"
namespace
paddle
{
namespace
operators
{
class
DiagOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Diagonal"
),
"Input(Diagonal) of DiagOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of DiagOp should not be null."
);
auto
s_dims
=
ctx
->
GetInputDim
(
"Diagonal"
);
PADDLE_ENFORCE
(
s_dims
.
size
()
==
1
,
"The rank of Input(Diagonal) should only be 1."
);
ctx
->
SetOutputDim
(
"Out"
,
{
s_dims
[
0
],
s_dims
[
0
]});
}
};
class
DiagOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
void
Make
()
override
{
AddInput
(
"Diagonal"
,
"Diagonal values of square matrix. It is a tensor with rank 1."
);
AddOutput
(
"Out"
,
"A square matrix."
);
AddComment
(
R"DOC(
Return a square matrix with specified diagonal values.
)DOC"
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
diag
,
ops
::
DiagOp
,
ops
::
DiagOpMaker
,
paddle
::
framework
::
EmptyGradOpMaker
);
REGISTER_OP_CPU_KERNEL
(
diag
,
ops
::
DiagKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
DiagKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
DiagKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
DiagKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
);
paddle/fluid/operators/diag_op.cu
0 → 100644
浏览文件 @
1bfff020
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/diag_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
diag
,
ops
::
DiagKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
,
ops
::
DiagKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
,
ops
::
DiagKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
DiagKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
);
paddle/fluid/operators/diag_op.h
0 → 100644
浏览文件 @
1bfff020
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/platform/for_range.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
struct
DiagFunctor
{
DiagFunctor
(
const
T
*
diagonal
,
int64_t
numel
,
T
*
output
)
:
diagonal_
(
diagonal
),
numel_
(
numel
),
output_
(
output
)
{}
HOSTDEVICE
void
operator
()(
size_t
idx
)
const
{
output_
[
idx
*
numel_
+
idx
]
=
diagonal_
[
idx
];
}
const
T
*
diagonal_
;
int64_t
numel_
;
T
*
output_
;
};
template
<
typename
DeviceContext
,
typename
T
>
class
DiagKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
diagonal
=
context
.
Input
<
framework
::
Tensor
>
(
"Diagonal"
);
auto
*
diag_data
=
diagonal
->
data
<
T
>
();
auto
numel
=
diagonal
->
numel
();
auto
*
out
=
context
.
Output
<
framework
::
Tensor
>
(
"Out"
);
T
*
out_data
=
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
math
::
SetConstant
<
DeviceContext
,
T
>
set_zero
;
auto
&
dev_ctx
=
context
.
template
device_context
<
DeviceContext
>();
set_zero
(
dev_ctx
,
out
,
static_cast
<
T
>
(
0
));
platform
::
ForRange
<
DeviceContext
>
for_range
(
dev_ctx
,
numel
);
DiagFunctor
<
T
>
functor
(
diag_data
,
numel
,
out_data
);
for_range
(
functor
);
}
};
}
// namespace operators
}
// namespace paddle
python/paddle/fluid/layers/tensor.py
浏览文件 @
1bfff020
...
...
@@ -28,7 +28,7 @@ __all__ = [
'tensor_array_to_tensor'
,
'concat'
,
'sums'
,
'assign'
,
'fill_constant_batch_size_like'
,
'fill_constant'
,
'argmin'
,
'argmax'
,
'argsort'
,
'ones'
,
'zeros'
,
'reverse'
,
'has_inf'
,
'has_nan'
,
'isfinite'
,
'range'
,
'linspace'
,
'zeros_like'
'range'
,
'linspace'
,
'zeros_like'
,
'diag'
]
...
...
@@ -890,3 +890,39 @@ def zeros_like(x, out=None):
type
=
'fill_zeros_like'
,
inputs
=
{
'X'
:
[
x
]},
outputs
=
{
'Out'
:
[
out
]})
out
.
stop_gradient
=
True
return
out
def
diag
(
diagonal
):
"""
**diag**
This function creates a square matrix which has diagonal values specified by `diagonal`.
Args:
diagonal(Variable|numpy.ndarray): The input tensor specifying diagonal values, should be of rank 1.
Returns:
Variable: The tensor variable storing the square matrix.
Examples:
.. code-block:: python
# [[3, 0, 0]
# [0, 4, 0]
# [0, 0, 5]
data = fluid.layers.diag(np.arange(3, 6))
"""
helper
=
LayerHelper
(
"diag"
,
**
locals
())
if
not
isinstance
(
diagonal
,
Variable
):
diagonal
=
assign
(
diagonal
)
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
diagonal
.
dtype
)
helper
.
append_op
(
type
=
'diag'
,
inputs
=
{
'Diagonal'
:
[
diagonal
]},
outputs
=
{
'Out'
:
[
out
]})
out
.
stop_gradient
=
True
return
out
python/paddle/fluid/tests/unittests/test_diag.py
0 → 100644
浏览文件 @
1bfff020
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
class
TestDiagOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"diag"
self
.
init_config
()
self
.
inputs
=
{
'Diagonal'
:
self
.
case
}
self
.
outputs
=
{
'Out'
:
np
.
diag
(
self
.
inputs
[
'Diagonal'
])}
def
test_check_output
(
self
):
self
.
check_output
()
def
init_config
(
self
):
self
.
case
=
np
.
arange
(
3
,
6
)
class
TestDiagOpCase1
(
TestDiagOp
):
def
init_config
(
self
):
self
.
case
=
np
.
array
([
3
],
dtype
=
'int32'
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录