public void Update() { IEnumerable <CoefficientDisplayModel> coefficientDisplayModels = facade.GetAll(); Coefficients.Clear(); foreach (CoefficientDisplayModel coefficientDisplayModel in coefficientDisplayModels) { Coefficients.Add(coefficientDisplayModel); } }
public void Solve() { var epochsCount = 1900; var learningRate = Entities.Average(e => Math.Abs(e.TargetValue)) / 3000; var batchSize = Math.Min(20, EntitiesCount); //Coefficients.AddRange(new double[FeaturesCount + 1]); var random = new Random(); for (int i = 0; i < FeaturesCount + 1; i++) { Coefficients.Add(random.NextDouble()); } for (int epochNumber = 1; epochNumber < epochsCount + 1; epochNumber++) { var batch = Entities.OrderBy(e => random.NextDouble()).Take(batchSize).ToList(); //var sumError = 0d; foreach (var entity in batch) { var predictedValue = Predict(entity.NormalizedFeatures); var error = predictedValue - entity.TargetValue; //sumError += Math.Pow(error, 2); for (int i = 0; i < FeaturesCount; i++) { Coefficients[i] -= learningRate * error * entity.NormalizedFeatures[i]; } Coefficients[FeaturesCount] -= learningRate * error; } //Console.WriteLine($"Epoch: {epochNumber}\tError = {sumError}"); //foreach (var coefficient in Coefficients) //{ // Console.Write($"{coefficient} "); //} //Console.WriteLine(); //Console.WriteLine(); } Denormalize(); }
public void ConvertByteArrayToCoefficients(BitArray array) { Coefficients.Clear(); for (int i = 0; i < _degree + 1; i++) { byte[] coefficientArr = array.ToByteArray().Skip(i * sizeof(double)).Take(sizeof(double)).ToArray(); double value = BitConverter.ToDouble(array.ToByteArray(), i * sizeof(double)); while (double.IsNaN(value)) { // Console.WriteLine(string.Concat(array.Cast<bool>().Select(Convert.ToByte))); //Console.WriteLine(coefficientArr.ToBinaryString()); array.Set((i + 1) * sizeof(double) * 8 - _randomProvider.Next(17) - 1, false); coefficientArr = array.ToByteArray().Skip(i * sizeof(double)).Take(sizeof(double)).ToArray(); value = BitConverter.ToDouble(array.ToByteArray(), i * sizeof(double)); } Coefficients.Add(value); } }