Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/ops/gen_sdca_ops.py: 12%

348 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1"""Python wrappers around TensorFlow ops. 

2 

3This file is MACHINE GENERATED! Do not edit. 

4""" 

5 

6import collections 

7 

8from tensorflow.python import pywrap_tfe as pywrap_tfe 

9from tensorflow.python.eager import context as _context 

10from tensorflow.python.eager import core as _core 

11from tensorflow.python.eager import execute as _execute 

12from tensorflow.python.framework import dtypes as _dtypes 

13from tensorflow.security.fuzzing.py import annotation_types as _atypes 

14 

15from tensorflow.python.framework import op_def_registry as _op_def_registry 

16from tensorflow.python.framework import ops as _ops 

17from tensorflow.python.framework import op_def_library as _op_def_library 

18from tensorflow.python.util.deprecation import deprecated_endpoints 

19from tensorflow.python.util import dispatch as _dispatch 

20from tensorflow.python.util.tf_export import tf_export 

21 

22from typing import TypeVar 

23 

24@_dispatch.add_fallback_dispatch_list 

25@_dispatch.add_type_based_api_dispatcher 

26@tf_export(v1=['train.sdca_fprint']) 

27@deprecated_endpoints('train.sdca_fprint') 

28def sdca_fprint(input, name=None): 

29 r"""Computes fingerprints of the input strings. 

30 

31 Args: 

32 input: A `Tensor` of type `string`. 

33 vector of strings to compute fingerprints on. 

34 name: A name for the operation (optional). 

35 

36 Returns: 

37 A `Tensor` of type `int64`. 

38 """ 

39 _ctx = _context._context or _context.context() 

40 tld = _ctx._thread_local_data 

41 if tld.is_eager: 

42 try: 

43 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

44 _ctx, "SdcaFprint", name, input) 

45 return _result 

46 except _core._NotOkStatusException as e: 

47 _ops.raise_from_not_ok_status(e, name) 

48 except _core._FallbackException: 

49 pass 

50 try: 

51 _result = _dispatcher_for_sdca_fprint( 

52 (input, name,), None) 

53 if _result is not NotImplemented: 

54 return _result 

55 return sdca_fprint_eager_fallback( 

56 input, name=name, ctx=_ctx) 

57 except _core._SymbolicException: 

58 pass # Add nodes to the TensorFlow graph. 

59 except (TypeError, ValueError): 

60 _result = _dispatch.dispatch( 

61 sdca_fprint, (), dict(input=input, name=name) 

62 ) 

63 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

64 return _result 

65 raise 

66 else: 

67 _result = _dispatcher_for_sdca_fprint( 

68 (input, name,), None) 

69 if _result is not NotImplemented: 

70 return _result 

71 # Add nodes to the TensorFlow graph. 

72 try: 

73 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

74 "SdcaFprint", input=input, name=name) 

75 except (TypeError, ValueError): 

76 _result = _dispatch.dispatch( 

77 sdca_fprint, (), dict(input=input, name=name) 

78 ) 

79 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

80 return _result 

81 raise 

82 _result = _outputs[:] 

83 if _execute.must_record_gradient(): 

84 _attrs = () 

85 _inputs_flat = _op.inputs 

86 _execute.record_gradient( 

87 "SdcaFprint", _inputs_flat, _attrs, _result) 

88 _result, = _result 

89 return _result 

90 

91SdcaFprint = tf_export("raw_ops.SdcaFprint")(_ops.to_raw_op(sdca_fprint)) 

92_dispatcher_for_sdca_fprint = sdca_fprint._tf_type_based_dispatcher.Dispatch 

93 

94 

95def sdca_fprint_eager_fallback(input, name, ctx): 

96 input = _ops.convert_to_tensor(input, _dtypes.string) 

97 _inputs_flat = [input] 

98 _attrs = None 

99 _result = _execute.execute(b"SdcaFprint", 1, inputs=_inputs_flat, 

100 attrs=_attrs, ctx=ctx, name=name) 

101 if _execute.must_record_gradient(): 

102 _execute.record_gradient( 

103 "SdcaFprint", _inputs_flat, _attrs, _result) 

104 _result, = _result 

105 return _result 

106 

107_SdcaOptimizerOutput = collections.namedtuple( 

108 "SdcaOptimizer", 

109 ["out_example_state_data", "out_delta_sparse_weights", "out_delta_dense_weights"]) 

110 

111 

112@_dispatch.add_fallback_dispatch_list 

113@_dispatch.add_type_based_api_dispatcher 

114@tf_export(v1=['train.sdca_optimizer']) 

115@deprecated_endpoints('train.sdca_optimizer') 

116def sdca_optimizer(sparse_example_indices, sparse_feature_indices, sparse_feature_values, dense_features, example_weights, example_labels, sparse_indices, sparse_weights, dense_weights, example_state_data, loss_type, l1, l2, num_loss_partitions, num_inner_iterations, adaptative=True, name=None): 

