我试图以可读格式(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();
}
我也开到任何其他的想法来保存这个类在其他可读的格式,如果它的建议
非常感谢您
我用你试过代码(我也已经公开了这些变量),但现在它导致空文件和异常:Array不是一维数组。 – christantoan
@christantoan right;所以这个异常告诉你,如果XmlSerializer是一维的,那么它只是快乐的序列化数组。你有一个二维数组。所以......它告诉你问题是什么。选项:1:压平数据(只要存储尺寸,所有数组都可以进行矢量化); 2:使用不同的序列化程序 –
注意:我强烈建议为序列化创建单独的DTO模型;你的域模型可能有一个二维数组,而DTO模型可以有一维数组,一个高度和一个宽度(尽管技术上至少有一个是多余的,因为1D数组包含一个长度) –