1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_KERNELS_SQUARED_LOSS_H_ 17 #define TENSORFLOW_CORE_KERNELS_SQUARED_LOSS_H_ 18 19 #include "tensorflow/core/kernels/loss.h" 20 21 namespace tensorflow { 22 23 class SquaredLossUpdater : public DualLossUpdater { 24 public: 25 // Closed form solution that decreases the dual squared loss. 26 // See page 23 of http://arxiv.org/pdf/1309.2375v2.pdf for the derivation of 27 // the update rule when the example weights are equal to 1.0. 28 // Note: There is a typo in the formula in the paper: the denominator should 29 // be 1 + ||x_i||^2/(\lambda n) (without the 2 multiplier). 30 // 31 // The CoCoA+ modification is detailed in readme.md. ComputeUpdatedDual(const int num_loss_partitions,const double label,const double example_weight,const double current_dual,const double wx,const double weighted_example_norm)32 double ComputeUpdatedDual(const int num_loss_partitions, const double label, 33 const double example_weight, 34 const double current_dual, const double wx, 35 const double weighted_example_norm) const final { 36 const double delta_numerator = label - current_dual - wx; 37 const double delta_denominator = 38 1 + num_loss_partitions * weighted_example_norm * example_weight; 39 return current_dual + delta_numerator / delta_denominator; 40 } 41 42 // Dual of squared loss function. 43 // https://en.wikipedia.org/wiki/Convex_conjugate ComputeDualLoss(const double current_dual,const double example_label,const double example_weight)44 double ComputeDualLoss(const double current_dual, const double example_label, 45 const double example_weight) const final { 46 // Dual of the squared loss function = b * (y + b/2), where b is the 47 // dual variable and y is the label. This is Dual(-b). 48 return current_dual * (0.5 * current_dual - example_label) * example_weight; 49 } 50 51 // Squared loss for linear regression. ComputePrimalLoss(const double wx,const double example_label,const double example_weight)52 double ComputePrimalLoss(const double wx, const double example_label, 53 const double example_weight) const final { 54 const double error = wx - example_label; 55 return error * error * example_weight * 0.5; 56 } 57 PrimalLossDerivative(const double wx,const double label,const double example_weight)58 inline double PrimalLossDerivative(const double wx, const double label, 59 const double example_weight) const final { 60 return (wx - label) * example_weight; 61 } 62 SmoothnessConstant()63 inline double SmoothnessConstant() const final { return 1.0; } 64 65 // Labels don't require conversion for linear regression. ConvertLabel(float * const example_label)66 Status ConvertLabel(float* const example_label) const final { 67 return Status::OK(); 68 } 69 }; 70 71 } // namespace tensorflow 72 73 #endif // TENSORFLOW_CORE_KERNELS_SQUARED_LOSS_H_ 74