如何在Unity中保存List <double [] []>以便于检索

时间:2019-03-01 17:37:49

标签: c# list unity3d matrix

我正在尝试将神经网络中使用的权重列表(double [] []列表)保存到文件中。但是我不知道如何保存它,因为当前,我的输出是:“ double [] [] double [] []”

如何将矩阵列表保存到文件中?

这是我的代码:

public void Save() {
    List<double[][]> weights = NN[neuron].GetWeights();

    Text(weights.ToString());
}


void Text(string input) {
    string path = Application.dataPath + "/text.text";

    File.WriteAllText(path, input);
}

3 个答案:

答案 0 :(得分:3)

您假设在ToString上调用List<double[][]>将列表的 contents 输出为字符串,并且还可能假设结果字符串为容易地将其解析为List<double[][]>。这两个假设都不正确。

  

如何将矩阵列表保存到文件中?

有可用的序列化库(JSON,XML,二进制等),它们可以相对容易地对这些结构进行序列化和反序列化,或者您可以编写代码以遍历列表并编写2-某种有组织格式的D数组,但是ToString的默认实现无法为您完成。

答案 1 :(得分:1)

好吧,这比我计划的要长一点。但是至少是本地的(并经过测试)...;-)

保存方法

void SaveToFile(string fileName, List<double[][]> weightsList)
{
  using (var w = new StreamWriter(fileName))
  {
    w.WriteLine(weightsList.Count);
    foreach (var weight2D in weightsList)
    {
      w.WriteLine(weight2D.Length);
      foreach (var weight1D in weight2D)
      {
        w.WriteLine(weight1D.Length);
        foreach (var val in weight1D)
        {
          w.WriteLine(val.ToString(CultureInfo.InvariantCulture));
        }
      }
    }
  }
}

读取方法

List<double[][]> ReadFromFile(string fileName)
{
  var output = new List<double[][]>();
  using (var r = new StreamReader(fileName))
  {
    int outputCount = int.Parse(r.ReadLine());
    for (int c = 0; c < outputCount; c++)
    {
      var weight2D = new double[int.Parse(r.ReadLine())][];
      for (int d = 0; d < weight2D.Length; d++)
      {
        weight2D[d] = new double[int.Parse(r.ReadLine())];
        for (int i = 0; i < weight2D[d].Length; i++)
        {
          weight2D[d][i] = double.Parse(r.ReadLine(), CultureInfo.InvariantCulture);
        }
      }
      output.Add(weight2D);
    }
  }
  return output;
}

使用示例:

public void Save()
{
  List<double[][]> weights = Cars[currentChild].GetWeights();

  SaveToFile(Application.dataPath + "/weights.txt", weights);
}

public void Load()
{
  List<double[][]> weights = ReadFromFile(Application.dataPath + "/weights.txt");

  // todo: set weights etc...
}

答案 2 :(得分:0)

我将制定一种特定于打印列表的方法。

public void Save() {
    List<double[][]> weights = Cars[currentChild].GetWeights();
    CreateTextSpecial(weights);
}


void CreateText(string input) {
    string path = Application.dataPath + "/weights.text";
    File.WriteAllText(path, input);
}

void CreateTextSpecial(List<double[][]> inputList){
    string textToAppend = "";
    for(int j=0;j<inputList.Count;j++){
        textToAppend += "[";
        for(int k=0;k<inputList[j].length;k++){
            textToAppend +="[";
            for(int l=0;l<inputList[j][k].length){
                textToAppend += inputList[j][k][l]+", "; 
            }
            textToAppend +="]\n";
        }
        textToAppend+="]\n";
    }

    string path = Application.dataPath+"/weights.text";
    File.WriteAllText(path, textToAppend);
}

基本上,它循环遍历提供的列表中的所有矩阵(?),并以以下格式将其打印出来:

[[0.01,0.0214,0.055,]

[0.11,0.022,0.321,0.55,0.123,]]

[[1.205,2.210,]

[0.34124,25128.250,222,2.5293,]]