保存encog网络c#

时间:2014-02-21 17:49:53

标签: c# save neural-network encog

我有一个关于如何在encog库c#中保存神经网络的问题。我想从输入和输出层序列化隐藏层和数据的权重。如果我想成功反序列化,还有必要保存网络的某个结构。 在我创建网络和序列化BasicNetwork对象的代码部分下面,当然它是不正确的。我已经找到了很多关于如何使用java版本的信息,但注意到了c#。

                BasicNetwork network = CreateNet(nettype,res11[i],1,2);
                INeuralDataSet trainingSet = new BasicNeuralDataSet(masStudyInput, masStudyOutput);
                INeuralDataSet TestingSet = new BasicNeuralDataSet(masTestInput, mastestOutput);
                ITrain train = new ResilientPropagation(network, trainingSet);

                int epoch = 1;
                //network.Structure.Layers.
                MessageBox.Show("Start");

                do
                {

                    train.Iteration();

                    mist = GetMistake(ref network, ref TestingSet);
                    chart1.Invoke((Action)(() =>
                    {
                        chart1.Series[0].Points.AddY(train.Error);
                        chart1.Series[1].Points.AddY(mist);
                    }));
                    network.
                    if (mist < 0.8)
                   {
                    string XMLfilename = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\" + mist + ".xml";
                    System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(BasicNetwork));
                    TextWriter writerr = new StreamWriter(XMLfilename);
                    writer.Serialize(writerr, network);
                    writerr.Close();
                    }
                    epoch++;

                }
                while ((epoch < 1000));SS

2 个答案:

答案 0 :(得分:8)

Encog还有saver / loader:

EncogDirectoryPersistence.SaveObject(new System.IO.FileInfo(txtSaveFile.Text), _network);

_network = (BasicNetwork)EncogDirectoryPersistence.LoadObject(new FileInfo(txtSaveFile.Text));

答案 1 :(得分:2)

网络是可序列化的,因此您可以使用任何所需的序列化程序。 一个例子:

var serializer = new BinaryFormatter();
using (var ms = new MemoryStream())
{
    serializer.Serialize(ms, network);
    ms.Position = 0;
    byte[] serialized = ms.ToArray();
    return serialized;
}