private static double Learn(ImageBatch imb, LabelBatch lab, int x, Net net) { int correctNum = 0; double costSum = 0; for (int i = x * MINIBATCH_SIZE; i < x * MINIBATCH_SIZE + MINIBATCH_SIZE; i++) { formChanger.ChangeImage(imb[i].GetBitmap(), lab[i] + " (" + (i + 1) + "/" + imb.Count() + ")"); net.LoadSource(imb[i], lab[i]); //载入样本 var isCorrect = net.BeginReason(out var say); //正推 var cost = net.Evaluation(); //计算cost costSum += cost; formChanger.AddNeuronNote(net, lab[i], say, cost); net.Recall();//反向传播 if (isCorrect) { correctNum++; } PrintInForm(isCorrect, say, cost); } var averageCost = costSum / MINIBATCH_SIZE; formChanger.AddCost(averageCost, Convert.ToDouble(correctNum) / MINIBATCH_SIZE); net.Update(0.1, Net.UpdateOptimizer.SGD); ResultWriter.WriteResult(net); PrintInConsole(averageCost, correctNum); return(averageCost); }
private static void BeginLearning(int startPos) { var imageBatch = new ImageBatch(DataReader.ReadTrainImage()); var labelBatch = new LabelBatch(DataReader.ReadTrainLabel()); var net = ResultWriter.ReadResult(); //net.InitMomentumLists(); for (int x = startPos / MINIBATCH_SIZE; x < (60000 / MINIBATCH_SIZE); x++) { ResultWriter.WriteLog("start:" + x * MINIBATCH_SIZE + " to " + (x * MINIBATCH_SIZE + MINIBATCH_SIZE) + "\n"); for (; ;) { var averageCost = Learn(imageBatch, labelBatch, x, net);//学习minibatch的一份 if (averageCost < 0.01) { ResultWriter.WriteLog("cost:" + averageCost + "\n"); break; } } } }