Пример #1
0
int main()
{
	string filename= "iris.data.txt";
	vector<iovector> TrainSetIn; // 训练特征集合
	vector<iovector> TrainSetOut;// 训练标注集合
	vector<iovector>  TestSetIn;  // 测试集合
	vector<iovector>  TestSetOut;
	if(0 != (Load(filename,TrainSetIn,TrainSetOut,TestSetIn,TestSetOut)))
	{
		return 1;
	}
	/* 初始化网络 */
	CNeuralNet  mynet( 4, 3, 10, 1);// 输入层维度,输出层维度,隐层维度,隐层层数
	/* 训练网络 */
	for(int epoch = 0; epoch < 1000; ++epoch )
    mynet.TrainEpoch(TrainSetIn, TrainSetOut, 0.4);
	
	
	/* 测试 */
	double accuracy = 0;  
	 int positive = 0;
	for(int i = 0; i < TestSetIn.size(); ++i )
	{
	      vector<double> output;
	     if(mynet.CalculateOutput(TestSetIn[i],output))
	     {
		       string TestLabel;
			   string TrueLabel;

			   GetLabel(TestSetOut[i],TrueLabel);
		       GetLabel(output,TestLabel);
		       cout<<"输出类别: "<<TestLabel <<" ("<<TrueLabel<<")" << endl;
			   if(0 ==(strcmp(TestLabel.c_str(), TrueLabel.c_str())))
				   positive ++;
	     }
	}
	accuracy = 1.0*positive / TestSetIn.size();
	cout << "分类正确率:accuracy =  " << accuracy*100 << "%" <<endl;
	system("pause");
	return 0;
}
Пример #2
0
int main(void) {

	std::vector<std::string> datasets = std::vector<std::string>();
	datasets.push_back("util/DS_AND.txt");
	datasets.push_back("util/DS_OR.txt");
	datasets.push_back("util/DS_NAND.txt");
	datasets.push_back("util/DS_NOR.txt");
	datasets.push_back("util/DS_XOR.txt");
	datasets.push_back("util/DS_XNOR.txt");

	for(std::string ds : datasets) {
		std::cout << ds << std::endl;
		TrainingData td(ds);
		std::vector<unsigned> topo;
		td.getTopology(topo);

		Neural::Network mynet(topo);

		std::vector<double> inputs, targets;//, outputs;
		unsigned trainingPass = 0;

		while(!td.isEOF()) {
			trainingPass++;
			std::cout << trainingPass << ";";
			if(td.getNextInputs(inputs) != topo[0]) {
				std::cout << -1 << std::endl;
				break;
			}
			mynet.feedForward(inputs);
//			mynet.getResults(outputs);
			td.getNextTargets(targets);
			assert(targets.size()==topo.back());
			mynet.backProp(targets);
			std::cout << mynet.getRecentAvgError() << std::endl;
		}
	}
/*
	TrainingData td("trainingData.txt");
	std::vector<unsigned> topo;
	td.getTopology(topo);

	Neural::Network mynet(topo);


	std::vector<double> inputs, targets, outputs;
	unsigned trainingPass = 0;

	while(!td.isEOF()) {
		trainingPass++;
		std::cout << std::endl << "Pass " << trainingPass;
		if(td.getNextInputs(inputs) != topo[0])
			break;

		printVector(": Inputs", inputs);
		mynet.feedForward(inputs);

		mynet.getResults(outputs);
		printVector("Outputs", outputs);

		td.getNextTargets(targets);
		printVector("Targets", targets);
		assert(targets.size()==topo.back());

		mynet.backProp(targets);

		std::cout << "Net recent avg error: " << mynet.getRecentAvgError() << std::endl;
	}

	std::cout << std::endl << "Done" << std::endl;
*/
	return 0;
}