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.

README.md 15 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <div align=center><img src="https://github.com/AllenZYJ/Edge-Computing-Engine/blob/debian-gcc10-test/picture/01.svg"/></div>
  2. # Edge-Engine
  3. ## Edge : 一个开源的科学计算引擎
  4. [README for English_version](./README_EN.md)
  5. 声明:本项目禁止闭源商用,如有需要请和作者取得联系
  6. email: zk@likedge.top
  7. <br>[![GitHub license](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://raw.githubusercontent.com/tesseract-ocr/tesseract/master/LICENSE)</br>
  8. ------
  9. > 项目开始日期 : 2019/10/01
  10. >
  11. > 目前项目总代码 : 810 行
  12. >
  13. > 测试 : main.cpp | nerual_network.cpp | 新增全连接神经网络架构(新增全连接网络正向传播和反向传播的测试demo)
  14. >
  15. > 测试环境:
  16. >
  17. > MacBook Pro
  18. >
  19. > 编译器环境:
  20. >
  21. > Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
  22. > Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  23. > Target: x86_64-apple-darwin18.7.0
  24. > Thread model: posix
  25. <img src="https://github.com/AllenZYJ/Edge-Computing-Engine/blob/debian-gcc10-test/image-20200418210521131.png" alt="image-20200418210521131" />
  26. ------
  27. 这是什么?
  28. <img src="https://github.com/AllenZYJ/Edge-Computing-Engine/blob/debian-gcc10-test/picture/path.png" alt="image-20200418210521131" />
  29. ## 安装编译
  30. ```
  31. git clone git@github.com:AllenZYJ/Edge-Computing-Engine.git
  32. cd to install_diff
  33. ```
  34. 进入install_diff目录:
  35. 执行
  36. ```
  37. make
  38. make install
  39. ```
  40. 编译demo入口程序
  41. ```shell
  42. ➜ edge-computing-engine git:(master) ✗ g++ main.cpp -o ma -lautodiff
  43. ```
  44. 或者BP测试程序
  45. ```shell
  46. ➜ edge-computing-engine git:(master) ✗ g++ nerual_network.cpp -o ma
  47. ```
  48. 运行
  49. ```shell
  50. ➜ edge-computing-engine git:(master) ✗ ./main
  51. ```
  52. 最新卷积实现:
  53. ```c++
  54. double conv_test(Matrix mid1,int input_dim = 3,int output_channels = 3,int stride = 1,int kernel_size = 2,int mode = 0,int padding = 0)
  55. ```
  56. 序贯模型api使用方法:
  57. edge_network(int input, int num_neuron)
  58. 作为序列模型api
  59. edge_network作为一个类型存在,位于matrix_grad.h中结构体类型的数据
  60. 定义了前向传播函数,前向传播无激活版,反向传播,末层反向传播,四大最常用的函数主体.
  61. 完整的序列模型:
  62. <img src="https://github.com/AllenZYJ/Edge-Computing-Engine/raw/conv_review/image-20200128154352842.png" alt="image-20200128154352842">
  63. ## 新的demo程序实现5层全连接层,可自定义神经元和激活函数,损失函数
  64. 全连接层使用方法:
  65. 第一层的权重自定义,而后调用forward函数前向传播一层,自动求出激活以后的值,激活函数可自定义.
  66. 首先定义一个权重矩阵和偏置矩阵,第一个矩阵的维度大小使用数据列去定义:
  67. ```c
  68. Matrix bias1 = CreateRandMat(2,1);
  69. Matrix weight1 = CreateRandMat(2,data.col);
  70. ```
  71. 之后可以输出第一层前向传播的值,同时可以定义下一层的bias的维度, row使用第一层的权重矩阵的行,第二层的权重矩阵的行使用了第一层的输出的行, 而列自行定义即可, 这一点体现了前向传播算法的维度相容. 也就是:
  72. ```c
  73. Matrix output1 = sequaltial.forward(get_T(get_row(data_mine,index)),weight1,bias1);
  74. ```
  75. ```c
  76. Matrix weight2 = CreateRandMat(output1.row,2);
  77. Matrix bias2 = CreateRandMat(weight2.row,1);
  78. Matrix output2 = sequaltial.forward(output1,weight2,bias2);
  79. ```
  80. 同时第二层的输出也可以求出来,以此类推 .
  81. 最终输出代码见nerual_test.cpp ![nerual_test1](/Users/zhangyiji/Documents/code/cpp_demo/my_os/Edge-Computing-Engine/picture/nerual_test1.png)
  82. 代码:
  83. ```c
  84. Matrix data_mine = CreateRandMat(2,1);
  85. Matrix label = CreateMatrix(2,1);
  86. Matrix weight1 = CreateRandMat(2,2);
  87. Matrix weight2 = CreateRandMat(2,2);
  88. Matrix weight3 = CreateRandMat(2,2);
  89. Matrix weight4 = CreateRandMat(2,2);
  90. for(int epoch = 0;epoch<20;epoch++)
  91. {
  92. cout_mat(weight1);
  93. edge_network sequaltial(2,2);
  94. Matrix output1 = sequaltial.forward(data_mine,weight1);
  95. Matrix output2 = sequaltial.forward(output1,weight2);
  96. Matrix output3 = sequaltial.forward(output2,weight3);
  97. Matrix output4 = sequaltial.forward(output3,weight4);
  98. Matrix output_end = sequaltial.end_layer_backward(label,output4);
  99. //get the forward
  100. Matrix backward1 = sequaltial.backward(output_end,output3,weight4);
  101. Matrix grad_w1w2 = mul_simple(backward1,data_mine);
  102. Matrix backward2 = sequaltial.backward(backward1,output2,weight3);
  103. Matrix grad_w3w4 = mul_simple(backward2,data_mine);
  104. Matrix backward3 = sequaltial.backward(backward2,output1,weight2);
  105. Matrix grad_w5w6 = mul_simple(backward3,data_mine);
  106. Matrix backward4 = sequaltial.backward(backward3,output4,weight1);
  107. Matrix grad_w7w8 = mul_simple(backward4,data_mine);
  108. weight1 = subtract(weight1,times_mat(0.0001,padding(grad_w1w2,2,2)));
  109. weight2 = subtract(weight2,times_mat(0.0001,padding(grad_w3w4,2,2)));
  110. weight3 = subtract(weight3,times_mat(0.0001,padding(grad_w5w6,2,2)));
  111. weight4 = subtract(weight4,times_mat(0.0001,padding(grad_w7w8,2,2)));
  112. }
  113. ```
  114. ```shell
  115. ---------epoch: 0------------
  116. loss: 4.65667
  117. loss: 3.28273
  118. ---------epoch: 1------------
  119. loss: 4.65655
  120. loss: 3.28265
  121. ---------epoch: 2------------
  122. loss: 4.65643
  123. loss: 3.28257
  124. ---------epoch: 3------------
  125. loss: 4.65631
  126. loss: 3.28249
  127. ---------epoch: 4------------
  128. loss: 4.65619
  129. loss: 3.2824
  130. ---------epoch: 5------------
  131. loss: 4.65607
  132. loss: 3.28232
  133. ---------epoch: 6------------
  134. loss: 4.65596
  135. loss: 3.28224
  136. ---------epoch: 7------------
  137. loss: 4.65584
  138. loss: 3.28216
  139. ---------epoch: 8------------
  140. loss: 4.65572
  141. loss: 3.28208
  142. ---------epoch: 9------------
  143. loss: 4.6556
  144. loss: 3.282
  145. ---------epoch: 10------------
  146. loss: 4.65548
  147. loss: 3.28192
  148. ---------epoch: 11------------
  149. loss: 4.65536
  150. loss: 3.28184
  151. ---------epoch: 12------------
  152. loss: 4.65524
  153. loss: 3.28176
  154. ---------epoch: 13------------
  155. loss: 4.65512
  156. loss: 3.28168
  157. ---------epoch: 14------------
  158. loss: 4.65501
  159. loss: 3.2816
  160. ---------epoch: 15------------
  161. loss: 4.65489
  162. loss: 3.28152
  163. ---------epoch: 16------------
  164. loss: 4.65477
  165. loss: 3.28144
  166. ---------epoch: 17------------
  167. loss: 4.65465
  168. loss: 3.28136
  169. ---------epoch: 18------------
  170. loss: 4.65453
  171. loss: 3.28128
  172. ---------epoch: 19------------
  173. loss: 4.65441
  174. loss: 3.2812
  175. ```
  176. ## Bp反向传播的demo程序基于Pytorch官方代码模拟实现测试
  177. 迭代结果 :
  178. W1: 0.6944 1.52368
  179. -1.46644 -0.154097
  180. W2: 1.10079
  181. 0.462984
  182. loss: 0.559269
  183. epoch:100 , 可自行测试.
  184. 输出最终损失和参数迭代结果.
  185. -----------split-line-----------
  186. 2.79955
  187. 0.36431
  188. -0.451694
  189. epoch: 100 error: 6.05895
  190. -----------split-line-----------
  191. 0.009167(sum of loss)
  192. ### 目前实现的程序接口
  193. ### API:
  194. - [x] Matrix read_csv(string &file_path)读取格式化文件(csv),返回一个自动计算长度的矩阵.
  195. - [x] 实现格式化文件写入接口.比较pandas.to_csv.
  196. - [x] 矩阵广播机制,实现padding接口
  197. - [x] 全连接层前向传播和反向传播接口,支持自动求导
  198. - [x] 矩阵微分和自动求导接口封装
  199. - [x] int save_txt(Matrix mid1,string path = "./",string delimiter = ",",string header="./") 设计文件流获取文件头部接口 , 写入格式化文件 , 已设计支持矩阵类型数据写入,支持自定义表头,写入文件路径 , 自定义分隔符,默认为" , ".
  200. - [x] Create a matrix : create(row,cols)开辟一个矩阵结构的内存,元素初值为0;
  201. - [x] Change the element for matrix void move_ele(int &ele1, int &ele2),修改某一个位置的元素的值.
  202. - [x] Matrix1+Matrix2 : Matrix add(Matrix mid1,Matrix mid2,int flag=1),矩阵加和操作接口,可选位运算加速.
  203. - [x] Flag is how to compete the ele ,default 1 ,bitwise operation(位运算加速).
  204. - [x] Matrix1-Matrix2 : Matrix subtract(Matrix mid1,Matrix mid2)
  205. - [x] Matrix1*Matrix2 : Matrix mul(Matrix mid1,Matrix mid2)
  206. - [x] Matrix1*n : Matrix times_mat(int times,Matrix mid1)
  207. - [x] Matrix1's Transposition : Matrix get_T(Matrix mid1)矩阵转置
  208. - [x] Mul(matrix1,matrix2)矩阵乘积(完整数学定义).
  209. - [x] double* flatten(Matrix mid1) : Return a flattened array.矩阵展开
  210. - [x] Matrix matrix_rs(Matrix mid1,int rs_row,int rs_col) 矩阵的结构压缩
  211. - [x] double matrix_sum(Matrix mid1)矩阵求和
  212. - [x] double matrix_mean(Matrix mid1)均值
  213. - [x] Matrix appply(Matrix mid1,Matrix mid2,int axis = 0)矩阵拼接
  214. - [x] Matrix iloc(Matrix mid1,int start_x=0,int end_x=0,int start_y=0,int end_y=0)矩阵切片
  215. - [x] Matrix mul_simple(Matrix mid1,Matrix mid2)为了贴合机器学习的需要,实现了矩阵对应元素相乘,请与传统意义的矩阵乘法区分开.
  216. - [x] Relu激活函数矩阵接口
  217. - [x] 均方误差矩阵接口
  218. - [x] 创建随机权重矩阵接口
  219. ### 即将着手开发:
  220. - [ ] 卷积神经网络定义(包括但不限于卷积核,池化层定义,自定义损失接口).
  221. - [ ] 随机森林算法封装.
  222. - [ ] 主流网络架构实现.
  223. ## 反向传播测试demo:
  224. ```c
  225. #include<iostream>
  226. #include<ctime>
  227. #include<string>
  228. #include<time.h>
  229. #include<math.h>
  230. #include<fstream>
  231. #include<stdlib.h>
  232. #include"./matrix/matrix_def.h"
  233. #include"./matrix/matrix_pro.h"
  234. #include"./welcome/score_wel.cpp"
  235. #include"./logistic/logistic_def.h"
  236. #include"./file_pro/data_read.h"
  237. using namespace std;
  238. clock_t start, stop;
  239. double duration;
  240. int main()
  241. {
  242. welcome();
  243. string path = "./data/nerual_data.csv";
  244. Matrix data = read_csv(path);
  245. Matrix bais = CreateMatrix(data.row,1);
  246. Matrix x = iloc(data,0,100,0,2);
  247. Matrix y = iloc(data,0,100,2,3);
  248. int N=100,in_Dim=2,H_num=2,out_Dim=2;
  249. double learning_rate = 0.0001;
  250. Matrix W1 = CreateRandMat(in_Dim,H_num);
  251. Matrix W2 = CreateRandMat(H_num,out_Dim);
  252. cout_mat(W1);
  253. cout_mat(W2);
  254. for(int epoch = 0;epoch<100;epoch++)
  255. {
  256. Matrix x_w1 = mul(x,W1);
  257. Matrix re = mat_relu(x_w1);
  258. Matrix out = mul(re,W2);
  259. Matrix mat_sq = mat_sq_loss(out,y);
  260. Matrix grad_y_pred = times_mat(2.0,subtract(out,y));
  261. Matrix grad_w2 = mul(get_T(re),grad_y_pred);
  262. Matrix grad_h_relu = mul(grad_y_pred,get_T(W2));
  263. Matrix grad_h_relu_copy = mat_relu(grad_h_relu);
  264. Matrix grad_w1 = mul(get_T(x),grad_h_relu_copy);
  265. Matrix dw1 = times_mat(learning_rate,mul(get_T(x),grad_h_relu_copy));
  266. W1 = subtract(W1,dw1);
  267. W2 = subtract(W2,times_mat(learning_rate,grad_w2));
  268. cout<<"W1: ";
  269. cout_mat(W1);
  270. cout<<"W2: ";
  271. cout_mat(W2);
  272. cout<<"loss"<<": ";
  273. cout<<matrix_sum(mat_sq)/100<<endl;
  274. }
  275. }
  276. ```
  277. ## 演示:矩阵乘法
  278. Matrix **A**:
  279. | 第1列 | 第2列 | 第3列 | 第4列 | 第5列 |
  280. | ------- | ------- | ------- | ------- | ------- |
  281. | 72.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
  282. | 0.0000 | 64.0000 | 0.0000 | 0.0000 | 0.0000 |
  283. | 16.0000 | 8.0000 | 0.0000 | 0.0000 | 0.0000 |
  284. | 0.0000 | 0.0000 | 56.0000 | 16.0000 | 32.0000 |
  285. | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
  286. | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
  287. MAtrix **B**:
  288. | 第1列 | 第2列 | 第3列 | 第4列 | 第5列 | 第6列 |
  289. | ------- | ------- | ------- | ------- | ------ | ------ |
  290. | 72.0000 | 0.0000 | 16.0000 | 0.0000 | 0.0000 | 0.0000 |
  291. | 0.0000 | 64.0000 | 8.0000 | 0.0000 | 0.0000 | 0.0000 |
  292. | 0.0000 | 0.0000 | 0.0000 | 56.0000 | 0.0000 | 0.0000 |
  293. | 0.0000 | 0.0000 | 0.0000 | 16.0000 | 0.0000 | 0.0000 |
  294. | 0.0000 | 0.0000 | 0.0000 | 32.0000 | 0.0000 | 0.0000 |
  295. To
  296. | 第1列 | 第2列 | 第3列 | 第4列 | 第5列 | 第6列 |
  297. | --------- | --------- | --------- | --------- | ------ | ------ |
  298. | 5184.0000 | 0.0000 | 1152.0000 | 0.0000 | 0.0000 | 0.0000 |
  299. | 0.0000 | 4096.0000 | 512.0000 | 0.0000 | 0.0000 | 0.0000 |
  300. | 1152.0000 | 512.0000 | 320.0000 | 0.0000 | 0.0000 | 0.0000 |
  301. | 0.0000 | 0.0000 | 0.0000 | 4416.0000 | 0.0000 | 0.0000 |
  302. | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
  303. | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
  304. ## 演示: 矩阵展开(flatten).
  305. double* flatten(Matrix mid1)
  306. | 1 | 2 | 3 |
  307. | :--: | :--: | :--: |
  308. | 2 | 4 | 6 |
  309. | 7 | 8 | 9 |
  310. ​ To
  311. | 1 | 2 | 3 | 2 | 4 | 6 | 7 | 8 | 9 |
  312. | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | :----------------- |
  313. | | | | | | | | | Like numpy.flatten |
  314. function:
  315. ## 演示: 邻接矩阵的参数定义:
  316. ​ Matrix appply(Matrix mid1,Matrix mid2,int axis = 0)
  317. > 参数 axis=0 :
  318. | 0 | 7 | 2 |
  319. | ---- | ---- | ---- |
  320. | 0 | 3 | 1 |
  321. | 0 | 0 | 0 |
  322. | 0 | 0 | 11 |
  323. | 0 | 7 | 2 |
  324. | 0 | 3 | 1 |
  325. | 0 | 0 | 0 |
  326. | 0 | 0 | 11 |
  327. ------
  328. > axis = 1:
  329. | 0 | 7 | 2 | 0 | 7 | 2 |
  330. | ---- | ---- | ---- | ---- | ---- | ---- |
  331. | 0 | 3 | 1 | 0 | 3 | 1 |
  332. | 0 | 0 | 0 | 0 | 0 | 0 |
  333. | 0 | 0 | 11 | 0 | 0 | 11 |
  334. ------
  335. ## 更新2019/11/18/00:12
  336. - [x] read_csv 通过文件流读取逗号分隔符文件,返回一个自动计算长度的矩阵.
  337. 例如 CSV's head :
  338. | -0.017612 | 14.053064 | 0 |
  339. | --------- | --------- | ---- |
  340. | -1.395634 | 4.662541 | 1 |
  341. | -0.752157 | 6.53862 | 0 |
  342. | -1.322371 | 7.152853 | 0 |
  343. | 0.423363 | 11.054677 | 0 |
  344. | 0.406704 | 7.067335 | 1 |
  345. Get:
  346. <img src="https://github.com/AllenZYJ/Edge-Computing-Engine/blob/debian-gcc10-test/picture/WX20191119-105411@2x.png" alt="image-20200418210521131" />
  347. ## Logistic Regression demo base Edge:
  348. ```c
  349. #include<iostream>
  350. #include<ctime>
  351. #include<string>
  352. #include <time.h>
  353. #include <math.h>
  354. #include <fstream>
  355. #include"./matrix/matrix_def.h"
  356. #include"./matrix/matrix_pro.h"
  357. #include"./welcome/score_wel.cpp"
  358. #include"./logistic/logistic_def.h"
  359. #include"./file_pro/data_read.h"
  360. using namespace std;
  361. clock_t start, stop;
  362. double duration;
  363. int main()
  364. {
  365. welcome();
  366. string path = "./new_data2.csv";
  367. Matrix data = read_csv(path);
  368. Matrix bais = CreateMatrix(data.row,1);
  369. data = appply(data,bais,1);
  370. Matrix y = iloc(data,0,0,3,4);
  371. Matrix x_1 = iloc(data,0,0,0,3);
  372. Matrix x_2 = get_T(x_1);
  373. double alpha = 0.002;
  374. int max_epoch = 100;
  375. Matrix weight = CreateMatrix(3,1);
  376. change_va(weight,0,0,1);
  377. change_va(weight,1,0,1);
  378. change_va(weight,2,0,1);
  379. int epoch = 0;
  380. for(epoch = 0;epoch<=max_epoch;epoch++)
  381. {
  382. cout<<"-----------split-line-----------"<<endl;
  383. Matrix temp_mul = mul(x_1,weight);
  384. Matrix h =e_sigmoid(temp_mul);
  385. Matrix error = subtract(y,h);
  386. Matrix temp_update = mul(x_2,error);
  387. Matrix updata = add(weight,times_mat(alpha,temp_update),0);
  388. cout_mat(weight);
  389. cout<<"epoch: "<<epoch<<" error: "<<matrix_sum(error)<<endl;
  390. cout<<"-----------split-line-----------"<<endl;
  391. }
  392. stop = clock();
  393. printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);
  394. return 0;
  395. }
  396. ```
  397. Something :
  398. > 1. 矩阵元素默认为1
  399. > 2. 使用位运算加速防止填充过大的数值,但是会损失一定精度,慎用.
  400. > 3. 记得delete(matrix)在你使用完一个矩阵计算单元以后.
  401. > 4. api接口更多的接近于pandas和numpy的使用习惯.
  402. > 5. 更多的细节参见目前最新的代码
  403. > 6. 欢迎star和关注.
  404. > 7. autodiff部分感谢国外博主Omar的思路提醒.
  405. >
  406. ------
  407. 个人小站:[极度空间](http://likedge.top/)
  408. 作者邮箱:zk@likedge.top | edge@ibooker.org.cn
  409. QQ:2533524298

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