Skip to content

3.2. The Training Setup

gino-useaible edited this page May 16, 2017 · 3 revisions

We will need to setup the references and connection strings to start. Create a project, we recommend a console application, name it accordingly and we can start adding the references.

Right-click references on your solution explorer for your new solution, then click Add References. Then select the RLM Core and the other libraries.

Once that has been added, we need to configure the connection strings for your local SQL Server. Open App.config by double-clicking on it. Add the App settings below:

<appSettings>
   <add key="RLMConnStr" value="Server=.;Database={{dbName}}"/>
</appSettings>

If you don’t have a local server, you can change the string of the value variable and change it to your liking. The server can be changed. If you have a username and password for that SQL Server, add the keywords after you add a semi-colon (;) below:

;User=SampleUsername;Password=SamplePassword

As mentioned on Chapter 2, we will need to use .Net Framework 4.6.2 so let’s make sure that your new solution is targeting that. Right click on your solution name and go to properties. On the Application tab, you should see Target Framework. Make sure that it is 4.6.2.

Then finally, the namespace need to be defined.

using RLM.Models;
using RLM.Enums;
using RLM;

Let's start coding!

Our first approach would be identifying the inputs as well as the outputs. We have 2 inputs, this will be the items to compare, and 1 output, which will be the result.

We will also need the ideal data, of which our training will be based upon. For this, we will use the XOR table which contains the possibilities of inputs and with it, its correct output.

static IEnumerable<dynamic> xorTable = new List<dynamic>
{
    new { Input1 = "True", Input2 = "True", Output = "False" },
    new { Input1 = "True", Input2 = "False", Output = "True" },
    new { Input1 = "False", Input2 = "True", Output = "True" },
    new { Input1 = "False", Input2 = "False", Output = "False" },
};

Let’s start coding the program.

var rlmNet = new RlmNetwork($"RLM_XOR_SAMPLE_{Guid.NewGuid().ToString("N")}");

We have appended a GUID here so that we have a unique RLM Network everytime we run this example, to have a fresh start. You can remove this or simply change the name to something static to use the same network all the time.

rlmNet.DataPersistenceComplete += RlmNet_DataPersistenceComplete;
rlmNet.DataPersistenceProgress += RlmNet_DataPersistenceProgress;

Here are some events that we can subscribe to. The DataPersistenceComplete notifies as if the DB Write is done. DataPersistenceProgress shows us what the progress is and of what total. Below is a sample method that runs which are subscribed to the events. These will display on the console once it starts.

private static void RlmNet_DataPersistenceComplete()
{
	Console.WriteLine("Done");
}

private static void RlmNet_DataPersistenceProgress(long processed, long total)
{
	Console.WriteLine("{0} of {1}", processed.ToString(), total.ToString());
}

We will now be loading the network. This is not a database, but a table in your defined database.

if (!rlmNet.LoadNetwork("XOR_SAMPLE"))
{

	// here you declare our inputs
	var ins = new List<RlmIO>();
	ins.Add(new RlmIO("XORInput1", typeof(bool).ToString(), 0, 1, RlmInputType.Distinct));
	ins.Add(new RlmIO("XORInput2", typeof(bool).ToString(), 0, 1, RlmInputType.Distinct));

	// and here, declare our outputs
	var outs = new List<RlmIO>();
	outs.Add(new RlmIO("XOROutput", typeof(bool).ToString(), 0, 1));

	// creates a new network 
	rlmNet.NewNetwork("XOR_SAMPLE", ins, outs);

}

Here we have declared our inputs and outputs, all of the type boolean, as well as creating the network. This code is enclosed in an if statement that checks if there’s already a network created for the App.

Now, we will need to set the network settings.

rlmNet.NumSessions = 50;
rlmNet.StartRandomness = 50;
rlmNet.EndRandomness = 0;

We’re just going to start with 50 sessions, which is not too many yet good enough to squeeze some learning in. The StartRandomness is the percentage of randomness applied to the engine, to try new values or not. This will get lower as the sessions move up until it reaches the EndRandomness set.

rlmNet.ResetRandomizationCounter();

You usually need to call this when you want to do multiple trainings on the same network. This resets the randomization of the RLM back to the original start randomness after each training set.