提交 35279398 编写于 作者: P Peng Li

Fix merge and style error

上级 b063b0c4
...@@ -42,8 +42,7 @@ bool CRFLayer::init(const LayerMap& layerMap, ...@@ -42,8 +42,7 @@ bool CRFLayer::init(const LayerMap& layerMap,
CHECK_EQ(parameters_[0]->getSize(), numClasses_ * (numClasses_ + 2)); CHECK_EQ(parameters_[0]->getSize(), numClasses_ * (numClasses_ + 2));
parameter_ = parameters_[0]; parameter_ = parameters_[0];
weight_.reset( weight_.reset(new Weight(numClasses_ + 2, numClasses_, parameter_));
new Weight(numClasses_ + 2, numClasses_, parameter_));
// We don't need sequenceStartPositions because each sample of output_ is // We don't need sequenceStartPositions because each sample of output_ is
// for the cost of one sequence. // for the cost of one sequence.
...@@ -95,16 +94,18 @@ void CRFLayer::backward(const UpdateCallback& callback) { ...@@ -95,16 +94,18 @@ void CRFLayer::backward(const UpdateCallback& callback) {
for (int i = 0; i < numSequences; ++i) { for (int i = 0; i < numSequences; ++i) {
crfs_[i].backward(output.value->getData() + numClasses_ * starts[i], crfs_[i].backward(output.value->getData() + numClasses_ * starts[i],
label.ids->getData() + starts[i], label.ids->getData() + starts[i],
starts[i + 1] - starts[i], needWGrad); starts[i + 1] - starts[i],
real instanceWeight = weightLayer_ ? needWGrad);
getInputValue(*weightLayer_)->getElement(i, 0) : real(1.0f); real instanceWeight = weightLayer_
? getInputValue(*weightLayer_)->getElement(i, 0)
: real(1.0f);
instanceWeight *= coeff_; instanceWeight *= coeff_;
MatrixPtr grad = output.grad->subRowMatrix(starts[i], starts[i + 1]); MatrixPtr grad = output.grad->subRowMatrix(starts[i], starts[i + 1]);
grad->add(*crfs_[i].getXGrad(), real(1.0f), instanceWeight); grad->add(*crfs_[i].getXGrad(), real(1.0f), instanceWeight);
if (needWGrad) { if (needWGrad) {
weight_->getWGrad()->add(*crfs_[i].getWGrad(), real(1.0f), weight_->getWGrad()->add(
instanceWeight); *crfs_[i].getWGrad(), real(1.0f), instanceWeight);
} }
} }
......
...@@ -23,11 +23,8 @@ limitations under the License. */ ...@@ -23,11 +23,8 @@ limitations under the License. */
using namespace paddle; // NOLINT using namespace paddle; // NOLINT
P_DECLARE_bool(use_gpu); DECLARE_int32(gpu_id);
P_DECLARE_int32(gpu_id); DECLARE_bool(thread_local_rand_use_global_seed);
P_DECLARE_double(checkgrad_eps);
P_DECLARE_bool(thread_local_rand_use_global_seed);
P_DECLARE_bool(prev_batch_state);
static inline bool getNextSequence(std::vector<int>& seq, int numClasses) { static inline bool getNextSequence(std::vector<int>& seq, int numClasses) {
for (auto& v : seq) { for (auto& v : seq) {
...@@ -96,8 +93,8 @@ TEST(CRFLayer, cost) { ...@@ -96,8 +93,8 @@ TEST(CRFLayer, cost) {
real diff = fabs(trueCost - cost); real diff = fabs(trueCost - cost);
diff /= fabs(cost) < fabs(trueCost) ? fabs(cost) : fabs(trueCost); diff /= fabs(cost) < fabs(trueCost) ? fabs(cost) : fabs(trueCost);
VLOG(1) << "cost=" << cost << " trueCost=" << trueCost VLOG(1) << "cost=" << cost << " trueCost=" << trueCost << " diff=" << diff
<< " diff=" << diff << std::endl; << std::endl;
if (typeid(real) == typeid(double)) { // NOLINT if (typeid(real) == typeid(double)) { // NOLINT
EXPECT_LE(diff, 1e-10); EXPECT_LE(diff, 1e-10);
} else { } else {
...@@ -107,9 +104,7 @@ TEST(CRFLayer, cost) { ...@@ -107,9 +104,7 @@ TEST(CRFLayer, cost) {
} }
} }
inline real epsilon() { inline real epsilon() { return typeid(real) == typeid(double) ? 1e-10 : 0.06; }
return typeid(real) == typeid(double) ? 1e-10 : 0.06;
}
TestConfig initTestConfig(size_t numClasses, bool withWeight) { TestConfig initTestConfig(size_t numClasses, bool withWeight) {
TestConfig config; TestConfig config;
...@@ -117,16 +112,17 @@ TestConfig initTestConfig(size_t numClasses, bool withWeight) { ...@@ -117,16 +112,17 @@ TestConfig initTestConfig(size_t numClasses, bool withWeight) {
config.layerConfig.set_size(numClasses); config.layerConfig.set_size(numClasses);
config.biasSize = 0; config.biasSize = 0;
config.inputDefs.push_back({INPUT_SEQUENCE_DATA, "layer_0", config.inputDefs.push_back({INPUT_SEQUENCE_DATA,
numClasses, numClasses * (numClasses + 2)}); "layer_0",
numClasses,
numClasses * (numClasses + 2)});
config.layerConfig.add_inputs(); config.layerConfig.add_inputs();
config.inputDefs.push_back({INPUT_SEQUENCE_LABEL, "layer_label", config.inputDefs.push_back(
numClasses, 0}); {INPUT_SEQUENCE_LABEL, "layer_label", numClasses, 0});
config.layerConfig.add_inputs(); config.layerConfig.add_inputs();
if (withWeight) { if (withWeight) {
config.inputDefs.push_back({INPUT_DENSE_DIM_DATA, "layer_weight", config.inputDefs.push_back({INPUT_DENSE_DIM_DATA, "layer_weight", 1, 0});
1, 0});
config.layerConfig.add_inputs(); config.layerConfig.add_inputs();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册