You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. ███████╗██████╗ ██████╗ ███████╗ ███████╗███╗ ██╗ ██████╗ ██╗███╗ ██╗███████╗
  3. ██╔════╝██╔══██╗██╔════╝ ██╔════╝ ██╔════╝████╗ ██║██╔════╝ ██║████╗ ██║██╔════╝
  4. █████╗ ██║ ██║██║ ███╗█████╗ █████╗ ██╔██╗ ██║██║ ███╗██║██╔██╗ ██║█████╗
  5. ██╔══╝ ██║ ██║██║ ██║██╔══╝ ██╔══╝ ██║╚██╗██║██║ ██║██║██║╚██╗██║██╔══╝
  6. ███████╗██████╔╝╚██████╔╝███████╗ ███████╗██║ ╚████║╚██████╔╝██║██║ ╚████║███████╗
  7. ╚══════╝╚═════╝ ╚═════╝ ╚══════╝ ╚══════╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝╚═╝ ╚═══╝╚══════╝
  8. Author:Edge
  9. Web:likedge.top
  10. Date:20200925
  11. */
  12. #include<iostream>
  13. #include<ctime>
  14. #include<string>
  15. #include <time.h>
  16. #include <math.h>
  17. #include <fstream>
  18. #include "./autodiff/node.h"
  19. #include"./matrix/matrix_def.h"
  20. #include"./matrix/matrix_pro.h"
  21. #include"./welcome/score_wel.cpp"
  22. #include"./logistic/logistic_def.h"
  23. #include"./file_pro/data_read.h"
  24. #include"./grad_edge/matrix_grad.h"
  25. #include"./data_struct/data_struct_pro.h"
  26. using namespace std;
  27. clock_t start, stop;
  28. double duration;
  29. Node z=1;
  30. Node t1 = 1,a13 = 1;
  31. /*自定义的损失函数*/
  32. Node loss_act(Node t1,Node a13)
  33. {
  34. Node loss = 0.5*(pow((t1-a13),2));
  35. return loss;
  36. }
  37. Node sigmoid_act(Node z)
  38. {
  39. Node sigmoid_act = 1/(1+(1/exp(z)));
  40. return sigmoid_act;
  41. }
  42. //loss
  43. Node (*loss)(Node,Node) = loss_act;
  44. Node (*act)(Node) = sigmoid_act;
  45. int main()
  46. {
  47. Matrix data_1 = ones(1,3);
  48. Matrix data_2 = ones(3,1);
  49. cout_mat(mul(data_1,data_2));
  50. welcome();
  51. //begin
  52. cout<<"auto build on gcc"<<endl;
  53. cout<<"---------autodiff for neraul network-----------"<<endl;
  54. Matrix data_mine = ones(3,3);
  55. cout<<"data mine"<<endl;
  56. cout_mat(data_mine);
  57. cout<<"data mine"<<endl;
  58. Matrix label = CreateRandMat(3,1);
  59. cout_mat(label);
  60. Matrix weight1 = CreateRandMat(3,3);
  61. cout<<"weight"<<endl;
  62. cout_mat(weight1);
  63. Matrix bais1 = ones(3,1);
  64. cout_mat(bais1);
  65. Matrix weight2 = CreateRandMat(3,3);
  66. Matrix bais2 = ones(3,1);
  67. for(int epoch = 0;epoch<10;epoch++)
  68. {
  69. cout<<"---------epoch: "<<epoch<<"------------"<<endl;
  70. cout_mat(weight1);
  71. int input_dim = 3;
  72. int output_dim = 3;
  73. edge_network sequaltial(input_dim,output_dim);
  74. // define the network
  75. Matrix output1 = sequaltial.forward(data_mine,weight1,bais1);
  76. Matrix output1_without_act = sequaltial.forward_without_act(data_mine,weight1,bais1);
  77. // layer1
  78. Matrix output2 = sequaltial.forward(output1,weight2,bais2);
  79. Matrix output2_without_act = sequaltial.forward_without_act(output1,weight2,bais2);
  80. // layer2
  81. Matrix output_end = sequaltial.end_layer_backward(label,output2_without_act,*loss,*act);
  82. // layer3
  83. Matrix backward3 = sequaltial.backward(output_end,output1_without_act,weight2,*act);
  84. // bp
  85. Matrix weight_2_grad = mul(output_end,get_T(output1));
  86. Matrix weight_1_grad = mul(backward3,get_T(data_mine));
  87. weight1 = subtract(weight1,times_mat(0.001,weight_1_grad));
  88. bais1 = subtract(bais1,times_mat(0.001,backward3));
  89. weight2 = subtract(weight2,times_mat(0.001,weight_2_grad));
  90. bais2 = subtract(bais2,times_mat(0.001,output_end));
  91. cout<<"neraul end;"<<endl;
  92. }
  93. return 0;
  94. }

Edge : 一个开源的科学计算引擎