-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSimpleRNN.go
executable file
·271 lines (234 loc) · 7.6 KB
/
SimpleRNN.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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 LSimpleRNN struct {
activation string
activityRegularizer regularizer.Regularizer
biasConstraint constraint.Constraint
biasInitializer initializer.Initializer
biasRegularizer regularizer.Regularizer
dropout float64
dtype DataType
goBackwards bool
inputs []Layer
kernelConstraint constraint.Constraint
kernelInitializer initializer.Initializer
kernelRegularizer regularizer.Regularizer
name string
recurrentConstraint constraint.Constraint
recurrentDropout float64
recurrentInitializer initializer.Initializer
recurrentRegularizer regularizer.Regularizer
returnSequences bool
returnState bool
shape tf.Shape
stateful bool
timeMajor bool
trainable bool
units float64
unroll bool
useBias bool
layerWeights []*tf.Tensor
}
func SimpleRNN(units float64) *LSimpleRNN {
return &LSimpleRNN{
activation: "tanh",
activityRegularizer: ®ularizer.NilRegularizer{},
biasConstraint: &constraint.NilConstraint{},
biasInitializer: initializer.Zeros(),
biasRegularizer: ®ularizer.NilRegularizer{},
dropout: 0,
dtype: Float32,
goBackwards: false,
kernelConstraint: &constraint.NilConstraint{},
kernelInitializer: initializer.GlorotUniform(),
kernelRegularizer: ®ularizer.NilRegularizer{},
name: UniqueName("simple_rnn"),
recurrentConstraint: &constraint.NilConstraint{},
recurrentDropout: 0,
recurrentInitializer: initializer.Orthogonal(),
recurrentRegularizer: ®ularizer.NilRegularizer{},
returnSequences: false,
returnState: false,
stateful: false,
timeMajor: false,
trainable: true,
units: units,
unroll: false,
useBias: true,
}
}
func (l *LSimpleRNN) SetActivation(activation string) *LSimpleRNN {
l.activation = activation
return l
}
func (l *LSimpleRNN) SetActivityRegularizer(activityRegularizer regularizer.Regularizer) *LSimpleRNN {
l.activityRegularizer = activityRegularizer
return l
}
func (l *LSimpleRNN) SetBiasConstraint(biasConstraint constraint.Constraint) *LSimpleRNN {
l.biasConstraint = biasConstraint
return l
}
func (l *LSimpleRNN) SetBiasInitializer(biasInitializer initializer.Initializer) *LSimpleRNN {
l.biasInitializer = biasInitializer
return l
}
func (l *LSimpleRNN) SetBiasRegularizer(biasRegularizer regularizer.Regularizer) *LSimpleRNN {
l.biasRegularizer = biasRegularizer
return l
}
func (l *LSimpleRNN) SetDropout(dropout float64) *LSimpleRNN {
l.dropout = dropout
return l
}
func (l *LSimpleRNN) SetDtype(dtype DataType) *LSimpleRNN {
l.dtype = dtype
return l
}
func (l *LSimpleRNN) SetGoBackwards(goBackwards bool) *LSimpleRNN {
l.goBackwards = goBackwards
return l
}
func (l *LSimpleRNN) SetKernelConstraint(kernelConstraint constraint.Constraint) *LSimpleRNN {
l.kernelConstraint = kernelConstraint
return l
}
func (l *LSimpleRNN) SetKernelInitializer(kernelInitializer initializer.Initializer) *LSimpleRNN {
l.kernelInitializer = kernelInitializer
return l
}
func (l *LSimpleRNN) SetKernelRegularizer(kernelRegularizer regularizer.Regularizer) *LSimpleRNN {
l.kernelRegularizer = kernelRegularizer
return l
}
func (l *LSimpleRNN) SetName(name string) *LSimpleRNN {
l.name = name
return l
}
func (l *LSimpleRNN) SetRecurrentConstraint(recurrentConstraint constraint.Constraint) *LSimpleRNN {
l.recurrentConstraint = recurrentConstraint
return l
}
func (l *LSimpleRNN) SetRecurrentDropout(recurrentDropout float64) *LSimpleRNN {
l.recurrentDropout = recurrentDropout
return l
}
func (l *LSimpleRNN) SetRecurrentInitializer(recurrentInitializer initializer.Initializer) *LSimpleRNN {
l.recurrentInitializer = recurrentInitializer
return l
}
func (l *LSimpleRNN) SetRecurrentRegularizer(recurrentRegularizer regularizer.Regularizer) *LSimpleRNN {
l.recurrentRegularizer = recurrentRegularizer
return l
}
func (l *LSimpleRNN) SetReturnSequences(returnSequences bool) *LSimpleRNN {
l.returnSequences = returnSequences
return l
}
func (l *LSimpleRNN) SetReturnState(returnState bool) *LSimpleRNN {
l.returnState = returnState
return l
}
func (l *LSimpleRNN) SetShape(shape tf.Shape) *LSimpleRNN {
l.shape = shape
return l
}
func (l *LSimpleRNN) SetStateful(stateful bool) *LSimpleRNN {
l.stateful = stateful
return l
}
func (l *LSimpleRNN) SetTimeMajor(timeMajor bool) *LSimpleRNN {
l.timeMajor = timeMajor
return l
}
func (l *LSimpleRNN) SetTrainable(trainable bool) *LSimpleRNN {
l.trainable = trainable
return l
}
func (l *LSimpleRNN) SetUnroll(unroll bool) *LSimpleRNN {
l.unroll = unroll
return l
}
func (l *LSimpleRNN) SetUseBias(useBias bool) *LSimpleRNN {
l.useBias = useBias
return l
}
func (l *LSimpleRNN) SetLayerWeights(layerWeights []*tf.Tensor) *LSimpleRNN {
l.layerWeights = layerWeights
return l
}
func (l *LSimpleRNN) GetShape() tf.Shape {
return l.shape
}
func (l *LSimpleRNN) GetDtype() DataType {
return l.dtype
}
func (l *LSimpleRNN) SetInputs(inputs ...Layer) Layer {
l.inputs = inputs
return l
}
func (l *LSimpleRNN) GetInputs() []Layer {
return l.inputs
}
func (l *LSimpleRNN) GetName() string {
return l.name
}
func (l *LSimpleRNN) GetLayerWeights() []*tf.Tensor {
return l.layerWeights
}
type jsonConfigLSimpleRNN struct {
ClassName string `json:"class_name"`
Name string `json:"name"`
Config map[string]interface{} `json:"config"`
InboundNodes [][][]interface{} `json:"inbound_nodes"`
}
func (l *LSimpleRNN) GetKerasLayerConfig() interface{} {
inboundNodes := [][][]interface{}{
{},
}
for _, input := range l.inputs {
inboundNodes[0] = append(inboundNodes[0], []interface{}{
input.GetName(),
0,
0,
map[string]bool{},
})
}
return jsonConfigLSimpleRNN{
ClassName: "SimpleRNN",
Name: l.name,
Config: map[string]interface{}{
"activation": l.activation,
"activity_regularizer": l.activityRegularizer.GetKerasLayerConfig(),
"bias_constraint": l.biasConstraint.GetKerasLayerConfig(),
"bias_initializer": l.biasInitializer.GetKerasLayerConfig(),
"bias_regularizer": l.biasRegularizer.GetKerasLayerConfig(),
"dropout": l.dropout,
"dtype": l.dtype.String(),
"go_backwards": l.goBackwards,
"kernel_constraint": l.kernelConstraint.GetKerasLayerConfig(),
"kernel_initializer": l.kernelInitializer.GetKerasLayerConfig(),
"kernel_regularizer": l.kernelRegularizer.GetKerasLayerConfig(),
"name": l.name,
"recurrent_constraint": l.recurrentConstraint.GetKerasLayerConfig(),
"recurrent_dropout": l.recurrentDropout,
"recurrent_initializer": l.recurrentInitializer.GetKerasLayerConfig(),
"recurrent_regularizer": l.recurrentRegularizer.GetKerasLayerConfig(),
"return_sequences": l.returnSequences,
"return_state": l.returnState,
"stateful": l.stateful,
"time_major": l.timeMajor,
"trainable": l.trainable,
"units": l.units,
"unroll": l.unroll,
"use_bias": l.useBias,
},
InboundNodes: inboundNodes,
}
}
func (l *LSimpleRNN) GetCustomLayerDefinition() string {
return ``
}