117 r"""Distributed version of Stochastic Dual Coordinate Ascent (SDCA) optimizer for 

118 

119 linear models with L1 + L2 regularization. As global optimization objective is 

120 strongly-convex, the optimizer optimizes the dual objective at each step. The 

121 optimizer applies each update one example at a time. Examples are sampled 

122 uniformly, and the optimizer is learning rate free and enjoys linear convergence 

123 rate. 

124 

125 [Proximal Stochastic Dual Coordinate Ascent](http://arxiv.org/pdf/1211.2717v1.pdf).<br> 

126 Shai Shalev-Shwartz, Tong Zhang. 2012 

127 

128 $$Loss Objective = \sum f_{i} (wx_{i}) + (l2 / 2) * |w|^2 + l1 * |w|$$ 

129 

130 [Adding vs. Averaging in Distributed Primal-Dual Optimization](http://arxiv.org/abs/1502.03508).<br> 

131 Chenxin Ma, Virginia Smith, Martin Jaggi, Michael I. Jordan, 

132 Peter Richtarik, Martin Takac. 2015 

133 

134 [Stochastic Dual Coordinate Ascent with Adaptive Probabilities](https://arxiv.org/abs/1502.08053).<br> 

135 Dominik Csiba, Zheng Qu, Peter Richtarik. 2015 

136 

137 Args: 

138 sparse_example_indices: A list of `Tensor` objects with type `int64`. 

139 a list of vectors which contain example indices. 

140 sparse_feature_indices: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `int64`. 

141 a list of vectors which contain feature indices. 

142 sparse_feature_values: A list of `Tensor` objects with type `float32`. 

143 a list of vectors which contains feature value 

144 associated with each feature group. 

145 dense_features: A list of `Tensor` objects with type `float32`. 

146 a list of matrices which contains the dense feature values. 

147 example_weights: A `Tensor` of type `float32`. 

148 a vector which contains the weight associated with each 

149 example. 

150 example_labels: A `Tensor` of type `float32`. 

151 a vector which contains the label/target associated with each 

152 example. 

153 sparse_indices: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `int64`. 

154 a list of vectors where each value is the indices which has 

155 corresponding weights in sparse_weights. This field maybe omitted for the 

156 dense approach. 

157 sparse_weights: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `float32`. 

158 a list of vectors where each value is the weight associated with 

159 a sparse feature group. 

160 dense_weights: A list with the same length as `dense_features` of `Tensor` objects with type `float32`. 

161 a list of vectors where the values are the weights associated 

162 with a dense feature group. 

163 example_state_data: A `Tensor` of type `float32`. 

164 a list of vectors containing the example state data. 

165 loss_type: A `string` from: `"logistic_loss", "squared_loss", "hinge_loss", "smooth_hinge_loss", "poisson_loss"`. 

166 Type of the primal loss. Currently SdcaSolver supports logistic, 

167 squared and hinge losses. 

168 l1: A `float`. Symmetric l1 regularization strength. 

169 l2: A `float`. Symmetric l2 regularization strength. 

170 num_loss_partitions: An `int` that is `>= 1`. 

171 Number of partitions of the global loss function. 

172 num_inner_iterations: An `int` that is `>= 1`. 

173 Number of iterations per mini-batch. 

174 adaptative: An optional `bool`. Defaults to `True`. 

175 Whether to use Adaptive SDCA for the inner loop. 

176 name: A name for the operation (optional). 

177 

178 Returns: 

179 A tuple of `Tensor` objects (out_example_state_data, out_delta_sparse_weights, out_delta_dense_weights). 

180 

181 out_example_state_data: A `Tensor` of type `float32`. 

182 out_delta_sparse_weights: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `float32`. 

183 out_delta_dense_weights: A list with the same length as `dense_features` of `Tensor` objects with type `float32`. 

184 """ 

185 _ctx = _context._context or _context.context() 

186 tld = _ctx._thread_local_data 

187 if tld.is_eager: 

188 try: 

189 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

190 _ctx, "SdcaOptimizer", name, sparse_example_indices, 

191 sparse_feature_indices, sparse_feature_values, dense_features, 

192 example_weights, example_labels, sparse_indices, sparse_weights, 

193 dense_weights, example_state_data, "loss_type", loss_type, 

194 "adaptative", adaptative, "l1", l1, "l2", l2, "num_loss_partitions", 

195 num_loss_partitions, "num_inner_iterations", num_inner_iterations) 

196 _result = _SdcaOptimizerOutput._make(_result) 

197 return _result 

198 except _core._NotOkStatusException as e: 

199 _ops.raise_from_not_ok_status(e, name) 

200 except _core._FallbackException: 

201 pass 

202 try: 

203 _result = _dispatcher_for_sdca_optimizer( 

204 (sparse_example_indices, sparse_feature_indices, 

205 sparse_feature_values, dense_features, example_weights, 

206 example_labels, sparse_indices, sparse_weights, dense_weights, 

207 example_state_data, loss_type, l1, l2, num_loss_partitions, 

208 num_inner_iterations, adaptative, name,), None) 

209 if _result is not NotImplemented: 

210 return _result 

211 return sdca_optimizer_eager_fallback( 

212 sparse_example_indices, sparse_feature_indices, 

213 sparse_feature_values, dense_features, example_weights, 

214 example_labels, sparse_indices, sparse_weights, dense_weights, 

215 example_state_data, loss_type=loss_type, adaptative=adaptative, 

216 l1=l1, l2=l2, num_loss_partitions=num_loss_partitions, 

217 num_inner_iterations=num_inner_iterations, name=name, ctx=_ctx) 

218 except _core._SymbolicException: 

219 pass # Add nodes to the TensorFlow graph. 

220 except (TypeError, ValueError): 

221 _result = _dispatch.dispatch( 

222 sdca_optimizer, (), dict(sparse_example_indices=sparse_example_indices, 

223 sparse_feature_indices=sparse_feature_indices, 

224 sparse_feature_values=sparse_feature_values, 

225 dense_features=dense_features, 

226 example_weights=example_weights, 

227 example_labels=example_labels, 

228 sparse_indices=sparse_indices, 

229 sparse_weights=sparse_weights, 

230 dense_weights=dense_weights, 

231 example_state_data=example_state_data, 

232 loss_type=loss_type, l1=l1, l2=l2, 

233 num_loss_partitions=num_loss_partitions, 

234 num_inner_iterations=num_inner_iterations, 

235 adaptative=adaptative, name=name) 

236 ) 

