forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradientDescentOptimizer.cs
More file actions
64 lines (56 loc) · 2.5 KB
/
Copy pathGradientDescentOptimizer.cs
File metadata and controls
64 lines (56 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
namespace Tensorflow.Train
{
/// <summary>
/// Optimizer that implements the gradient descent algorithm.
/// </summary>
public class GradientDescentOptimizer : Optimizer
{
private bool _useTensor;
/// <summary>
/// Construct a new gradient descent optimizer.
/// </summary>
/// <param name="learning_rate">A Tensor or a floating point value. The learning
/// rate to use.</param>
/// <param name="use_locking">If true use locks for update operations.</param>
/// <param name="name">Optional name prefix for the operations created when applying
/// gradients.Defaults to "GradientDescent".</param>
/// <remarks>
/// When eager execution is enabled, `learning_rate` can be a callable that
/// takes no arguments and returns the actual value to use.This can be useful
/// for changing these values across different invocations of optimizer
/// functions.
/// </remarks>
public GradientDescentOptimizer(float learning_rate, bool use_locking = false, string name = "GradientDescent")
: base(learning_rate, use_locking, name)
{
_lr = learning_rate;
_useTensor = false;
}
public GradientDescentOptimizer(Tensor learning_rate, bool use_locking = false, string name = "GradientDescent")
: base(learning_rate, use_locking, name)
{
_lr_t = learning_rate;
_useTensor = true;
}
public override void _prepare()
{
if (!_useTensor)
{
var lr = _call_if_callable(_lr);
_lr_t = ops.convert_to_tensor(lr, name: "learning_rate");
}
}
}
}