static void Main() { ILogger logger = new ConsoleLogger(); IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsetting.json") .Build(); // Get topology. int[] topology = configuration.GetSection("topology").Get <int[]>(); // Read data. TrainingData[] data; string trainingDataFileName = Directory.GetCurrentDirectory() + "\\trainingData.json"; if (File.Exists(trainingDataFileName)) { data = TrainingData.ReadFromFile(trainingDataFileName); } else { data = TrainingData.GenerateData(topology.First(), topology.Last(), 2000); TrainingData.WriteToFile(data, trainingDataFileName); } // Train network. Net net = new Net(topology); for (int i = 0; i < data.Length; i++) { logger.LogLine($"Pass: {i + 1}"); logger.LogLine($"Input: [{string.Join(", ", data[i].In.Select(_ => _.ToString("F3")))}]"); logger.LogLine($"Targets: [{string.Join(", ", data[i].Out.Select(_ => _.ToString("F3")))}]"); net.FeedForward(data[i].In); var resultVals = net.GetResults(); logger.LogLine($"Outputs: [{string.Join(", ", resultVals.Select(_ => _.ToString("F3")))}]"); net.BackProp(data[i].Out); logger.LogLine($"Net average error: {net.RecentAverageError.ToString("F9")}\n"); } logger.LogLine("Done"); System.Console.Read(); }
public static TrainingData[] GenerateData(int inputValsCount, int outputValsCount, int dataCount) { var random = new Random(0); var result = new TrainingData[dataCount]; for (int i = 0; i < dataCount; i++) { result[i] = new TrainingData { In = new double[inputValsCount], Out = new double[outputValsCount], }; for (int j = 0; j < inputValsCount; j++) { result[i].In[j] = random.Next(2); } for (int j = 0; j < outputValsCount; j++) { result[i].Out[j] = result[i].In.Any(_ => _ == 0) ? 1 : 0; } } return(result); }