Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into imperative_shared_ptr
test=developrevert-15207-remove_op_handle_lock_and_fix_var
commit
ddfb9f1123
@ -0,0 +1,148 @@
|
||||
// Copyright (c) 2018 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.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "paddle/fluid/framework/ir/graph_viz_pass.h"
|
||||
#include "paddle/fluid/framework/ir/node.h"
|
||||
#include "paddle/fluid/framework/ir/transpose_flatten_concat_fuse_pass.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
namespace ir {
|
||||
|
||||
template <int times>
|
||||
std::unique_ptr<ir::Graph> TransposeFlattenConcatFusePass<times>::ApplyImpl(
|
||||
std::unique_ptr<ir::Graph> graph) const {
|
||||
const std::string pattern_name =
|
||||
"transpose_flatten" + std::to_string(times) + "_concat_fuse";
|
||||
FusePassBase::Init(pattern_name, graph.get());
|
||||
|
||||
GraphPatternDetector gpd;
|
||||
std::vector<PDNode *> input_nodes;
|
||||
for (int i = 0; i < times; i++) {
|
||||
input_nodes.push_back(gpd.mutable_pattern()
|
||||
->NewNode("x" + std::to_string(i))
|
||||
->assert_is_op_input("transpose2", "X")
|
||||
->AsInput());
|
||||
}
|
||||
|
||||
patterns::TransposeFlattenConcat pattern(gpd.mutable_pattern(), pattern_name);
|
||||
pattern(input_nodes, times);
|
||||
|
||||
auto handler = [&](const GraphPatternDetector::subgraph_t &subgraph,
|
||||
Graph *g) {
|
||||
const int kNumFields = 5;
|
||||
const int kTransOffset = 1;
|
||||
const int kTransOutOffset = 2;
|
||||
const int kFlattenOffset = 3;
|
||||
const int kFlattenOutOffset = 4;
|
||||
std::vector<Node *> nodes;
|
||||
|
||||
for (int i = 0; i < times; i++) {
|
||||
PADDLE_ENFORCE(
|
||||
subgraph.at(pattern.GetPDNode("transpose" + std::to_string(i))));
|
||||
PADDLE_ENFORCE(
|
||||
subgraph.at(pattern.GetPDNode("transpose_out" + std::to_string(i))));
|
||||
PADDLE_ENFORCE(
|
||||
subgraph.at(pattern.GetPDNode("flatten" + std::to_string(i))));
|
||||
PADDLE_ENFORCE(
|
||||
subgraph.at(pattern.GetPDNode("flatten_out" + std::to_string(i))));
|
||||
PADDLE_ENFORCE(subgraph.at(input_nodes[i]));
|
||||
|
||||
nodes.push_back(subgraph.at(input_nodes[i]));
|
||||
nodes.push_back(
|
||||
subgraph.at(pattern.GetPDNode("transpose" + std::to_string(i))));
|
||||
nodes.push_back(
|
||||
subgraph.at(pattern.GetPDNode("transpose_out" + std::to_string(i))));
|
||||
nodes.push_back(
|
||||
subgraph.at(pattern.GetPDNode("flatten" + std::to_string(i))));
|
||||
nodes.push_back(
|
||||
subgraph.at(pattern.GetPDNode("flatten_out" + std::to_string(i))));
|
||||
}
|
||||
|
||||
Node *concat_op = subgraph.at(pattern.GetPDNode("concat"));
|
||||
Node *concat_out = subgraph.at(pattern.GetPDNode("concat_out"));
|
||||
std::vector<std::string> input_names;
|
||||
std::vector<int> trans_axis = boost::get<std::vector<int>>(
|
||||
nodes[kTransOffset]->Op()->GetAttr("axis"));
|
||||
int flatten_axis =
|
||||
boost::get<int>(nodes[kFlattenOffset]->Op()->GetAttr("axis"));
|
||||
int concat_axis = boost::get<int>(concat_op->Op()->GetAttr("axis"));
|
||||
std::string output_name = concat_out->Name();
|
||||
|
||||
for (int i = 0; i < times; i++) {
|
||||
input_names.push_back(nodes[i * kNumFields]->Name());
|
||||
}
|
||||
|
||||
framework::OpDesc new_op_desc;
|
||||
new_op_desc.SetType("fusion_transpose_flatten_concat");
|
||||
new_op_desc.SetInput("X", input_names);
|
||||
new_op_desc.SetAttr("trans_axis", trans_axis);
|
||||
new_op_desc.SetAttr("flatten_axis", flatten_axis);
|
||||
new_op_desc.SetAttr("concat_axis", concat_axis);
|
||||
new_op_desc.SetOutput("Out", {output_name});
|
||||
new_op_desc.Flush();
|
||||
|
||||
// Create a new node for the fused op.
|
||||
auto *new_conv_op = graph->CreateOpNode(&new_op_desc);
|
||||
|
||||
std::unordered_set<const Node *> delete_nodes;
|
||||
|
||||
for (int i = 0; i < times; i++) {
|
||||
nodes[i * kNumFields]->outputs.push_back(new_conv_op);
|
||||
new_conv_op->inputs.push_back(nodes[i * kNumFields]);
|
||||
delete_nodes.insert(nodes[i * kNumFields + kTransOffset]);
|
||||
delete_nodes.insert(nodes[i * kNumFields + kTransOutOffset]);
|
||||
delete_nodes.insert(nodes[i * kNumFields + kFlattenOffset]);
|
||||
delete_nodes.insert(nodes[i * kNumFields + kFlattenOutOffset]);
|
||||
}
|
||||
delete_nodes.insert(concat_op);
|
||||
|
||||
new_conv_op->outputs.push_back(concat_out);
|
||||
concat_out->inputs.push_back(new_conv_op);
|
||||
|
||||
// Delete the unneeded nodes.
|
||||
GraphSafeRemoveNodes(graph.get(), delete_nodes);
|
||||
};
|
||||
|
||||
gpd(graph.get(), handler);
|
||||
return graph;
|
||||
}
|
||||
|
||||
template class TransposeFlattenConcatFusePass<1>;
|
||||
template class TransposeFlattenConcatFusePass<3>;
|
||||
template class TransposeFlattenConcatFusePass<4>;
|
||||
template class TransposeFlattenConcatFusePass<5>;
|
||||
template class TransposeFlattenConcatFusePass<6>;
|
||||
|
||||
} // namespace ir
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
||||
|
||||
REGISTER_PASS(transpose_flatten_concat_fuse_pass,
|
||||
paddle::framework::ir::TransposeFlattenConcatFusePass<1>);
|
||||
|
||||
REGISTER_PASS(transpose_flatten3_concat_fuse_pass,
|
||||
paddle::framework::ir::TransposeFlattenConcatFusePass<3>);
|
||||
|
||||
REGISTER_PASS(transpose_flatten4_concat_fuse_pass,
|
||||
paddle::framework::ir::TransposeFlattenConcatFusePass<4>);
|
||||
|
||||
REGISTER_PASS(transpose_flatten5_concat_fuse_pass,
|
||||
paddle::framework::ir::TransposeFlattenConcatFusePass<5>);
|
||||
|
||||
REGISTER_PASS(transpose_flatten6_concat_fuse_pass,
|
||||
paddle::framework::ir::TransposeFlattenConcatFusePass<6>);
|
@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2018 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.
|
||||
|
||||
#pragma once
|
||||
#include "paddle/fluid/framework/ir/fuse_pass_base.h"
|
||||
#include "paddle/fluid/framework/ir/graph_pattern_detector.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
namespace ir {
|
||||
|
||||
// There may be many transpose-flatten structures in a model, and the output of
|
||||
// these structures will be used as inputs to the concat Op. This pattern will
|
||||
// be detected by our pass. The times here represents the repeat times of this
|
||||
// structure.
|
||||
template <int times>
|
||||
class TransposeFlattenConcatFusePass : public FusePassBase {
|
||||
public:
|
||||
virtual ~TransposeFlattenConcatFusePass() {}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<ir::Graph> ApplyImpl(std::unique_ptr<ir::Graph> graph) const;
|
||||
};
|
||||
|
||||
} // namespace ir
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,35 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
|
||||
#pragma once
|
||||
#include "paddle/fluid/framework/eigen.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class DataNormKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override;
|
||||
};
|
||||
|
||||
template <typename DeviceContext, typename T>
|
||||
class DataNormGradKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& ctx) const override;
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,162 @@
|
||||
/* Copyright (c) 2018 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/fluid/operators/teacher_student_sigmoid_loss_op.h"
|
||||
#include "paddle/fluid/operators/math/math_function.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
|
||||
class TeacherStudentSigmoidLossOp : public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput("Y"), "Output(Y) should be not null.");
|
||||
|
||||
auto x_dims = ctx->GetInputDim("X");
|
||||
auto label_dims = ctx->GetInputDim("Label");
|
||||
PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "Input(X)'s rank should be 2.");
|
||||
PADDLE_ENFORCE_EQ(label_dims.size(), 2UL,
|
||||
"Input(Label)'s rank should be 2.");
|
||||
PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0],
|
||||
"The 1st dimension of Input(X) and Input(Label) should "
|
||||
"be equal.");
|
||||
PADDLE_ENFORCE_EQ(label_dims[1], 1UL,
|
||||
"The 2nd dimension of "
|
||||
"Input(Label) should be 1.");
|
||||
ctx->SetOutputDim("Y", {x_dims[0], 1});
|
||||
ctx->ShareLoD("X", /*->*/ "Y");
|
||||
}
|
||||
|
||||
protected:
|
||||
// Explicitly set that the data type of computation kernel of
|
||||
// teacher_student_sigmoid_loss
|
||||
// is determined by its input "X".
|
||||
framework::OpKernelType GetExpectedKernelType(
|
||||
const framework::ExecutionContext& ctx) const override {
|
||||
return framework::OpKernelType(ctx.Input<Tensor>("X")->type(),
|
||||
ctx.device_context());
|
||||
}
|
||||
};
|
||||
|
||||
class TeacherStudentSigmoidLossGradientOp
|
||||
: public framework::OperatorWithKernel {
|
||||
public:
|
||||
using framework::OperatorWithKernel::OperatorWithKernel;
|
||||
|
||||
void InferShape(framework::InferShapeContext* ctx) const override {
|
||||
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput("Label"), "Input(Label) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Y")),
|
||||
"Input(Y@GRAD) should be not null.");
|
||||
PADDLE_ENFORCE(ctx->HasOutput(framework::GradVarName("X")),
|
||||
"Output(X@GRAD) should be not null.");
|
||||
|
||||
auto x_dims = ctx->GetInputDim("X");
|
||||
auto label_dims = ctx->GetInputDim("Label");
|
||||
auto dy_dims = ctx->GetInputDim(framework::GradVarName("Y"));
|
||||
PADDLE_ENFORCE_EQ(x_dims.size(), 2, "Input(X)'s rank should be 2.");
|
||||
PADDLE_ENFORCE_EQ(dy_dims.size(), 2, "Input(Y@Grad)'s rank should be 2.");
|
||||
PADDLE_ENFORCE_EQ(label_dims.size(), 2, "Input(Label)'s rank should be 2.");
|
||||
PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0],
|
||||
"The 1st dimension of Input(X) and Input(Label) should "
|
||||
"be equal.");
|
||||
PADDLE_ENFORCE_EQ(x_dims[0], dy_dims[0],
|
||||
"The 1st dimension of Input(X) and Input(Y@Grad) should "
|
||||
"be equal.");
|
||||
PADDLE_ENFORCE_EQ(dy_dims[1], 1,
|
||||
"The 2nd dimension of Input(Y@Grad) should be 1.");
|
||||
PADDLE_ENFORCE_EQ(label_dims[1], 1,
|
||||
"When Attr(soft_label) == false, the 2nd dimension of "
|
||||
"Input(Label) should be 1.");
|
||||
ctx->SetOutputDim(framework::GradVarName("X"), x_dims);
|
||||
ctx->ShareLoD("X", framework::GradVarName("X"));
|
||||
}
|
||||
|
||||
protected:
|
||||
// Explicitly set that the data type of computation kernel of
|
||||
// teacher_student_sigmoid_loss
|
||||
// is determined by its input "X".
|
||||
framework::OpKernelType GetExpectedKernelType(
|
||||
const framework::ExecutionContext& ctx) const override {
|
||||
return framework::OpKernelType(ctx.Input<Tensor>("X")->type(),
|
||||
ctx.device_context());
|
||||
}
|
||||
};
|
||||
|
||||
class TeacherStudentSigmoidLossOpMaker
|
||||
: public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
void Make() override {
|
||||
AddInput("X",
|
||||
"(Tensor, default Tensor<float>), a 2-D tensor with shape [N x 1],"
|
||||
" where N is the batch size and D is the output. "
|
||||
"This input is a probability computed by the previous operator, "
|
||||
"which is almost always the result of a softmax operator.");
|
||||
AddInput("Label",
|
||||
"(Tensor), the ground truth which is a 2-D tensor. "
|
||||
"Label is a Tensor<float> with shape [N x 1]. ");
|
||||
AddOutput("Y",
|
||||
"(Tensor, default Tensor<float>), a 2-D tensor with shape "
|
||||
"[N x 1]. The teacher student sigmoid loss.");
|
||||
AddAttr<float>(
|
||||
"soft_max_up_bound",
|
||||
"fp32, if input > soft_max_up_bound, will be bound, default 15.0")
|
||||
.SetDefault(15.0);
|
||||
AddAttr<float>(
|
||||
"soft_max_lower_bound",
|
||||
"fp32, if input < soft_max_lower_bound, will be bound, default -15.0")
|
||||
.SetDefault(-15.0);
|
||||
AddComment(R"DOC(
|
||||
TeacherStudentSigmoidLoss Operator.
|
||||
|
||||
It's similarity to SigmoidCrossEntropyWithLogits Operator. The difference is that
|
||||
we add another label(z') to original.
|
||||
loss = max(x, 0) - x * z + log(1 + exp(-abs(x))) + max(x, 0) - x * z' + log(1 + exp(-abs(x)))
|
||||
z is click or not
|
||||
z' is teacher value
|
||||
label = {-2, -1, [0, 2]}
|
||||
when z' is not exist, clk = 0 : label = -2;
|
||||
when z' is not exist, clk = 1 : label = -1;
|
||||
when z' is exist , clk = 0 : label = 0 + z';
|
||||
when z' is exist , clk = 1 : label = 1 + z';
|
||||
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OPERATOR(teacher_student_sigmoid_loss,
|
||||
ops::TeacherStudentSigmoidLossOp,
|
||||
ops::TeacherStudentSigmoidLossOpMaker,
|
||||
paddle::framework::DefaultGradOpDescMaker<true>);
|
||||
|
||||
REGISTER_OPERATOR(teacher_student_sigmoid_loss_grad,
|
||||
ops::TeacherStudentSigmoidLossGradientOp);
|
||||
|
||||
REGISTER_OP_CPU_KERNEL(teacher_student_sigmoid_loss,
|
||||
ops::TeacherStudentSigmoidLossOpKernel<float>,
|
||||
ops::TeacherStudentSigmoidLossOpKernel<double>);
|
||||
|
||||
REGISTER_OP_CPU_KERNEL(teacher_student_sigmoid_loss_grad,
|
||||
ops::TeacherStudentSigmoidLossGradOpKernel<float>,
|
||||
ops::TeacherStudentSigmoidLossGradOpKernel<double>);
|
@ -0,0 +1,118 @@
|
||||
/* Copyright (c) 2018 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. */
|
||||
|
||||
#pragma once
|
||||
#include "paddle/fluid/framework/eigen.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
template <typename T>
|
||||
class TeacherStudentSigmoidLossOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
Tensor* y = context.Output<Tensor>("Y");
|
||||
const Tensor* x = context.Input<Tensor>("X");
|
||||
const Tensor* labels = context.Input<Tensor>("Label");
|
||||
T* y_data = y->mutable_data<T>(context.GetPlace());
|
||||
const T* x_data = x->data<T>();
|
||||
const T* label_data = labels->data<T>();
|
||||
int64_t batch_size = x->dims()[0];
|
||||
// loss = max(x, 0) - x * z + log(1 + exp(-abs(x))) + max(x, 0) - x * z' +
|
||||
// log(1 + exp(-abs(x)))
|
||||
// z is click or not
|
||||
// z' is value q of feed_fine
|
||||
// label = {-2, -1, [0, 2]}
|
||||
// when z' is not exist, clk = 0 : label = -2;
|
||||
// when z' is not exist, clk = 1 : label = -1;
|
||||
// when z' is exist , clk = 0 : label = 0 + z';
|
||||
// when z' is exist , clk = 1 : label = 1 + z';
|
||||
for (int i = 0; i < batch_size; ++i) {
|
||||
if (label_data[i] < -1.0) {
|
||||
y_data[i] = (x_data[i] > 0 ? x_data[i] : 0.0) +
|
||||
log(1.0 + exp(-fabs(x_data[i])));
|
||||
} else if (label_data[i] < 0.0) {
|
||||
y_data[i] = (x_data[i] > 0 ? x_data[i] : 0.0) - x_data[i] +
|
||||
log(1.0 + exp(-fabs(x_data[i])));
|
||||
} else if (label_data[i] < 1.0) {
|
||||
y_data[i] = (x_data[i] > 0 ? x_data[i] : 0.0) +
|
||||
log(1.0 + exp(-fabs(x_data[i]))) +
|
||||
(x_data[i] > 0 ? x_data[i] : 0.0) -
|
||||
x_data[i] * label_data[i] +
|
||||
log(1.0 + exp(-fabs(x_data[i])));
|
||||
} else {
|
||||
y_data[i] = (x_data[i] > 0 ? x_data[i] : 0.0) - x_data[i] +
|
||||
log(1.0 + exp(-fabs(x_data[i]))) +
|
||||
(x_data[i] > 0 ? x_data[i] : 0.0) -
|
||||
x_data[i] * (label_data[i] - 1.0) +
|
||||
log(1.0 + exp(-fabs(x_data[i])));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class TeacherStudentSigmoidLossGradOpKernel : public framework::OpKernel<T> {
|
||||
public:
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
const Tensor* x = context.Input<Tensor>("X");
|
||||
const T* x_data = x->data<T>();
|
||||
|
||||
Tensor* dx = context.Output<Tensor>(framework::GradVarName("X"));
|
||||
T* dx_data = dx->mutable_data<T>(context.GetPlace());
|
||||
|
||||
const Tensor* labels = context.Input<Tensor>("Label");
|
||||
const T* label_data = labels->data<T>();
|
||||
|
||||
T soft_max_up_bound =
|
||||
static_cast<T>(context.Attr<float>("soft_max_up_bound"));
|
||||
T soft_max_lower_bound =
|
||||
static_cast<T>(context.Attr<float>("soft_max_lower_bound"));
|
||||
|
||||
int64_t batch_size = x->dims()[0];
|
||||
|
||||
const framework::Tensor* dOut =
|
||||
context.Input<framework::Tensor>(framework::GradVarName("Y"));
|
||||
|
||||
const T* dout_data = dOut->data<T>();
|
||||
|
||||
for (int i = 0; i < batch_size; ++i) {
|
||||
T sum_val = x_data[i];
|
||||
if (sum_val > soft_max_up_bound) {
|
||||
sum_val = soft_max_up_bound;
|
||||
} else {
|
||||
if (sum_val < soft_max_lower_bound) {
|
||||
sum_val = soft_max_lower_bound;
|
||||
}
|
||||
}
|
||||
|
||||
T pred = 1.0 / (1.0 + exp(-sum_val));
|
||||
if (label_data[i] < -1.0) {
|
||||
dx_data[i] = 0.0 - pred;
|
||||
} else if (label_data[i] < 0.0) {
|
||||
dx_data[i] = 1.0 - pred;
|
||||
} else {
|
||||
dx_data[i] = label_data[i] - 2.0 * pred;
|
||||
}
|
||||
if (sum_val >= soft_max_up_bound || sum_val <= soft_max_lower_bound) {
|
||||
dx_data[i] = 0;
|
||||
}
|
||||
dx_data[i] *= dout_data[i] * -1;
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue