forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSGD.cs
More file actions
73 lines (62 loc) · 2.63 KB
/
SGD.cs
File metadata and controls
73 lines (62 loc) · 2.63 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
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using Tensorflow.Keras.ArgsDefinition;
namespace Tensorflow.Keras.Optimizers
{
public class SGD : OptimizerV2
{
protected override string _name => "SGD";
#pragma warning disable CS0169 // The field 'SGD.nesterov' is never used
bool nesterov;
#pragma warning restore CS0169 // The field 'SGD.nesterov' is never used
public SGD(float learning_rate,
float momentum = 0.0f,
bool nesterov = false,
float decay = 0.0f) : base(new OptimizerV2Args { })
{
_set_hyper("learning_rate", learning_rate);
_set_hyper("decay", decay);
_momentum = momentum > 0;
if (momentum < 0 || momentum > 1)
throw new ValueError($"momentum must be a number between 0 and 1, got {momentum}.");
_set_hyper("momentum", momentum);
#pragma warning disable CS1717 // Assignment made to same variable
nesterov = nesterov;
#pragma warning restore CS1717 // Assignment made to same variable
}
protected override void _create_slots(IVariableV1[] var_list)
{
if (_momentum)
foreach (var var in var_list)
add_slot(var, "momentum");
}
protected override void _prepare_local(DeviceDType device_dtype,
Dictionary<DeviceDType, Dictionary<string, Tensor>> _apply_state)
{
base._prepare_local(device_dtype, _apply_state);
_apply_state[device_dtype]["momentum"] = array_ops.identity(
_get_hyper("momentum", device_dtype.DType));
}
protected override Operation _resource_apply_dense(IVariableV1 var, Tensor grad, Dictionary<DeviceDType, Dictionary<string, Tensor>> _apply_state)
{
if (_momentum)
{
var momentum_var = get_slot(var, "momentum");
return gen_training_ops.resource_apply_keras_momentum(
var.Handle,
momentum_var.Handle,
_get_hyper("learning_rate", var.dtype),
grad,
_get_hyper("momentum", var.dtype),
use_locking: _use_locking,
use_nesterov: nesterov);
}
var device_dtype = _apply_state.Keys.FirstOrDefault(x => x.Device == var.Device && x.DType == var.dtype.as_base_dtype());
return gen_training_ops.resource_apply_gradient_descent(var.Handle,
_apply_state[device_dtype]["lr_t"],
grad,
use_locking: _use_locking);
}
}
}