简体   繁体   English

用 C# 实现神经网络

[英]Implementing a neural network in C#

I'm following the tutorial at this link: http://www.c-sharpcorner.com/UploadFile/rmcochran/AI_OOP_NeuralNet06192006090112AM/AI_OOP_NeuralNet.aspx我正在关注此链接上的教程: http : //www.c-sharpcorner.com/UploadFile/rmcochran/AI_OOP_NeuralNet06192006090112AM/AI_OOP_NeuralNet.aspx

I'm new to neural networking and I'm trying to edit the example in the above tutorial to match my problem.我是神经网络的新手,我正在尝试编辑上述教程中的示例以匹配我的问题。 I'm using multiple regression to find coefficients for 3 different sets of data and I then calculate the rsquared value for each set of data.我正在使用多元回归来查找 3 组不同数据的系数,然后计算每组数据的 rsquared 值。 I'm trying to create a neural network that will change the coefficient value to get the rsquared value as close to 100 as possible.我正在尝试创建一个神经网络,该网络将更改系数值以使 rsquared 值尽可能接近 100。

This is how I establish the coefficient and find the rsquared value for that coefficient.这就是我建立系数并找到该系数的 rsquared 值的方法。 All 3 coefficients use these same methods:所有 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;

This is the example that performs the training and I'm currently stuck on how to change this example to match what I'm trying to do.这是执行培训的示例,我目前一直在思考如何更改此示例以匹配我正在尝试执行的操作。

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");
}

How do I use this information to create a neural network to find the coefficient that will in turn have a rsquared value as close to 100 as possible?我如何使用这些信息来创建一个神经网络来找到一个 r 平方值尽可能接近 100 的系数?

Instead of building one, you can use Neuroph framework built in the .NET by using the Neuroph.NET from here https://github.com/starhash/Neuroph.NET/releases/tag/v1.0-beta 您可以从此处https://github.com/starhash/Neuroph.NET/releases/tag/v1.0-beta使用Neuroph.NET来使用.NET中内置的Neuroph框架,而不是构建一个框架。

It is a light conversion of the original Neuroph they did for the JAVA platform. 这是他们为JAVA平台所做的原始Neuroph的轻型转换。

Hope this helps you. 希望这对您有所帮助。

You can check out some github implementations online.您可以在线查看一些 github 实现。 I have a simple one that uses sigmoid to classify data, if that's what anyone else wanted.我有一个简单的使用 sigmoid 对数据进行分类的方法,如果这是其他人想要的。 Neural Network Implementation神经网络实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#神经网络优化 - C# Neural Network Optimization C#中的神经网络-NaN和无限 - Neural Network in C# - NaNs and Infinity 用于识别C#中手写数字的神经网络 - Neural Network for Recognition of Handwritten Digits in C# 如何以C#形式可视化神经网络算法的节点(代理) - How to visualize nodes(agents) of Neural Network algorithms in a form in c# 将表示神经网络的锯齿状数组转换为表示其神经通路的二维数组 C# - Converting a Jagged Array that represents a Neural Network to a 2D Array the represents its Neural Pathways C# C#Encog神经网络 - 尽管神经网络的整体误差很小,但预期输出与实际误差相差甚远 - C# Encog Neural Network - Expected output is very far off actual error despite low overall error of neural network 使用C#和CNTK,如何创建前馈神经网络? - Using C# and CNTK, how do you create a feed forward neural network? 如何在 c# Z2D50972FECD376129545507F1062089Z 内核中运行 python 神经网络 keras 脚本 - How to run python neural network keras script in c# .net core app Matlab R2017a是否为c#(。net Assemly)部署了神经网络 - does the matlab R2017a deploy neural network for c#(.net Assemly) c# Encog 框架,神经网络,为什么在训练网络时会出现内部错误? - c# Encog Framework, neural network, Why do I get an internal error when I train my network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM