public double[] Predict(double[] values) { if (values.Length != InputLayer.Neurons.Length) { throw new ArgumentException("Number of input values must match number of input neurons.", nameof(values)); } InputLayer.InsertValues(values); HiddenLayer.Compute(); OutputLayer.Compute(); return(OutputLayer.GetValues()); }
public void Train(IReadOnlyList <TrainingDataSet> trainingDataSets, double learningRate, double learningDataPercentage) { if (learningDataPercentage > 1 || learningDataPercentage < 0) { throw new ArgumentException("Percentage of training data must be a value between 0 and 1.", nameof(learningDataPercentage)); } foreach (var dataSet in trainingDataSets) { if (dataSet.Inputs.Length != InputLayer.Neurons.Length) { throw new ArgumentException($"Data set's number of input values ({dataSet.Inputs.Length}) does not match size of input layer ({InputLayer.Neurons.Length})", nameof(trainingDataSets)); } if (dataSet.Outputs.Length != OutputLayer.Neurons.Length) { throw new ArgumentException($"Data set's number of output values ({dataSet.Outputs.Length}) does not match size of output layer ({OutputLayer.Neurons.Length})", nameof(trainingDataSets)); } } var learningDataCount = (int)Math.Floor(trainingDataSets.Count * learningDataPercentage); for (int i = 0; i < learningDataCount; i++) { InputLayer.InsertValues(trainingDataSets[i].Inputs); HiddenLayer.Compute(); OutputLayer.Compute(); OutputLayer.ComputeDelta(trainingDataSets[i].Outputs); HiddenLayer.ComputeDelta(); InputLayer.ComputeDelta(); HiddenLayer.UpdateWeights(learningRate); OutputLayer.UpdateWeights(learningRate); } var total = 0d; for (int i = learningDataCount; i < trainingDataSets.Count; i++) { var outcome = Predict(trainingDataSets[i].Inputs); for (int j = 0; j < outcome.Length; j++) { total += Math.Abs(trainingDataSets[i].Outputs[j] + outcome[j]) / 2; } } Accuracy = total / ((trainingDataSets.Count - learningDataCount) * OutputLayer.Neurons.Length); }
public Vector <float> Compute(StatePair input, bool training) { // Forward propagate. var img = InputLayer.Compute(input.Spatial); for (int i = 0; i < ConvolutionalLayers.Length; i++) { img = SubSampleLayers[i].Compute(ConvolutionalLayers[i].Compute(img)); } _vecp.left = FlattenLayer.Compute(img); _vecp.right = LinearHiddenLayer.Compute(VectorInput.Compute(input.Linear)); IsOutputFromTraining = false; var res = OutputLayer.Compute(CombinationLayer.Compute(_vecp)); if (ValuesComputed != null) { ValuesComputed(res, training); } return(res); }