本文整理汇总了C#中Encog.Neural.Networks.BasicNetwork.Compute方法的典型用法代码示例。如果您正苦于以下问题:C# BasicNetwork.Compute方法的具体用法?C# BasicNetwork.Compute怎么用?C# BasicNetwork.Compute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encog.Neural.Networks.BasicNetwork
的用法示例。
在下文中一共展示了BasicNetwork.Compute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
//create a neural network withtout using a factory
var network = new BasicNetwork();
network.AddLayer(new BasicLayer(null, true, 2));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 2));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
network.Structure.FinalizeStructure();
network.Reset();
IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
IMLTrain train = new ResilientPropagation(network, trainingSet);
int epoch = 1;
do
{
train.Iteration();
Console.WriteLine($"Epoch #{epoch} Error: {train.Error}");
epoch++;
} while (train.Error > 0.01);
train.FinishTraining();
Console.WriteLine("Neural Network Results:");
foreach (IMLDataPair iPair in trainingSet)
{
IMLData output = network.Compute(iPair.Input);
Console.WriteLine($"{iPair.Input[0]}, {iPair.Input[0]}, actual={output[0]}, ideal={iPair.Ideal[0]}");
}
EncogFramework.Instance.Shutdown();
Console.ReadKey();
}
示例2: Main
static void Main(string[] args)
{
var network = new BasicNetwork();
network.AddLayer(new BasicLayer(null, true, 2));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
network.Structure.FinalizeStructure();
network.Reset();
var trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
var train = new ResilientPropagation(network, trainingSet);
var epoch = 1;
do
{
train.Iteration();
} while (train.Error > 0.01);
train.FinishTraining();
foreach (var pair in trainingSet)
{
var output = network.Compute(pair.Input);
Console.WriteLine(pair.Input[0] + @", " + pair.Input[1] + @" , actual=" + output[0] + @", ideal=" + pair.Ideal[0]);
}
EncogFramework.Instance.Shutdown();
Console.ReadLine();
}
示例3: EvaluateNetworks
public static double EvaluateNetworks(BasicNetwork network, BasicMLDataSet set)
{
int count = 0;
int correct = 0;
foreach (IMLDataPair pair in set)
{
IMLData input = pair.Input;
IMLData actualData = pair.Ideal;
IMLData predictData = network.Compute(input);
double actual = actualData[0];
double predict = predictData[0];
double diff = Math.Abs(predict - actual);
Direction actualDirection = DetermineDirection(actual);
Direction predictDirection = DetermineDirection(predict);
if (actualDirection == predictDirection)
correct++;
count++;
Console.WriteLine(@"Number" + @"count" + @": actual=" + Format.FormatDouble(actual, 4) + @"(" + actualDirection + @")"
+ @",predict=" + Format.FormatDouble(predict, 4) + @"(" + predictDirection + @")" + @",diff=" + diff);
}
double percent = correct / (double)count;
Console.WriteLine(@"Direction correct:" + correct + @"/" + count);
Console.WriteLine(@"Directional Accuracy:"
+ Format.FormatPercent(percent));
return percent;
}
示例4: MeasurePerformance
/// <summary>
/// Measure the performance of the network
/// </summary>
/// <param name = "network">Network to analyze</param>
/// <param name = "dataset">Dataset with input and ideal data</param>
/// <returns>Error % of correct bits, returned by the network.</returns>
public static double MeasurePerformance(BasicNetwork network, BasicNeuralDataSet dataset)
{
int correctBits = 0;
float threshold = 0.0f;
IActivationFunction activationFunction = network.GetActivation(network.LayerCount - 1); //get the activation function of the output layer
if (activationFunction is ActivationSigmoid)
{
threshold = 0.5f; /* > 0.5, range of sigmoid [0..1]*/
}
else if (activationFunction is ActivationTANH)
{
threshold = 0.0f; /*> 0, range of bipolar sigmoid is [-1..1]*/
}
else
throw new ArgumentException("Bad activation function");
int n = (int) dataset.Count;
Parallel.For(0, n, (i) =>
{
IMLData actualOutputs = network.Compute(dataset.Data[i].Input);
lock (LockObject)
{
for (int j = 0, k = actualOutputs.Count; j < k; j++)
if ((actualOutputs[j] > threshold && dataset.Data[i].Ideal[j] > threshold)
|| (actualOutputs[j] < threshold && dataset.Data[i].Ideal[j] < threshold))
correctBits++;
}
});
long totalOutputBitsCount = dataset.Count*dataset.Data[0].Ideal.Count;
return (double) correctBits/totalOutputBitsCount;
}
示例5: EvaluateNetwork
public void EvaluateNetwork(BasicNetwork trainedNetwork, BasicMLDataSet trainingData)
{
foreach (var trainingItem in trainingData)
{
var output = trainedNetwork.Compute(trainingItem.Input);
Console.WriteLine("Input:{0}, {1} Ideal: {2} Actual : {3}", trainingItem.Input[0], trainingItem.Input[1], trainingItem.Ideal, output[0]);
}
Console.ReadKey();
}
示例6: Main
private static void Main(string[] args)
{
// create a neural network, without using a factory
var network = new BasicNetwork();
network.AddLayer(new BasicLayer(null, true, 2));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
network.Structure.FinalizeStructure();
network.Reset();
// create training data
IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
// train the neural network
IMLTrain train = new Backpropagation(network, trainingSet, 0.5, 0.2);
int epoch = 1;
do
{
train.Iteration();
Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
epoch++;
}
while (train.Error > 0.01);
// test the neural network
Console.WriteLine(@"Neural Network Results:");
foreach (IMLDataPair pair in trainingSet)
{
IMLData output = network.Compute(pair.Input);
Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
+ @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
}
Console.Read();
}
示例7: Run
public override void Run()
{
testNetwork = new BasicNetwork();
testNetwork.AddLayer(new BasicLayer(null, true, 2));
testNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 4));
testNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
testNetwork.Structure.FinalizeStructure();
testNetwork.Reset();
// create training data
IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
// train the neural network
IMLTrain train = new Backpropagation(testNetwork, trainingSet);
//IMLTrain train = new ResilientPropagation(testNetwork, trainingSet); //Encog manual says it is the best general one
int epoch = 1;
do
{
train.Iteration();
Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
epoch++;
} while (train.Error > 0.0001);
// test the neural network
Console.WriteLine(@"Neural Network Results:");
foreach (IMLDataPair pair in trainingSet)
{
IMLData output = testNetwork.Compute(pair.Input);
Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
+ @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
}
}
示例8: Predict
public void Predict(BasicNetwork network)
{
Console.WriteLine(@"Year Actual Predict Closed Loop Predict Denormalized Value Real Value");
for (int year = EvaluateStart; year < EvaluateEnd; year++)
{
// calculate based on actual data
IMLData input = new BasicMLData(WindowSize);
for (var i = 0; i < input.Count; i++)
{
input.Data[i] = _normalizedSunspots[(year - WindowSize) + i];
}
IMLData output = network.Compute(input);
double prediction = output.Data[0];
_closedLoopSunspots[year] = prediction;
// calculate "closed loop", based on predicted data
for (var i = 0; i < input.Count; i++)
{
input.Data[i] = _closedLoopSunspots[(year - WindowSize) + i];
}
output = network.Compute(input);
double closedLoopPrediction = output.Data[0];
// display
Console.WriteLine((StartingYear + year)
+ @" " + Format.FormatDouble(_normalizedSunspots[year], 5)
+ @" " + Format.FormatDouble(prediction, 5)
+ @" " + Format.FormatDouble(closedLoopPrediction, 5)
+ @" Accuracy:" +
Format.FormatDouble(_normalizedSunspots[year] - prediction, 5)
+ " Denormalized:" + array.Stats.DeNormalize(prediction)
+ " Real value:" + Sunspots[year]);
}
}
示例9: Predict
public void Predict(BasicNetwork network)
{
Console.WriteLine(@"Year Actual Predict Closed Loop Predict Denormalized Value Real Value");
for (var year = EvaluateStart; year < EvaluateEnd; year++)
{
// calculate based on actual data
var input = new BasicMLData(WindowSize);
for (var i = 0; i < input.Count; i++)
{
input[i] = _normalizedForexPair[(year - WindowSize) + i];
}
IMLData output = network.Compute(input);
var prediction = output[0];
_closedLoopForexPair[year] = prediction;
// calculate "closed loop", based on predicted data
for (var i = 0; i < input.Count; i++)
{
input[i] = _closedLoopForexPair[(year - WindowSize) + i];
}
output = network.Compute(input);
var closedLoopPrediction = output[0];
// display
Console.WriteLine("{0} {1} {2} {3} Accuracy:{4} Denormalized:{5} Real value:{6}",
(StartingYear + year),
Format.FormatDouble(_normalizedForexPair[year], 5),
Format.FormatDouble(prediction, 5),
Format.FormatDouble(closedLoopPrediction, 5),
Format.FormatDouble(_normalizedForexPair[year] - prediction, 5),
array.Stats.DeNormalize(prediction),
ForexPair[year]);
}
}
示例10: CallNN
// Wrap it to be linq friendly
private static double CallNN(BasicNetwork network, double input1, double input2)
{
double[] input = new[] { input1, input2 };
double[] output = new double[1];
network.Compute(input, output);
return output[0];
}
示例11: Predict
public void Predict(BasicNetwork network)
{
Console.WriteLine(@"Year Actual Predict Closed Loop Predict");
for (int year = EvaluateStart; year < EvaluateEnd; year++)
{
// calculate based on actual data
var input = new BasicMLData(WindowSize);
for (var i = 0; i < input.Count; i++)
{
input[i] = _normalizedSunspots[(year - WindowSize) + i];
}
IMLData output = network.Compute(input);
double prediction = output[0];
_closedLoopSunspots[year] = prediction;
// calculate "closed loop", based on predicted data
for (var i = 0; i < input.Count; i++)
{
input[i] = _closedLoopSunspots[(year - WindowSize) + i];
}
output = network.Compute(input);
double closedLoopPrediction = output[0];
// display
Console.WriteLine((StartingYear + year)
+ @" " + Format.FormatDouble(_normalizedSunspots[year], 2)
+ @" " + Format.FormatDouble(prediction, 2)
+ @" " + Format.FormatDouble(closedLoopPrediction, 2));
}
}
示例12: Evaluate
/// <summary>
/// Evaluate the network and display (to the console) the output for every
/// value in the training set. Displays ideal and actual.
/// </summary>
/// <param name="network">The network to evaluate.</param>
/// <param name="training">The training set to evaluate.</param>
public static void Evaluate(BasicNetwork network,
INeuralDataSet training)
{
foreach (INeuralDataPair pair in training)
{
INeuralData output = network.Compute(pair.Input);
Console.WriteLine("Input="
+ EncogUtility.FormatNeuralData(pair.Input)
+ ", Actual=" + EncogUtility.FormatNeuralData(output)
+ ", Ideal="
+ EncogUtility.FormatNeuralData(pair.Ideal));
}
}
示例13: Learn
public List<double[]> Learn(double[][] data, double[][] ideal)
{
double[][] origData = (double[][])data.Clone();
int n = data.Length;
int m = data[0].Length;
double[][] output = new double[n][];
double[][] sgmNeighbours = new double[n][];
for (var i = 0; i < n; i++)
{
double[] sgmN = new double[SegmentationData.SEGMENT_NEIGHBOURS];
Array.Copy(data[i], m - SegmentationData.SEGMENT_NEIGHBOURS, sgmN, 0, SegmentationData.SEGMENT_NEIGHBOURS);
sgmNeighbours[i] = sgmN;
data[i] = data[i].Take(m - SegmentationData.SEGMENT_NEIGHBOURS).ToArray();
output[i] = new double[m - SegmentationData.SEGMENT_NEIGHBOURS];
data[i].CopyTo(output[i], 0);
}
IMLDataSet trainingSet = new BasicMLDataSet(data, output);
int inputLayerSize = layersConfiguration[0] - SegmentationData.SEGMENT_NEIGHBOURS;
int trainingLayerSize = layersConfiguration[1];
BasicNetwork oneLayerAutoencoder = new BasicNetwork();
oneLayerAutoencoder.AddLayer(new BasicLayer(null, BIAS, inputLayerSize));
oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, trainingLayerSize));
oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, inputLayerSize));
oneLayerAutoencoder.Structure.FinalizeStructure();
oneLayerAutoencoder.Reset();
IMLTrain train = new ResilientPropagation(oneLayerAutoencoder, trainingSet);
//IMLTrain train = new Backpropagation(oneLayerAutoencoder, trainingSet, LEARNING_RATE, MOMENTUM);
int epoch = 1;
List<double[]> errors = new List<double[]>();
double[] trainError = new double[AUTOENCODER_MAX_ITER];
do
{
train.Iteration();
ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
trainError[epoch - 1] = train.Error;
epoch++;
//errors.Add(train.Error);
} while (train.Error > EPS && epoch < AUTOENCODER_MAX_ITER);
errors.Add(trainError);
train.FinishTraining();
BasicNetwork encoder = new BasicNetwork();
encoder.AddLayer(new BasicLayer(null, BIAS, oneLayerAutoencoder.GetLayerNeuronCount(0)));
encoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, oneLayerAutoencoder.GetLayerNeuronCount(1)));
encoder.Structure.FinalizeStructure();
encoder.Reset();
//przypisanie wag do encodera
for (int i = 0; i < encoder.LayerCount - 1; i++)
for (int f = 0; f < encoder.GetLayerNeuronCount(i); f++)
for (int t = 0; t < encoder.GetLayerNeuronCount(i + 1); t++)
encoder.SetWeight(i, f, t, oneLayerAutoencoder.GetWeight(i, f, t));
//Compare2Networks(oneLayerAutoencoder, encoder);
for(int l=1; l<layersConfiguration.Count -2; l++)
{
inputLayerSize = layersConfiguration[l];
trainingLayerSize = layersConfiguration[l+1];
oneLayerAutoencoder = new BasicNetwork();
oneLayerAutoencoder.AddLayer(new BasicLayer(null, BIAS, inputLayerSize));
oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, trainingLayerSize));
oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, inputLayerSize));
oneLayerAutoencoder.Structure.FinalizeStructure();
oneLayerAutoencoder.Reset();
//liczenie outputu z dotychczasowego encodera
double[][] input = new double[n][];
double[][] newOutput = new double[n][];
for(int ni = 0; ni <n; ni++)
{
IMLData res = encoder.Compute(new BasicMLData(data[ni]));
double[] resD = new double[res.Count];
for(int i=0; i<res.Count; i++)
resD[i] = res[i];
input[ni] = resD;
newOutput[ni] = new double[res.Count];
input[ni].CopyTo(newOutput[ni], 0);
}
BasicMLDataSet newTrainingSet = new BasicMLDataSet(input, newOutput);
train = new ResilientPropagation(oneLayerAutoencoder, newTrainingSet);
//train = new Backpropagation(oneLayerAutoencoder, newTrainingSet, LEARNING_RATE, MOMENTUM);
epoch = 1;
trainError = new double[AUTOENCODER_MAX_ITER];
do
{
train.Iteration();
ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
trainError[epoch - 1] = train.Error;
epoch++;
} while (train.Error > EPS && epoch < AUTOENCODER_MAX_ITER);
//.........这里部分代码省略.........
示例14: Execute
/// <summary>
/// Program entry point.
/// </summary>
/// <param name="app">Holds arguments and other info.</param>
public void Execute(IExampleInterface app)
{
// create a neural network, without using a factory
var network = new BasicNetwork();
network.AddLayer(new BasicLayer(null, true, 2));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
network.Structure.FinalizeStructure();
network.Reset();
// create training data
IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
// train the neural network using online (batch=1)
Propagation train = new Backpropagation(network, trainingSet, 0.7, 0.3);
train.BatchSize = 1;
int epoch = 1;
do
{
train.Iteration();
Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
epoch++;
} while (train.Error > 0.01);
// test the neural network
Console.WriteLine(@"Neural Network Results:");
foreach (IMLDataPair pair in trainingSet)
{
IMLData output = network.Compute(pair.Input);
Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
+ @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
}
}
示例15: TestModel
public void TestModel(string testDataPath, BasicNetwork model, ProblemType problem, ActivationType activation)
{
TestDataPath = testDataPath;
var csvReader = new ReadCSV(testDataPath, true, CSVFormat.DecimalPoint);
var values = new List<double[]>();
var originalValues = new List<double[]>();
while (csvReader.Next())
{
values.Add(ProblemType.Classification == problem
? new[] {csvReader.GetDouble(0), csvReader.GetDouble(1)}
: new[] {csvReader.GetDouble(0)});
originalValues.Add(ProblemType.Classification == problem
? new[] { csvReader.GetDouble(0), csvReader.GetDouble(1) }
: new[] { csvReader.GetDouble(0) });
}
csvReader.Close();
Normalize(values, _valuesMins, _valuesMaxes, activation);
var answers = new List<double>();
foreach (var value in values)
{
var answer = new double[LastLayerSize];
model.Compute(value, answer);
answers.Add(problem == ProblemType.Regression ? DenormalizeAnswer(answer[0], activation) : GetClassFromAnswer(answer));
}
AnswerPath = Path.GetFullPath(TestDataPath) + ".solved";
var lines = new List<string>();
lines.Add(problem == ProblemType.Classification ? "x,y,clc" : "x,y");
lines.AddRange(answers.Select((t, i) =>
problem == ProblemType.Regression
? originalValues[i][0].ToString(CultureInfo.InvariantCulture) + "," + t.ToString(CultureInfo.InvariantCulture)
: originalValues[i][0].ToString(CultureInfo.InvariantCulture) + "," + originalValues[i][1].ToString(CultureInfo.InvariantCulture) + "," + t.ToString(CultureInfo.InvariantCulture)));
File.WriteAllLines(AnswerPath, lines);
}