Hands-On Neural Network Programming with C#
上QQ阅读APP看书,第一时间看更新

Training the network

There are two ways of training the network. One is to a minimum error value, the other is to a maximum error value. Both functions have defaults, although you may wish to make the threshold different for your training, as follows:

public NNManager TrainNetworkToMinimum()
{
var minError = GetDouble("\tMinimum Error: ", 0.000000001, 1.0);
Console.WriteLine("\tTraining...");
_network.Train(_dataSets, minError);
Console.WriteLine("\t**Training Complete**", Color.Green);
return this;
}

public NNManager TrainNetworkToMaximum()
{
varmaxEpoch = GetInput("\tMax Epoch: ", 1, int.MaxValue);
if(!maxEpoch.HasValue)
{
return this;
}

Console.WriteLine("\tTraining...");
_network.Train(_dataSets, maxEpoch.Value);
Console.WriteLine("\t**Training Complete**", Color.Green);
return this;
}

In both of the preceding function definitions, the neural network Train function is called to perform the actual training. This function in turn calls the forward and backward propagation functions for each dataset from within each iteration of the training loop, as follows:

public void Train(List<DataSet>dataSets, int numEpochs)
{
for (var i = 0; i<numEpochs; i++)
{
foreach (var dataSet in dataSets)
{
ForwardPropagate(dataSet.Values);
BackPropagate(dataSet.Targets);
}
}
}