Skip to content

Commit b858921

Browse files
committed
Activation code added
1 parent 7af1c6f commit b858921

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

NeuralNetwork.NET/APIs/Structs/Tensor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ public void Reshape(int n, int chw, out Tensor tensor)
219219
tensor = new Tensor(Ptr, n, chw);
220220
}
221221

222+
/// <summary>
223+
/// Checks whether or not the current instance has the same shape of the input <see cref="Tensor"/>
224+
/// </summary>
225+
/// <param name="tensor">The instance to compare</param>
226+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
227+
public bool MatchShape(in Tensor tensor) => Entities == tensor.Entities && Length == tensor.Length;
228+
222229
/// <summary>
223230
/// Frees the memory associated with the current instance
224231
/// </summary>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using JetBrains.Annotations;
4+
using NeuralNetworkNET.APIs.Structs;
5+
using NeuralNetworkNET.Extensions;
6+
using NeuralNetworkNET.Networks.Activations.Delegates;
7+
8+
namespace NeuralNetworkNET.cpuDNN
9+
{
10+
public static class CpuDnn
11+
{
12+
#region Activation
13+
14+
public static unsafe void ActivationForward(in Tensor x, [NotNull] ActivationFunction f, in Tensor y)
15+
{
16+
// Setup
17+
int n = x.Entities, l = x.Length;
18+
if (!y.MatchShape(x)) throw new ArgumentException("The target tensor must have the same shape as the input");
19+
float* py = y, px = x;
20+
21+
// Execute the activation in parallel
22+
void Kernel(int i)
23+
{
24+
int offset = i * l;
25+
for (int j = 0; j < l; j++)
26+
{
27+
int target = offset + j;
28+
py[target] = f(px[target]);
29+
}
30+
}
31+
Parallel.For(0, n, Kernel).AssertCompleted();
32+
}
33+
34+
public static unsafe void ActivationBackward(in Tensor x, in Tensor y, [NotNull] ActivationFunction f_, in Tensor dx)
35+
{
36+
// Check
37+
if (!y.MatchShape(x)) throw new ArgumentException(nameof(y), "The input tensors must have the same shape");
38+
if (!dx.MatchShape(x)) throw new ArgumentException(nameof(y), "The output tensor must have the same shape as the input");
39+
int
40+
n = x.Entities,
41+
l = x.Length;
42+
float* px = x, py = y, pdx = dx;
43+
44+
// Loop in parallel
45+
void Kernel(int i)
46+
{
47+
int offset = i * l;
48+
for (int j = 0; j < l; j++)
49+
{
50+
int target = offset + j;
51+
pdx[target] = f_(px[target]) * py[target];
52+
}
53+
}
54+
Parallel.For(0, n, Kernel).AssertCompleted();
55+
}
56+
57+
#endregion
58+
}
59+
}

0 commit comments

Comments
 (0)