在 C# 中实现神经网络

Implementing a neural network in C#

我正在学习 link 中的教程:http://www.c-sharpcorner.com/UploadFile/rmcochran/AI_OOP_NeuralNet06192006090112AM/AI_OOP_NeuralNet.aspx

我是神经网络的新手,我正在尝试编辑上述教程中的示例以匹配我的问题。我正在使用多元回归来查找 3 组不同数据的系数,然后计算每组数据的 rsquared 值。我正在尝试创建一个神经网络,它将更改系数值以使 rsquared 值尽可能接近 100。

这就是我建立系数并找到该系数的 rsquared 值的方式。所有 3 个系数都使用这些相同的方法:

Calculations calc = new Calculations();
Vector<double> lowRiskCoefficient = MultipleRegression.QR(                                            Matrix<double>.Build.DenseOfColumnArrays(lowRiskShortRatingList.ToArray(), lowRiskMediumRatingList.ToArray(), lowRiskLongRatingList.ToArray()),                                            Vector<double>.Build.Dense(lowRiskWeekReturnList.ToArray()));
                decimal lowRiskShortCoefficient = Convert.ToDecimal(lowRiskCoefficient[0]);
                decimal lowRiskMediumCoefficient = Convert.ToDecimal(lowRiskCoefficient[1]);
                decimal lowRiskLongCoefficient = Convert.ToDecimal(lowRiskCoefficient[2]);
                List<decimal> lowRiskWeekReturnDecimalList = new List<decimal>(lowRiskWeekReturnList.Count);
                lowRiskWeekReturnList.ForEach(i => lowRiskWeekReturnDecimalList.Add(Convert.ToDecimal(i)));
                List<decimal> lowRiskPredictedReturnList = new List<decimal>(lowRiskWeekReturnList.Count);
                List<decimal> lowRiskResidualValueList = new List<decimal>(lowRiskWeekReturnList.Count);
                for (int i = 0; i < lowRiskWeekReturnList.Count; i++)
                {
                    decimal lowRiskPredictedValue = (Convert.ToDecimal(lowRiskShortRatingList.ElementAtOrDefault(i)) * lowRiskShortCoefficient) + (Convert.ToDecimal(lowRiskMediumRatingList.ElementAtOrDefault(i)) * lowRiskMediumCoefficient) +
                        (Convert.ToDecimal(lowRiskLongRatingList.ElementAtOrDefault(i)) * lowRiskLongCoefficient);
                    lowRiskPredictedReturnList.Add(lowRiskPredictedValue);
                    lowRiskResidualValueList.Add(calc.calculateResidual(lowRiskWeekReturnDecimalList.ElementAtOrDefault(i), lowRiskPredictedValue));
                }
                decimal lowRiskTotalSumofSquares = calc.calculateTotalSumofSquares(lowRiskWeekReturnDecimalList, lowRiskWeekReturnDecimalList.Average());
                decimal lowRiskTotalSumofRegression = calc.calculateTotalSumofRegression(lowRiskPredictedReturnList, lowRiskWeekReturnDecimalList.Average());
                decimal lowRiskTotalSumofErrors = calc.calculateTotalSumofErrors(lowRiskResidualValueList);
                decimal lowRiskRSquared = lowRiskTotalSumofRegression / lowRiskTotalSumofSquares;

这是执行训练的示例,我目前正在研究如何更改此示例以匹配我正在尝试做的事情。

private void button1_Click(object sender, EventArgs e)
{
net = new NeuralNet();
double high, mid, low;
high = .9;
low = .1;
mid = .5; 
// initialize with
//   2 perception neurons
//   2 hidden layer neurons
//   1 output neuron
net.Initialize(1, 2, 2, 1);  
double[][] input = new double[4][];
input[0] = new double[] {high, high};
input[1] = new double[] {low, high};
input[2] = new double[] {high, low};
input[3] = new double[] {low, low};
double[][] output = new double[4][];
output[0] = new double[] { low };
output[1] = new double[] { high };
output[2] = new double[] { high };
output[3] = new double[] { low };
double ll, lh, hl, hh;
int count;
count = 0;
do
{
    count++;
    for (int i = 0; i < 100; i++)
        net.Train(input, output);
    net.ApplyLearning();
    net.PerceptionLayer[0].Output = low;
    net.PerceptionLayer[1].Output = low;
    net.Pulse();
    ll = net.OutputLayer[0].Output;
    net.PerceptionLayer[0].Output = high;
    net.PerceptionLayer[1].Output = low;
    net.Pulse();
    hl = net.OutputLayer[0].Output;
    net.PerceptionLayer[0].Output = low;
    net.PerceptionLayer[1].Output = high;
    net.Pulse();
    lh = net.OutputLayer[0].Output;
    net.PerceptionLayer[0].Output = high;
    net.PerceptionLayer[1].Output = high;
    net.Pulse();
    hh = net.OutputLayer[0].Output;
}
while (hh > mid || lh < mid || hl < mid || ll > mid);
MessageBox.Show((count*100).ToString() + " iterations required for training");
}

我如何使用此信息创建一个神经网络来找到 rsquared 值尽可能接近 100 的系数?

您可以使用此处 https://github.com/starhash/Neuroph.NET/releases/tag/v1.0-beta

中的 Neuroph.NET 在 .NET 中构建的 Neuroph 框架,而不是构建一个

这是他们为 JAVA 平台所做的原始 Neuroph 的轻微转换。

希望对您有所帮助。