本文整理汇总了C#中Network.Error方法的典型用法代码示例。如果您正苦于以下问题:C# Network.Error方法的具体用法?C# Network.Error怎么用?C# Network.Error使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Network
的用法示例。
在下文中一共展示了Network.Error方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public static void Run()
{
var trainingSets = GenerateTrainingSets(100);
Network network;
double error = 0;
do
{
Console.WriteLine("Optimizing...");
network = new Network(trainingSets[0].Inputs.Length, 2, 10, trainingSets[0].Outputs.Length);
network.Weights = BruteOptimizer.OptimizeMulti(network, trainingSets);
error = network.Error(trainingSets, network.Weights);
Console.WriteLine("Error from last optimization attempt: " + error);
} while (error > 3);
Console.WriteLine("Optimization complete!");
while (true)
{
Console.Write("Enter space-separated inputs: ");
var inputs = Console.ReadLine().Split(' ');
if (inputs.Length == 1)
{
break;
}
var inputArray = new double[] { double.Parse(inputs[0].Trim()), double.Parse(inputs[1].Trim()) };
Console.WriteLine(network.Pulse(inputArray)[0]);
}
}
示例2: Run
public static void Run()
{
var trainingSets = LoadTrainingSets();
Network network = new Network(13, 1, 4, 3);
network.AddBiasNeuron(0);
double error = 0;
double deltaDrop = 0;
int tries = 0;
do
{
//if (tries % 5 == 0 && deltaDrop < 0.5)
//{
// Console.Write("Randomizing weights");
// network.RandomizeWeights();
//}
Console.WriteLine("Optimizing...");
//network.Weights = BruteOptimizer.OptimizeMulti(network, trainingSets);
network.Weights = BackPropOptimizer.Optimize(network, trainingSets, 2, 1);
var newError = network.Error(trainingSets, network.Weights);
deltaDrop = error - newError;
error = newError;
Console.WriteLine("Error from last optimization attempt: " + error);
tries++;
} while (error > 5 /*false || error > 10 || deltaDrop < 1*/);
Console.WriteLine("Optimization complete!");
while (true)
{
Console.Write("Enter comma-separated inputs: ");
var inputs = Console.ReadLine().Split(',');
if (inputs.Length == 1)
{
break;
}
var inputArray = new double[13];
for (var i = 0; i < 13; i++)
{
var minVal = RawTrainingSets.Min(set => set.Inputs[i]);
var maxVal = RawTrainingSets.Max(set => set.Inputs[i]);
inputArray[i] = Normalize(double.Parse(inputs[i].Trim()), minVal, maxVal);
}
var output = network.Pulse(inputArray);
Console.WriteLine(output[0] + " " + output[1] + " " + output[2]);
}
}
示例3: Optimize
/// <summary>
/// Optimizes weights for a given training set
/// </summary>
/// <param name="network">The network to optimize</param>
/// <param name="set">The set to optimize for</param>
/// <param name="trainingFactor">The training factor (how large the changes should be)</param>
/// <returns>The optimized weights</returns>
public static double[][][] Optimize(Network network, TrainingSet set, double trainingFactor = 0.1)
{
var outputs = network.PulseDetailed(set.Inputs, true);
var deltas = new double[network.Weights.Length][];
for (var layer = network.Weights.Length - 1; layer >= 0; layer--)
{
deltas[layer] = new double[network.Weights[layer].Length];
for (var neuron = 0; neuron < network.Weights[layer].Length; neuron++)
{
if (layer == network.Weights.Length - 1)
{
deltas[layer][neuron] = Delta(network, set, layer, neuron);
}
else
{
deltas[layer][neuron] = Delta(network, set, layer, neuron, deltas[layer + 1]);
}
for (var input = 0; input < network.Weights[layer][neuron].Length; input++)
{
var delta = deltas[layer][neuron];
var errorPrime = 0.0;
if (input < outputs[layer].Length)
{
//No need for layer-1 since the addition of the inputs pushes all the layers +1
errorPrime = delta * outputs[layer/* - 1*/][input]; //Error prime = (d Error) / (d weight)
}
else
{
//Assume it's a bias neuron of value 1
errorPrime = delta * 1;
}
var deltaWeight = (-1.0) * trainingFactor * errorPrime;
var preError = network.Error(set, network.Weights);
var preErrorWeight = network.Weights[layer][neuron][input];
network.Weights[layer][neuron][input] += deltaWeight;
var postError = network.Error(set, network.Weights);
if (postError > preError)
{
network.Weights[layer][neuron][input] -= deltaWeight;
}
}
}
}
return network.Weights;
}