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
Encog also has saver/loader:
EncogDirectoryPersistence.SaveObject(new System.IO.FileInfo(txtSaveFile.Text), _network);
_network = (BasicNetwork)EncogDirectoryPersistence.LoadObject(new FileInfo(txtSaveFile.Text));
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With