237 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

238 return _result 

239 raise 

240 else: 

241 _result = _dispatcher_for_sdca_optimizer( 

242 (sparse_example_indices, sparse_feature_indices, 

243 sparse_feature_values, dense_features, example_weights, 

244 example_labels, sparse_indices, sparse_weights, dense_weights, 

245 example_state_data, loss_type, l1, l2, num_loss_partitions, 

246 num_inner_iterations, adaptative, name,), None) 

247 if _result is not NotImplemented: 

248 return _result 

249 # Add nodes to the TensorFlow graph. 

250 if not isinstance(sparse_example_indices, (list, tuple)): 

251 raise TypeError( 

252 "Expected list for 'sparse_example_indices' argument to " 

253 "'sdca_optimizer' Op, not %r." % sparse_example_indices) 

254 _attr_num_sparse_features = len(sparse_example_indices) 

255 if not isinstance(sparse_feature_indices, (list, tuple)): 

256 raise TypeError( 

257 "Expected list for 'sparse_feature_indices' argument to " 

258 "'sdca_optimizer' Op, not %r." % sparse_feature_indices) 

259 if len(sparse_feature_indices) != _attr_num_sparse_features: 

260 raise ValueError( 

261 "List argument 'sparse_feature_indices' to 'sdca_optimizer' Op with length %d " 

262 "must match length %d of argument 'sparse_example_indices'." % 

263 (len(sparse_feature_indices), _attr_num_sparse_features)) 

264 if not isinstance(sparse_indices, (list, tuple)): 

265 raise TypeError( 

266 "Expected list for 'sparse_indices' argument to " 

267 "'sdca_optimizer' Op, not %r." % sparse_indices) 

268 if len(sparse_indices) != _attr_num_sparse_features: 

269 raise ValueError( 

270 "List argument 'sparse_indices' to 'sdca_optimizer' Op with length %d " 

271 "must match length %d of argument 'sparse_example_indices'." % 

272 (len(sparse_indices), _attr_num_sparse_features)) 

273 if not isinstance(sparse_weights, (list, tuple)): 

274 raise TypeError( 

275 "Expected list for 'sparse_weights' argument to " 

276 "'sdca_optimizer' Op, not %r." % sparse_weights) 

277 if len(sparse_weights) != _attr_num_sparse_features: 

278 raise ValueError( 

279 "List argument 'sparse_weights' to 'sdca_optimizer' Op with length %d " 

280 "must match length %d of argument 'sparse_example_indices'." % 

281 (len(sparse_weights), _attr_num_sparse_features)) 

282 if not isinstance(sparse_feature_values, (list, tuple)): 

283 raise TypeError( 

284 "Expected list for 'sparse_feature_values' argument to " 

285 "'sdca_optimizer' Op, not %r." % sparse_feature_values) 

286 _attr_num_sparse_features_with_values = len(sparse_feature_values) 

287 if not isinstance(dense_features, (list, tuple)): 

288 raise TypeError( 

289 "Expected list for 'dense_features' argument to " 

290 "'sdca_optimizer' Op, not %r." % dense_features) 

291 _attr_num_dense_features = len(dense_features) 

292 if not isinstance(dense_weights, (list, tuple)): 

293 raise TypeError( 

294 "Expected list for 'dense_weights' argument to " 

295 "'sdca_optimizer' Op, not %r." % dense_weights) 

296 if len(dense_weights) != _attr_num_dense_features: 

297 raise ValueError( 

298 "List argument 'dense_weights' to 'sdca_optimizer' Op with length %d " 

299 "must match length %d of argument 'dense_features'." % 

300 (len(dense_weights), _attr_num_dense_features)) 

301 loss_type = _execute.make_str(loss_type, "loss_type") 

302 l1 = _execute.make_float(l1, "l1") 

303 l2 = _execute.make_float(l2, "l2") 

304 num_loss_partitions = _execute.make_int(num_loss_partitions, "num_loss_partitions") 

305 num_inner_iterations = _execute.make_int(num_inner_iterations, "num_inner_iterations") 

306 if adaptative is None: 

307 adaptative = True 

308 adaptative = _execute.make_bool(adaptative, "adaptative") 

309 try: 

310 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

311 "SdcaOptimizer", sparse_example_indices=sparse_example_indices, 

312 sparse_feature_indices=sparse_feature_indices, 

313 sparse_feature_values=sparse_feature_values, 

314 dense_features=dense_features, 

315 example_weights=example_weights, 

316 example_labels=example_labels, 

317 sparse_indices=sparse_indices, 

318 sparse_weights=sparse_weights, 

319 dense_weights=dense_weights, 

320 example_state_data=example_state_data, 

321 loss_type=loss_type, l1=l1, l2=l2, 

322 num_loss_partitions=num_loss_partitions, 

323 num_inner_iterations=num_inner_iterations, 

324 adaptative=adaptative, name=name) 

325 except (TypeError, ValueError): 

326 _result = _dispatch.dispatch( 

327 sdca_optimizer, (), dict(sparse_example_indices=sparse_example_indices, 

328 sparse_feature_indices=sparse_feature_indices, 

329 sparse_feature_values=sparse_feature_values, 

330 dense_features=dense_features, 

331 example_weights=example_weights, 

332 example_labels=example_labels, 

333 sparse_indices=sparse_indices, 

334 sparse_weights=sparse_weights, 

335 dense_weights=dense_weights, 

336 example_state_data=example_state_data, 

337 loss_type=loss_type, l1=l1, l2=l2, 

338 num_loss_partitions=num_loss_partitions, 

339 num_inner_iterations=num_inner_iterations, 

340 adaptative=adaptative, name=name) 

341 ) 

342 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

343 return _result 

344 raise 

345 _result = _outputs[:] 

346 if _execute.must_record_gradient(): 

347 _attrs = ("loss_type", _op.get_attr("loss_type"), "adaptative", 

348 _op._get_attr_bool("adaptative"), "num_sparse_features", 

349 _op._get_attr_int("num_sparse_features"), 

350 "num_sparse_features_with_values", 

351 _op._get_attr_int("num_sparse_features_with_values"), 

352 "num_dense_features", _op._get_attr_int("num_dense_features"), 

353 "l1", _op.get_attr("l1"), "l2", _op.get_attr("l2"), 

354 "num_loss_partitions", _op._get_attr_int("num_loss_partitions"), 

355 "num_inner_iterations", 

356 _op._get_attr_int("num_inner_iterations")) 

357 _inputs_flat = _op.inputs 

358 _execute.record_gradient( 

359 "SdcaOptimizer", _inputs_flat, _attrs, _result) 

360 _result = _result[:1] + [_result[1:1 + _attr_num_sparse_features]] + _result[1 + _attr_num_sparse_features:] 

361 _result = _result[:2] + [_result[2:]] 

362 _result = _SdcaOptimizerOutput._make(_result) 

363 return _result 

364 

365SdcaOptimizer = tf_export("raw_ops.SdcaOptimizer")(_ops.to_raw_op(sdca_optimizer)) 

366_dispatcher_for_sdca_optimizer = sdca_optimizer._tf_type_based_dispatcher.Dispatch 

367 

368 

369def sdca_optimizer_eager_fallback(sparse_example_indices, sparse_feature_indices, sparse_feature_values, dense_features, example_weights, example_labels, sparse_indices, sparse_weights, dense_weights, example_state_data, loss_type, l1, l2, num_loss_partitions, num_inner_iterations, adaptative, name, ctx): 

370 if not isinstance(sparse_example_indices, (list, tuple)): 

371 raise TypeError( 

372 "Expected list for 'sparse_example_indices' argument to " 

373 "'sdca_optimizer' Op, not %r." % sparse_example_indices) 

374 _attr_num_sparse_features = len(sparse_example_indices) 

375 if not isinstance(sparse_feature_indices, (list, tuple)): 

376 raise TypeError( 

377 "Expected list for 'sparse_feature_indices' argument to " 

378 "'sdca_optimizer' Op, not %r." % sparse_feature_indices) 

379 if len(sparse_feature_indices) != _attr_num_sparse_features: 

380 raise ValueError( 

381 "List argument 'sparse_feature_indices' to 'sdca_optimizer' Op with length %d " 

382 "must match length %d of argument 'sparse_example_indices'." % 

383 (len(sparse_feature_indices), _attr_num_sparse_features)) 

384 if not isinstance(sparse_indices, (list, tuple)): 

385 raise TypeError( 

386 "Expected list for 'sparse_indices' argument to " 

387 "'sdca_optimizer' Op, not %r." % sparse_indices) 

388 if len(sparse_indices) != _attr_num_sparse_features: 

389 raise ValueError( 

390 "List argument 'sparse_indices' to 'sdca_optimizer' Op with length %d " 

391 "must match length %d of argument 'sparse_example_indices'." % 

392 (len(sparse_indices), _attr_num_sparse_features)) 

393 if not isinstance(sparse_weights, (list, tuple)): 

394 raise TypeError( 

395 "Expected list for 'sparse_weights' argument to " 

396 "'sdca_optimizer' Op, not %r." % sparse_weights) 

397 if len(sparse_weights) != _attr_num_sparse_features: 

398 raise ValueError( 

399 "List argument 'sparse_weights' to 'sdca_optimizer' Op with length %d " 

400 "must match length %d of argument 'sparse_example_indices'." % 

401 (len(sparse_weights), _attr_num_sparse_features)) 

402 if not isinstance(sparse_feature_values, (list, tuple)): 

403 raise TypeError( 

404 "Expected list for 'sparse_feature_values' argument to " 

405 "'sdca_optimizer' Op, not %r." % sparse_feature_values) 

406 _attr_num_sparse_features_with_values = len(sparse_feature_values) 

407 if not isinstance(dense_features, (list, tuple)): 

408 raise TypeError( 

409 "Expected list for 'dense_features' argument to " 

410 "'sdca_optimizer' Op, not %r." % dense_features) 

411 _attr_num_dense_features = len(dense_features) 

412 if not isinstance(dense_weights, (list, tuple)): 

413 raise TypeError( 

414 "Expected list for 'dense_weights' argument to " 

415 "'sdca_optimizer' Op, not %r." % dense_weights) 

416 if len(dense_weights) != _attr_num_dense_features: 

417 raise ValueError( 

418 "List argument 'dense_weights' to 'sdca_optimizer' Op with length %d " 

419 "must match length %d of argument 'dense_features'." % 

420 (len(dense_weights), _attr_num_dense_features)) 

421 loss_type = _execute.make_str(loss_type, "loss_type") 

422 l1 = _execute.make_float(l1, "l1") 

423 l2 = _execute.make_float(l2, "l2") 

424 num_loss_partitions = _execute.make_int(num_loss_partitions, "num_loss_partitions") 

425 num_inner_iterations = _execute.make_int(num_inner_iterations, "num_inner_iterations") 

426 if adaptative is None: 

427 adaptative = True 

428 adaptative = _execute.make_bool(adaptative, "adaptative") 

429 sparse_example_indices = _ops.convert_n_to_tensor(sparse_example_indices, _dtypes.int64) 

430 sparse_feature_indices = _ops.convert_n_to_tensor(sparse_feature_indices, _dtypes.int64) 

431 sparse_feature_values = _ops.convert_n_to_tensor(sparse_feature_values, _dtypes.float32) 

432 dense_features = _ops.convert_n_to_tensor(dense_features, _dtypes.float32) 

433 example_weights = _ops.convert_to_tensor(example_weights, _dtypes.float32) 

434 example_labels = _ops.convert_to_tensor(example_labels, _dtypes.float32) 

435 sparse_indices = _ops.convert_n_to_tensor(sparse_indices, _dtypes.int64) 

436 sparse_weights = _ops.convert_n_to_tensor(sparse_weights, _dtypes.float32) 

437 dense_weights = _ops.convert_n_to_tensor(dense_weights, _dtypes.float32) 

438 example_state_data = _ops.convert_to_tensor(example_state_data, _dtypes.float32) 

439 _inputs_flat = list(sparse_example_indices) + list(sparse_feature_indices) + list(sparse_feature_values) + list(dense_features) + [example_weights, example_labels] + list(sparse_indices) + list(sparse_weights) + list(dense_weights) + [example_state_data] 

440 _attrs = ("loss_type", loss_type, "adaptative", adaptative, 

441 "num_sparse_features", _attr_num_sparse_features, 

442 "num_sparse_features_with_values", _attr_num_sparse_features_with_values, 

443 "num_dense_features", _attr_num_dense_features, "l1", l1, "l2", l2, 

444 "num_loss_partitions", num_loss_partitions, "num_inner_iterations", 

445 num_inner_iterations) 

446 _result = _execute.execute(b"SdcaOptimizer", _attr_num_sparse_features + 

447 _attr_num_dense_features + 1, 

448 inputs=_inputs_flat, attrs=_attrs, ctx=ctx, 

449 name=name) 

450 if _execute.must_record_gradient(): 

451 _execute.record_gradient( 

452 "SdcaOptimizer", _inputs_flat, _attrs, _result) 

453 _result = _result[:1] + [_result[1:1 + _attr_num_sparse_features]] + _result[1 + _attr_num_sparse_features:] 

454 _result = _result[:2] + [_result[2:]] 

455 _result = _SdcaOptimizerOutput._make(_result) 

456 return _result 

457 

458_SdcaOptimizerV2Output = collections.namedtuple( 

459 "SdcaOptimizerV2", 

460 ["out_example_state_data", "out_delta_sparse_weights", "out_delta_dense_weights"]) 

461 

462 

463def sdca_optimizer_v2(sparse_example_indices, sparse_feature_indices, sparse_feature_values, dense_features, example_weights, example_labels, sparse_indices, sparse_weights, dense_weights, example_state_data, loss_type, l1, l2, num_loss_partitions, num_inner_iterations, adaptive=True, name=None): 

464 r"""Distributed version of Stochastic Dual Coordinate Ascent (SDCA) optimizer for 

465 

466 linear models with L1 + L2 regularization. As global optimization objective is 

467 strongly-convex, the optimizer optimizes the dual objective at each step. The 

468 optimizer applies each update one example at a time. Examples are sampled 

469 uniformly, and the optimizer is learning rate free and enjoys linear convergence 

470 rate. 

471 

472 [Proximal Stochastic Dual Coordinate Ascent](http://arxiv.org/pdf/1211.2717v1.pdf).<br> 

473 Shai Shalev-Shwartz, Tong Zhang. 2012 

474 

475 $$Loss Objective = \sum f_{i} (wx_{i}) + (l2 / 2) * |w|^2 + l1 * |w|$$ 

476 

477 [Adding vs. Averaging in Distributed Primal-Dual Optimization](http://arxiv.org/abs/1502.03508).<br> 

478 Chenxin Ma, Virginia Smith, Martin Jaggi, Michael I. Jordan, 

479 Peter Richtarik, Martin Takac. 2015 

480 

481 [Stochastic Dual Coordinate Ascent with Adaptive Probabilities](https://arxiv.org/abs/1502.08053).<br> 

482 Dominik Csiba, Zheng Qu, Peter Richtarik. 2015 

483 

484 Args: 

485 sparse_example_indices: A list of `Tensor` objects with type `int64`. 

486 a list of vectors which contain example indices. 

487 sparse_feature_indices: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `int64`. 

488 a list of vectors which contain feature indices. 

489 sparse_feature_values: A list of `Tensor` objects with type `float32`. 

490 a list of vectors which contains feature value 

491 associated with each feature group. 

492 dense_features: A list of `Tensor` objects with type `float32`. 

493 a list of matrices which contains the dense feature values. 

494 example_weights: A `Tensor` of type `float32`. 

495 a vector which contains the weight associated with each 

496 example. 

497 example_labels: A `Tensor` of type `float32`. 

498 a vector which contains the label/target associated with each 

499 example. 

500 sparse_indices: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `int64`. 

501 a list of vectors where each value is the indices which has 

502 corresponding weights in sparse_weights. This field maybe omitted for the 

503 dense approach. 

504 sparse_weights: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `float32`. 

505 a list of vectors where each value is the weight associated with 

506 a sparse feature group. 

507 dense_weights: A list with the same length as `dense_features` of `Tensor` objects with type `float32`. 

508 a list of vectors where the values are the weights associated 

509 with a dense feature group. 

510 example_state_data: A `Tensor` of type `float32`. 

511 a list of vectors containing the example state data. 

512 loss_type: A `string` from: `"logistic_loss", "squared_loss", "hinge_loss", "smooth_hinge_loss", "poisson_loss"`. 

513 Type of the primal loss. Currently SdcaSolver supports logistic, 

514 squared and hinge losses. 

515 l1: A `float`. Symmetric l1 regularization strength. 

516 l2: A `float`. Symmetric l2 regularization strength. 

517 num_loss_partitions: An `int` that is `>= 1`. 

518 Number of partitions of the global loss function. 

519 num_inner_iterations: An `int` that is `>= 1`. 

520 Number of iterations per mini-batch. 

521 adaptive: An optional `bool`. Defaults to `True`. 

522 Whether to use Adaptive SDCA for the inner loop. 

523 name: A name for the operation (optional). 

524 

525 Returns: 

526 A tuple of `Tensor` objects (out_example_state_data, out_delta_sparse_weights, out_delta_dense_weights). 

527 

528 out_example_state_data: A `Tensor` of type `float32`. 

529 out_delta_sparse_weights: A list with the same length as `sparse_example_indices` of `Tensor` objects with type `float32`. 

530 out_delta_dense_weights: A list with the same length as `dense_features` of `Tensor` objects with type `float32`. 

531 """ 

