-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSyncBatchNormalization.go
executable file
·212 lines (182 loc) · 6.66 KB
/
SyncBatchNormalization.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package layer
import "github.com/codingbeard/tfkg/layer/constraint"
import "github.com/codingbeard/tfkg/layer/initializer"
import "github.com/codingbeard/tfkg/layer/regularizer"
import tf "github.com/galeone/tensorflow/tensorflow/go"
type LSyncBatchNormalization struct {
axis float64
betaConstraint constraint.Constraint
betaInitializer initializer.Initializer
betaRegularizer regularizer.Regularizer
center bool
dtype DataType
epsilon float64
gammaConstraint constraint.Constraint
gammaInitializer initializer.Initializer
gammaRegularizer regularizer.Regularizer
inputs []Layer
momentum float64
movingMeanInitializer initializer.Initializer
movingVarianceInitializer initializer.Initializer
name string
scale bool
shape tf.Shape
trainable bool
layerWeights []*tf.Tensor
}
func SyncBatchNormalization() *LSyncBatchNormalization {
return &LSyncBatchNormalization{
axis: -1,
betaConstraint: &constraint.NilConstraint{},
betaInitializer: initializer.Zeros(),
betaRegularizer: ®ularizer.NilRegularizer{},
center: true,
dtype: Float32,
epsilon: 0.001,
gammaConstraint: &constraint.NilConstraint{},
gammaInitializer: initializer.Ones(),
gammaRegularizer: ®ularizer.NilRegularizer{},
momentum: 0.99,
movingMeanInitializer: initializer.Zeros(),
movingVarianceInitializer: initializer.Ones(),
name: UniqueName("sync_batch_normalization"),
scale: true,
trainable: true,
}
}
func (l *LSyncBatchNormalization) SetAxis(axis float64) *LSyncBatchNormalization {
l.axis = axis
return l
}
func (l *LSyncBatchNormalization) SetBetaConstraint(betaConstraint constraint.Constraint) *LSyncBatchNormalization {
l.betaConstraint = betaConstraint
return l
}
func (l *LSyncBatchNormalization) SetBetaInitializer(betaInitializer initializer.Initializer) *LSyncBatchNormalization {
l.betaInitializer = betaInitializer
return l
}
func (l *LSyncBatchNormalization) SetBetaRegularizer(betaRegularizer regularizer.Regularizer) *LSyncBatchNormalization {
l.betaRegularizer = betaRegularizer
return l
}
func (l *LSyncBatchNormalization) SetCenter(center bool) *LSyncBatchNormalization {
l.center = center
return l
}
func (l *LSyncBatchNormalization) SetDtype(dtype DataType) *LSyncBatchNormalization {
l.dtype = dtype
return l
}
func (l *LSyncBatchNormalization) SetEpsilon(epsilon float64) *LSyncBatchNormalization {
l.epsilon = epsilon
return l
}
func (l *LSyncBatchNormalization) SetGammaConstraint(gammaConstraint constraint.Constraint) *LSyncBatchNormalization {
l.gammaConstraint = gammaConstraint
return l
}
func (l *LSyncBatchNormalization) SetGammaInitializer(gammaInitializer initializer.Initializer) *LSyncBatchNormalization {
l.gammaInitializer = gammaInitializer
return l
}
func (l *LSyncBatchNormalization) SetGammaRegularizer(gammaRegularizer regularizer.Regularizer) *LSyncBatchNormalization {
l.gammaRegularizer = gammaRegularizer
return l
}
func (l *LSyncBatchNormalization) SetMomentum(momentum float64) *LSyncBatchNormalization {
l.momentum = momentum
return l
}
func (l *LSyncBatchNormalization) SetMovingMeanInitializer(movingMeanInitializer initializer.Initializer) *LSyncBatchNormalization {
l.movingMeanInitializer = movingMeanInitializer
return l
}
func (l *LSyncBatchNormalization) SetMovingVarianceInitializer(movingVarianceInitializer initializer.Initializer) *LSyncBatchNormalization {
l.movingVarianceInitializer = movingVarianceInitializer
return l
}
func (l *LSyncBatchNormalization) SetName(name string) *LSyncBatchNormalization {
l.name = name
return l
}
func (l *LSyncBatchNormalization) SetScale(scale bool) *LSyncBatchNormalization {
l.scale = scale
return l
}
func (l *LSyncBatchNormalization) SetShape(shape tf.Shape) *LSyncBatchNormalization {
l.shape = shape
return l
}
func (l *LSyncBatchNormalization) SetTrainable(trainable bool) *LSyncBatchNormalization {
l.trainable = trainable
return l
}
func (l *LSyncBatchNormalization) SetLayerWeights(layerWeights []*tf.Tensor) *LSyncBatchNormalization {
l.layerWeights = layerWeights
return l
}
func (l *LSyncBatchNormalization) GetShape() tf.Shape {
return l.shape
}
func (l *LSyncBatchNormalization) GetDtype() DataType {
return l.dtype
}
func (l *LSyncBatchNormalization) SetInputs(inputs ...Layer) Layer {
l.inputs = inputs
return l
}
func (l *LSyncBatchNormalization) GetInputs() []Layer {
return l.inputs
}
func (l *LSyncBatchNormalization) GetName() string {
return l.name
}
func (l *LSyncBatchNormalization) GetLayerWeights() []*tf.Tensor {
return l.layerWeights
}
type jsonConfigLSyncBatchNormalization struct {
ClassName string `json:"class_name"`
Name string `json:"name"`
Config map[string]interface{} `json:"config"`
InboundNodes [][][]interface{} `json:"inbound_nodes"`
}
func (l *LSyncBatchNormalization) GetKerasLayerConfig() interface{} {
inboundNodes := [][][]interface{}{
{},
}
for _, input := range l.inputs {
inboundNodes[0] = append(inboundNodes[0], []interface{}{
input.GetName(),
0,
0,
map[string]bool{},
})
}
return jsonConfigLSyncBatchNormalization{
ClassName: "SyncBatchNormalization",
Name: l.name,
Config: map[string]interface{}{
"axis": l.axis,
"beta_constraint": l.betaConstraint.GetKerasLayerConfig(),
"beta_initializer": l.betaInitializer.GetKerasLayerConfig(),
"beta_regularizer": l.betaRegularizer.GetKerasLayerConfig(),
"center": l.center,
"dtype": l.dtype.String(),
"epsilon": l.epsilon,
"gamma_constraint": l.gammaConstraint.GetKerasLayerConfig(),
"gamma_initializer": l.gammaInitializer.GetKerasLayerConfig(),
"gamma_regularizer": l.gammaRegularizer.GetKerasLayerConfig(),
"momentum": l.momentum,
"moving_mean_initializer": l.movingMeanInitializer.GetKerasLayerConfig(),
"moving_variance_initializer": l.movingVarianceInitializer.GetKerasLayerConfig(),
"name": l.name,
"scale": l.scale,
"trainable": l.trainable,
},
InboundNodes: inboundNodes,
}
}
func (l *LSyncBatchNormalization) GetCustomLayerDefinition() string {
return ``
}