Skip to content

Commit b84d991

Browse files
committed
Inception layer pool gradient test added
1 parent fb738d1 commit b84d991

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

Unit/NeuralNetwork.NET.Cuda.Unit/CuDnnInceptionLayerTest.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public unsafe void InceptionPoolPipeline()
232232
Buffer.BlockCopy(conv.Biases, 0, inception.Biases, sizeof(float) * (3 + 2 + 2 + 2 + 2), sizeof(float) * conv.Biases.Length);
233233
fixed (float* px = x)
234234
{
235+
// Forward + Z
235236
Tensor.Reshape(px, x.GetLength(0), x.GetLength(1), out Tensor xTensor);
236237
pool.Forward(xTensor, out Tensor zTemp, out Tensor aTemp);
237238
conv.Forward(aTemp, out Tensor zConv, out Tensor aConv);
@@ -241,14 +242,35 @@ public unsafe void InceptionPoolPipeline()
241242
for (int i = 0; i < zConv.Entities; i++)
242243
Buffer.MemoryCopy(pzInc + i * zInc.Length, preshaped + i * zConv.Length, sizeof(float) * zConv.Length, sizeof(float) * zConv.Length);
243244
Assert.IsTrue(reshaped.ContentEquals(zConv));
244-
zTemp.Free();
245-
aTemp.Free();
246-
zConv.Free();
247-
zInc.Free();
245+
246+
// A
248247
float* paInc = (float*)aInc.Ptr.ToPointer() + 12 * 12 * (3 + 2 + 2);
249248
for (int i = 0; i < aConv.Entities; i++)
250249
Buffer.MemoryCopy(paInc + i * aInc.Length, preshaped + i * aConv.Length, sizeof(float) * aConv.Length, sizeof(float) * aConv.Length);
251250
Assert.IsTrue(reshaped.ContentEquals(aConv));
251+
252+
// Backpropagation
253+
Tensor.New(xTensor.Entities, xTensor.Length, out Tensor z1);
254+
KerasWeightsProvider.FillWithHeEtAlUniform(z1, 10);
255+
z1.Duplicate(out Tensor z2);
256+
conv.Backpropagate(aConv, zTemp, pool.ActivationFunctions.ActivationPrime);
257+
pool.Backpropagate(zTemp, z1, ActivationFunctions.ReLUPrime);
258+
inception.Backpropagate(aInc, z2, ActivationFunctions.ReLUPrime);
259+
Assert.IsTrue(z1.ContentEquals(z2));
260+
261+
// Gradient
262+
conv.ComputeGradient(aTemp, aConv, out Tensor dJdwConv, out Tensor dJdbConv);
263+
inception.ComputeGradient(xTensor, aInc, out Tensor dJdwInc, out Tensor dJdbInc);
264+
Tensor.Reshape((float*)dJdwInc.Ptr.ToPointer() + (3 * 3 + 3 * 2 + 3 * 3 * 2 * 2 + 3 * 2 + 5 * 5 * 2 * 2), 1, dJdwConv.Size, out Tensor dJdwInc0);
265+
Tensor.Reshape((float*)dJdbInc.Ptr.ToPointer() + 11, 1, dJdbConv.Size, out Tensor dJdbInc0);
266+
Assert.IsTrue(dJdwConv.ContentEquals(dJdwInc0, 1e-5f));
267+
Assert.IsTrue(dJdbConv.ContentEquals(dJdbInc0, 1e-5f));
268+
269+
// Cleanup
270+
zTemp.Free();
271+
aTemp.Free();
272+
zConv.Free();
273+
zInc.Free();
252274
aConv.Free();
253275
aInc.Free();
254276
reshaped.Free();

0 commit comments

Comments
 (0)