532 _ctx = _context._context or _context.context() 

533 tld = _ctx._thread_local_data 

534 if tld.is_eager: 

535 try: 

536 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

537 _ctx, "SdcaOptimizerV2", name, sparse_example_indices, 

538 sparse_feature_indices, sparse_feature_values, dense_features, 

539 example_weights, example_labels, sparse_indices, sparse_weights, 

540 dense_weights, example_state_data, "loss_type", loss_type, "adaptive", 

541 adaptive, "l1", l1, "l2", l2, "num_loss_partitions", 

542 num_loss_partitions, "num_inner_iterations", num_inner_iterations) 

543 _result = _SdcaOptimizerV2Output._make(_result) 

544 return _result 

545 except _core._NotOkStatusException as e: 

546 _ops.raise_from_not_ok_status(e, name) 

547 except _core._FallbackException: 

548 pass 

549 try: 

550 return sdca_optimizer_v2_eager_fallback( 

551 sparse_example_indices, sparse_feature_indices, 

552 sparse_feature_values, dense_features, example_weights, 

553 example_labels, sparse_indices, sparse_weights, dense_weights, 

554 example_state_data, loss_type=loss_type, adaptive=adaptive, l1=l1, 

555 l2=l2, num_loss_partitions=num_loss_partitions, 

556 num_inner_iterations=num_inner_iterations, name=name, ctx=_ctx) 

557 except _core._SymbolicException: 

558 pass # Add nodes to the TensorFlow graph. 

559 # Add nodes to the TensorFlow graph. 

560 if not isinstance(sparse_example_indices, (list, tuple)): 

561 raise TypeError( 

562 "Expected list for 'sparse_example_indices' argument to " 

563 "'sdca_optimizer_v2' Op, not %r." % sparse_example_indices) 

564 _attr_num_sparse_features = len(sparse_example_indices) 

565 if not isinstance(sparse_feature_indices, (list, tuple)): 

566 raise TypeError( 

567 "Expected list for 'sparse_feature_indices' argument to " 

568 "'sdca_optimizer_v2' Op, not %r." % sparse_feature_indices) 

569 if len(sparse_feature_indices) != _attr_num_sparse_features: 

570 raise ValueError( 

571 "List argument 'sparse_feature_indices' to 'sdca_optimizer_v2' Op with length %d " 

572 "must match length %d of argument 'sparse_example_indices'." % 

573 (len(sparse_feature_indices), _attr_num_sparse_features)) 

574 if not isinstance(sparse_indices, (list, tuple)): 

575 raise TypeError( 

576 "Expected list for 'sparse_indices' argument to " 

577 "'sdca_optimizer_v2' Op, not %r." % sparse_indices) 

578 if len(sparse_indices) != _attr_num_sparse_features: 

579 raise ValueError( 

580 "List argument 'sparse_indices' to 'sdca_optimizer_v2' Op with length %d " 

581 "must match length %d of argument 'sparse_example_indices'." % 

582 (len(sparse_indices), _attr_num_sparse_features)) 

583 if not isinstance(sparse_weights, (list, tuple)): 

584 raise TypeError( 

585 "Expected list for 'sparse_weights' argument to " 

586 "'sdca_optimizer_v2' Op, not %r." % sparse_weights) 

587 if len(sparse_weights) != _attr_num_sparse_features: 

588 raise ValueError( 

589 "List argument 'sparse_weights' to 'sdca_optimizer_v2' Op with length %d " 

590 "must match length %d of argument 'sparse_example_indices'." % 

591 (len(sparse_weights), _attr_num_sparse_features)) 

592 if not isinstance(sparse_feature_values, (list, tuple)): 

593 raise TypeError( 

594 "Expected list for 'sparse_feature_values' argument to " 

595 "'sdca_optimizer_v2' Op, not %r." % sparse_feature_values) 

596 _attr_num_sparse_features_with_values = len(sparse_feature_values) 

597 if not isinstance(dense_features, (list, tuple)): 

598 raise TypeError( 

599 "Expected list for 'dense_features' argument to " 

600 "'sdca_optimizer_v2' Op, not %r." % dense_features) 

601 _attr_num_dense_features = len(dense_features) 

602 if not isinstance(dense_weights, (list, tuple)): 

603 raise TypeError( 

604 "Expected list for 'dense_weights' argument to " 

605 "'sdca_optimizer_v2' Op, not %r." % dense_weights) 

606 if len(dense_weights) != _attr_num_dense_features: 

607 raise ValueError( 

608 "List argument 'dense_weights' to 'sdca_optimizer_v2' Op with length %d " 

609 "must match length %d of argument 'dense_features'." % 

610 (len(dense_weights), _attr_num_dense_features)) 

611 loss_type = _execute.make_str(loss_type, "loss_type") 

612 l1 = _execute.make_float(l1, "l1") 

613 l2 = _execute.make_float(l2, "l2") 

