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"""GradientDescent for TensorFlow.""" 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from tensorflow.python.framework import ops 22from tensorflow.python.ops import math_ops 23from tensorflow.python.ops import resource_variable_ops 24from tensorflow.python.training import optimizer 25from tensorflow.python.training import training_ops 26from tensorflow.python.util.tf_export import tf_export 27 28 29@tf_export(v1=["train.GradientDescentOptimizer"]) 30class GradientDescentOptimizer(optimizer.Optimizer): 31 """Optimizer that implements the gradient descent algorithm. 32 """ 33 34 def __init__(self, learning_rate, use_locking=False, name="GradientDescent"): 35 """Construct a new gradient descent optimizer. 36 37 Args: 38 learning_rate: A Tensor or a floating point value. The learning 39 rate to use. 40 use_locking: If True use locks for update operations. 41 name: Optional name prefix for the operations created when applying 42 gradients. Defaults to "GradientDescent". 43 44 @compatibility(eager) 45 When eager execution is enabled, `learning_rate` can be a callable that 46 takes no arguments and returns the actual value to use. This can be useful 47 for changing these values across different invocations of optimizer 48 functions. 49 @end_compatibility 50 """ 51 super(GradientDescentOptimizer, self).__init__(use_locking, name) 52 self._learning_rate = learning_rate 53 self._learning_rate_tensor = None 54 55 def _apply_dense(self, grad, var): 56 return training_ops.apply_gradient_descent( 57 var, 58 math_ops.cast(self._learning_rate_tensor, var.dtype.base_dtype), 59 grad, 60 use_locking=self._use_locking).op 61 62 def _resource_apply_dense(self, grad, handle): 63 return training_ops.resource_apply_gradient_descent( 64 handle.handle, math_ops.cast(self._learning_rate_tensor, 65 grad.dtype.base_dtype), 66 grad, use_locking=self._use_locking) 67 68 def _resource_apply_sparse_duplicate_indices(self, grad, handle, indices): 69 return resource_variable_ops.resource_scatter_add( 70 handle.handle, indices, -grad * self._learning_rate) 71 72 def _apply_sparse_duplicate_indices(self, grad, var): 73 delta = ops.IndexedSlices( 74 grad.values * 75 math_ops.cast(self._learning_rate_tensor, var.dtype.base_dtype), 76 grad.indices, grad.dense_shape) 77 return var.scatter_sub(delta, use_locking=self._use_locking) 78 79 def _prepare(self): 80 learning_rate = self._call_if_callable(self._learning_rate) 81 self._learning_rate_tensor = ops.convert_to_tensor( 82 learning_rate, name="learning_rate") 83