我遇到問題返回一個列表,這樣我就可以將它保存在一個文件中,然後加載它,以便權重被保存並在另一次被檢索。對不起,這個愚蠢的問題,但我怎麼可以調用並保存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;
}
您是否嘗試過在此處粘貼代碼時格式化代碼?同樣,看到你的代碼的一小部分,你有問題,而不是你的項目的所有來源,這將是很好的。 –
對不起! – Etienne