0

我在返回一个列表时遇到问题,这样我就可以将它保存在一个文件中,然后加载它,以便保存权重并再次检索。抱歉这个愚蠢的问题,但我如何从 SaveNetwork 方法调用和保存权重列表,我无法真正掌握我能做些什么来解决这个问题。我知道我还没有创建列表权重的新实例,但是如果我这样做,我将丢失存储在此列表中的当前权重。

public class Neuron
{
    private double bias;                       // Bias value.
    private double error;                      // Sum of error.
    private double input;                      // Sum of inputs.
    private double gradient = 5;               // Steepness of sigmoid curve.
    private double learnRate = 0.01;           // Learning rate.
    private double output = double.MinValue;   // Preset value of neuron.

    public List<Weight> weights;              // Collection of weights to inputs.      

    public Neuron() { }

    public Neuron(Layer inputs, Random rnd)
    {
       weights = new List<Weight>();

        foreach (Neuron input in inputs)
        {
            Weight w = new Weight();
            w.Input = input;
            w.Value = rnd.NextDouble() * 2 - 1;
            weights.Add(w);                    
        }          
    }

public static void SaveNetwork(string path)
    {

        FileStream FS = new FileStream(path, FileMode.Create);
        BinaryFormatter BF = new BinaryFormatter();

        BF.Serialize(FS,/* The List in this case is List weights ***/        );
        FS.Close();
    }

    public void LoadNetwork(string path)
    {
        FileStream FS = new FileStream(path, FileMode.Open);
        BinaryFormatter BF = new BinaryFormatter();
        weights = (List<Weight>)BF.Deserialize(FS);
        FS.Close();
    }

对此进行更新——我使用了与下面的代码类似的层次结构,该代码取自 Dynamic Notions 博客,该博客解释了如何创建神经网络。我想要实现的是,在神经网络学会之后,我希望能够保存列表权重,以便在程序停止时我能够加载权重以跳过网络的训练。所以基本上从类网络我想访问这个在神经类中的列表,而不用新方法创建一个新实例,否则我只会得到一个空列表。希望它更清楚,因为我不知道如何更好地解释它......非常感谢

public class Network{

//some variables.. 

    [STAThread]
    static void Main()
    {
        new Network();
    } 
    public Network()
    {
        LoadPatterns();
        Initialise();
        Train();
        Test();
    } 

    private void Train()
    {
       double error;
       do
       {
       error = 0;
       foreach (Pattern pattern in _patterns)
        {
            double delta = pattern.Output - Activate(pattern);
            AdjustWeights(delta);
            error += Math.Pow(delta, 2);
        }

        Console.WriteLine("Iteration {0}\tError {1:0.000}", _iteration, error);
        _iteration++;
        if (_iteration > _restartAfter) Initialise();

    } while (error > 0.1);
} 

private void Test()
{
} 

 // Must be able to call and save the List<Weight> From here

private double Activate(Pattern pattern)
{
} 

 private void AdjustWeights(double delta)
 {
    _output.AdjustWeights(delta);

    foreach (Neuron neuron in _hidden)
    {
        neuron.AdjustWeights(_output.ErrorFeedback(neuron));
    }
 } 

 private void Initialise()
 {
    _inputs = new Layer(_inputDims);
    _hidden = new Layer(_hiddenDims, _inputs, _rnd);
    _output = new Neuron(_hidden, _rnd);
    _iteration = 0;
    Console.WriteLine("Network Initialised");
} 

private void LoadPatterns()
{
}

} 

public class Layer : List<Neuron>
{

public Layer(int size)
{
    for (int i = 0; i < size; i++)
        base.Add(new Neuron());
}

 public Layer(int size, Layer layer, Random rnd)
    {
        for (int i = 0; i < size; i++)
        base.Add(new Neuron(layer, rnd)); //this is where Neuron class is instantiated
   }

 }

public class Neuron
 {

    //some other vars 
    private List<Weight> _weights;              // This is the list in question. 

    public Neuron() { } 

    public Neuron(Layer inputs, Random rnd)
    {
      _weights = new List<Weight>();
      foreach (Neuron input in inputs)
        {
        Weight w = new Weight();
        w.Input = input;
        w.Value = rnd.NextDouble() * 2 - 1;
        _weights.Add(w);
        }
    }


}
public class Weight
{
 public Neuron Input;
 public double Value;
}
4

1 回答 1

0

从您有评论的地方删除static关键字SaveNetwork()并使用。weights

于 2012-12-08T17:41:39.097 回答