public static void test() { AvilaDataProcessing dataProcessing = new AvilaDataProcessing(new AvilaDataConverter(new AvilaClassifier())); AvilaEvaluater evaluater = new AvilaEvaluater(new AvilaDataConverter(new AvilaClassifier())); var inputDataSet = dataProcessing.GetData(@"C:\Work\DataSets\avila\avila-tr.txt"); var processDataSet = dataProcessing.GetProcessDataCollection(10000, inputDataSet); ProcessData[] trainDataSet; ProcessData[] testDataSet; (trainDataSet, testDataSet) = dataProcessing.DivideCollection(0.7, processDataSet); trainDataSet = trainDataSet.Take(6000).ToArray(); //Model avilaFNN = new Model(new CrossEntropyLoss(), // new FullConnectLayer(new Direct(), 10, 10, true, new Normal()), // new FullConnectLayer(new Sigmoid(), 10, 12, false, new Normal()), // new FullConnectLayer(new LeakyReLU(0.1), 12, 16, false, new Normal()), // new SoftMaxLayer(16, 12, new Normal()) // ); Model avilaFNN = new Model(new CrossEntropyLoss(), "AvilaFNN", new FullConnectLayer(new Direct(1), 10, 10, true, new Normal()), new FullConnectLayer(new Sigmoid(), 10, 16, false, new Normal(0, 2)), new SoftMaxLayer(16, 12, new Normal(0, 5)) ); Trainer trainer1 = new Trainer(100, false, 0, new ExponentialDelayOptimizer(1, 0.9, 0.1)); //Trainer trainer2 = new Trainer(200, false, 0, new ExponentialDelayOptimizer(2, 0.95, 0.08)); avilaFNN = (Model)trainer1.Train(avilaFNN, trainDataSet); trainDataSet = trainDataSet.SelectPermutation().ToArray(); //Console.WriteLine(avilaFNN.layers[2].Weight[0,0]); Console.WriteLine(evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); avilaFNN = (Model)trainer1.Train(avilaFNN, trainDataSet); //Console.WriteLine(avilaFNN.layers[2].Weight[0, 0]); Console.WriteLine(evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //for (int i = 0; i <= 49; i++) //{ // trainDataSet = trainDataSet.SelectPermutation().ToList(); // Trainer trainer2 = new Trainer(60, false, 0, new ExponentialDelayOptimizer(2, 0.95, 0.05)); // avilaFNN = (Model)trainer2.Train(avilaFNN, trainDataSet); // //Console.WriteLine(avilaFNN.layers[2].Weight[0, 0]); // Console.WriteLine($"第1轮第{i}次循环:"+evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //} //for (int i = 0; i <= 49; i++) //{ // trainDataSet = trainDataSet.SelectPermutation().ToList(); // Trainer trainer2 = new Trainer(200, false, 0, new ExponentialDelayOptimizer(5, 0.9, 0.05)); // avilaFNN = (Model)trainer2.Train(avilaFNN, trainDataSet); // //Console.WriteLine(avilaFNN.layers[2].Weight[0, 0]); // Console.WriteLine($"第2轮第{i}次循环:"+ evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //} //for (int i = 0; i <= 1; i++) //{ // trainDataSet = trainDataSet.SelectPermutation().ToList(); // Trainer trainer2 = new Trainer(600, false, 0, new ExponentialDelayOptimizer(10, 0.9, 0.02)); // avilaFNN = (Model)trainer2.Train(avilaFNN, trainDataSet); // //Console.WriteLine(avilaFNN.layers[2].Weight[0, 0]); // Console.WriteLine(evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //} //for (int i = 0; i <= 99; i++) //{ // trainDataSet = trainDataSet.SelectPermutation().ToList(); // Trainer trainer2 = new Trainer(200, false, 0, new ExponentialDelayOptimizer(5, 0.95, 0.02)); // avilaFNN = (Model)trainer2.Train(avilaFNN, trainDataSet); // //Console.WriteLine(avilaFNN.layers[2].Weight[0, 0]); // Console.WriteLine($"第3轮第{i}次循环:"+evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //} //for (int i = 0; i <= 1; i++) //{ // trainDataSet = trainDataSet.SelectPermutation().ToList(); // Trainer trainer2 = new Trainer(600, false, 0, new ExponentialDelayOptimizer(10, 0.9, 0.02)); // avilaFNN = (Model)trainer2.Train(avilaFNN, trainDataSet); // //Console.WriteLine(avilaFNN.layers[2].Weight[0, 0]); // Console.WriteLine(evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //} //for (int i = 0; i <= 149; i++) //{ // trainDataSet = trainDataSet.SelectPermutation().ToList(); // Trainer trainer2 = new Trainer(100, false, 0, new ExponentialDelayOptimizer(1, 0.98, 0.01)); // avilaFNN = (Model)trainer2.Train(avilaFNN, trainDataSet); // //Console.WriteLine(avilaFNN.layers[2].Weight[0, 0]); // Console.WriteLine($"第4轮第{i}次循环:"+evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //} evaluater.EvaluateInDetails(testDataSet, avilaFNN, new AvilaClassifier()); }
public static void train(string dataPath) { //define dataprocesser AvilaDataProcessing dataProcessing = new AvilaDataProcessing(new AvilaDataConverter(new AvilaClassifier())); //define evaluater AvilaEvaluater evaluater = new AvilaEvaluater(new AvilaDataConverter(new AvilaClassifier())); //create dataset var inputDataSet = dataProcessing.GetData(dataPath); var processDataSet = dataProcessing.GetProcessDataCollection(10000, inputDataSet); ProcessData[] trainDataSet; ProcessData[] testDataSet; (trainDataSet, testDataSet) = dataProcessing.DivideCollection(0.7, processDataSet); trainDataSet = trainDataSet.Take(6000).ToArray(); //define the model Model avilaFNN = new Model(new CrossEntropyLoss(), "AvilaFNN", new FullConnectLayer(new Direct(1), 10, 10, true, new Normal()), new FullConnectLayer(new Sigmoid(), 10, 16, false, new Normal()), new SoftMaxLayer(16, 12, new Normal()) ); //define optimizer Trainer trainer = new Trainer(100, false, 0, new ExponentialDelayOptimizer(2, 0.95, 0.1)); //train for (int i = 0; i <= 19; i++) { trainDataSet = trainDataSet.SelectPermutation().ToArray(); trainer = new Trainer(100, false, 0, new ExponentialDelayOptimizer(0.5, 0.9, 0.08)); avilaFNN = (Model)trainer.Train(avilaFNN, trainDataSet); Console.WriteLine($"epoch {i}:" + evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); } for (int i = 0; i <= 19; i++) { trainDataSet = trainDataSet.SelectPermutation().ToArray(); trainer = new Trainer(60, false, 0, new ExponentialDelayOptimizer(0.5, 0.9, 0.05)); avilaFNN = (Model)trainer.Train(avilaFNN, trainDataSet); Console.WriteLine($"epoch {i}:" + evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); } for (int i = 0; i <= 19; i++) { trainDataSet = trainDataSet.SelectPermutation().ToArray(); trainer = new Trainer(30, false, 0, new ExponentialDelayOptimizer(0.2, 0.9, 0.05)); avilaFNN = (Model)trainer.Train(avilaFNN, trainDataSet); Console.WriteLine($"epoch {i}:" + evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); } //for (int i = 0; i <= 49; i++) //{ // trainDataSet = trainDataSet.SelectPermutation().ToArray(); // trainer = new Trainer(5, false, 0, new ExponentialDelayOptimizer(0.05, 0.9, 0.1)); // avilaFNN = (Model)trainer.Train(avilaFNN, trainDataSet); // Console.WriteLine($"epoch {i}:" + evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //} //for (int i = 0; i <= 99; i++) //{ // trainer = new Trainer(64, false, 0, new ExponentialDelayOptimizer(0.2, 0.95, 0.1)); // avilaFNN = (Model)trainer.Train(avilaFNN, trainDataSet); // Console.WriteLine($"epoch {i}:" + evaluater.Evaluate(testDataSet, avilaFNN, new AvilaClassifier())); //} //evaluate evaluater.EvaluateInDetails(trainDataSet, avilaFNN, new AvilaClassifier()); evaluater.EvaluateInDetails(testDataSet, avilaFNN, new AvilaClassifier()); }