Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/training/proximal_gradient_descent.py: 52%

27 statements  

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

1# Copyright 2015 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"""ProximalGradientDescent for TensorFlow.""" 

17from tensorflow.python.framework import ops 

18# pylint: disable=unused-import 

19from tensorflow.python.ops import math_ops 

20# pylint: enable=unused-import 

21from tensorflow.python.training import optimizer 

22from tensorflow.python.training import training_ops 

23from tensorflow.python.util.tf_export import tf_export 

24 

25 

26@tf_export(v1=["train.ProximalGradientDescentOptimizer"]) 

27class ProximalGradientDescentOptimizer(optimizer.Optimizer): 

28 # pylint: disable=line-too-long 

29 """Optimizer that implements the proximal gradient descent algorithm. 

30 

31 References: 

32 Efficient Learning using Forward-Backward Splitting: 

33 [Duchi et al., 2009](http://papers.nips.cc/paper/3793-efficient-learning-using-forward-backward-splitting) 

34 ([pdf](http://papers.nips.cc/paper/3793-efficient-learning-using-forward-backward-splitting.pdf)) 

35 """ 

36 

37 def __init__(self, learning_rate, l1_regularization_strength=0.0, 

38 l2_regularization_strength=0.0, use_locking=False, 

39 name="ProximalGradientDescent"): 

40 """Construct a new proximal gradient descent optimizer. 

41 

42 Args: 

43 learning_rate: A Tensor or a floating point value. The learning 

44 rate to use. 

45 l1_regularization_strength: A float value, must be greater than or 

46 equal to zero. 

47 l2_regularization_strength: A float value, must be greater than or 

48 equal to zero. 

49 use_locking: If True use locks for update operations. 

50 name: Optional name prefix for the operations created when applying 

51 gradients. Defaults to "GradientDescent". 

52 """ 

53 super(ProximalGradientDescentOptimizer, self).__init__(use_locking, name) 

54 self._learning_rate = learning_rate 

55 self._l1_regularization_strength = l1_regularization_strength 

56 self._l2_regularization_strength = l2_regularization_strength 

57 self._l1_regularization_strength_tensor = None 

58 self._l2_regularization_strength_tensor = None 

59 

60 def _apply_dense(self, grad, var): 

61 return training_ops.apply_proximal_gradient_descent( 

62 var, 

63 self._learning_rate_tensor, 

64 self._l1_regularization_strength_tensor, 

65 self._l2_regularization_strength_tensor, 

66 grad, 

67 use_locking=self._use_locking).op 

68 

69 def _resource_apply_dense(self, grad, var): 

70 return training_ops.resource_apply_proximal_gradient_descent( 

71 var.handle, 

72 self._learning_rate_tensor, 

73 self._l1_regularization_strength_tensor, 

74 self._l2_regularization_strength_tensor, 

75 grad, 

76 use_locking=self._use_locking) 

77 

78 def _apply_sparse(self, grad, var): 

79 return training_ops.sparse_apply_proximal_gradient_descent( 

80 var, 

81 self._learning_rate_tensor, 

82 self._l1_regularization_strength_tensor, 

83 self._l2_regularization_strength_tensor, 

84 grad.values, 

85 grad.indices, 

86 use_locking=self._use_locking).op 

87 

88 def _resource_apply_sparse(self, grad, var, indices): 

89 return training_ops.resource_sparse_apply_proximal_gradient_descent( 

90 var.handle, 

91 math_ops.cast(self._learning_rate_tensor, grad.dtype), 

92 math_ops.cast(self._l1_regularization_strength_tensor, grad.dtype), 

93 math_ops.cast(self._l2_regularization_strength_tensor, grad.dtype), 

94 grad, 

95 indices, 

96 use_locking=self._use_locking) 

97 

98 def _prepare(self): 

99 self._learning_rate_tensor = ops.convert_to_tensor(self._learning_rate, 

100 name="learning_rate") 

101 self._l1_regularization_strength_tensor = ops.convert_to_tensor( 

102 self._l1_regularization_strength, name="l1_regularization_strength") 

103 self._l2_regularization_strength_tensor = ops.convert_to_tensor( 

104 self._l2_regularization_strength, name="l2_regularization_strength")