This repository was archived by the owner on Feb 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPooling.cs
More file actions
172 lines (151 loc) · 6.66 KB
/
Pooling.cs
File metadata and controls
172 lines (151 loc) · 6.66 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
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
namespace NeuralNetwork;
[Serializable]
public unsafe class Pooling2D : Layer
{
private delegate void PoolingMethod(int batch);
private PoolingMethod forwardMethod, backPropMethod;
private (ushort y, ushort x)[,,,] indexedOut;
private readonly (byte x, byte y) strides;
private readonly (byte x, byte y) sizeOfPooling;
private readonly string method;
private readonly Padding2D paddingLayer;
public Pooling2D(string method,
(byte x, byte y) sizeOfPooling,
(byte x, byte y) strides,
string padding = "valid", string name = null) : base(name)
{
this.method = method;
this.sizeOfPooling = sizeOfPooling;
this.strides = strides;
if (padding != "valid")
{
paddingLayer = new Padding2D(padding, sizeOfPooling, strides);
Apply(paddingLayer);
}
}
public sealed override void Init(Optimizer optimizer)
{
InitDelegates();
int outXLength = (int)((inputShape.n3 - sizeOfPooling.x) / (float)strides.x + 1);
int outYLength = (int)((inputShape.n2 - sizeOfPooling.y) / (float)strides.y + 1);
outputShape = inputShape.Change((3, outXLength), (2, outYLength));
input = new Tensor(inputShape);
inputDerivatives = new Tensor(inputShape);
output = new Tensor(outputShape);
outputDerivatives = new Tensor(outputShape);
inputDerivatives.Fill(0);
indexedOut = new (ushort y, ushort x)[inputShape.n0, inputShape.n1,
outYLength, outXLength];
}
private void InitDelegates()
{
switch (method.ToLower())
{
case "max":
forwardMethod += Max;
backPropMethod += BackMaxMin;
break;
case "min":
forwardMethod += Min;
backPropMethod += BackMaxMin;
break;
case "average":
forwardMethod += Average;
backPropMethod += BackAverage;
break;
}
}
protected sealed override void ForwardAction(int batch) => forwardMethod(batch);
private void Max(int batch)
{
float max = 0;
for (int inputChannel = 0; inputChannel < inputShape.n1; inputChannel++)
for (int i = 0, iOut = 0; iOut < outputShape.n2; i += strides.y, iOut++)
for (int j = 0, jOut = 0; jOut < outputShape.n3; j += strides.x, jOut++)
{
for (int y = 0; y < sizeOfPooling.y; y++)
for (int x = 0; x < sizeOfPooling.x; x++)
{
if (x == 0 && y == 0)
{
max = input[batch, inputChannel, i, j];
indexedOut[batch, inputChannel, iOut, jOut] = ((ushort)i, (ushort)j);
continue;
}
if (input[batch, inputChannel, i + y, j + x] > max)
{
max = input[batch, inputChannel, i + y, j + x];
indexedOut[batch, inputChannel, iOut, jOut] = ((ushort)(i + y), (ushort)(j + x));
}
}
output[batch, inputChannel, iOut, jOut] = max;
}
}
private void Min(int batch)
{
float min = 0;
for (int inputChannel = 0; inputChannel < inputShape.n1; inputChannel++)
for (int i = 0, iOut = 0; iOut < outputShape.n2; i += strides.y, iOut++)
for (int j = 0, jOut = 0; jOut < outputShape.n3; j += strides.x, jOut++)
{
for (int y = 0; y < sizeOfPooling.y; y++)
for (int x = 0; x < sizeOfPooling.x; x++)
{
if (x == 0 && y == 0)
{
min = input[batch, inputChannel, i, j];
indexedOut[batch, inputChannel, iOut, jOut] = ((ushort)(i), (ushort)(j));
continue;
}
if (input[batch, inputChannel, i + y, j + x] < min)
{
min = input[batch, inputChannel, i + y, j + x];
indexedOut[batch, inputChannel, iOut, jOut] = ((ushort)(i + y), (ushort)(j + x));
}
}
output[batch, inputChannel, iOut, jOut] = min;
}
}
private void Average(int batch)
{
float sum;
for (int inputChannel = 0; inputChannel < inputShape.n1; inputChannel++)
for (int i = 0, iOut = 0; iOut < outputShape.n2; i += strides.y, iOut++)
for (int j = 0, jOut = 0; jOut < outputShape.n3; j += strides.x, jOut++)
{
sum = 0;
for (int y = 0; y < sizeOfPooling.y; y++)
for (int x = 0; x < sizeOfPooling.x; x++)
{
sum += input[batch, inputChannel, i + y, j + x];
}
output[batch, inputChannel, iOut, jOut] = sum / (sizeOfPooling.y * sizeOfPooling.x);
}
}
protected sealed override void BackPropAction(int batch) => backPropMethod(batch);
private void BackMaxMin(int batch)
{
for (int inputChannel = 0; inputChannel < inputShape.n1; inputChannel++)
for (int iOut = 0; iOut < outputShape.n2; iOut++)
for (int jOut = 0; jOut < outputShape.n3; jOut++)
{
inputDerivatives[batch, inputChannel, indexedOut[batch, inputChannel, iOut, jOut].y,
indexedOut[batch, inputChannel, iOut, jOut].x] += outputDerivatives[batch, inputChannel, iOut, jOut];
}
}
private void BackAverage(int batch)
{
float averageDelta;
for (int inputChannel = 0; inputChannel < outputShape.n1; inputChannel++)
for (int i = 0, iOut = 0; iOut < outputShape.n2; i += strides.y, iOut++)
for (int j = 0, jOut = 0; jOut < outputShape.n3; j += strides.x, jOut++)
{
averageDelta = outputDerivatives[batch, inputChannel, iOut, jOut] / (sizeOfPooling.x * sizeOfPooling.y);
for (int y = 0; y < sizeOfPooling.y; y++)
for (int x = 0; x < sizeOfPooling.x; x++)
{
inputDerivatives[batch, inputChannel, i + y, j + x] += averageDelta;
}
}
}
}