Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include "Eigen/Dense"
  4. #define SLOPE 4
  5. #define ETA 0.1
  6. #define THRESHOLD 0.01
  7. #define RANDOM true
  8.  
  9. using Eigen::MatrixXd;
  10. using namespace std;
  11.  
  12. MatrixXd v;
  13. MatrixXd w;
  14.  
  15. MatrixXd randomUniform(int rows, int cols, double min, double max);
  16. double sumSquaredError(const Eigen::Ref<const MatrixXd> target, const Eigen::Ref<const MatrixXd> predicted);
  17. double tf(double d);
  18. double dtf(double d);
  19.  
  20. int main() {
  21. srand((unsigned int) time(0));
  22. int numInputs, numHiddenNodes, numClasses, numOutputs, numRows, numCols;
  23. long n; //Temp storage variable
  24. scanf("%d %d %d", &numInputs, &numHiddenNodes, &numClasses);
  25. scanf("%d %d", &numRows, &numCols);
  26. numOutputs = numCols - numInputs;
  27.  
  28. MatrixXd raw(numRows, numCols);
  29. for (int r = 0; r < numRows; r++) {
  30. for (int c = 0; c < numCols; c++) {
  31. double d;
  32. scanf("%lf", &d);
  33. raw(r, c) = d;
  34. }
  35. }
  36.  
  37. MatrixXd x = raw.block(0, 0, numRows, numInputs);
  38. MatrixXd xb(numRows, numInputs + 1);
  39. n = x.cols();
  40. xb << Eigen::VectorXd::Constant(x.rows(), -1.0), x.leftCols(n);
  41. MatrixXd t = raw.block(0, numInputs, numRows, numOutputs);
  42. v = randomUniform(numInputs + 1, numHiddenNodes, 0, 1.0);
  43. w = randomUniform(numHiddenNodes + 1, numOutputs, 0, 1.0);
  44. // Enter training loop
  45. int count = 0;
  46. double sum = 1;
  47. while (count++ < 100000 && sum > THRESHOLD) {
  48.  
  49. MatrixXd h = xb * v;
  50. h = h.unaryExpr(&tf).eval();
  51. MatrixXd hb(h.rows(), h.cols() + 1);
  52. n = h.cols();
  53. hb << Eigen::VectorXd::Constant(h.rows(),-1.0), h.leftCols(n);
  54.  
  55. MatrixXd y = hb * w;
  56. y = y.unaryExpr(&tf).eval();
  57.  
  58. sum = sumSquaredError(t, y);
  59. if (count % 1000 == 0) cout << "Current Sum: " << sum << endl;
  60.  
  61. MatrixXd dy = (t-y).cwiseProduct(y.unaryExpr(&dtf));
  62. MatrixXd dhb = hb.unaryExpr(&dtf).cwiseProduct(dy * w.transpose());
  63. w += (ETA * (hb.transpose() * dy).array()).matrix();
  64.  
  65. MatrixXd dh(dhb.rows(), dhb.cols() - 1);
  66. dh << dhb.leftCols(dhb.cols() - 1);
  67. v += (ETA * (xb.transpose() * dh).array()).matrix();
  68. }
  69.  
  70. int numTestRows, numTestCols;
  71. scanf("%d %d", &numTestRows, &numTestCols);
  72. MatrixXd testRaw(numTestRows, numTestCols);
  73.  
  74. for (int r = 0; r < numTestRows; r++) {
  75. for (int c = 0; c < numTestCols; c++) {
  76. double d;
  77. scanf("%lf", &d);
  78. testRaw(r, c) = d;
  79. }
  80. }
  81.  
  82. MatrixXd testX = testRaw.block(0, 0, numTestRows, numInputs);
  83. MatrixXd testXb(testX.rows(), testX.cols() + 1);
  84. testXb << Eigen::VectorXd::Constant(testX.rows(), -1.0), testX.leftCols(testX.cols());
  85. MatrixXd testT = raw.block(0, numInputs, numTestRows, numOutputs);
  86. MatrixXd testH = testXb * v;
  87. testH = testH.unaryExpr(&tf).eval();
  88. MatrixXd testHb(testH.rows(), testH.cols() + 1);
  89. testHb << Eigen::VectorXd::Constant(testH.rows(), -1.0), testH.leftCols(testH.cols());
  90.  
  91. MatrixXd testY = testHb * w;
  92. testY = testY.unaryExpr(&tf).eval();
  93. cout << "Target" << endl;
  94. cout << testT << endl;
  95. cout << "Predicted" << endl;
  96. cout << testY << endl;
  97. cout << "Error on Test Data: " << sumSquaredError(testT, testY) << endl;
  98. return 0;
  99. }
  100.  
  101. double tf(double d) {
  102. return 1.0/(1+exp(-SLOPE*d));
  103. }
  104.  
  105. double dtf(double d) {
  106. return d * (1.0 - d);
  107. }
  108.  
  109. double sumSquaredError(const Eigen::Ref<const MatrixXd> target, const Eigen::Ref<const MatrixXd> predicted) {
  110. MatrixXd error = target-predicted;
  111. error = error.cwiseProduct(error);
  112. return error.sum();
  113. }
  114.  
  115. MatrixXd randomUniform(int rows, int cols, double min, double max) {
  116. MatrixXd out(rows, cols);
  117.  
  118. unsigned int seed = 0;
  119.  
  120. if (RANDOM) {
  121. std::random_device rd;
  122. seed = rd();
  123. cout << seed << endl;
  124. }
  125.  
  126. std::mt19937 generator(seed);
  127. std::uniform_real_distribution<double> distribution(min, max);
  128.  
  129. for (int r = 0; r < rows; r++) {
  130. for (int c = 0; c < cols; c++) {
  131. out(r, c) = distribution(generator);
  132. }
  133. }
  134. return out;
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement