forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance fused_elementwise_activation op and add python api in contrib…
….layers (PaddlePaddle#17236) * Enhance fused_elementwise_activation op. test=develop * Move the api fused_elementwise_activation to contrib. test=develop * Add including files. test=develop * Add the support of sigmoid in fused_elementwise_activetion op. * Update API.spec. test=develop
- Loading branch information
Showing
9 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 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 | ||
|
||
from . import nn | ||
from .nn import * | ||
|
||
__all__ = [] | ||
__all__ += nn.__all__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# 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. | ||
""" | ||
Contrib layers just related to the neural network. | ||
""" | ||
|
||
from __future__ import print_function | ||
|
||
import numpy as np | ||
import six | ||
import os | ||
import inspect | ||
from paddle.fluid.layer_helper import LayerHelper | ||
|
||
__all__ = ['fused_elemwise_activation', ] | ||
|
||
|
||
def fused_elemwise_activation(x, | ||
y, | ||
functor_list, | ||
axis=-1, | ||
scale=0.0, | ||
save_intermediate_out=True): | ||
""" | ||
**Fused elementwise_add/mul and activation layers** | ||
This function computes an elementwise_add/mul cooperated with an activation. | ||
.. math:: | ||
out = Unary(Binary(x, y)) | ||
or | ||
.. math:: | ||
out = Binary(x, Unary(y)) | ||
Unary operators can be: `scale`, `relu`, `tanh`. Binary operators can be: | ||
`elementwise_add`, `elementwise_mul`. | ||
Args: | ||
x (Variable): left operation of the binary operator. | ||
y (Variable): right operator of the binary operator. | ||
functor_list (list of str): types of operator which will be executed | ||
by this layer. For example, ['elementwise_add', 'relu'] | ||
(out = elementwise_add(x, relu(y))), | ||
or ['relu', 'elemmentwise_add'] (out = relu(elementwise_add(x, y))). | ||
axis (int32, default -1): axis of elementwise op. | ||
scale (float32, default 0): parameter of scale op. | ||
save_intermediate_out (bool, default True): whether to save the | ||
intermediate result, Unary(y) or Binary(x, y). | ||
Returns: | ||
Variable: The computation result. | ||
""" | ||
if isinstance(functor_list, str): | ||
functor_list = functor_list.split(',') | ||
|
||
if not isinstance(functor_list, list) or len(functor_list) != 2: | ||
raise ValueError( | ||
'functor_list should be a list of str, and the length should be 2.') | ||
|
||
helper = LayerHelper('fused_elemwise_activation', **locals()) | ||
out = helper.create_variable_for_type_inference(dtype=x.dtype) | ||
intermediate_out = helper.create_variable_for_type_inference(dtype=x.dtype) | ||
helper.append_op( | ||
type='fused_elemwise_activation', | ||
inputs={'X': x, | ||
'Y': y}, | ||
outputs={'Out': out, | ||
'IntermediateOut': intermediate_out}, | ||
attrs={ | ||
'axis': axis, | ||
'scale': scale, | ||
'save_intermediate_out': save_intermediate_out, | ||
'functor_list': functor_list | ||
}) | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters