Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine square_error_cost layer #5216

Merged
merged 7 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions paddle/operators/activation_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,6 @@ class Relu6OpMaker : public framework::OpProtoAndCheckerMaker {
}
};

template <typename AttrType>
class PowOpMaker : public framework::OpProtoAndCheckerMaker {
public:
PowOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("X", "Input of Pow operator");
AddOutput("Y", "Output of Pow operator");
AddComment("Pow activation operator, pow(x, factor) = x^factor");
AddAttr<AttrType>("factor", "The exponential factor of Pow")
.SetDefault(static_cast<AttrType>(1));
}
};

template <typename AttrType>
class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
public:
Expand Down Expand Up @@ -368,6 +355,18 @@ It is recommended to use the defaults for this activation.
}
};

class PowOpMaker : public framework::OpProtoAndCheckerMaker {
public:
PowOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("X", "Input of Pow operator");
AddOutput("Y", "Output of Pow operator");
AddComment("Pow activation operator, pow(x, factor) = x^factor");
AddAttr<float>("factor", "The exponential factor of Pow")
.SetDefault(static_cast<float>(1));
}
};

} // namespace operators
} // namespace paddle

Expand Down Expand Up @@ -430,9 +429,6 @@ REGISTER_OP(elu, ops::ActivationOp, ops::ELUOpMaker<float>, elu_grad,
REGISTER_OP(relu6, ops::ActivationOp, ops::Relu6OpMaker<float>, relu6_grad,
ops::ActivationOpGrad);

REGISTER_OP(pow, ops::ActivationOp, ops::PowOpMaker<float>, pow_grad,
ops::ActivationOpGrad);

REGISTER_OP(stanh, ops::ActivationOp, ops::STanhOpMaker<float>, stanh_grad,
ops::ActivationOpGrad);

Expand All @@ -446,6 +442,16 @@ REGISTER_OP(thresholded_relu, ops::ActivationOp,
REGISTER_OP(hard_sigmoid, ops::ActivationOp, ops::HardSigmoidOpMaker<float>,
hard_sigmoid_grad, ops::ActivationOpGrad);

REGISTER_OP(pow, ops::ActivationOp, ops::PowOpMaker, pow_grad,
ops::ActivationOpGrad);

REGISTER_OP_CPU_KERNEL(pow, ops::PowKernel<paddle::platform::CPUPlace, float>,
ops::PowKernel<paddle::platform::CPUPlace, double>);

REGISTER_OP_CPU_KERNEL(pow_grad,
ops::PowGradKernel<paddle::platform::CPUPlace, float>,
ops::PowGradKernel<paddle::platform::CPUPlace, double>);

#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, functor, grad_functor) \
REGISTER_OP_CPU_KERNEL( \
act_type, \
Expand Down
7 changes: 7 additions & 0 deletions paddle/operators/activation_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

namespace ops = paddle::operators;

REGISTER_OP_GPU_KERNEL(pow, ops::PowKernel<paddle::platform::GPUPlace, float>,
ops::PowKernel<paddle::platform::GPUPlace, double>);

REGISTER_OP_CPU_KERNEL(pow_grad,
ops::PowGradKernel<paddle::platform::GPUPlace, float>,
ops::PowGradKernel<paddle::platform::GPUPlace, double>);

#define REGISTER_ACTIVATION_GPU_KERNEL(act_type, functor, grad_functor) \
REGISTER_OP_GPU_KERNEL( \
act_type, \
Expand Down
106 changes: 80 additions & 26 deletions paddle/operators/activation_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/platform/device_context.h"
#include "paddle/platform/hostdevice.h"
#include "paddle/platform/transform.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -547,31 +550,6 @@ struct ELUGradFunctor : public BaseActivationFunctor<T> {
}
};

template <typename T>
struct PowFunctor : public BaseActivationFunctor<T> {
float factor;
typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
return {{"factor", &factor}};
}
template <typename Device, typename X, typename Y>
void operator()(Device d, X x, Y y) const {
y.device(d) = x.pow(static_cast<T>(factor));
}
};

template <typename T>
struct PowGradFunctor : public BaseActivationFunctor<T> {
float factor;
typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
return {{"factor", &factor}};
}
template <typename Device, typename X, typename Y, typename dY, typename dX>
void operator()(Device d, X x, Y y, dY dy, dX dx) const {
dx.device(d) = dy * static_cast<T>(factor) *
x.pow(static_cast<T>(factor - static_cast<T>(1)));
}
};

template <typename T>
struct STanhFunctor : public BaseActivationFunctor<T> {
float scale_a;
Expand Down Expand Up @@ -664,6 +642,83 @@ struct HardSigmoidGradFunctor : public BaseActivationFunctor<T> {
}
};

namespace {
template <typename T>
struct Pow {
explicit Pow(const T& factor) : factor_(factor) {}

HOSTDEVICE T operator()(const T& a) const { return std::pow(a, factor_); }
T factor_;
};
} // namespace

template <typename Place, typename T>
struct PowFunctor {
void operator()(const platform::DeviceContext& ctx,
const framework::Tensor& in, const T& factor,
framework::Tensor* out) {
auto* in_data = in.data<T>();
auto* out_data = out->data<T>();
platform::Transform<Place> trans;
trans(ctx, in_data, in_data + in.numel(), out_data, Pow<T>(factor));
}
};

template <typename Place, typename T>
class PowKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<framework::Tensor>("X");
auto* out = context.Output<framework::Tensor>("Y");
out->mutable_data<T>(context.GetPlace());
T factor = static_cast<T>(context.Attr<float>("factor"));
PowFunctor<Place, T> functor;
functor(context.device_context(), *in, factor, out);
}
};

namespace {
template <typename T>
struct PowGrad {
explicit PowGrad(const T& factor) : factor_(factor) {}

HOSTDEVICE T operator()(const T& a, const T& b) const {
return a * factor_ * std::pow(b, factor_ - 1);
}
T factor_;
};
} // namespace

template <typename Place, typename T>
struct PowGradFunctor {
void operator()(const platform::DeviceContext& ctx,
const framework::Tensor& in1, const framework::Tensor& in2,
const T& factor, framework::Tensor* out) {
auto* in1_data = in1.data<T>();
auto* in2_data = in2.data<T>();
auto* out_data = out->data<T>();
platform::Transform<Place> trans;
trans(ctx, in1_data, in1_data + in1.numel(), in2_data, out_data,
PowGrad<T>(factor));
}
};

template <typename Place, typename T>
class PowGradKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in = context.Input<framework::Tensor>("X");
auto* out_grad =
context.Input<framework::Tensor>(framework::GradVarName("Y"));
auto* in_grad =
context.Output<framework::Tensor>(framework::GradVarName("X"));
in_grad->mutable_data<T>(context.GetPlace());
T factor = static_cast<T>(context.Attr<float>("factor"));
PowGradFunctor<Place, T> functor;
functor(context.device_context(), *out_grad, *in, factor, in_grad);
}
};

} // namespace operators
} // namespace paddle

Expand All @@ -681,7 +736,6 @@ struct HardSigmoidGradFunctor : public BaseActivationFunctor<T> {
__macro(square, SquareFunctor, SquareGradFunctor); \
__macro(brelu, BReluFunctor, BReluGradFunctor); \
__macro(soft_relu, SoftReluFunctor, SoftReluGradFunctor); \
__macro(pow, PowFunctor, PowGradFunctor); \
__macro(stanh, STanhFunctor, STanhGradFunctor); \
__macro(softplus, SoftplusFunctor, SoftplusGradFunctor); \
__macro(softsign, SoftsignFunctor, SoftsignGradFunctor); \
Expand Down
5 changes: 1 addition & 4 deletions python/paddle/v2/framework/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ def square_error_cost(input, label, **kwargs):

square_out = helper.create_tmp_variable(dtype=input.data_type)
helper.append_op(
type='pow',
inputs={'X': [minus_out]},
outputs={'Y': [square_out]},
attrs={'factor': 2.0})
type='square', inputs={'X': [minus_out]}, outputs={'Y': [square_out]})
return square_out


Expand Down
5 changes: 3 additions & 2 deletions python/paddle/v2/framework/tests/op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,16 @@ def find_actual(target_name, fetch_list):
+ ") has different lod at " + str(place))
else:
idx = find_actual(out_name, fetch_list)
actual_t = outs[idx]
actual = outs[idx]
actual_t = np.array(actual)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the purpose of these changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because type of outs[idx] is LoDTensor, not numpy array. We should convert it to numpy array before comparing.

expect = self.outputs[out_name]
expect_t = expect[0] if isinstance(expect, tuple) else expect
self.assertTrue(
np.allclose(
actual_t, expect_t, atol=atol),
"Output (" + out_name + ") has diff at " + str(place))
if isinstance(expect, tuple):
self.assertListEqual(actual_t.lod(), expect[1],
self.assertListEqual(actual.lod(), expect[1],
"Output (" + out_name +
") has different lod at " + str(place))

Expand Down
16 changes: 16 additions & 0 deletions python/paddle/v2/framework/tests/test_activation_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ def test_check_grad(self):
self.check_grad(['X'], 'Y', max_relative_error=0.02)


class TestPow1(OpTest):
def setUp(self):
self.op_type = "pow"
self.inputs = {
'X': np.random.uniform(-2, 2, [11, 17]).astype("float32")
}
self.attrs = {'factor': 2.0}
self.outputs = {'Y': np.power(self.inputs['X'], 2)}

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"
Expand Down