614 num_loss_partitions = _execute.make_int(num_loss_partitions, "num_loss_partitions") 

615 num_inner_iterations = _execute.make_int(num_inner_iterations, "num_inner_iterations") 

616 if adaptive is None: 

617 adaptive = True 

618 adaptive = _execute.make_bool(adaptive, "adaptive") 

619 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

620 "SdcaOptimizerV2", sparse_example_indices=sparse_example_indices, 

621 sparse_feature_indices=sparse_feature_indices, 

622 sparse_feature_values=sparse_feature_values, 

623 dense_features=dense_features, 

624 example_weights=example_weights, 

625 example_labels=example_labels, 

626 sparse_indices=sparse_indices, 

627 sparse_weights=sparse_weights, 

628 dense_weights=dense_weights, 

629 example_state_data=example_state_data, 

630 loss_type=loss_type, l1=l1, l2=l2, 

631 num_loss_partitions=num_loss_partitions, 

632 num_inner_iterations=num_inner_iterations, 

633 adaptive=adaptive, name=name) 

634 _result = _outputs[:] 

635 if _execute.must_record_gradient(): 

636 _attrs = ("loss_type", _op.get_attr("loss_type"), "adaptive", 

637 _op._get_attr_bool("adaptive"), "num_sparse_features", 

638 _op._get_attr_int("num_sparse_features"), 

639 "num_sparse_features_with_values", 

640 _op._get_attr_int("num_sparse_features_with_values"), 

641 "num_dense_features", _op._get_attr_int("num_dense_features"), 

642 "l1", _op.get_attr("l1"), "l2", _op.get_attr("l2"), 

643 "num_loss_partitions", _op._get_attr_int("num_loss_partitions"), 

644 "num_inner_iterations", 

645 _op._get_attr_int("num_inner_iterations")) 

646 _inputs_flat = _op.inputs 

647 _execute.record_gradient( 

648 "SdcaOptimizerV2", _inputs_flat, _attrs, _result) 

649 _result = _result[:1] + [_result[1:1 + _attr_num_sparse_features]] + _result[1 + _attr_num_sparse_features:] 

650 _result = _result[:2] + [_result[2:]] 

651 _result = _SdcaOptimizerV2Output._make(_result) 

652 return _result 

653 

654SdcaOptimizerV2 = tf_export("raw_ops.SdcaOptimizerV2")(_ops.to_raw_op(sdca_optimizer_v2)) 

655 

656 

657def sdca_optimizer_v2_eager_fallback(sparse_example_indices, sparse_feature_indices, sparse_feature_values, dense_features, example_weights, example_labels, sparse_indices, sparse_weights, dense_weights, example_state_data, loss_type, l1, l2, num_loss_partitions, num_inner_iterations, adaptive, name, ctx): 

658 if not isinstance(sparse_example_indices, (list, tuple)): 

659 raise TypeError( 

660 "Expected list for 'sparse_example_indices' argument to " 

661 "'sdca_optimizer_v2' Op, not %r." % sparse_example_indices) 

662 _attr_num_sparse_features = len(sparse_example_indices) 

663 if not isinstance(sparse_feature_indices, (list, tuple)): 

664 raise TypeError( 

665 "Expected list for 'sparse_feature_indices' argument to " 

666 "'sdca_optimizer_v2' Op, not %r." % sparse_feature_indices) 

667 if len(sparse_feature_indices) != _attr_num_sparse_features: 

668 raise ValueError( 

669 "List argument 'sparse_feature_indices' to 'sdca_optimizer_v2' Op with length %d " 

670 "must match length %d of argument 'sparse_example_indices'." % 

671 (len(sparse_feature_indices), _attr_num_sparse_features)) 

672 if not isinstance(sparse_indices, (list, tuple)): 

673 raise TypeError( 

674 "Expected list for 'sparse_indices' argument to " 

675 "'sdca_optimizer_v2' Op, not %r." % sparse_indices) 

676 if len(sparse_indices) != _attr_num_sparse_features: 

677 raise ValueError( 

678 "List argument 'sparse_indices' to 'sdca_optimizer_v2' Op with length %d " 

679 "must match length %d of argument 'sparse_example_indices'." % 

680 (len(sparse_indices), _attr_num_sparse_features)) 

681 if not isinstance(sparse_weights, (list, tuple)): 

682 raise TypeError( 

683 "Expected list for 'sparse_weights' argument to " 

684 "'sdca_optimizer_v2' Op, not %r." % sparse_weights) 

685 if len(sparse_weights) != _attr_num_sparse_features: 

686 raise ValueError( 

687 "List argument 'sparse_weights' to 'sdca_optimizer_v2' Op with length %d " 

688 "must match length %d of argument 'sparse_example_indices'." % 

689 (len(sparse_weights), _attr_num_sparse_features)) 

690 if not isinstance(sparse_feature_values, (list, tuple)): 

691 raise TypeError( 

692 "Expected list for 'sparse_feature_values' argument to " 

693 "'sdca_optimizer_v2' Op, not %r." % sparse_feature_values) 

694 _attr_num_sparse_features_with_values = len(sparse_feature_values) 

695 if not isinstance(dense_features, (list, tuple)): 

696 raise TypeError( 

697 "Expected list for 'dense_features' argument to " 

698 "'sdca_optimizer_v2' Op, not %r." % dense_features) 

699 _attr_num_dense_features = len(dense_features) 

700 if not isinstance(dense_weights, (list, tuple)): 

701 raise TypeError( 

702 "Expected list for 'dense_weights' argument to " 

703 "'sdca_optimizer_v2' Op, not %r." % dense_weights) 

704 if len(dense_weights) != _attr_num_dense_features: 

