Skip to content

Commit 0929906

Browse files
committed
More convolution methods refactored and moved
1 parent 5584d31 commit 0929906

File tree

4 files changed

+123
-104
lines changed

4 files changed

+123
-104
lines changed

NeuralNetwork.NET/Networks/Layers/Cpu/ConvolutionalLayer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ public override unsafe void Backpropagate(in Tensor dy, in Tensor z, ActivationF
9999
fixed (float* pw = Weights)
100100
{
101101
Tensor.Reshape(pw, OutputInfo.Channels, KernelInfo.Size, out Tensor wTensor);
102-
wTensor.Rotate180(KernelInfo.Channels, out Tensor w180);
103-
dy.ConvoluteBackwards(OutputInfo, w180, KernelInfo, out Tensor delta);
104-
w180.Free();
105-
z.InPlaceActivationAndHadamardProduct(delta, activationPrime);
106-
delta.Free();
102+
Tensor.New(z.Entities, InputInfo.Size, out Tensor dx);
103+
CpuDnn.ConvolutionBackwardData(dy, OutputInfo, wTensor, KernelInfo, dx, InputInfo);
104+
CpuDnn.ActivationBackward(z, dx, activationPrime, z);
105+
dx.Free();
107106
}
108107
}
109108

@@ -114,7 +113,8 @@ public override void ComputeGradient(in Tensor a, in Tensor delta, out Tensor dJ
114113
ConvolutionExtensions.ConvoluteGradient(a180, InputInfo, delta, OutputInfo, out Tensor dJdwM);
115114
dJdwM.Reshape(1, Weights.Length, out dJdw);
116115
a180.Free();
117-
ConvolutionExtensions.CompressVertically(delta, OutputInfo.Channels, out dJdb);
116+
Tensor.New(1, Biases.Length, out dJdb);
117+
CpuDnn.ConvolutionBackwardBias(delta, OutputInfo, dJdb);
118118
}
119119

120120
#endregion

NeuralNetwork.NET/cpuDNN/CpuBlas.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class CpuBlas
1212
/// </summary>
1313
/// <param name="x">The <see cref="Tensor"/> to transpose</param>
1414
/// <param name="y">The output <see cref="Tensor"/></param>
15-
public static unsafe void Transpose(in this Tensor x, in Tensor y)
15+
public static unsafe void Transpose(in Tensor x, in Tensor y)
1616
{
1717
// Setup
1818
if (!y.MatchShape(x.Length, x.Entities)) throw new ArgumentException("The output tensor doesn't have the right shape");
@@ -32,10 +32,10 @@ void Kernel(int i)
3232
/// <summary>
3333
/// Performs the multiplication between two matrices
3434
/// </summary>
35-
/// <param name="m1">The first matrix to multiply</param>
36-
/// <param name="m2">The second matrix to multiply</param>
37-
/// <param name="result">The resulting matrix</param>
38-
public static unsafe void Multiply(in this Tensor x1, in Tensor x2, in Tensor y)
35+
/// <param name="x1">The first matrix to multiply</param>
36+
/// <param name="x2">The second matrix to multiply</param>
37+
/// <param name="y">The resulting matrix</param>
38+
public static unsafe void Multiply(in Tensor x1, in Tensor x2, in Tensor y)
3939
{
4040
// Initialize the parameters and the result matrix
4141
if (x1.Length != x2.Entities) throw new ArgumentOutOfRangeException("Invalid matrices sizes");
@@ -64,5 +64,30 @@ void Kernel(int i)
6464
}
6565
Parallel.For(0, n, Kernel).AssertCompleted();
6666
}
67+
68+
/// <summary>
69+
/// Compresses a <see cref="Tensor"/> into a row by summing the components column by column
70+
/// </summary>
71+
/// <param name="x">The <see cref="Tensor"/> to compress</param>
72+
/// <param name="y">The resulting <see cref="Tensor"/></param>
73+
public static unsafe void CompressVertically(in Tensor x, in Tensor y)
74+
{
75+
// Preliminary checks and declarations
76+
int
77+
n = x.Entities,
78+
l = x.Length;
79+
if (!y.MatchShape(1, x.Length)) throw new ArgumentException("The output tensor doesn't have the right shape", nameof(y));
80+
float* px = x, py = y;
81+
82+
// Compress the tensor
83+
void Kernel(int j)
84+
{
85+
float sum = 0;
86+
for (int i = 0; i < n; i++)
87+
sum += px[i * l + j];
88+
py[j] = sum;
89+
}
90+
Parallel.For(0, l, Kernel).AssertCompleted();
91+
}
6792
}
6893
}

0 commit comments

Comments
 (0)