2012-12-08 33 views
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(); 
    } 

更新这个 - 我使用了类似的层次结构如下这是从动态观念博客中介绍如何创建一个神经网络所采取的代码。我想要实现的是,之后神经网络已经学会了我希望能够保存列表权重,这样我就能够加载权重,如果程序停止以跳过网络训练。所以基本上从类网络我想访问这个列表,这是在神经类没有创建一个新的方法中的新实例,否则我只会得到一个空的列表。希望它更清楚怎么我不知道该如何解释好......非常感谢

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; 
} 
+0

您是否尝试过在此处粘贴代码时格式化代码?同样,看到你的代码的一小部分,你有问题,而不是你的项目的所有来源,这将是很好的。 –

+1

对不起! – Etienne

回答

0

SaveNetwork()取出static关键字,并使用weights,你有你的评论。

+0

是的,我知道,但我没有写我不好意思,这是因为这个方法是从另一个类中调用,而我没有在后者中实例化。更好的解释是,我有一个类Form1,然后我调用另一个类网络,并从网络类我叫这个神经元类(列表所在位置)这就是为什么我做了这种方法的公共静态。如果我不宣布它为无效,那么我不能直接从Form1类调用SaveNetwork方法。如果我在form1类中实例化神经元类,那么我将丢失权重列表中的数据:/ – Etienne

+0

@Etienne您是否了解static关键字的作用?它声明你的方法不属于或作用于你的Neuron类的特定实例,而是一种类方法。您将无法访问您的权重属性,因为它是一个实例属性。如果您想要保存表单中的权重,则需要通过网络类中的另一种方法来完成此操作,或者退出表单类以引用Neuron类。 – rro

+0

@raymond是的,你有一个点...你能给我提供一个例子如何从网络类coz即时从字面上没有这个想法.. – Etienne

相关问题