本文整理汇总了C#中Network.PulseDetailed方法的典型用法代码示例。如果您正苦于以下问题:C# Network.PulseDetailed方法的具体用法?C# Network.PulseDetailed怎么用?C# Network.PulseDetailed使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Network
的用法示例。
在下文中一共展示了Network.PulseDetailed方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Delta
/// <summary>
/// Calculates the "delta value" for a specified neuron.
/// For output neurons, delta = (calculated - actual)*(calculated - calculated^2)
/// For hidden neurons in level l, delta = (calculated - calculated^2)* (sum n in neurons in l+1 [ delta((l+1)[n]) * weight(l[n] -> (l+1)[n]) ])
/// </summary>
/// <param name="network">The network to calculate the delta on</param>
/// <param name="set">The training set to calculate the delta on</param>
/// <param name="innerLayer">The inner layer index to calculate the training set on</param>
/// <param name="neuron">The neuron index to calculate the training set on</param>
/// <param name="deltas">The delta values for the L+1 layer</param>
/// <returns>The delta value for the specified neuron</returns>
public static double Delta(Network network, TrainingSet set, int innerLayer, int neuron, double[] deltas = null)
{
var isOutputLayer = innerLayer == network.Layers.Length - 1;
if (isOutputLayer)
{
var output = network.Pulse(set.Inputs)[neuron];
return (output - set.Outputs[neuron]) * (output - Math.Pow(output, 2));
}
else
{
var outputs = network.PulseDetailed(set.Inputs, false);
var actualOutput = outputs[innerLayer][neuron];
var summation = 0.0;
for (var n = 0; n < network.Weights[innerLayer + 1].Length; n++)
{
summation += deltas[n] * network.Weights[innerLayer + 1][n][neuron];
}
return (actualOutput - Math.Pow(actualOutput, 2)) * summation;
}
}
示例2: 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;
}