Add tangent operator (#29207)

As the title
revert-31562-mean
joejiong 4 years ago committed by GitHub
parent 95e334810a
commit 87e75a77c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -249,6 +249,15 @@ $$out = cos(x)$$
)DOC";
UNUSED constexpr char TanDoc[] = R"DOC(
Tangent Operator. Computes tangent of x element-wise.
Input range is `(k*pi-pi/2, k*pi+pi/2)` and output range is `(-inf, inf)`.
$$out = tan(x)$$
)DOC";
UNUSED constexpr char SinDoc[] = R"DOC(
Sine Activation Operator.
@ -709,6 +718,7 @@ REGISTER_ACTIVATION_OP_MAKER(Abs, AbsDoc);
REGISTER_ACTIVATION_OP_MAKER(Ceil, CeilDoc);
REGISTER_ACTIVATION_OP_MAKER(Floor, FloorDoc);
REGISTER_ACTIVATION_OP_MAKER(Cos, CosDoc);
REGISTER_ACTIVATION_OP_MAKER(Tan, TanDoc);
REGISTER_ACTIVATION_OP_MAKER(Sin, SinDoc);
REGISTER_ACTIVATION_OP_MAKER(Sinh, SinhDoc);
REGISTER_ACTIVATION_OP_MAKER(Cosh, CoshDoc);

@ -584,6 +584,39 @@ struct SinFunctor : public BaseActivationFunctor<T> {
}
};
template <typename T>
struct Tangent {
HOSTDEVICE T operator()(const T& val) const { return tan(val); }
};
template <>
struct Tangent<platform::float16> {
HOSTDEVICE platform::float16 operator()(const platform::float16& val) const {
return platform::float16(tan(static_cast<float>(val)));
}
};
// Tangent'(x) = -Tangent(x)
template <typename T>
struct TanGradFunctor : public BaseActivationFunctor<T> {
template <typename Device, typename X, typename Out, typename dOut,
typename dX>
void operator()(Device d, X x, Out out, dOut dout, dX dx) const {
dx.device(d) = dout / x.unaryExpr(Cosine<T>()).square();
}
static constexpr ActBwdOpFwdDeps FwdDeps() { return kDepX; }
};
// Tangent(x) = tan(x)
template <typename T>
struct TanFunctor : public BaseActivationFunctor<T> {
template <typename Device, typename X, typename Out>
void operator()(Device d, X x, Out out) const {
out.device(d) = x.unaryExpr(Tangent<T>());
}
};
template <typename T>
struct Sinh {
HOSTDEVICE T operator()(const T& val) const { return sinh(val); }
@ -1942,6 +1975,7 @@ struct LogGradGradFunctor : public BaseActivationFunctor<T> {
__macro(ceil, Ceil, CeilFunctor, ZeroGradFunctor); \
__macro(floor, Floor, FloorFunctor, ZeroGradFunctor); \
__macro(cos, Cos, CosFunctor, CosGradFunctor); \
__macro(tan, Tan, TanFunctor, TanGradFunctor); \
__macro(acos, Acos, AcosFunctor, AcosGradFunctor); \
__macro(sin, Sin, SinFunctor, SinGradFunctor); \
__macro(asin, Asin, AsinFunctor, AsinGradFunctor); \

@ -134,6 +134,7 @@ from .tensor.math import asin #DEFINE_ALIAS
from .tensor.math import atan #DEFINE_ALIAS
from .tensor.math import ceil #DEFINE_ALIAS
from .tensor.math import cos #DEFINE_ALIAS
from .tensor.math import tan #DEFINE_ALIAS
from .tensor.math import cosh #DEFINE_ALIAS
from .tensor.math import cumsum #DEFINE_ALIAS
# from .tensor.math import elementwise_add #DEFINE_ALIAS

@ -43,6 +43,7 @@ __unary_func__ = [
'ceil',
'floor',
'cos',
'tan',
'acos',
'sin',
'sinh',
@ -244,6 +245,19 @@ Examples:
""")
add_sample_code(globals()["tan"], r"""
Examples:
.. code-block:: python
import paddle
x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3])
out = paddle.tan(x)
print(out)
# [-0.42279324, -0.20271005, 0.10033467, 0.30933627]
""")
add_sample_code(globals()["acos"], r"""
Examples:
.. code-block:: python

File diff suppressed because it is too large Load Diff

@ -104,6 +104,7 @@ from .math import asin #DEFINE_ALIAS
from .math import atan #DEFINE_ALIAS
from .math import ceil #DEFINE_ALIAS
from .math import cos #DEFINE_ALIAS
from .math import tan #DEFINE_ALIAS
from .math import cosh #DEFINE_ALIAS
from .math import cumsum #DEFINE_ALIAS
# from .math import elementwise_add #DEFINE_ALIAS

@ -33,6 +33,7 @@ from ..fluid.layers import acos #DEFINE_ALIAS
from ..fluid.layers import asin #DEFINE_ALIAS
from ..fluid.layers import ceil #DEFINE_ALIAS
from ..fluid.layers import cos #DEFINE_ALIAS
from ..fluid.layers import tan #DEFINE_ALIAS
from ..fluid.layers import sinh #DEFINE_ALIAS
from ..fluid.layers import cosh #DEFINE_ALIAS
# from ..fluid.layers import elementwise_add #DEFINE_ALIAS

Loading…
Cancel
Save