-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
method.go
59 lines (53 loc) · 1.92 KB
/
method.go
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
// Copyright 2019 spaGO Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gd
import (
mat "github.com/nlpodyssey/spago/pkg/mat32"
"github.com/nlpodyssey/spago/pkg/ml/nn"
)
const (
// None represents the absence of a specific gradient descent optimization method.
None int = iota
// SGD represents the SGD gradient descent optimization method.
SGD
// AdaGrad represents the AdaGrad gradient descent optimization method.
AdaGrad
// Adam represents the Adam gradient descent optimization method.
Adam
// RAdam represents the RAdam gradient descent optimization method.
RAdam
// RMSProp represents the RMSProp gradient descent optimization method.
RMSProp
)
// MethodConfig is an empty interface implemented by the configuration structures of
// AdaGrad, Adam, RMSProp and SGD.
type MethodConfig interface{}
// Method is implemented by any optimization method.
type Method interface {
// Label returns the enumeration-like value which identifies this gradient descent method.
Label() int
// Delta returns the difference between the current params and where the method wants it to be.
Delta(param nn.Param) mat.Matrix
// NewSupport returns a new support structure with the given dimensions.
NewSupport(r, c int) *nn.Payload
}
// GetOrSetPayload returns the payload from param, if it already exists, otherwise
// a new payload is created, assigned to the param, and returned.
func GetOrSetPayload(param nn.Param, m Method) *nn.Payload {
payload := param.Payload()
switch {
case payload == nil:
payload := m.NewSupport(param.Value().Dims())
param.SetPayload(payload)
return payload
case payload.Label == None:
payload := m.NewSupport(param.Value().Dims())
param.SetPayload(payload)
return payload
case payload.Label == m.Label():
return payload
default:
panic("gd: support structure non compatible with the optimization method")
}
}