705 raise ValueError( 

706 "List argument 'dense_weights' to 'sdca_optimizer_v2' Op with length %d " 

707 "must match length %d of argument 'dense_features'." % 

708 (len(dense_weights), _attr_num_dense_features)) 

709 loss_type = _execute.make_str(loss_type, "loss_type") 

710 l1 = _execute.make_float(l1, "l1") 

711 l2 = _execute.make_float(l2, "l2") 

712 num_loss_partitions = _execute.make_int(num_loss_partitions, "num_loss_partitions") 

713 num_inner_iterations = _execute.make_int(num_inner_iterations, "num_inner_iterations") 

714 if adaptive is None: 

715 adaptive = True 

716 adaptive = _execute.make_bool(adaptive, "adaptive") 

717 sparse_example_indices = _ops.convert_n_to_tensor(sparse_example_indices, _dtypes.int64) 

718 sparse_feature_indices = _ops.convert_n_to_tensor(sparse_feature_indices, _dtypes.int64) 

719 sparse_feature_values = _ops.convert_n_to_tensor(sparse_feature_values, _dtypes.float32) 

720 dense_features = _ops.convert_n_to_tensor(dense_features, _dtypes.float32) 

721 example_weights = _ops.convert_to_tensor(example_weights, _dtypes.float32) 

722 example_labels = _ops.convert_to_tensor(example_labels, _dtypes.float32) 

723 sparse_indices = _ops.convert_n_to_tensor(sparse_indices, _dtypes.int64) 

724 sparse_weights = _ops.convert_n_to_tensor(sparse_weights, _dtypes.float32) 

725 dense_weights = _ops.convert_n_to_tensor(dense_weights, _dtypes.float32) 

726 example_state_data = _ops.convert_to_tensor(example_state_data, _dtypes.float32) 

727 _inputs_flat = list(sparse_example_indices) + list(sparse_feature_indices) + list(sparse_feature_values) + list(dense_features) + [example_weights, example_labels] + list(sparse_indices) + list(sparse_weights) + list(dense_weights) + [example_state_data] 

728 _attrs = ("loss_type", loss_type, "adaptive", adaptive, 

729 "num_sparse_features", _attr_num_sparse_features, 

730 "num_sparse_features_with_values", _attr_num_sparse_features_with_values, 

731 "num_dense_features", _attr_num_dense_features, "l1", l1, "l2", l2, 

732 "num_loss_partitions", num_loss_partitions, "num_inner_iterations", 

733 num_inner_iterations) 

734 _result = _execute.execute(b"SdcaOptimizerV2", _attr_num_sparse_features + 

735 _attr_num_dense_features + 1, 

736 inputs=_inputs_flat, attrs=_attrs, ctx=ctx, 

737 name=name) 

738 if _execute.must_record_gradient(): 

739 _execute.record_gradient( 

740 "SdcaOptimizerV2", _inputs_flat, _attrs, _result) 

741 _result = _result[:1] + [_result[1:1 + _attr_num_sparse_features]] + _result[1 + _attr_num_sparse_features:] 

742 _result = _result[:2] + [_result[2:]] 

743 _result = _SdcaOptimizerV2Output._make(_result) 

744 return _result 

745 

746 

747@_dispatch.add_fallback_dispatch_list 

748@_dispatch.add_type_based_api_dispatcher 

749@tf_export(v1=['train.sdca_shrink_l1']) 

750@deprecated_endpoints('train.sdca_shrink_l1') 

751def sdca_shrink_l1(weights, l1, l2, name=None): 

752 r"""Applies L1 regularization shrink step on the parameters. 

753 

754 Args: 

755 weights: A list of `Tensor` objects with type mutable `float32`. 

756 a list of vectors where each value is the weight associated with a 

757 feature group. 

758 l1: A `float`. Symmetric l1 regularization strength. 

759 l2: A `float`. 

760 Symmetric l2 regularization strength. Should be a positive float. 

761 name: A name for the operation (optional). 

762 

763 Returns: 

764 The created Operation. 

765 """ 

766 _ctx = _context._context or _context.context() 

767 tld = _ctx._thread_local_data 

768 if tld.is_eager: 

769 raise RuntimeError("sdca_shrink_l1 op does not support eager execution. Arg 'weights' is a ref.") 

770 else: 

771 _result = _dispatcher_for_sdca_shrink_l1( 

772 (weights, l1, l2, name,), None) 

773 if _result is not NotImplemented: 

774 return _result 

775 # Add nodes to the TensorFlow graph. 

776 if not isinstance(weights, (list, tuple)): 

777 raise TypeError( 

778 "Expected list for 'weights' argument to " 

779 "'sdca_shrink_l1' Op, not %r." % weights) 

780 _attr_num_features = len(weights) 

781 l1 = _execute.make_float(l1, "l1") 

782 l2 = _execute.make_float(l2, "l2") 

783 try: 

784 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

785 "SdcaShrinkL1", weights=weights, l1=l1, l2=l2, name=name) 

786 except (TypeError, ValueError): 

787 _result = _dispatch.dispatch( 

788 sdca_shrink_l1, (), dict(weights=weights, l1=l1, l2=l2, name=name) 

789 ) 

790 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

791 return _result 

792 raise 

793 return _op 

794SdcaShrinkL1 = tf_export("raw_ops.SdcaShrinkL1")(_ops.to_raw_op(sdca_shrink_l1)) 

795_dispatcher_for_sdca_shrink_l1 = sdca_shrink_l1._tf_type_based_dispatcher.Dispatch 

796 

797 

798def sdca_shrink_l1_eager_fallback(weights, l1, l2, name, ctx): 

799 raise RuntimeError("sdca_shrink_l1 op does not support eager execution. Arg 'weights' is a ref.")