//  // // # # #### # # -= Visix Library =-  // // ## # # # ## ## Demo.cpp - Neural net demonstration  // // ### # # # ###  // // # ### #### ### Demonstrates the usage of the neurex library  // // # ## # # ## ##  // // # # # # # # R1 (C)2003-2004 Markus Ewald -> License.txt  // //  // #include "Neurex/Neurex.h" #include #include using namespace Neurex; using namespace std; namespace { /// Round floating point to an integer template inline TargetType round(VarType Value) { return (Value >= 0) ? static_cast(Value + 0.5) : static_cast(Value - 0.5); } } // ############################################################################################# // // # main() # // // ############################################################################################# // /** Program entry point */ int main(void) { srand(time(NULL)); // Create a net with 2 inputs and 3 outputs NeuralNet Net(2, 3); Net.randomizeWeights(); cout << "Neural net using " << Net.getNeuronCount() << " neurons " << "in " << Net.getLayerCount() << " layers." << endl; // // Train the net for XOR, OR, AND // cout << "Training net..." << endl; for(size_t nCount = 0; nCount < 1000; ++nCount) { // Train net for 0, 0 Net.setInput(0, 0); Net.setInput(1, 0); Net.setOutput(0, 0); // XOR 0 ^ 0 == 0 Net.setOutput(1, 0); // OR 0 | 0 == 0 Net.setOutput(2, 0); // AND 0 & 0 == 0 Net.train(); // Train net for 0, 1 Net.setInput(0, 0); Net.setInput(1, 1); Net.setOutput(0, 1); // XOR 0 ^ 1 == 1 Net.setOutput(1, 1); // OR 0 | 1 == 1 Net.setOutput(2, 0); // AND 0 & 1 == 0 Net.train(); // Train net for 1, 0 Net.setInput(0, 1); Net.setInput(1, 0); Net.setOutput(0, 1); // XOR 1 ^ 0 == 1 Net.setOutput(1, 1); // OR 1 | 0 == 1 Net.setOutput(2, 0); // AND 1 & 0 == 0 Net.train(); // Train net for 1, 1 Net.setInput(0, 1); Net.setInput(1, 1); Net.setOutput(0, 0); // XOR 1 ^ 1 == 0 Net.setOutput(1, 1); // OR 1 | 1 == 1 Net.setOutput(2, 1); // AND 1 & 1 == 1 Net.train(); } cout << "Training complete!" << endl << endl; // // Use the net to compute XOR, OR, AND // cout << "Results for inputs 0, 0" << endl; Net.setInput(0, 0); Net.setInput(1, 0); Net.compute(); cout << " XOR == " << round(Net.getOutput(0)) << " (" << Net.getOutput(0) << ")" << endl; cout << " OR == " << round(Net.getOutput(1)) << " (" << Net.getOutput(1) << ")" << endl; cout << " AND == " << round(Net.getOutput(2)) << " (" << Net.getOutput(2) << ")" << endl; cout << endl; cout << "Results for inputs 0, 1" << endl; Net.setInput(0, 0); Net.setInput(1, 1); Net.compute(); cout << " XOR == " << round(Net.getOutput(0)) << " (" << Net.getOutput(0) << ")" << endl; cout << " OR == " << round(Net.getOutput(1)) << " (" << Net.getOutput(1) << ")" << endl; cout << " AND == " << round(Net.getOutput(2)) << " (" << Net.getOutput(2) << ")" << endl; cout << endl; cout << "Results for inputs 1, 0" << endl; Net.setInput(0, 1); Net.setInput(1, 0); Net.compute(); cout << " XOR == " << round(Net.getOutput(0)) << " (" << Net.getOutput(0) << ")" << endl; cout << " OR == " << round(Net.getOutput(1)) << " (" << Net.getOutput(1) << ")" << endl; cout << " AND == " << round(Net.getOutput(2)) << " (" << Net.getOutput(2) << ")" << endl; cout << endl; cout << "Results for inputs 1, 1" << endl; Net.setInput(0, 1); Net.setInput(1, 1); Net.compute(); cout << " XOR == " << round(Net.getOutput(0)) << " (" << Net.getOutput(0) << ")" << endl; cout << " OR == " << round(Net.getOutput(1)) << " (" << Net.getOutput(1) << ")" << endl; cout << " AND == " << round(Net.getOutput(2)) << " (" << Net.getOutput(2) << ")" << endl; cout << endl; return 0; }