我使用此方法,需要返回weights8spr
,但我不知道如何。
有谁能够帮助我?最简单的return(weights8spr)
不起作用,因为在输入Im中不提供双精度数组。
public class Run {
public Run(List<dataVM2> TrainDataForStations)
{
double[] resultMAX1 = new double[] { 40.0, 1200.0, 100.0, 100.0, 10000.0 };
double[] resultMIN1 = new double[] { -50.0, 0.0, 0.0, 0.0, 0.0 };
double d1 = 0.0;
double d2 = 1.0;
int numItemsspr = TrainDataForStations.Count;
double[][] trainData = new double[numItemsspr][];
Random rnd = new Random(1);
double[][] MassiveDataspr8 = new double[numItemsspr][];
for (var i = 0; i < numItemsspr; ++i)
{
trainData[i] = new double[] { TrainDataForStations[i].TemperatureC1, TrainDataForStations[i].SolarRadiation1, TrainDataForStations[i].Wetness1, TrainDataForStations[i].WindSpeed1, TrainDataForStations[i].gen1 };
}
int maxcol = 0;
for (int i = 0; i < trainData.Length; i++)
{
if (trainData[i].Length > maxcol)
maxcol = trainData[i].Length;
}
//data normalization
for (int j = 0; j < MassiveDataspr8.Length; j++)
{
MassiveDataspr8[j] = new double[maxcol];
for (int i = 0; i < maxcol; i++)
{
MassiveDataspr8[j][i] = (((trainData[j][i] - resultMIN1[i]) * (d2 - d1)) / (resultMAX1[i] - resultMIN1[i])) + d1;
}
}
int NumInput = 4;
int NumHidden = 25;
int NumOutput = 1;
int rndSeed = 0;
NeuralNetworkData neuralform = new NeuralNetworkData(NumInput, NumHidden, NumOutput, rnd);
int maxEpochs = 1000;
double learnRate = 0.005;
double momentum = 0.001;
double[] weights8spr = new NeuralNetworkTrainer(neuralform, rnd).Train(MassiveDataspr8, maxEpochs, learnRate, momentum);
}
}
答案 0 :(得分:0)
方法必须具有返回类型。如果未返回值,则该类型将为伪类型void
,否则为真实类型。
public class StationsTrainer {
public double[] Run(List<dataVM2> TrainDataForStations)
{
...
double[] weights8spr = new NeuralNetworkTrainer(neuralform, rnd)
.Train(MassiveDataspr8, maxEpochs, learnRate, momentum);
return weights8spr;
}
}
并为班级选择一个更好的名称。例如。 StationsTrainer
。
答案 1 :(得分:0)
您正在类的构造函数中执行该代码,并且无法更改构造函数的返回类型。
您应该在类中创建一个方法,然后稍微重构代码。
public class Run
{
// default constructor not needed
public Run()
{
}
public double[] RunMethod(List<dataVM2> TrainDataForStations)
{
// put your code here
// ...
// return the double[]
return weights8spr;
}
}
执行如下:
var run = new Run();
var weights = run.RunMethod(listOfTrainDataForStations);