myTsne.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <iostream>
  2. #include "src/t_sne/tsne.h"
  3. #include <random>
  4. #include "RunDataLite.h"
  5. void generateRandomData(const size_t& N, const size_t& D, double* X);
  6. double euclidenDistance(double* y1, double* y2, int D);
  7. int main(int argv, char* argc[]) {
  8. int amountOfSolutions = 1000;
  9. int amountOfBits = 1600;
  10. bool log = false;
  11. std::string filepath("generated.metalog");
  12. if (argv > 1) {
  13. std::cout << argc[1] << std::endl;
  14. filepath = argc[1];
  15. }
  16. RunDataLite* data = new RunDataLite(filepath);
  17. if (data->badFile()) return -1;
  18. data->solutionVec.erase(data->solutionVec.begin());
  19. //N -> amount of dataPoints
  20. //D -> Dimension of DataPoints
  21. size_t N = data->solutionVec.size(), D = data->solutionVec[0].bitVec.size();
  22. std::cout << "N:" << N << " D:" << D << std::endl;
  23. double* X = new double[N * D];
  24. int nD = 0;
  25. for (int n = 0; n < N; n++) {
  26. const SolutionPointData& sol = data->solutionVec[n];
  27. if(log)std::cout << "[";
  28. for (int d = 0; d < D; d++) {
  29. X[ nD + d] = sol.bitVec[d]?1.0:0.0;
  30. if (log)std::cout << (sol.bitVec[d] ? "1" : "0");
  31. }
  32. if(log)std::cout << "]" << std::endl;
  33. nD += D;
  34. }
  35. if (log) {
  36. std::cout << std::endl << "Second:" << std::endl << std::endl;
  37. nD = 0;
  38. for (int n = 0; n < N; n++) {
  39. std::cout << "[";
  40. for (int d = 0; d < D; d++) {
  41. std::cout << (X[nD + d]);
  42. }
  43. std::cout << "]" << std::endl;
  44. nD += D;
  45. }
  46. std::cout << std::endl << "End:" << std::endl << std::endl;
  47. }
  48. //no_dims -> dimension of target
  49. size_t no_dims = 2;
  50. double* Y = new double[N * no_dims];
  51. //Perplexity and theta
  52. double perplexity = 20;
  53. double theta = 0;
  54. //learnig rate epsillon;
  55. double eta = 200;
  56. //Random
  57. int rand_seed = 17235761;
  58. bool skip_random_init = false;
  59. //Iter changes
  60. int max_iter = 750, stop_lying_iter = 250, mom_switch_iter = 100;
  61. TSNE::run(X, N, D, Y, no_dims, perplexity, theta, eta, rand_seed, skip_random_init, max_iter, stop_lying_iter, mom_switch_iter);
  62. std::cout << "Finished:" << std::endl;
  63. for (size_t i = 0; i < N; i++) {
  64. std::cout << Y[i * no_dims] << ", " << Y[i * no_dims + 1] << ", " << data->solutionVec[i].objectiveFunction << std::endl;
  65. }
  66. return 0;
  67. }
  68. void generateRandomData(const size_t& N, const size_t& D, double* X)
  69. {
  70. //init rnd
  71. std::random_device rd; //Will be used to obtain a seed for the random number engine
  72. std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
  73. std::uniform_real_distribution<double> doubleDistr(0.0, 1.0);
  74. //GenerateData
  75. for (size_t i = 0; i < N; i++) {
  76. double rnd = doubleDistr(gen);
  77. for (size_t j = 0; j < D; j++) {
  78. X[i * D + j] = (doubleDistr(gen) < rnd ? 1.0 : 0.0);
  79. }
  80. }
  81. if (N < 20) {
  82. for (size_t i = 0; i < N; i++) {
  83. std::cout << "[";
  84. for (size_t j = 0; j < D; j++) {
  85. std::cout << (X[i * D + j] != 0.0 ? "1" : "0");
  86. }
  87. std::cout << "]" << std::endl;
  88. }
  89. }
  90. }
  91. double euclidenDistance( double* y1, double* y2, int D) {
  92. double sum = 0.0;
  93. std::cout << "[";
  94. for (size_t j = 0; j < D; j++) {
  95. std::cout << (y1[j] != 0.0 ? "1" : "0");
  96. }
  97. std::cout << "]" << std::endl;
  98. std::cout << "[";
  99. for (size_t j = 0; j < D; j++) {
  100. std::cout << (y2[j] != 0.0 ? "1" : "0");
  101. }
  102. std::cout << "]" << std::endl;
  103. for (int i = 0; i < D; i++) {
  104. sum += (y1[i] - y2[i]) * (y1[i] - y2[i]);
  105. }
  106. return std::sqrt(sum);
  107. }