www.pudn.com > 基于VC的神经网络开发程序包(源码).rar > xor.cpp


/* 
 *	annie - Neural Network Library 
 *	http://annie.sourceforge.net/ 
 * 
 *	EXAMPLE - A two layer network that is trained to compute the XOR function. 
 * 
 *	Last Modified On: 
 *		January 12, 2003 
 * 
 *  Author(s): 
 *		Asim Shankar 
 *     
 *	A two-layer (one hidden, one output) feed-forward neural network. 
 *	2 inputs 
 *	3 hidden neurons 
 *	1 output 
 *	Trained using backpropagation. 
 * 
 *	This network can be created in two ways, one using a multi-layer network 
 *	(MultiLayerNetwork) and building it up, or simply using a TwoLayerNetwork. 
 *	This example uses the latter. 
 */ 
#include  
#include   
#include "../include/annie.h" 
 
 
using namespace std; 
 
//All members of the NeuralNetwork library are in this namespace 
using namespace annie; 
 
int main() 
{ 
	//srand((unsigned)time(NULL)); 
	srand(123); 
 
	//Set the input/output training values 
	real input1[]={0,0}; real output1[]={0}; 
	real input2[]={1,0}; real output2[]={1}; 
	real input3[]={0,1}; real output3[]={1}; 
	real input4[]={1,1}; real output4[]={0}; 
 
	try 
	{ 
		TwoLayerNetwork net(2,3,1); 
		net.connectAll(); 
 
		//Create a training set, 2 inputs, 1 output 
		TrainingSet T(2,1); 
		//Add the various sample data to the training set 
		T.addIOpair(input1,output1); 
		T.addIOpair(input2,output2); 
		T.addIOpair(input3,output3); 
		T.addIOpair(input4,output4); 
 
		//Outputs will be placed in this vector 
		VECTOR output;  
 
		cout<<"Results before training:"<