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

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