Merge pull request #4071 from QiJune/activation_ops
Implement activation related operatorsupdate-doc-pybind
commit
8c3b8af31e
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,100 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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. */
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/operators/activation_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(sigmoid,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float,
|
||||
ops::SigmoidFunctor<float>>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
sigmoid_grad, ops::ActivationGradKernel<paddle::platform::GPUPlace, float,
|
||||
ops::SigmoidGradFunctor<float>>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
exp,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float, ops::ExpFunctor>);
|
||||
REGISTER_OP_GPU_KERNEL(exp_grad,
|
||||
ops::ActivationGradKernel<paddle::platform::GPUPlace,
|
||||
float, ops::ExpGradFunctor>);
|
||||
REGISTER_OP_GPU_KERNEL(relu,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float,
|
||||
ops::ReluFunctor<float>>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
relu_grad, ops::ActivationGradKernel<paddle::platform::GPUPlace, float,
|
||||
ops::ReluGradFunctor<float>>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
tanh,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float, ops::TanhFunctor>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
tanh_grad, ops::ActivationGradKernel<paddle::platform::GPUPlace, float,
|
||||
ops::TanhGradFunctor<float>>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
sqrt,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float, ops::SqrtFunctor>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
sqrt_grad, ops::ActivationGradKernel<paddle::platform::GPUPlace, float,
|
||||
ops::SqrtGradFunctor<float>>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
abs,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float, ops::AbsFunctor>);
|
||||
REGISTER_OP_GPU_KERNEL(abs_grad,
|
||||
ops::ActivationGradKernel<paddle::platform::GPUPlace,
|
||||
float, ops::AbsGradFunctor>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(reciprocal,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float,
|
||||
ops::ReciprocalFunctor<float>>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
reciprocal_grad,
|
||||
ops::ActivationGradKernel<paddle::platform::GPUPlace, float,
|
||||
ops::ReciprocalGradFunctor<float>>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
log,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float, ops::LogFunctor>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
log_grad, ops::ActivationGradKernel<paddle::platform::GPUPlace, float,
|
||||
ops::LogGradFunctor<float>>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(square,
|
||||
ops::ActivationKernel<paddle::platform::GPUPlace, float,
|
||||
ops::SquareFunctor>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
square_grad, ops::ActivationGradKernel<paddle::platform::GPUPlace, float,
|
||||
ops::SquareGradFunctor<float>>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(brelu,
|
||||
ops::BReluKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(brelu_grad,
|
||||
ops::BReluGradKernel<paddle::platform::GPUPlace, float>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(soft_relu,
|
||||
ops::SoftReluKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
soft_relu_grad, ops::SoftReluGradKernel<paddle::platform::GPUPlace, float>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(pow, ops::PowKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(pow_grad,
|
||||
ops::PowGradKernel<paddle::platform::GPUPlace, float>);
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(stanh,
|
||||
ops::STanhKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(stanh_grad,
|
||||
ops::STanhGradKernel<paddle::platform::GPUPlace, float>);
|
File diff suppressed because it is too large
Load Diff
@ -1,67 +0,0 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/operators/sigmoid_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class SigmoidOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"),
|
||||
"Input(X) of SigmoidOp should not be null.");
|
||||
PADDLE_ENFORCE_NOT_NULL(ctx.OutputVar("Y"),
|
||||
"Output(Y) of SigmoidOp should not be null.");
|
||||
|
||||
ctx.Output<framework::LoDTensor>("Y")->Resize(
|
||||
ctx.Input<Tensor>("X")->dims());
|
||||
}
|
||||
};
|
||||
|
||||
class SigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
SigmoidOpMaker(framework::OpProto *proto,
|
||||
framework::OpAttrChecker *op_checker)
|
||||
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||
AddInput("X", "sigmoid input");
|
||||
AddOutput("Y", "sigmoid output");
|
||||
AddComment("Sigmoid function");
|
||||
}
|
||||
};
|
||||
|
||||
class SigmoidOpGrad : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
protected:
|
||||
void InferShape(const framework::InferShapeContext &ctx) const override {
|
||||
ctx.Output<framework::LoDTensor>(framework::GradVarName("X"))
|
||||
->Resize(ctx.Input<Tensor>("Y")->dims());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP(sigmoid, ops::SigmoidOp, ops::SigmoidOpMaker, sigmoid_grad,
|
||||
ops::SigmoidOpGrad);
|
||||
REGISTER_OP_CPU_KERNEL(sigmoid,
|
||||
ops::SigmoidKernel<paddle::platform::CPUPlace, float>);
|
||||
REGISTER_OP_CPU_KERNEL(
|
||||
sigmoid_grad, ops::SigmoidGradKernel<paddle::platform::CPUPlace, float>);
|
@ -1,23 +0,0 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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. */
|
||||
|
||||
#define EIGEN_USE_GPU
|
||||
#include "paddle/operators/sigmoid_op.h"
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
REGISTER_OP_GPU_KERNEL(sigmoid,
|
||||
ops::SigmoidKernel<paddle::platform::GPUPlace, float>);
|
||||
REGISTER_OP_GPU_KERNEL(
|
||||
sigmoid_grad, ops::SigmoidGradKernel<paddle::platform::GPUPlace, float>);
|
@ -1,62 +0,0 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/framework/eigen.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
template <typename T, int MajorType = Eigen::RowMajor,
|
||||
typename IndexType = Eigen::DenseIndex>
|
||||
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
|
||||
|
||||
template <typename Place, typename T>
|
||||
class SigmoidKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
auto input = context.Input<Tensor>("X");
|
||||
auto output = context.Output<Tensor>("Y");
|
||||
output->mutable_data<T>(context.GetPlace());
|
||||
|
||||
// The clipping is used in Paddle's raw implenmention
|
||||
auto X = EigenVector<T>::Flatten(*input);
|
||||
auto Y = EigenVector<T>::Flatten(*output);
|
||||
auto place = context.GetEigenDevice<Place>();
|
||||
|
||||
Y.device(place) = 1. / (1. + (-X).exp());
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Place, typename T>
|
||||
class SigmoidGradKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
auto Y_t = context.Input<Tensor>("Y");
|
||||
auto dY_t = context.Input<Tensor>(framework::GradVarName("Y"));
|
||||
auto dX_t = context.Output<Tensor>(framework::GradVarName("X"));
|
||||
|
||||
dX_t->mutable_data<T>(context.GetPlace());
|
||||
|
||||
auto dX = EigenVector<T>::Flatten(*dX_t);
|
||||
auto Y = EigenVector<T>::Flatten(*Y_t);
|
||||
auto dY = EigenVector<T>::Flatten(*dY_t);
|
||||
dX.device(context.GetEigenDevice<Place>()) = dY * Y * (1. - Y);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,223 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestExp(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "exp"
|
||||
self.inputs = {
|
||||
'X': np.random.uniform(0.1, 1, [11, 17]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Y': np.exp(self.inputs['X'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.007)
|
||||
|
||||
|
||||
class TestSigmoid(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "sigmoid"
|
||||
self.inputs = {
|
||||
'X': np.random.uniform(0.1, 1, [11, 17]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Y': 1 / (1 + np.exp(-self.inputs['X']))}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.008)
|
||||
|
||||
|
||||
class TestTanh(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "tanh"
|
||||
self.inputs = {
|
||||
'X': np.random.uniform(0.1, 1, [11, 17]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Y': np.tanh(self.inputs['X'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.007)
|
||||
|
||||
|
||||
class TestSqrt(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "sqrt"
|
||||
self.inputs = {
|
||||
'X': np.random.uniform(0.1, 1, [11, 17]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Y': np.sqrt(self.inputs['X'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.007)
|
||||
|
||||
|
||||
class TestAbs(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "abs"
|
||||
x = np.random.uniform(-1, 1, [4, 4]).astype("float32")
|
||||
# Because we set delta = 0.005 in caculating numeric gradient,
|
||||
# if x is too small, such as 0.002, x_neg will be -0.003
|
||||
# x_pos will be 0.007, so the numeric gradient is unaccurate.
|
||||
# we should avoid this
|
||||
x[np.abs(x) < 0.005] = 0.02
|
||||
self.inputs = {'X': x}
|
||||
self.outputs = {'Y': np.abs(self.inputs['X'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.007)
|
||||
|
||||
|
||||
class TestRelu(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "relu"
|
||||
x = np.random.uniform(-1, 1, [11, 17]).astype("float32")
|
||||
# The same reason with TestAbs
|
||||
x[np.abs(x) < 0.005] = 0.02
|
||||
self.inputs = {'X': x}
|
||||
self.outputs = {'Y': np.maximum(self.inputs['X'], 0)}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.007)
|
||||
|
||||
|
||||
class TestBRelu(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "brelu"
|
||||
x = np.random.uniform(-1, 1, [4, 4]).astype("float32")
|
||||
t_min = 1
|
||||
t_max = 4
|
||||
# The same with TestAbs
|
||||
x[np.abs(x - t_min) < 0.005] = t_min + 0.02
|
||||
x[np.abs(x - t_max) < 0.005] = t_max + 0.02
|
||||
|
||||
self.inputs = {'X': x}
|
||||
self.attrs = {'t_min': t_min, 't_max': t_max}
|
||||
t = np.copy(x)
|
||||
t[t < t_min] = t_min
|
||||
t[t > t_max] = t_max
|
||||
self.outputs = {'Y': t}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.02)
|
||||
|
||||
|
||||
class TestSoftRelu(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "soft_relu"
|
||||
x = np.random.uniform(-3, 3, [4, 4]).astype("float32")
|
||||
threshold = 2
|
||||
# The same reason with TestAbs
|
||||
x[np.abs(x - threshold) < 0.005] = threshold + 0.02
|
||||
x[np.abs(x + threshold) < 0.005] = -threshold + 0.02
|
||||
self.inputs = {'X': x}
|
||||
self.attrs = {'threshold': threshold}
|
||||
t = np.copy(x)
|
||||
t[t < -threshold] = -threshold
|
||||
t[t > threshold] = threshold
|
||||
self.outputs = {'Y': np.log((np.exp(t) + 1))}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.02)
|
||||
|
||||
|
||||
class TestReciprocal(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "reciprocal"
|
||||
self.inputs = {'X': np.random.uniform(1, 2, [11, 17]).astype("float32")}
|
||||
self.outputs = {'Y': np.reciprocal(self.inputs['X'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.01)
|
||||
|
||||
|
||||
class TestLog(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "log"
|
||||
self.inputs = {
|
||||
'X': np.random.uniform(0.1, 1, [11, 17]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Y': np.log(self.inputs['X'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.007)
|
||||
|
||||
|
||||
class TestSquare(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "square"
|
||||
self.inputs = {
|
||||
'X': np.random.uniform(0.1, 1, [11, 17]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Y': np.square(self.inputs['X'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.007)
|
||||
|
||||
|
||||
class TestPow(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "pow"
|
||||
self.inputs = {'X': np.random.uniform(1, 2, [11, 17]).astype("float32")}
|
||||
self.attrs = {'factor': 3}
|
||||
self.outputs = {'Y': np.power(self.inputs['X'], 3)}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.02)
|
||||
|
||||
|
||||
class TestSTanh(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "stanh"
|
||||
self.inputs = {
|
||||
'X': np.random.uniform(0.1, 1, [11, 17]).astype("float32")
|
||||
}
|
||||
scale_a = 2.0 / 3.0
|
||||
scale_b = 1.7159
|
||||
self.attrs = {'scale_a': scale_a, 'scale_b': scale_b}
|
||||
self.outputs = {'Y': scale_b * np.tanh(self.inputs['X'] * scale_a)}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Y', max_relative_error=0.007)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
@ -1,22 +0,0 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestSigmoidOp(OpTest):
|
||||
def setUp(self):
|
||||
self.op_type = "sigmoid"
|
||||
self.inputs = {
|
||||
'X': np.random.uniform(0.1, 1, [11, 17]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Y': 1 / (1 + np.exp(-self.inputs['X']))}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(["X"], "Y", max_relative_error=0.007)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue