2016-06-14 72 views
2

我試圖以可讀格式(XML)保存類。 問題是,生成的文件只能輸出爲:XML序列化結果空的XML

<?xml version="1.0" encoding="Windows-1252"?> 
<ExtremeLearningMachine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0" encoding="Windows-1252"?> 
<ExtremeLearningMachine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0" encoding="Windows-1252"?> 
<ExtremeLearningMachine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0" encoding="Windows-1252"?> 
<ExtremeLearningMachine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /> 

這裏是我的類:

public class ExtremeLearningMachine { 
    public ExtremeLearningMachine() 
    { 

    } 
    int input, hidden; //only have 1 output neuron 
    double[,] W1, W2; 
    public ExtremeLearningMachine(int inputNeuron, int hiddenNeuron) { input = inputNeuron; hidden = hiddenNeuron; } 
    public void train(int dataCount, double[,] trainingSet) { 
     //set matrix 
     double[,] trainInput = new double[input, dataCount], desireOutput = new double[1, dataCount]; 
     for (int i = 0; i < dataCount; i++) { 
      for (int j = 0; j < input; j++) trainInput[j, i] = trainingSet[i, j]; 
      desireOutput[0, i] = trainingSet[i, input]; 
     } 
     //W1 
     W1 = new double[hidden, input]; 
     for (int i = 0; i < hidden; i++) { for (int j = 0; j < input; j++)W1[i, j] = Random.value; } 
     //hidden 
     //double[,] H = new double[hidden, dataCount]; 
     double[,] H = Matrix.Multiply(W1, trainInput); 
     //activation function(binary sigmoid) 
     for (int i = 0; i < hidden; i++) { 
      for (int j = 0; j < dataCount; j++) H[i, j] = 1f/(1f + Mathf.Exp((float)-H[i, j])); 
     } 
     //W2 
     W2 = Matrix.Multiply(desireOutput, H.PseudoInverse()); 
    } 
    public double test(double[,] set) {//only [~,1] allowed 
     double[,] H = Matrix.Multiply(W1, set.Transpose()); 
     //activation function(binary sigmoid) 
     for (int i = 0; i < hidden; i++) H[i, 0] = 1f/(1f + Mathf.Exp((float)-H[i, 0])); 
     H = Matrix.Multiply(W2, H); 
     return H[0, 0]; 
    } 
} 

這裏是我的節省代碼:

void save() 
{ 
    System.Xml.Serialization.XmlSerializer writer = 
     new System.Xml.Serialization.XmlSerializer(typeof(ExtremeLearningMachine)); 

    string path = Directory.GetCurrentDirectory() + "\\ElmTrain.xml"; 
    System.IO.FileStream file = System.IO.File.Create(path); 
    for(int i=0;i<elm.Length;i++) 
    writer.Serialize(file, elm[i]); 
    file.Close(); 
} 

而且我的加載代碼,在遇到任何問題(我還沒有測試過,因爲我無法保存):

void load() 
{ 
    System.Xml.Serialization.XmlSerializer reader = 
    new System.Xml.Serialization.XmlSerializer(typeof(ExtremeLearningMachine)); 
    System.IO.StreamReader file = new System.IO.StreamReader("//ElmTrain.xml"); 
    elm = (ExtremeLearningMachine[])reader.Deserialize(file); 
    file.Close(); 
} 

我也開到任何其他的想法來保存這個類在其他可讀的格式,如果它的建議

非常感謝您

回答

0

首先,ExtremeLearningMachine沒有任何公共成員序列化,所以是:期待它是空的; XmlSerializer僅序列化公共字段和屬性。例如,嘗試添加公共屬性以補充您的私人字段。

其次:不要將多個片段序列化到同一個文檔。相反,請創建一個容器,並對其進行序列化。坦率地說,你可以只使用ExtremeLearningMachine[]作爲容器,因爲你已經有:

var writer = new XmlSerializer(typeof(ExtremeLearningMachine[])); 
string path = Path.Combine(Directory.GetCurrentDirectory(), "ElmTrain.xml"); 
using(var file = File.Create(path)) { 
    writer.Serialize(file, elm); 
} 

和:

var reader = new XmlSerializer(typeof(ExtremeLearningMachine[])); 
using(var file = File.OpenRead(path)) { 
    elm = (ExtremeLearningMachine[])reader.Deserialize(file); 
} 
+0

我用你試過代碼(我也已經公開了這些變量),但現在它導致空文件和異常:Array不是一維數組。 – christantoan

+0

@christantoan right;所以這個異常告訴你,如果XmlSerializer是一維的,那麼它只是快樂的序列化數組。你有一個二維數組。所以......它告訴你問題是什麼。選項:1:壓平數據(只要存儲尺寸,所有數組都可以進行矢量化); 2:使用不同的序列化程序 –

+0

注意:我強烈建議爲序列化創建單獨的DTO模型;你的域模型可能有一個二維數組,而DTO模型可以有一維數組,一個高度和一個寬度(儘管技術上至少有一個是多餘的,因爲1D數組包含一個長度) –

0

雖然轉換嘗試下面的代碼:

  XmlSerializer serializer = new XmlSerializer(typeof(ExtremeLearningMachine)); 
      MemoryStream memStream = new MemoryStream(); 
      serializer.Serialize(memStream, elm); 
      FileStream file = new FileStream(folderName + "\\ElmTrain.xml", FileMode.Create, FileAccess.ReadWrite);//Provide correct path as foldername 
      memStream.WriteTo(file); 
      file.Close();