您好我目前有一个数组,其中包含文本文件的所有值,并且当前运行时会回读该文件,但我希望在运行时为数组中的每个值生成输出。现在,我已经在程序运行结束时生成了一个输出,所有值都已经被读回但我想在程序打印回来的每个值旁边生成一个输出列表。输出已使用训练算法创建。这是我的代码:
这是我定义数组和其他变量的地方:
`double[] x = new double [3501];`
double output = 1;
double netSum;
double input = 0;
double[] x_P = new double[3501];
double error = input - output;
Random r = new Random();
double[] weights = { r.NextDouble(), r.NextDouble(), r.NextDouble() };
double learningrate = 0.1;
double delta; enter code here
这是将文本文件放入数组的地方:
try
{
using (StreamReader sr = new StreamReader("test.txt"))
{
for (int y = 0; y < 3501; y++)
{
String line = sr.ReadLine();
x[y] = Double.Parse(line);
Console.WriteLine(line);
}
}
这是我用来创建输出的算法:
while (error != 0.01)
{
error = 1;
foreach (double x_value in x)
{
netSum = 0.01;
double i = 1;
for (i = 1; i < 3501; i++)
{
netSum = x[1] * weights[1] + x[2] * weights[2];
}
x_P[2] = Sigmoid(netSum);
output = x_P[2];
if (output >= error)
{
for (i = 1; i < 3501; i++)
{
delta = x_value - output;
error = x[2] - x_P[2];
weights[(int)i] += learningrate * delta * x_value;
error += Math.Abs(error);
}
}
}
Console.WriteLine(output);
Console.ReadLine();
}
}
catch (Exception e)
{
// Log the exception and quit...
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
public static double Sigmoid(double x)
{
return 1 / (1 + Math.Exp(-x));
}
public double Derivative(double x)
{
double s = Sigmoid(x);
return s * (1 - s);
}
所以我基本上想要在打印数值时为x中的每个值打印输出会有人能帮助我吗?
答案 0 :(得分:0)
您可以将Console.WriteLine
放在foreach
循环中:
foreach (double x_value in x)
{
...
Console.WriteLine(output);
}
Console.ReadLine();