-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
add cross-entropy-op #2965
add cross-entropy-op #2965
Changes from all commits
0eb3f95
651de48
c48ec83
970c4b8
91656b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* 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/cross_entropy_op.h" | ||
#include "paddle/framework/op_registry.h" | ||
#include "paddle/framework/tensor.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class OnehotCrossEntropyOp : public framework::OperatorWithKernel { | ||
protected: | ||
void InferShape( | ||
const std::vector<const framework::Tensor *> &inputs, | ||
const std::vector<framework::Tensor *> &outputs) const override { | ||
PADDLE_ENFORCE(inputs.size() == 2, | ||
"Input size of OnehotCrossEntropyOp must be two"); | ||
PADDLE_ENFORCE(outputs.size() == 1, | ||
"Output size of OnehotCrossEntropyOp must be one"); | ||
PADDLE_ENFORCE(inputs[0] != nullptr && inputs[1] != nullptr, | ||
"Inputs of OnehotCrossEntropyOp must all be set"); | ||
PADDLE_ENFORCE(outputs[0] != nullptr, | ||
"Outputs of OnehotCrossEntropyOp must all be set"); | ||
PADDLE_ENFORCE(inputs[0]->dims().size() == 2, "X's dimension must be 2."); | ||
PADDLE_ENFORCE(outputs[0]->dims().size() == 1, | ||
"label's dimension must be 1."); | ||
outputs[0]->set_dims(framework::make_ddim({inputs[0]->dims()[0]})); | ||
} | ||
}; | ||
|
||
class OnehotCrossEntropyOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
OnehotCrossEntropyOpMaker(framework::OpProto *proto, | ||
framework::OpAttrChecker *op_checker) | ||
: framework::OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput("X", "The first input of OnehotCrossEntropyOp"); | ||
AddInput("label", "The second input of OnehotCrossEntropyOp"); | ||
AddOutput("Y", "The output of OnehotCrossEntropyOp"); | ||
AddComment(R"DOC( | ||
OnehotCrossEntropy Operator. | ||
|
||
Y[i] = -log(X[i][j]) | ||
|
||
)DOC"); | ||
} | ||
}; | ||
} // namespace operators | ||
} // namespace paddle | ||
|
||
REGISTER_OP(onehot_cross_entropy, | ||
paddle::operators::OnehotCrossEntropyOp, | ||
paddle::operators::OnehotCrossEntropyOpMaker); | ||
REGISTER_OP_CPU_KERNEL( | ||
onehot_cross_entropy, | ||
paddle::operators::OnehotCrossEntropyOpKernel<::paddle::platform::CPUPlace, | ||
float>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "paddle/operators/cross_entropy_op.h" | ||
#include "paddle/framework/op_registry.h" | ||
|
||
REGISTER_OP_GPU_KERNEL(onehot_cross_entropy, | ||
paddle::operators::OnehotCrossEntropyOpKernel< | ||
::paddle::platform::GPUPlace, float>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* 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 "glog/logging.h" | ||
#include "paddle/framework/operator.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
template <typename Place, typename T> | ||
class OnehotCrossEntropyOpKernel : public framework::OpKernel { | ||
public: | ||
constexpr T LOG_THRESHOLD() const { return static_cast<T>(1e-20); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be easier to have constant instead of a function here? I tried the following program builds and runs: #include <iostream>
template <typename T>
struct Hello {
const T kThreshold = static_cast<T>(1e-20);
};
int main() {
Hello<float> h;
std::cout << h.kThreshold << "\n";
return 0;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constexpr可以实现编译时求值,没有了运行时调用的开销,是觉得这个使用方法不够直接么? |
||
|
||
void Compute(const framework::KernelContext& context) const override { | ||
auto X = context.Input(0)->Get<framework::Tensor>(); | ||
const T* X_data = X.data<T>(); | ||
const int* label_data = | ||
context.Input(1)->Get<framework::Tensor>().data<int>(); | ||
auto* Y = context.Output(0)->GetMutable<framework::Tensor>(); | ||
|
||
Y->mutable_data<T>(context.GetPlace()); | ||
|
||
T* Y_data = Y->data<T>(); | ||
|
||
int batch_size = X.dims()[0]; | ||
int class_num = X.dims()[1]; | ||
|
||
// Y[i] = -log(X[i][j]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the code if it is unnecessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the formula for onehot-cross-entropy, I put it here to help readers get an easier understanding of the following computation logic. May be I should write with more detail? |
||
for (int i = 0; i < batch_size; ++i) { | ||
Y_data[i] = -std::log( | ||
std::max(X_data[i * class_num + label_data[i]], LOG_THRESHOLD())); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
cc_library(paddle_pybind SHARED SRCS pybind.cc DEPS pybind python | ||
add_op fc_op sgd_op) | ||
add_op fc_op sgd_op cross_entropy_op) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
add_python_test(test_framework test_protobuf.py test_scope.py | ||
test_default_scope_funcs.py test_op_creation_methods.py | ||
test_tensor.py test_fc_op.py test_add_two_op.py test_sgd_op.py) | ||
test_tensor.py test_fc_op.py test_add_two_op.py test_sgd_op.py test_cross_entropy_op.py) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import unittest | ||
import numpy | ||
from op_test_util import OpTestMeta | ||
|
||
|
||
class TestSGD(unittest.TestCase): | ||
__metaclass__ = OpTestMeta | ||
|
||
def setUp(self): | ||
self.type = "onehot_cross_entropy" | ||
batch_size = 100 | ||
class_num = 10 | ||
self.X = numpy.random.random((batch_size, class_num)).astype("float32") | ||
self.label = 5 * numpy.ones(batch_size).astype("int32") | ||
Y = [] | ||
for i in range(0, batch_size): | ||
Y.append(-numpy.log(self.X[i][self.label[i]])) | ||
self.Y = numpy.array(Y).astype("float32") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
和这个PR无关,请问现在的CMakeLists必须一行行手写么?不能对这个目录下的文件进行自动编译么? @gangliao