/// <summary> /// Initializes the internal neural network. /// </summary> /// <param name="state">The state.</param> private void InitializeQNetwork(State state) { if (File.Exists(NetworkPath)) { Q = new QNetwork(NetworkPath); } else { var inputLength = state.ToArray().Length; Q = new QNetwork(p => { p.TrainingError = Parameters.QNetwork.TrainingError; p.MaxIterationPerTrainging = Parameters.QNetwork.MaxIterationPerTrainging; // Input Layer p.Layers.Add(new LayerParameters(null, false, inputLength)); // Hidden Layers for (int i = 0; i < Parameters.QNetwork.HiddenLayersCount; i++) { p.Layers.Add(new LayerParameters(new ActivationTANH(), true, Parameters.QNetwork.NeuronCountForHiddenLayers)); } // Output Layer p.Layers.Add(new LayerParameters(new ActivationTANH(), false, 3)); }); Q.OnTrainingEpochComplete += (e, a) => { a.AgentId = this.Parameters.Id; OnTrainingEpochComplete?.Invoke(e, a); }; } }
/// <summary> /// Trains the neural network with the passed in training set. /// Receive a list of tuple, where each tuple represent /// Tuple = (State, ExpectedValueForBuy, ExpectedValueForSell, ExpectedValueForWait) /// </summary> /// <param name="trainingSet">The training set.</param> public void Train(IList <Tuple <State, double[]> > trainingSet) { var trainingData = new List <IMLDataPair>(); foreach (var sample in trainingSet) { var flattenState = sample.Item1.ToArray(); var actuals = new BasicMLData(flattenState); var ideals = new BasicMLData(sample.Item2); trainingData.Add(new BasicMLDataPair(actuals, ideals)); } IMLDataSet dataSet = new BasicMLDataSet(trainingData); //IMLTrain train = new Backpropagation(NeuralNetwork, dataSet, Parameters.LearningRate, Parameters.LearningMomemtum); IMLTrain train = new ResilientPropagation(NeuralNetwork, dataSet); int epoch = 1; do { train.Iteration(); epoch++; OnTrainingEpochComplete?.Invoke(this, new OnTrainingEpochCompleteArgs() { Epoch = epoch, Error = train.Error }); } while (train.Error > Parameters.TrainingError && epoch < Parameters.MaxIterationPerTrainging); //foreach (var item in dataSet) //{ // var output = NeuralNetwork.Compute(item.Input); // Console.WriteLine("output: {0} - {1} - {2} | ideal: {3} - {4} - {5}", output[0], output[1], output[2], item.Ideal[0], item.Ideal[1], item.Ideal[2]); //} }