Advertisement
Guest User

Patch for Neural Network

a guest
May 4th, 2011
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 9.53 KB | None | 0 0
  1. === added file 'CMakeLists.txt'
  2. --- CMakeLists.txt  1970-01-01 00:00:00 +0000
  3. +++ CMakeLists.txt  2011-05-04 22:56:26 +0000
  4. @@ -0,0 +1,28 @@
  5. +# poner nombre proyecto y lenguaje
  6. +# estas 3 lineas consideralas obligatorias
  7. +# CXX es el lenguaje -> C++
  8. +PROJECT( DECORATOR CXX )
  9. +CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
  10. +CMAKE_POLICY(VERSION 2.6)
  11. +
  12. +# nombre archivo salida, se guarda en la variable NAME_OUTPUT
  13. +SET(NAME_OUTPUT main)
  14. +
  15. +# busca todos los archivos C++ del directorio actual (junto al CMakeLists.txt)
  16. +# y los guarda en la variable FICHEROS
  17. +AUX_SOURCE_DIRECTORY(${CMAKE_SOURCE_DIR} FICHEROS)
  18. +
  19. +# directorios donde se van a encontrar las librearias compartidas o estaticas
  20. +LINK_DIRECTORIES(${CMAKE_SOURCE_DIR})
  21. +
  22. +# crear un ejecutable con el nombre de la variable
  23. +ADD_EXECUTABLE(${NAME_OUTPUT} ${FICHEROS})
  24. +
  25. +# linkado de librerias. por ejemplo (los 2 serian igual de válidos):
  26. +#   TARGET_LINK_LIBRARIES(${NAME_OUTPUT} OgreMain)
  27. +#   TARGET_LINK_LIBRARIES(${NAME_OUTPUT} -lOgreMain)
  28. +TARGET_LINK_LIBRARIES(${NAME_OUTPUT})
  29. +
  30. +# para generar todo:
  31. +# cmake .   // genera los makefiles
  32. +# make      // ya sabes :D
  33.  
  34. === added file 'NNPrerequisitos.h'
  35. --- NNPrerequisitos.h   1970-01-01 00:00:00 +0000
  36. +++ NNPrerequisitos.h   2011-05-04 22:56:26 +0000
  37. @@ -0,0 +1,16 @@
  38. +#ifndef NNPrerequisitos
  39. +#define NNPrerequisitos
  40. +
  41. +#include <fstream>
  42. +#include <vector>
  43. +#include <string>
  44. +#include <cstring>
  45. +#include <cstdlib>
  46. +
  47. +class dataEntry;
  48. +class trainingDataSet;
  49. +class dataReader;
  50. +class neuralNetwork;
  51. +class neuralNetworkTrainer;
  52. +
  53. +#endif
  54.  
  55. === modified file 'dataReader.cpp'
  56. --- dataReader.cpp  2011-05-04 22:54:25 +0000
  57. +++ dataReader.cpp  2011-05-04 22:56:26 +0000
  58. @@ -43,11 +43,11 @@
  59.         //read data
  60.  
  61.         while ( !inputFile.eof() )
  62.  
  63.         {
  64.  
  65. -           getline(inputFile, line);              
  66.  
  67. +           getline(inputFile, line);
  68.  
  69.            
  70.  
  71.             //process line
  72.  
  73.             if (line.length() > 2 ) processLine(line);
  74.  
  75. -       }      
  76.  
  77. +       }
  78.  
  79.        
  80.  
  81.         //shuffle data
  82.  
  83.         //random_shuffle(data.begin(), data.end());
  84.  
  85. @@ -87,7 +87,7 @@
  86.     double* pattern = new double[nInputs];
  87.  
  88.     double* target = new double[nTargets];
  89.  
  90.    
  91.  
  92. -   //store inputs     
  93.  
  94. +   //store inputs
  95.  
  96.     char* cstr = new char[line.size()+1];
  97.  
  98.     char* t;
  99.  
  100.     strcpy(cstr, line.c_str());
  101.  
  102.  
  103. === modified file 'dataReader.h'
  104. --- dataReader.h    2011-05-04 22:54:25 +0000
  105. +++ dataReader.h    2011-05-04 22:56:26 +0000
  106. @@ -8,9 +8,7 @@
  107.  #ifndef _DATAREADER
  108.  
  109.  #define _DATAREADER
  110.  
  111.  
  112.  
  113. -//include standard header files
  114.  
  115. -#include <vector>
  116.  
  117. -#include <string>
  118.  
  119. +#include "NNPrerequisitos.h"
  120.  
  121.  
  122.  
  123.  /*******************************************************************
  124.  
  125.  * stores a data item
  126.  
  127. @@ -26,7 +24,7 @@
  128.  
  129.  
  130.     dataEntry(double* p, double* t): pattern(p), target(t) {}
  131.  
  132.        
  133.  
  134. -   ~dataEntry()
  135.  
  136. +   virtual ~dataEntry()
  137.  
  138.     {              
  139.  
  140.         delete[] pattern;
  141.  
  142.         delete[] target;
  143.  
  144. @@ -46,6 +44,7 @@
  145.     std::vector<dataEntry*> validationSet;
  146.  
  147.  
  148.  
  149.     trainingDataSet(){}
  150.  
  151. +   virtual ~trainingDataSet(){}
  152.  
  153.    
  154.  
  155.     void clear()
  156.  
  157.     {
  158.  
  159. @@ -91,7 +90,7 @@
  160.  public:
  161.  
  162.  
  163.  
  164.     dataReader(): creationApproach(NONE), numTrainingSets(-1) {}
  165.  
  166. -   ~dataReader();
  167.  
  168. +   virtual ~dataReader();
  169.  
  170.    
  171.  
  172.     bool loadDataFile( const char* filename, int nI, int nT );
  173.  
  174.     void setCreationApproach( int approach, double param1 = -1, double param2 = -1 );
  175.  
  176.  
  177. === modified file 'main.cpp'
  178. --- main.cpp    2011-05-04 22:54:25 +0000
  179. +++ main.cpp    2011-05-04 22:56:26 +0000
  180. @@ -10,13 +10,14 @@
  181.  #include <ctime>
  182.  
  183.  
  184.  
  185.  //custom includes
  186.  
  187. +#include "dataReader.h"
  188.  
  189.  #include "neuralNetwork.h"
  190.  
  191.  #include "neuralNetworkTrainer.h"
  192.  
  193.  
  194.  
  195.  //use standard namespace
  196.  
  197.  using namespace std;
  198.  
  199.  
  200.  
  201. -void main()
  202.  
  203. +int main()
  204.  
  205.  {     
  206.  
  207.     //seed random number generator
  208.  
  209.     srand( (unsigned int) time(0) );
  210.  
  211. @@ -45,5 +46,7 @@
  212.     nn.saveWeights("weights.csv");
  213.  
  214.        
  215.  
  216.     cout << endl << endl << "-- END OF PROGRAM --" << endl;
  217.  
  218. -   char c; cin >> c;
  219.  
  220. +   //char c; cin >> c;
  221.  
  222. +  
  223.  
  224. +   return 0;
  225.  
  226.  }
  227.  
  228.  
  229. === modified file 'neuralNetwork.cpp'
  230. --- neuralNetwork.cpp   2011-05-04 22:54:25 +0000
  231. +++ neuralNetwork.cpp   2011-05-04 22:56:26 +0000
  232. @@ -1,12 +1,14 @@
  233. +#include "neuralNetwork.h"
  234.  
  235. +
  236.  
  237. +#include "dataReader.h"
  238.  
  239. +#include "neuralNetworkTrainer.h"
  240.  
  241. +
  242.  
  243.  //standard includes
  244.  
  245.  #include <iostream>
  246.  
  247.  #include <vector>
  248.  
  249.  #include <fstream>
  250.  
  251.  #include <math.h>
  252.  
  253.  
  254.  
  255. -//include definition file
  256.  
  257. -#include "neuralNetwork.h"
  258.  
  259. -
  260.  
  261.  using namespace std;
  262.  
  263.  
  264.  
  265.  /*******************************************************************
  266.  
  267. @@ -72,7 +74,7 @@
  268.  /*******************************************************************
  269.  
  270.  * Load Neuron Weights
  271.  
  272.  ********************************************************************/
  273.  
  274. -bool neuralNetwork::loadWeights(char* filename)
  275.  
  276. +bool neuralNetwork::loadWeights(const char* filename)
  277.  
  278.  {
  279.  
  280.     //open file for reading
  281.  
  282.     fstream inputFile;
  283.  
  284. @@ -163,7 +165,7 @@
  285.  /*******************************************************************
  286.  
  287.  * Save Neuron Weights
  288.  
  289.  ********************************************************************/
  290.  
  291. -bool neuralNetwork::saveWeights(char* filename)
  292.  
  293. +bool neuralNetwork::saveWeights(const char* filename)
  294.  
  295.  {
  296.  
  297.     //open file for reading
  298.  
  299.     fstream outputFile;
  300.  
  301. @@ -314,15 +316,6 @@
  302.     return 1/(1+exp(-x));
  303.  
  304.  } 
  305.  
  306.  /*******************************************************************
  307.  
  308. -* Output Clamping
  309.  
  310. -********************************************************************/
  311.  
  312. -inline int neuralNetwork::clampOutput( double x )
  313.  
  314. -{
  315.  
  316. -   if ( x < 0.1 ) return 0;
  317.  
  318. -   else if ( x > 0.9 ) return 1;
  319.  
  320. -   else return -1;
  321.  
  322. -}
  323.  
  324. -/*******************************************************************
  325.  
  326.  * Feed Forward Operation
  327.  
  328.  ********************************************************************/
  329.  
  330.  void neuralNetwork::feedForward(double* pattern)
  331.  
  332.  
  333. === modified file 'neuralNetwork.h'
  334. --- neuralNetwork.h 2011-05-04 22:54:25 +0000
  335. +++ neuralNetwork.h 2011-05-04 22:56:26 +0000
  336. @@ -8,15 +8,13 @@
  337.  #ifndef NNetwork
  338.  
  339.  #define NNetwork
  340.  
  341.  
  342.  
  343. -#include "dataReader.h"
  344.  
  345. -
  346.  
  347. -class neuralNetworkTrainer;
  348.  
  349. +#include "NNPrerequisitos.h"
  350.  
  351.  
  352.  
  353.  class neuralNetwork
  354.  
  355.  {
  356.  
  357.     //class members
  358.  
  359.     //--------------------------------------------------------------------------------------------
  360.  
  361. -private:
  362.  
  363. +public:
  364.  
  365.  
  366.  
  367.     //number of neurons
  368.  
  369.     int nInput, nHidden, nOutput;
  370.  
  371. @@ -32,7 +30,7 @@
  372.        
  373.  
  374.     //Friends
  375.  
  376.     //--------------------------------------------------------------------------------------------
  377.  
  378. -   friend neuralNetworkTrainer;
  379.  
  380. +   //friend neuralNetworkTrainer;
  381.  
  382.    
  383.  
  384.     //public methods
  385.  
  386.     //--------------------------------------------------------------------------------------------
  387.  
  388. @@ -41,11 +39,11 @@
  389.  
  390.  
  391.     //constructor & destructor
  392.  
  393.     neuralNetwork(int numInput, int numHidden, int numOutput);
  394.  
  395. -   ~neuralNetwork();
  396.  
  397. +   virtual ~neuralNetwork();
  398.  
  399.  
  400.  
  401.     //weight operations
  402.  
  403. -   bool loadWeights(char* inputFilename);
  404.  
  405. -   bool saveWeights(char* outputFilename);
  406.  
  407. +   bool loadWeights(const char* inputFilename);
  408.  
  409. +   bool saveWeights(const char* outputFilename);
  410.  
  411.     int* feedForwardPattern( double* pattern );
  412.  
  413.     double getSetAccuracy( std::vector<dataEntry*>& set );
  414.  
  415.     double getSetMSE( std::vector<dataEntry*>& set );
  416.  
  417. @@ -53,11 +51,18 @@
  418.     //private methods
  419.  
  420.     //--------------------------------------------------------------------------------------------
  421.  
  422.  
  423.  
  424. -private:
  425.  
  426. +public:
  427.  
  428.  
  429.  
  430.     void initializeWeights();
  431.  
  432.     inline double activationFunction( double x );
  433.  
  434. -   inline int clampOutput( double x );
  435.  
  436. +
  437.  
  438. +   inline int clampOutput( double x )
  439.  
  440. +   {
  441.  
  442. +       if ( x < 0.1 ) return 0;
  443.  
  444. +       else if ( x > 0.9 ) return 1;
  445.  
  446. +       else return -1;
  447.  
  448. +   }
  449.  
  450. +  
  451.  
  452.     void feedForward( double* pattern );
  453.  
  454.    
  455.  
  456.  };
  457.  
  458.  
  459. === modified file 'neuralNetworkTrainer.cpp'
  460. --- neuralNetworkTrainer.cpp    2011-05-04 22:54:25 +0000
  461. +++ neuralNetworkTrainer.cpp    2011-05-04 22:56:26 +0000
  462. @@ -1,11 +1,13 @@
  463. +#include "neuralNetworkTrainer.h"
  464.  
  465. +
  466.  
  467. +#include "dataReader.h"
  468.  
  469. +#include "neuralNetwork.h"
  470.  
  471. +
  472.  
  473.  //standard includes
  474.  
  475.  #include <iostream>
  476.  
  477.  #include <fstream>
  478.  
  479.  #include <math.h>
  480.  
  481.  
  482.  
  483. -//include definition file
  484.  
  485. -#include "neuralNetworkTrainer.h"
  486.  
  487. -
  488.  
  489.  using namespace std;
  490.  
  491.  
  492.  
  493.  /*******************************************************************
  494.  
  495. @@ -50,6 +52,13 @@
  496.     for ( int i=0; i <= NN->nOutput; i++ ) outputErrorGradients[i] = 0;
  497.  
  498.  }
  499.  
  500.  
  501.  
  502. +/*******************************************************************
  503.  
  504. +* Destructor
  505.  
  506. +********************************************************************/
  507.  
  508. +neuralNetworkTrainer::~neuralNetworkTrainer()
  509.  
  510. +{
  511.  
  512. +
  513.  
  514. +}
  515.  
  516.  
  517.  
  518.  /*******************************************************************
  519.  
  520.  * Set training parameters
  521.  
  522.  
  523. === modified file 'neuralNetworkTrainer.h'
  524. --- neuralNetworkTrainer.h  2011-05-04 22:54:25 +0000
  525. +++ neuralNetworkTrainer.h  2011-05-04 22:56:26 +0000
  526. @@ -8,12 +8,7 @@
  527.  #ifndef NNetworkTrainer
  528.  
  529.  #define NNetworkTrainer
  530.  
  531.  
  532.  
  533. -//standard includes
  534.  
  535. -#include <fstream>
  536.  
  537. -#include <vector>
  538.  
  539. -
  540.  
  541. -//neural network header
  542.  
  543. -#include "neuralNetwork.h"
  544.  
  545. +#include "NNPrerequisitos.h"
  546.  
  547.  
  548.  
  549.  //Constant Defaults!
  550.  
  551.  #define LEARNING_RATE 0.001
  552.  
  553. @@ -76,6 +71,8 @@
  554.  public:   
  555.  
  556.    
  557.  
  558.     neuralNetworkTrainer( neuralNetwork* untrainedNetwork );
  559.  
  560. +   virtual ~neuralNetworkTrainer();
  561.  
  562. +  
  563.  
  564.     void setTrainingParameters( double lR, double m, bool batch );
  565.  
  566.     void setStoppingConditions( int mEpochs, double dAccuracy);
  567.  
  568.     void useBatchLearning( bool flag ){ useBatch = flag; }
  569.  
  570. @@ -94,4 +91,4 @@
  571.  };
  572.  
  573.  
  574.  
  575.  
  576.  
  577. -#endif
  578. \ No newline at end of file
  579. +#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement