Tests

Tests are actual execution events, small programs if you will. Because of the usage of OpenCL, these programs are compiled at runtime. To create a test, you only need to provide a single static Run function that encapsulates your code. Kelp.Net comes with a preconfigured tester, which makes it very simple to add your own tests. We will explore this in detail in our section on writing tests, for now, here is an example of a simple XOR test program:

public static void Run()
{
const int learningCount = 10000;

Real[][] trainData =
{
new Real[] { 0, 0 },
new Real[] { 1, 0 },
new Real[] { 0, 1 },
new Real[] { 1, 1 }
};

Real[][] trainLabel =
{
new Real[] { 0 },
new Real[] { 1 },
new Real[] { 1 },
new Real[] { 0 }
};

FunctionStack nn = new FunctionStack(
new Linear(2, 2, name: "l1 Linear"),
new ReLU(name: "l1 ReLU"),
new Linear(2, 1, name: "l2 Linear"));

nn.SetOptimizer(new AdaGrad());

RILogManager.Default?.SendDebug("Training...");
for (int i = 0; i < learningCount; i++)
{
                 //use MeanSquaredError for loss function
Trainer.Train(nn, trainData[0], trainLabel[0], new MeanSquaredError(), false);
Trainer.Train(nn, trainData[1], trainLabel[1], new MeanSquaredError(), false);
Trainer.Train(nn, trainData[2], trainLabel[2], new MeanSquaredError(), false);
Trainer.Train(nn, trainData[3], trainLabel[3], new MeanSquaredError(), false);

//If you do not update every time after training, you can update it as a mini batch
nn.Update();
}

RILogManager.Default?.SendDebug("Test Start...");
foreach (Real[] val in trainData)
{
NdArray result = nn.Predict(val)[0];
RILogManager.Default?.SendDebug($"{val[0]} xor {val[1]} = {(result.Data[0] > 0.5 ? 1 : 0)} {result}");
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset