Guest User

Untitled

a guest
Apr 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. Index: NeuralNet.h
  2. ===================================================================
  3. --- NeuralNet.h (revision 112)
  4. +++ NeuralNet.h (revision 113)
  5. @@ -12,8 +12,8 @@
  6. int mNumHidden;
  7. int mNumOutputs;
  8.  
  9. - Matrix::Matrix *mWin;
  10. - Matrix::Matrix *mWout;
  11. + Matrix::Matrix mWin;
  12. + Matrix::Matrix mWout;
  13.  
  14. std::vector<double> mInputActivation;
  15. std::vector<double> mHiddenActivation;
  16. @@ -21,7 +21,6 @@
  17.  
  18. public:
  19. NeuralNet(int numInputs, int numHidden, int numOutput);
  20. - ~NeuralNet();
  21. std::vector<double> update(const std::vector<double> &inputs);
  22. double backprop(const std::vector<double> &target, double learningRate);
  23.  
  24. Index: Util.cpp
  25. ===================================================================
  26. --- Util.cpp (revision 112)
  27. +++ Util.cpp (revision 113)
  28. @@ -7,6 +7,7 @@
  29. #include <cstdlib>
  30. #include <cassert>
  31. #include <cmath>
  32. +#include <vector>
  33.  
  34. // Convert a std::string to a double
  35. double util::str2double(const std::string &s) {
  36. @@ -57,7 +58,7 @@
  37. const std::vector<double> &actual) {
  38. assert(expected.size() == actual.size());
  39. double sum = 0.0;
  40. - for(int i = 0; i < expected.size(); ++i) {
  41. + for(size_t i = 0; i < expected.size(); ++i) {
  42. sum += pow(expected[i] - actual[i], 2.0);
  43. }
  44.  
  45. @@ -68,7 +69,7 @@
  46. assert(a.size() == b.size());
  47.  
  48. double sum = 0;
  49. - for(int i = 0; i < a.size(); ++i) {
  50. + for(size_t i = 0; i < a.size(); ++i) {
  51. sum += a[i] * b[i];
  52. }
  53.  
  54. @@ -81,9 +82,9 @@
  55. return (double) rand() / (double) RAND_MAX - 0.5;
  56. }
  57.  
  58. -bool util::classify(const std::vector<double> &result, int correctIndex)
  59. +bool util::classify(const std::vector<double> &result, size_t correctIndex)
  60. {
  61. - for (int i=0; i<result.size(); i++)
  62. + for (size_t i=0; i<result.size(); i++)
  63. {
  64. if (i != correctIndex && result[i] > result[correctIndex])
  65. {
  66. Index: Util.h
  67. ===================================================================
  68. --- Util.h (revision 112)
  69. +++ Util.h (revision 113)
  70. @@ -23,22 +23,9 @@
  71.  
  72. double multiplyVect(const std::vector<double> &a, const std::vector<double> &b);
  73.  
  74. - double forward_neuron_response(const std::vector<double> &weights,
  75. - const std::vector<double> &inputs);
  76. -
  77. - double output_neuron_delta(const double &expected,
  78. - const double &actual,
  79. - const std::vector<double> &weights,
  80. - const std::vector<double> &inputs);
  81. -
  82. - double hidden_neuron_delta(const std::vector<double> &inputs,
  83. - const std::vector<double> &odelta,
  84. - const std::vector<double> &hweights,
  85. - const std::vector<double> &oweights);
  86. -
  87. double small_random();
  88.  
  89. - bool classify(const std::vector<double> &result, int correctIndex);
  90. + bool classify(const std::vector<double> &result, size_t correctIndex);
  91. }
  92.  
  93. #endif /* _UTIL_H */
  94. Index: SConstruct
  95. ===================================================================
  96. --- SConstruct (revision 112)
  97. +++ SConstruct (revision 113)
  98. @@ -1,6 +1,5 @@
  99. env = Environment()
  100. buildenv = env.Clone()
  101. -buildenv.Append(CFLAGS = Split("-g -O2 -Wall"))
  102. buildenv.Append(CXXFLAGS = Split("-g -O2 -Wall"))
  103. Export('buildenv')
  104.  
  105. Index: NeuralNet.cpp
  106. ===================================================================
  107. --- NeuralNet.cpp (revision 112)
  108. +++ NeuralNet.cpp (revision 113)
  109. @@ -4,28 +4,18 @@
  110. #include <iostream>
  111.  
  112. NeuralNet::NeuralNet(int numInputs, int numHidden, int numOutput)
  113. + : mNumInputs(numInputs + 1),
  114. + mNumHidden(numHidden),
  115. + mNumOutputs(numOutput),
  116. + mWin(mNumInputs, mNumHidden),
  117. + mWout(mNumHidden, mNumOutputs),
  118. + mInputActivation(mNumInputs, 1.0),
  119. + mHiddenActivation(mNumHidden),
  120. + mOutputActivation(mNumOutputs)
  121. {
  122. - // Add an additional input as the bias node
  123. - mNumInputs = numInputs + 1;
  124. - mNumHidden = numHidden;
  125. - mNumOutputs = numOutput;
  126. -
  127. - mWin = new Matrix::Matrix(mNumInputs, mNumHidden);
  128. - mWout = new Matrix::Matrix(mNumHidden, mNumOutputs);
  129. -
  130. - mInputActivation = std::vector<double>(mNumInputs, 1.0);
  131. - mHiddenActivation = std::vector<double>(mNumHidden, 1.0);
  132. - mOutputActivation = std::vector<double>(mNumOutputs, 1.0);
  133. -
  134. initialize();
  135. }
  136.  
  137. -NeuralNet::~NeuralNet()
  138. -{
  139. - delete mWin;
  140. - delete mWout;
  141. -}
  142. -
  143. std::vector<double> NeuralNet::update(const std::vector<double> &inputs)
  144. {
  145. using namespace util;
  146. @@ -34,13 +24,13 @@
  147. for (int hiddenIdx = 0; hiddenIdx < mNumHidden; ++hiddenIdx)
  148. {
  149. mHiddenActivation[hiddenIdx] =
  150. - sigmoid(multiplyVect(mInputActivation, mWin->getColVector(hiddenIdx)));
  151. + sigmoid(multiplyVect(mInputActivation, mWin.getColVector(hiddenIdx)));
  152. }
  153.  
  154. for (int outputIdx = 0; outputIdx < mNumOutputs; ++outputIdx)
  155. {
  156. mOutputActivation[outputIdx] =
  157. - sigmoid(multiplyVect(mHiddenActivation, mWout->getColVector(outputIdx)));
  158. + sigmoid(multiplyVect(mHiddenActivation, mWout.getColVector(outputIdx)));
  159. }
  160.  
  161. return mOutputActivation;
  162. @@ -61,7 +51,7 @@
  163. // Compute hidden deltas
  164. for (int j = 0; j < mNumHidden; j++)
  165. {
  166. - double error = util::multiplyVect(output_deltas, mWout->getRowVector(j));
  167. + double error = util::multiplyVect(output_deltas, mWout.getRowVector(j));
  168. hidden_deltas[j] = util::d_sigmoid(mHiddenActivation[j]) * error;
  169. }
  170.  
  171. @@ -70,10 +60,10 @@
  172. {
  173. for (int i = 0; i < mNumOutputs; i++)
  174. {
  175. - double new_weight = mWout->get(j, i) +
  176. + double new_weight = mWout.get(j, i) +
  177. learningRate * (output_deltas[i] * mOutputActivation[j]);
  178.  
  179. - mWout->setElement(j, i, new_weight);
  180. + mWout.setElement(j, i, new_weight);
  181. }
  182. }
  183.  
  184. @@ -82,10 +72,10 @@
  185. {
  186. for (int j = 0; j < mNumHidden; j++)
  187. {
  188. - double new_weight = mWin->get(k, j) +
  189. + double new_weight = mWin.get(k, j) +
  190. learningRate * (hidden_deltas[j] * mInputActivation[k]);
  191.  
  192. - mWin->setElement(k, j, new_weight);
  193. + mWin.setElement(k, j, new_weight);
  194. }
  195. }
  196.  
  197. @@ -99,7 +89,7 @@
  198. {
  199. for (int j = 0; j < mNumHidden; j++)
  200. {
  201. - mWin->setElement(i,j, util::small_random());
  202. + mWin.setElement(i,j, util::small_random());
  203. }
  204. }
  205.  
  206. @@ -107,7 +97,7 @@
  207. {
  208. for (int j = 0; j < mNumOutputs; j++)
  209. {
  210. - mWout->setElement(i,j, util::small_random());
  211. + mWout.setElement(i,j, util::small_random());
  212. }
  213. }
  214. }
Add Comment
Please, Sign In to add comment