c# Encog 框架,神经网络,为什么我在训练我的网络时会出现内部错误?
c# Encog Framework, neural network, Why do I get an internal error when I train my network?
我按照 'Use Encog c#' 书中给出的说明进行操作,其中我不得不重新设计一些代码以满足我的需要。
我正在处理图像数据集,我一次将一个 'category' 加载到网络中,训练它,保存它并在控制台程序中继续下一个 'category'。
这就是我创建神经网络的方式:
public BasicNetwork CreateNetwork(ImageMLDataSet training)
{
var network = EncogUtility.SimpleFeedForward(training.InputSize, 100, 0, training.IdealSize, true);
return network;
}
和我的训练集:
imageDict 是一个字典变量
public ImageMLDataSet CreateTraining()
{
var downsample = new RGBDownsample();
var training = new ImageMLDataSet(downsample, false, -1, 1);
foreach (var item in imageDict)
{
ImageMLData data = new ImageMLData(item.Value);
training.Add(data);
}
Console.WriteLine("Training set created");
return training;
}
之后我使用 'ImagePair' 为我的图像分配 ID(在我的例子中是字典而不是 class)
并将其处理为 ImageMLData.
调用 'EncogUtility.TrainConsole(train, network, training, minutes);' 时,此方法内发生错误。
public void TrainNetwork(BasicNetwork network, IMLDataSet training)
{
float minutes = 1;
double strategyError = 0.25;
int strategyCycles = 50;
Console.WriteLine("Training initiated...");
var train = new ResilientPropagation(network, training);
try
{
train.AddStrategy(new ResetStrategy(strategyError, strategyCycles));
EncogUtility.TrainConsole(train, network, training, minutes);
}
catch(Exception e)
{
Console.WriteLine("Error at: " + e);
}
Console.WriteLine("Training stopped");
}
当我 运行 时,我捕获到异常:
Error at: Encog.EncogError: Nested Exception --->
System.NullReferenceException: Object reference is not set to an
instance of an object at
Encog.MathUtil.Error.ErrorCalculation.UpdateError(Double[] actual,
IMLData ideal, Double significance) ved
Encog.Neural.Networks.Training.Propagation.GradientWorker.Process(IMLDataPair
pair) at
Encog.Neural.Networks.Training.Propagation.GradientWorker.Run() ---
Slut på staksporing af indre undtagelser --- at
Encog.Neural.Networks.Training.Propagation.Propagation.Iteration()
at Encog.Util.Simple.EncogUtility.TrainConsole(IMLTrain train,
BasicNetwork network, IMLDataSet trainingSet, Double seconds) at
TreeSorting.NeuralNetwork.TrainNetwork(BasicNetwork network,
IMLDataSet training) in C:\Dokumenter\Monosoft\Monosoft
Project\ConsoleApp1\NeuralNetwork.cs:line 180
提前致谢:)
对于正在寻找此类问题解决方案的其他人:
我在声明新变量时通过更改 'var' 解决了这个问题,这似乎解决了我的问题。
感谢 GunnarSigfusson 提供的修复它的答案 c:
我按照 'Use Encog c#' 书中给出的说明进行操作,其中我不得不重新设计一些代码以满足我的需要。
我正在处理图像数据集,我一次将一个 'category' 加载到网络中,训练它,保存它并在控制台程序中继续下一个 'category'。
这就是我创建神经网络的方式:
public BasicNetwork CreateNetwork(ImageMLDataSet training)
{
var network = EncogUtility.SimpleFeedForward(training.InputSize, 100, 0, training.IdealSize, true);
return network;
}
和我的训练集: imageDict 是一个字典变量
public ImageMLDataSet CreateTraining()
{
var downsample = new RGBDownsample();
var training = new ImageMLDataSet(downsample, false, -1, 1);
foreach (var item in imageDict)
{
ImageMLData data = new ImageMLData(item.Value);
training.Add(data);
}
Console.WriteLine("Training set created");
return training;
}
之后我使用 'ImagePair' 为我的图像分配 ID(在我的例子中是字典而不是 class) 并将其处理为 ImageMLData.
调用 'EncogUtility.TrainConsole(train, network, training, minutes);' 时,此方法内发生错误。
public void TrainNetwork(BasicNetwork network, IMLDataSet training)
{
float minutes = 1;
double strategyError = 0.25;
int strategyCycles = 50;
Console.WriteLine("Training initiated...");
var train = new ResilientPropagation(network, training);
try
{
train.AddStrategy(new ResetStrategy(strategyError, strategyCycles));
EncogUtility.TrainConsole(train, network, training, minutes);
}
catch(Exception e)
{
Console.WriteLine("Error at: " + e);
}
Console.WriteLine("Training stopped");
}
当我 运行 时,我捕获到异常:
Error at: Encog.EncogError: Nested Exception ---> System.NullReferenceException: Object reference is not set to an instance of an object at Encog.MathUtil.Error.ErrorCalculation.UpdateError(Double[] actual, IMLData ideal, Double significance) ved Encog.Neural.Networks.Training.Propagation.GradientWorker.Process(IMLDataPair pair) at Encog.Neural.Networks.Training.Propagation.GradientWorker.Run() --- Slut på staksporing af indre undtagelser --- at Encog.Neural.Networks.Training.Propagation.Propagation.Iteration()
at Encog.Util.Simple.EncogUtility.TrainConsole(IMLTrain train, BasicNetwork network, IMLDataSet trainingSet, Double seconds) at TreeSorting.NeuralNetwork.TrainNetwork(BasicNetwork network, IMLDataSet training) in C:\Dokumenter\Monosoft\Monosoft Project\ConsoleApp1\NeuralNetwork.cs:line 180
提前致谢:)
对于正在寻找此类问题解决方案的其他人:
我在声明新变量时通过更改 'var' 解决了这个问题,这似乎解决了我的问题。 感谢 GunnarSigfusson 提供的修复它的答案 c: