-
Notifications
You must be signed in to change notification settings - Fork 6
/
convolutional_layer.go
296 lines (257 loc) · 9.46 KB
/
convolutional_layer.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package cnns
import (
"fmt"
"math/rand"
"github.com/LdDl/cnns/tensor"
"github.com/pkg/errors"
"gonum.org/v1/gonum/mat"
)
// ConvLayer Convolutional layer structure
// Oj - O{j}, activated output from previous layer for j-th neuron (in other words: previous summation input)
// Ok - O{k}, activated output from current layer for k-th node (in other words: activated summation input)
// SumInput - non-activated output for current layer for k-th node (in other words: summation input)
type ConvLayer struct {
Oj *mat.Dense
Ok *mat.Dense
Kernels []*mat.Dense
PreviousDeltaKernelsState []*mat.Dense
LocalDeltas []*mat.Dense
NextDeltaWeightSum *mat.Dense
Stride int
KernelSize int
OutputSize *tensor.TDsize
inputSize *tensor.TDsize
inChannels int
trainMode bool
}
// NewConvLayer Constructor for convolutional layer. You need to specify striding step, size (square) of kernel, amount of kernels, input size.
/*
inSize - size of input (width/height and number of channels)
stride - step on convolve operation
kernelSize - width==height of kernel
numberFilters - number of kernels
*/
func NewConvLayer(inSize *tensor.TDsize, stride, kernelSize, numberFilters int) Layer {
newLayer := &ConvLayer{
inputSize: inSize,
Stride: stride,
KernelSize: kernelSize,
Oj: mat.NewDense(inSize.Z*inSize.X, inSize.Y, nil),
Ok: mat.NewDense(numberFilters*((inSize.X-kernelSize)/stride+1), (inSize.Y-kernelSize)/stride+1, nil),
Kernels: make([]*mat.Dense, numberFilters),
PreviousDeltaKernelsState: make([]*mat.Dense, numberFilters),
LocalDeltas: make([]*mat.Dense, numberFilters),
NextDeltaWeightSum: &mat.Dense{},
OutputSize: &tensor.TDsize{X: (inSize.X-kernelSize)/stride + 1, Y: (inSize.Y-kernelSize)/stride + 1, Z: numberFilters},
inChannels: inSize.Z,
trainMode: false,
}
for f := 0; f < numberFilters; f++ {
if inSize.Z == 1 {
newLayer.Kernels[f] = mat.NewDense(kernelSize, kernelSize, nil)
for i := 0; i < kernelSize; i++ {
for h := 0; h < kernelSize; h++ {
newLayer.Kernels[f].Set(i, h, rand.Float64()-0.5)
}
}
newLayer.PreviousDeltaKernelsState[f] = mat.NewDense(kernelSize, kernelSize, nil)
newLayer.PreviousDeltaKernelsState[f].Zero()
continue
}
newLayer.Kernels[f] = mat.NewDense(kernelSize*kernelSize, inSize.Z, nil)
for i := 0; i < kernelSize*kernelSize; i++ {
for h := 0; h < inSize.Z; h++ {
newLayer.Kernels[f].Set(i, h, rand.Float64()-0.5)
}
}
newLayer.PreviousDeltaKernelsState[f] = mat.NewDense(kernelSize*kernelSize, inSize.Z, nil)
newLayer.PreviousDeltaKernelsState[f].Zero()
}
return newLayer
}
// SetCustomWeights Set user's weights for convolutional layer (make it carefully)
/*
kernels - slice of kernels
*/
func (conv *ConvLayer) SetCustomWeights(kernels []*mat.Dense) {
if len(conv.Kernels) != len(kernels) {
fmt.Println("Amount of custom filters has to be equal to layer's amount of filters. Skipping...")
return
}
for i := range kernels {
conv.Kernels[i].CloneFrom(kernels[i])
tr, tc := kernels[i].Dims()
conv.PreviousDeltaKernelsState[i] = mat.NewDense(tr, tc, nil)
conv.PreviousDeltaKernelsState[i].Zero()
}
}
// GetInputSize Returns dimensions of incoming data for convolutional layer
func (conv *ConvLayer) GetInputSize() *tensor.TDsize {
return conv.inputSize
}
// GetOutputSize Returns output size (dimensions) of convolutional layer
func (conv *ConvLayer) GetOutputSize() *tensor.TDsize {
return conv.OutputSize
}
// GetActivatedOutput Returns convolutional layer's output
func (conv *ConvLayer) GetActivatedOutput() *mat.Dense {
return conv.Ok
}
// GetWeights Returns convolutional layer's weights.
func (conv *ConvLayer) GetWeights() []*mat.Dense {
return conv.Kernels
}
// GetGradients Returns convolutional layer's gradients dense
func (conv *ConvLayer) GetGradients() *mat.Dense {
return conv.NextDeltaWeightSum
}
// FeedForward Feed data to convolutional layer
func (conv *ConvLayer) FeedForward(input *mat.Dense) error {
conv.Oj = input
err := conv.doActivation()
if err != nil {
return errors.Wrap(err, "Can't call FeedForward() on convolutional layer")
}
return nil
}
// doActivation Convolutional layer's output activation
func (conv *ConvLayer) doActivation() error {
resultMatrix := &mat.Dense{}
for i := range conv.Kernels {
feature, err := Convolve2D(conv.Oj, conv.Kernels[i], conv.inChannels, conv.Stride)
if err != nil {
return errors.Wrap(err, "Can't call doActivation() on Convolutional Layer")
}
if resultMatrix.IsEmpty() {
resultMatrix = feature
} else {
t := &mat.Dense{}
t.Stack(resultMatrix, feature)
resultMatrix = t
}
}
conv.Ok = resultMatrix
return nil
}
// CalculateGradients Evaluate convolutional layer's gradients
func (conv *ConvLayer) CalculateGradients(lossGradients *mat.Dense) error {
channels := conv.inChannels
features := conv.OutputSize.Z
errRows, errCols := lossGradients.Dims()
inputRows, inputCols := conv.Oj.Dims()
for f := 0; f < features; f++ {
partialErrors := ExtractChannel(lossGradients, errRows, errCols, features, f)
channelsStack := &mat.Dense{}
for c := 0; c < channels; c++ {
partialMatrix := ExtractChannel(conv.Oj, inputRows, inputCols, channels, c)
// dL/dF = Convolution(Input, LossGradient dL/dO)
partialLocalDeltas, err := Convolve2D(partialMatrix, partialErrors, 1, conv.Stride)
if err != nil {
return errors.Wrap(err, "Can't call CalculateGradients() while calculate Convolution(Input, LossGradient dL/dO)")
}
if channelsStack.IsEmpty() {
channelsStack = partialLocalDeltas
} else {
t := &mat.Dense{}
t.Stack(channelsStack, partialLocalDeltas)
channelsStack = t
}
}
conv.LocalDeltas[f] = channelsStack
}
conv.NextDeltaWeightSum = &mat.Dense{}
for f := 0; f < features; f++ {
// Add padding for each incoming loss gradient
partialErrors := ExtractChannel(lossGradients, errRows, errCols, features, f)
padded := ZeroPadding(partialErrors, conv.KernelSize-1)
// Rotate each kernel by 180 degrees and do full convolution
kernelR, kernelC := conv.Kernels[f].Dims()
channelStacked := &mat.Dense{}
for c := 0; c < channels; c++ {
partialKernel := ExtractChannel(conv.Kernels[f], kernelR, kernelC, channels, c)
partialRotatedKernel := Rot2D180(partialKernel)
// error = dL/dX = FullConvolution(LossGradient dL/dO, rot180(kernel))
dLdX, err := Convolve2D(padded, partialRotatedKernel, 1, conv.Stride)
if err != nil {
return errors.Wrap(err, "Can't call CalculateGradients() while calculate FullConvolution(LossGradient dL/dO, rot180(kernel))")
}
// Stack channels for each feature
if channelStacked.IsEmpty() {
channelStacked = dLdX
} else {
t := &mat.Dense{}
t.Stack(channelStacked, dLdX)
channelStacked = t
}
}
// Sum channels for each feature
if conv.NextDeltaWeightSum.IsEmpty() {
conv.NextDeltaWeightSum = channelStacked
} else {
t := &mat.Dense{}
t.Add(conv.NextDeltaWeightSum, channelStacked)
conv.NextDeltaWeightSum = t
}
}
return nil
}
// UpdateWeights Update convolutional layer's weights
func (conv *ConvLayer) UpdateWeights(lp *LearningParams) {
features := len(conv.Kernels)
for f := 0; f < features; f++ {
kernel := conv.Kernels[f]
previousDeltaWeights := conv.PreviousDeltaKernelsState[f]
partialErrors := conv.LocalDeltas[f]
// Evaluate ΔΣ(k)/Δw{j}{k}
// In FC layer we do: Δw.Mul(fc.LocalDelta, fc.Oj.T()), but fc.Oj.T() = 1.0 in case of convolutional layer. So we can skip this step
Δw := &mat.Dense{}
Δw.Scale(-1.0*lp.LearningRate, partialErrors)
// Inertia (as separated Scale() call)
// @todo - this should be optional
Δw.Scale(1.0-lp.Momentum, Δw)
previousDeltaWeights.Scale(lp.Momentum, previousDeltaWeights)
Δw.Add(Δw, previousDeltaWeights)
conv.PreviousDeltaKernelsState[f].CloneFrom(Δw)
// Update weights: w = w + Δw
kernel.Add(kernel, Δw)
}
}
// PrintOutput Pretty print convolutional layer's output
func (conv *ConvLayer) PrintOutput() {
fmt.Println("Printing Convolutional Layer output...")
}
// PrintWeights Pretty print convolutional layer's weights
func (conv *ConvLayer) PrintWeights() {
fmt.Println("Printing Convolutional Layer kernels...")
features := len(conv.Kernels)
for f := 0; f < features; f++ {
kernelRows, kernelCols := conv.Kernels[f].Dims()
fmt.Printf("\tKernel #%d:\n", f)
for c := 0; c < conv.inChannels; c++ {
partialKernel := ExtractChannel(conv.Kernels[f], kernelRows, kernelCols, conv.inChannels, c)
fmt.Printf("\tChannel #%d:\n", c)
rows, _ := partialKernel.Dims()
for r := 0; r < rows; r++ {
fmt.Printf("\t\t%v\n", partialKernel.RawRowView(r))
}
}
}
}
// SetActivationFunc Set activation function for layer
func (conv *ConvLayer) SetActivationFunc(f func(v float64) float64) {
// Nothing here. Just for interface.
fmt.Println("You can not set activation function for convolutional layer")
}
// SetActivationDerivativeFunc Set derivative of activation function
func (conv *ConvLayer) SetActivationDerivativeFunc(f func(v float64) float64) {
// Nothing here. Just for interface.
fmt.Println("You can not set derivative of activation function for convolutional layer")
}
// GetType Returns "conv" as layer's type
func (conv *ConvLayer) GetType() string {
return "conv"
}
// GetStride Returns stride of layer
func (conv *ConvLayer) GetStride() int {
return conv.Stride
}