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

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

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