/// <summary> /// Loads weight matrices of a matrix using hte passed in record URL /// </summary> /// <param name="RecordName"></param> /// <param name="InputDimensions"></param> /// <param name="HiddenLayerDimensions"></param> /// <param name="OutputDimensions"></param> private void LoadNNState(string RecordName, int InputDimensions, int HiddenLayerDimensions, int OutputDimensions) { Heuristics.Utilities.Matrices.Matrix W = new Heuristics.Utilities.Matrices.Matrix(HiddenLayerDimensions, OutputDimensions); Heuristics.Utilities.Matrices.Matrix V = new Heuristics.Utilities.Matrices.Matrix(InputDimensions, HiddenLayerDimensions); Heuristics.Utilities.Matrices.Matrix U = new Heuristics.Utilities.Matrices.Matrix(HiddenLayerDimensions, HiddenLayerDimensions); string Content = File.ReadAllText(SavedStatesDirectory + RecordName); try { foreach (string line in Content.Split('\n')) { List <string> chars = line.Split('_').ToList(); try { string Matrix = chars[0]; int i = Convert.ToInt32(chars[1]); int j = Convert.ToInt32(chars[2]); double Value = Convert.ToDouble(chars[3]); switch (Matrix) { case "W": W.SetValue(i, j, Value); break; case "U": U.SetValue(i, j, Value); break; case "V": V.SetValue(i, j, Value); break; default: throw new Exception(); } } catch { } } } catch (Exception e) { MessageBox.Show("There was an error loading the file!\n" + e.Message); } rnn = new RecurrentNeuralNetwork(InputDimensions, HiddenLayerDimensions, OutputDimensions, U, V, W); }
/// <summary> /// Trains the RNN /// </summary> private void Train() { if (IORecords == null || !IORecords.Any()) { return; } if (rnn == null) { rnn = new RecurrentNeuralNetwork(NumberOfRecordsToKeep, 100, 5); } IORecords = SplitIORecords(IORecords, PercentTrainingData * .01); int AmountPerBatch = (int)(IORecords.Count * (PercentTrainingData * .01) / NumberOfBatches);// / NumberOfBatches); int CurrentCounter = 0; int TotalTrainingData = NumberOfBatches * AmountPerBatch; while (CurrentCounter < TotalTrainingData) { List <Heuristics.Utilities.Matrices.Matrix> Inputs = new List <Heuristics.Utilities.Matrices.Matrix>(); List <Heuristics.Utilities.Matrices.Matrix> Outputs = new List <Heuristics.Utilities.Matrices.Matrix>(); for (int sample = 0; sample < AmountPerBatch; sample++) { SentenceIORecord temp = IORecords[sample + CurrentCounter]; Heuristics.Utilities.Matrices.Matrix Input = new Heuristics.Utilities.Matrices.Matrix(temp.Inputs.Count, NumberOfRecordsToKeep); Heuristics.Utilities.Matrices.Matrix Output = new Heuristics.Utilities.Matrices.Matrix(temp.Inputs.Count, 5); for (int i = 0; i < Input.Height; i++) { int Value = temp.Inputs[i]; for (int j = 0; j < Input.Width; j++) { if (j == Value) { Input.SetValue(i, j, 1); } else { Input.SetValue(i, j, 0); } } Value = temp.Output; for (int j = 0; j < Output.Width; j++) { if (j == Value) { Output.SetValue(i, j, 1); } else { Output.SetValue(i, j, 0); } } } Inputs.Add(Input); Outputs.Add(Output); } rnn.Train(Inputs, Outputs, .001, NumberOfEpochs, 0); CurrentCounter += AmountPerBatch; } SaveNNStates(); }