From 3e1a0792fb593953860162d57320c8602fd199eb Mon Sep 17 00:00:00 2001 From: Yuefeng Zhou Date: Tue, 9 Oct 2018 09:32:50 -0700 Subject: Create SDCAOptimizerV2 op to fix the "adaptative" typo. PiperOrigin-RevId: 216370193 --- .../linear_optimizer/python/ops/sdca_ops.py | 57 ++++--- .../api_def/base_api/api_def_SdcaOptimizerV2.pbtxt | 171 +++++++++++++++++++++ tensorflow/core/kernels/sdca_ops.cc | 8 +- tensorflow/core/ops/sdca_ops.cc | 28 ++++ tensorflow/python/ops/sdca_ops.py | 1 + 5 files changed, 246 insertions(+), 19 deletions(-) create mode 100644 tensorflow/core/api_def/base_api/api_def_SdcaOptimizerV2.pbtxt diff --git a/tensorflow/contrib/linear_optimizer/python/ops/sdca_ops.py b/tensorflow/contrib/linear_optimizer/python/ops/sdca_ops.py index b98adf862b..48ac429701 100644 --- a/tensorflow/contrib/linear_optimizer/python/ops/sdca_ops.py +++ b/tensorflow/contrib/linear_optimizer/python/ops/sdca_ops.py @@ -22,6 +22,7 @@ import collections from six.moves import range from tensorflow.contrib.linear_optimizer.python.ops.sharded_mutable_dense_hashtable import ShardedMutableDenseHashTable +from tensorflow.python.compat import compat from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops @@ -485,24 +486,44 @@ class SdcaModel(object): sparse_weights.append(batch_gathered_weights) # pylint: disable=protected-access - esu, sfw, dfw = gen_sdca_ops.sdca_optimizer( - sparse_example_indices, - sparse_feature_indices, - sparse_features_values, - self._convert_n_to_tensor(self._examples['dense_features']), - internal_convert_to_tensor(self._examples['example_weights']), - internal_convert_to_tensor(self._examples['example_labels']), - sparse_indices, - sparse_weights, - self._convert_n_to_tensor(self._slots[ - 'unshrinked_dense_features_weights']), - example_state_data, - loss_type=self._options['loss_type'], - l1=self._options['symmetric_l1_regularization'], - l2=self._symmetric_l2_regularization(), - num_loss_partitions=self._num_loss_partitions(), - num_inner_iterations=1, - adaptative=self._adaptive()) + if compat.forward_compatible(year=2018, month=10, day=30): + esu, sfw, dfw = gen_sdca_ops.sdca_optimizer_v2( + sparse_example_indices, + sparse_feature_indices, + sparse_features_values, + self._convert_n_to_tensor(self._examples['dense_features']), + internal_convert_to_tensor(self._examples['example_weights']), + internal_convert_to_tensor(self._examples['example_labels']), + sparse_indices, + sparse_weights, + self._convert_n_to_tensor(self._slots[ + 'unshrinked_dense_features_weights']), + example_state_data, + loss_type=self._options['loss_type'], + l1=self._options['symmetric_l1_regularization'], + l2=self._symmetric_l2_regularization(), + num_loss_partitions=self._num_loss_partitions(), + num_inner_iterations=1, + adaptive=self._adaptive()) + else: + esu, sfw, dfw = gen_sdca_ops.sdca_optimizer( + sparse_example_indices, + sparse_feature_indices, + sparse_features_values, + self._convert_n_to_tensor(self._examples['dense_features']), + internal_convert_to_tensor(self._examples['example_weights']), + internal_convert_to_tensor(self._examples['example_labels']), + sparse_indices, + sparse_weights, + self._convert_n_to_tensor(self._slots[ + 'unshrinked_dense_features_weights']), + example_state_data, + loss_type=self._options['loss_type'], + l1=self._options['symmetric_l1_regularization'], + l2=self._symmetric_l2_regularization(), + num_loss_partitions=self._num_loss_partitions(), + num_inner_iterations=1, + adaptative=self._adaptive()) # pylint: enable=protected-access with ops.control_dependencies([esu]): diff --git a/tensorflow/core/api_def/base_api/api_def_SdcaOptimizerV2.pbtxt b/tensorflow/core/api_def/base_api/api_def_SdcaOptimizerV2.pbtxt new file mode 100644 index 0000000000..c615dee8c7 --- /dev/null +++ b/tensorflow/core/api_def/base_api/api_def_SdcaOptimizerV2.pbtxt @@ -0,0 +1,171 @@ +op { + graph_op_name: "SdcaOptimizerV2" + visibility: HIDDEN + in_arg { + name: "sparse_example_indices" + description: < +Shai Shalev-Shwartz, Tong Zhang. 2012 + +$$Loss Objective = \sum f_{i} (wx_{i}) + (l2 / 2) * |w|^2 + l1 * |w|$$ + +[Adding vs. Averaging in Distributed Primal-Dual Optimization](http://arxiv.org/abs/1502.03508).
+Chenxin Ma, Virginia Smith, Martin Jaggi, Michael I. Jordan, +Peter Richtarik, Martin Takac. 2015 + +[Stochastic Dual Coordinate Ascent with Adaptive Probabilities](https://arxiv.org/abs/1502.08053).
+Dominik Csiba, Zheng Qu, Peter Richtarik. 2015 +END +} diff --git a/tensorflow/core/kernels/sdca_ops.cc b/tensorflow/core/kernels/sdca_ops.cc index 3bd4168dc7..d0e0b15da7 100644 --- a/tensorflow/core/kernels/sdca_ops.cc +++ b/tensorflow/core/kernels/sdca_ops.cc @@ -83,7 +83,11 @@ struct ComputeOptions { context, false, errors::InvalidArgument("Unsupported loss type: ", loss_type)); } - OP_REQUIRES_OK(context, context->GetAttr("adaptative", &adaptive)); + auto s = context->GetAttr("adaptative", &adaptive); + if (!s.ok()) { + s = context->GetAttr("adaptive", &adaptive); + } + OP_REQUIRES_OK(context, s); OP_REQUIRES_OK( context, context->GetAttr("num_sparse_features", &num_sparse_features)); OP_REQUIRES_OK(context, context->GetAttr("num_sparse_features_with_values", @@ -245,6 +249,8 @@ class SdcaOptimizer : public OpKernel { }; REGISTER_KERNEL_BUILDER(Name("SdcaOptimizer").Device(DEVICE_CPU), SdcaOptimizer); +REGISTER_KERNEL_BUILDER(Name("SdcaOptimizerV2").Device(DEVICE_CPU), + SdcaOptimizer); class SdcaShrinkL1 : public OpKernel { public: diff --git a/tensorflow/core/ops/sdca_ops.cc b/tensorflow/core/ops/sdca_ops.cc index fdf53a55dd..51d248f2d6 100644 --- a/tensorflow/core/ops/sdca_ops.cc +++ b/tensorflow/core/ops/sdca_ops.cc @@ -65,6 +65,34 @@ REGISTER_OP("SdcaOptimizer") .Output("out_delta_dense_weights: num_dense_features * float") .SetShapeFn(ApplySdcaOptimizerShapeFn); +// The SdcaOptimizerV2 op fixes the "adaptative" typo in v1. +REGISTER_OP("SdcaOptimizerV2") + .Attr( + "loss_type: {'logistic_loss', 'squared_loss', 'hinge_loss'," + "'smooth_hinge_loss', 'poisson_loss'}") + .Attr("adaptive : bool=false") + .Attr("num_sparse_features: int >= 0") + .Attr("num_sparse_features_with_values: int >= 0") + .Attr("num_dense_features: int >= 0") + .Attr("l1: float") + .Attr("l2: float") + .Attr("num_loss_partitions: int >= 1") + .Attr("num_inner_iterations: int >= 1") + .Input("sparse_example_indices: num_sparse_features * int64") + .Input("sparse_feature_indices: num_sparse_features * int64") + .Input("sparse_feature_values: num_sparse_features_with_values * float") + .Input("dense_features: num_dense_features * float") + .Input("example_weights: float") + .Input("example_labels: float") + .Input("sparse_indices: num_sparse_features * int64") + .Input("sparse_weights: num_sparse_features * float") + .Input("dense_weights: num_dense_features * float") + .Input("example_state_data: float") + .Output("out_example_state_data: float") + .Output("out_delta_sparse_weights: num_sparse_features * float") + .Output("out_delta_dense_weights: num_dense_features * float") + .SetShapeFn(ApplySdcaOptimizerShapeFn); + REGISTER_OP("SdcaShrinkL1") .Attr("num_features: int >= 0") .Attr("l1: float") diff --git a/tensorflow/python/ops/sdca_ops.py b/tensorflow/python/ops/sdca_ops.py index 4d5aeec591..a1c68343ed 100644 --- a/tensorflow/python/ops/sdca_ops.py +++ b/tensorflow/python/ops/sdca_ops.py @@ -29,4 +29,5 @@ from tensorflow.python.ops.gen_sdca_ops import * ops.NotDifferentiable("SdcaFprint") ops.NotDifferentiable("SdcaOptimizer") +ops.NotDifferentiable("SdcaOptimizerV2") ops.NotDifferentiable("SdcaShrinkL1") -- cgit v1.2.3