Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. #include <mlpack/core.hpp>
  2. #include <mlpack/methods/logistic_regression/logistic_regression.hpp>
  3. #include <mlpack/core/optimizers/sgd/test_function.hpp>
  4. #include <mlpack/core/optimizers/sgd/update_policies/vanilla_update.hpp>
  5. #include <mlpack/methods/ann/layer/layer.hpp>
  6. #include <mlpack/methods/ann/ffn.hpp>
  7.  
  8. #include "cmaes.hpp"
  9.  
  10. using namespace std;
  11. using namespace arma;
  12. using namespace mlpack;
  13. using namespace mlpack::ann;
  14. using namespace mlpack::optimization;
  15. using namespace mlpack::optimization::test;
  16.  
  17. using namespace mlpack::distribution;
  18. using namespace mlpack::regression;
  19.  
  20. /**
  21. * Train and evaluate a vanilla network with the specified structure.
  22. */
  23. template<typename MatType = arma::mat>
  24. void BuildVanillaNetwork(MatType& trainData,
  25. MatType& trainLabels,
  26. MatType& testData,
  27. MatType& testLabels,
  28. const size_t outputSize,
  29. const size_t hiddenLayerSize,
  30. const size_t maxEpochs,
  31. const double classificationErrorThreshold)
  32. {
  33. FFN<NegativeLogLikelihood<> > model;
  34. model.Add<Linear<> >(trainData.n_rows, hiddenLayerSize);
  35. model.Add<SigmoidLayer<> >();
  36. model.Add<Linear<> >(hiddenLayerSize, outputSize);
  37. model.Add<LogSoftMax<> >();
  38.  
  39.  
  40. int dim = trainData.n_rows * hiddenLayerSize * outputSize;
  41. arma::mat start1(dim, 1); start1.randu();
  42. arma::mat initialStdDeviations1(dim, 1); initialStdDeviations1.randu();
  43.  
  44. CMAES opt(dim, start1, initialStdDeviations1, 50000, 1e-8);
  45.  
  46.  
  47. model.Train(trainData, trainLabels, opt);
  48.  
  49. MatType predictionTemp;
  50. model.Predict(testData, predictionTemp);
  51. MatType prediction = arma::zeros<MatType>(1, predictionTemp.n_cols);
  52.  
  53. for (size_t i = 0; i < predictionTemp.n_cols; ++i)
  54. {
  55. prediction(i) = arma::as_scalar(arma::find(
  56. arma::max(predictionTemp.col(i)) == predictionTemp.col(i), 1)) + 1;
  57. }
  58.  
  59. size_t error = 0;
  60. for (size_t i = 0; i < testData.n_cols; i++)
  61. {
  62. if (int(arma::as_scalar(prediction.col(i))) ==
  63. int(arma::as_scalar(testLabels.col(i))))
  64. {
  65. error++;
  66. }
  67. }
  68.  
  69. double classificationError = 1 - double(error) / testData.n_cols;
  70. cout << "require " << classificationError << " <= " << classificationErrorThreshold << endl;
  71. }
  72.  
  73.  
  74. int main()
  75. {
  76. mlpack::math::RandomSeed(std::time(NULL));
  77.  
  78.  
  79. // SGD TEST CASE PASS
  80.  
  81. SGDTestFunction test;
  82.  
  83. size_t N = test.NumFunctions();
  84.  
  85. arma::mat start(N,1); start.fill(0.5);
  86. arma::mat initialStdDeviations(N,1); initialStdDeviations.fill(1.5);
  87.  
  88. CMAES s(N,start,initialStdDeviations,10000,1e-18);
  89.  
  90. arma::mat coordinates(N,1);
  91. double result = s.Optimize(test, coordinates);
  92.  
  93. cout <<
  94. "BOOST_REQUIRE_CLOSE(result, -1.0, 0.05); \n" <<
  95. "BOOST_REQUIRE_SMALL(coordinates[0], 1e-3); \n" <<
  96. "BOOST_REQUIRE_SMALL(coordinates[1], 1e-7); \n" <<
  97. "BOOST_REQUIRE_SMALL(coordinates[2], 1e-7);" << endl;
  98.  
  99. cout << endl << result << endl;
  100. cout << coordinates[0] << endl;
  101. cout << coordinates[1] << endl;
  102. cout << coordinates[2] << endl;
  103.  
  104. // Generate a two-Gaussian dataset.
  105. GaussianDistribution g1(arma::vec("1.0 1.0 1.0"), arma::eye<arma::mat>(3, 3));
  106. GaussianDistribution g2(arma::vec("9.0 9.0 9.0"), arma::eye<arma::mat>(3, 3));
  107.  
  108. arma::mat data(3, 1000);
  109. arma::Row<size_t> responses(1000);
  110. for (size_t i = 0; i < 500; ++i)
  111. {
  112. data.col(i) = g1.Random();
  113. responses[i] = 0;
  114. }
  115. for (size_t i = 500; i < 1000; ++i)
  116. {
  117. data.col(i) = g2.Random();
  118. responses[i] = 1;
  119. }
  120.  
  121. // Shuffle the dataset.
  122. arma::uvec indices = arma::shuffle(arma::linspace<arma::uvec>(0,
  123. data.n_cols - 1, data.n_cols));
  124. arma::mat shuffledData(3, 1000);
  125. arma::Row<size_t> shuffledResponses(1000);
  126. for (size_t i = 0; i < data.n_cols; ++i)
  127. {
  128. shuffledData.col(i) = data.col(indices[i]);
  129. shuffledResponses[i] = responses[indices[i]];
  130. }
  131.  
  132. // Create a test set.
  133. arma::mat testData(3, 1000);
  134. arma::Row<size_t> testResponses(1000);
  135. for (size_t i = 0; i < 500; ++i)
  136. {
  137. testData.col(i) = g1.Random();
  138. testResponses[i] = 0;
  139. }
  140. for (size_t i = 500; i < 1000; ++i)
  141. {
  142. testData.col(i) = g2.Random();
  143. testResponses[i] = 1;
  144. }
  145.  
  146. int dim = shuffledData.n_rows + 1;
  147. arma::mat start1(dim, 1); start1.fill(0.5);
  148. arma::mat initialStdDeviations1(dim, 1); initialStdDeviations1.fill(1.5);
  149.  
  150. CMAES test1(dim, start1, initialStdDeviations1, 50000, 1e-8);
  151.  
  152. LogisticRegression<arma::mat> lr(shuffledData, shuffledResponses, test1, 0.5);
  153.  
  154. // Ensure that the error is close to zero.
  155. const double acc = lr.ComputeAccuracy(data, responses);
  156. cout << "got this value = " << acc << " should be = 100.0 with tolerance = 0.3" << endl; // 0.3% error tolerance.
  157.  
  158. const double testAcc = lr.ComputeAccuracy(testData, testResponses);
  159. cout << "got this value = " << testAcc << " should be = 100.0 with tolerance = 0.3" << endl;
  160.  
  161. // Load the dataset.
  162. arma::mat dataset;
  163. data::Load("thyroid_train.csv", dataset, true);
  164.  
  165. arma::mat trainData = dataset.submat(0, 0, dataset.n_rows - 4,
  166. dataset.n_cols - 1);
  167.  
  168. arma::mat trainLabelsTemp = dataset.submat(dataset.n_rows - 3, 0,
  169. dataset.n_rows - 1, dataset.n_cols - 1);
  170. arma::mat trainLabels = arma::zeros<arma::mat>(1, trainLabelsTemp.n_cols);
  171. for (size_t i = 0; i < trainLabelsTemp.n_cols; ++i)
  172. {
  173. trainLabels(i) = arma::as_scalar(arma::find(
  174. arma::max(trainLabelsTemp.col(i)) == trainLabelsTemp.col(i), 1)) + 1;
  175. }
  176.  
  177. data::Load("thyroid_test.csv", dataset, true);
  178.  
  179. arma::mat testData1 = dataset.submat(0, 0, dataset.n_rows - 4,
  180. dataset.n_cols - 1);
  181.  
  182. arma::mat testLabelsTemp = dataset.submat(dataset.n_rows - 3, 0,
  183. dataset.n_rows - 1, dataset.n_cols - 1);
  184.  
  185. arma::mat testLabels = arma::zeros<arma::mat>(1, testLabelsTemp.n_cols);
  186. for (size_t i = 0; i < testLabels.n_cols; ++i)
  187. {
  188. testLabels(i) = arma::as_scalar(arma::find(
  189. arma::max(testLabelsTemp.col(i)) == testLabelsTemp.col(i), 1)) + 1;
  190. }
  191.  
  192. // Vanilla neural net with logistic activation function.
  193. // Because 92 percent of the patients are not hyperthyroid the neural
  194. // network must be significant better than 92%.
  195. BuildVanillaNetwork<>
  196. (trainData, trainLabels, testData1, testLabels, 3, 8, 70, 0.1);
  197.  
  198. return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement