2014-02-21 77 views
3

我有一個關於如何在encog庫c#中保存神經網絡的問題。我想要序列化來自輸入和輸出層的隱藏層和數據的權重。如果我想要成功地反序列化它,也需要保存網絡的某個地方結構。 在創建網絡和序列化BasicNetwork對象的代碼部分下面,當然這是不正確的。我發現了很多關於如何使用java版本的信息,但注意到了c#。保存encog網絡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

網絡是可序列化的,所以你可以使用你想要的任何序列化器。 一個例子:

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

Encog還具有保護程序/裝載機:

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

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