Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save encog network c#

I have an issue about how to save neural netwok in encog library c#. I want to serialize weights of hidden layer and data from input and output layers. Also it is necessary to save somewhere structure of the network, if i want deserialize it successfully. Below the part of code where i create network and serialize the BasicNetwork object, of course it is incorrect. I have found a lot of information how to do it with java version, but noting about 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
like image 510
user3338218 Avatar asked Dec 25 '22 13:12

user3338218


2 Answers

Encog also has saver/loader:

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

_network = (BasicNetwork)EncogDirectoryPersistence.LoadObject(new FileInfo(txtSaveFile.Text));
like image 150
Yuriy Zaletskyy Avatar answered Jan 07 '23 17:01

Yuriy Zaletskyy


Network is serializable, so you can use any serializer you want. An example:

var serializer = new BinaryFormatter();
using (var ms = new MemoryStream())
{
    serializer.Serialize(ms, network);
    ms.Position = 0;
    byte[] serialized = ms.ToArray();
    return serialized;
}
like image 25
Egor Antonov Avatar answered Jan 07 '23 19:01

Egor Antonov