Importing an existing network

This function will allow us to import a previously saved network. Again, note the return value that makes this a fluent interface:

public static Network ImportNetwork()
{

Get the filename of the previously saved network. Once opened, deserialize it into a network structure that we will deal with. (If it didn't work for some reason, abort!):

  var dn = GetHelperNetwork();
if (dn == null)
return null;

Create a new Network and a list of neurons to populate:

var network = new Network();
var allNeurons = new List<Neuron>();

Copy over the learning rate and the momentum that was previously saved:

network.LearningRate = dn.LearningRate;
network.Momentum = dn.Momentum;

Create input layers from our imported network data:

foreach (var n in dn.InputLayer)
{
var neuron = new Neuron
{
Id = n.Id,
Bias = n.Bias,
BiasDelta = n.BiasDelta,
Gradient = n.Gradient,
Value = n.Value
};

network.InputLayer?.Add(neuron);
allNeurons.Add(neuron);
}

Create our Hidden Layers from our imported network data:

  foreach (var layer in dn.HiddenLayers)
{
var neurons = new List<Neuron>();
foreach (var n in layer)
{
var neuron = new Neuron
{
Id = n.Id,
Bias = n.Bias,
BiasDelta = n.BiasDelta,
Gradient = n.Gradient,
Value = n.Value
};

neurons.Add(neuron);
allNeurons.Add(neuron);
}
network.HiddenLayers?.Add(neurons);
}

Create the OutputLayer neurons from our imported data:

foreach (var n in dn.OutputLayer)
{
var neuron = new Neuron
{
Id = n.Id,
Bias = n.Bias,
BiasDelta = n.BiasDelta,
Gradient = n.Gradient,
Value = n.Value
};

network.OutputLayer?.Add(neuron);
allNeurons.Add(neuron);
}

Finally, create the synapses that tie everything together:


foreach (var syn in dn.Synapses)
{
var synapse = new Synapse{ Id = syn.Id };
var inputNeuron = allNeurons.First(x =>x.Id==syn.InputNeuronId);
var outputNeuron = allNeurons.First(x =>x.Id==syn.OutputNeuronId);
synapse.InputNeuron = inputNeuron;
synapse.OutputNeuron = outputNeuron;
synapse.Weight = syn.Weight;
synapse.WeightDelta = syn.WeightDelta;

inputNeuron?.OutputSynapses?.Add(synapse);
outputNeuron?.InputSynapses?.Add(synapse);
}
return network;
}

The following is where we manually enter the data to be used by the network:

public NNManager GetTrainingDataFromUser()
{
var numDataSets = GetInput(" How many datasets are you going to enter? ", 1, int.MaxValue);

var newDatasets = new List<NNDataSet>();
for (var i = 0; i<numDataSets; i++)
{
var values = GetInputData($" Data Set {i + 1}: ");
if (values == null)
{
return this;
}

var expectedResult = GetExpectedResult($" Expected Result for Data
Set {i + 1}: ");
if (expectedResult == null)
{
return this;
}

newDatasets.Add(newNNDataSet(values, expectedResult));
}

_dataSets = newDatasets;
return this;
}
..................Content has been hidden....................

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