具体原理参考如下讲义:

1、神经网络

2、反向传导

3、梯度检验与高级优化

看完材料1和2就可以梳理清楚bp神经网络的基本工作原理,下面通过一个C语言实现的程序来练习这个算法

  1. //Backpropagation, 25x25x8 units, binary sigmoid function network
  2. //Written by Thomas Riga, University of Genoa, Italy
  3. //thomas@magister.magi.unige.it
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <conio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include <float.h>
  13. using namespace std;
  14.  
  15. double **input,
  16. *hidden,
  17. **output,
  18. **target,
  19. *bias,
  20. **weight_i_h,
  21. **weight_h_o,
  22. *errorsignal_hidden,
  23. *errorsignal_output;
  24.  
  25. int input_array_size,
  26. hidden_array_size,
  27. output_array_size,
  28. max_patterns,
  29. bias_array_size,
  30. gaset = -,
  31. number_of_input_patterns,
  32. pattern,
  33. file_loaded = ,
  34. ytemp = ,
  35. ztemp = ;
  36. double learning_rate,
  37. max_error_tollerance = 0.1;
  38. char filename[];
  39. #define IA 16807
  40. #define IM 2147483647
  41. #define AM (1.0 / IM)
  42. #define IQ 127773
  43. #define IR 2836
  44. #define NTAB 32
  45. #define NDIV (1+(IM-1) / NTAB)
  46. #define EPS 1.2e-7
  47. #define RNMX (1.0 - EPS)
  48. int compare_output_to_target();
  49. void load_data(char *arg);
  50. void save_data(char *argres);
  51. void forward_pass(int pattern);
  52. void backward_pass(int pattern);
  53. void custom();
  54. void compute_output_pattern();
  55. void get_file_name();
  56. float bedlam(long *idum);
  57. void learn();
  58. void make();
  59. void test();
  60. void print_data();
  61. void print_data_to_screen();
  62. void print_data_to_file();
  63. void output_to_screen();
  64. int getnumber();
  65. void change_learning_rate();
  66. void initialize_net();
  67. void clear_memory();
  68.  
  69. int main()
  70. {
  71. cout << "backpropagation network by Thomas Riga, University of Genoa, Italy" << endl;
  72. for(;;) {
  73. char choice;
  74. cout << endl << "1. load data" << endl;
  75. cout << "2. learn from data" << endl;
  76. cout << "3. compute output pattern" << endl;
  77. cout << "4. make new data file" << endl;
  78. cout << "5. save data" << endl;
  79. cout << "6. print data" << endl;
  80. cout << "7. change learning rate" << endl;
  81. cout << "8. exit" << endl << endl;
  82. cout << "Enter your choice (1-8)";
  83. do { choice = getch(); } while (choice != '' && choice != '' && choice != '' && choice != '' && choice != '' && choice != '' && choice != '' && choice != '');
  84. switch(choice) {
  85. case '':
  86. {
  87. if (file_loaded == ) clear_memory();
  88. get_file_name();
  89. file_loaded = ;
  90. load_data(filename);
  91. }
  92. break;
  93. case '': learn();
  94. break;
  95. case '': compute_output_pattern();
  96. break;
  97. case '': make();
  98. break;
  99. case '':
  100. {
  101. if (file_loaded == )
  102. {
  103. cout << endl
  104. << "there is no data loaded into memory"
  105. << endl;
  106. break;
  107. }
  108. cout << endl << "enter a filename to save data to: ";
  109. cin >> filename;
  110. save_data(filename);
  111. }
  112. break;
  113. case '': print_data();
  114. break;
  115. case '': change_learning_rate();
  116. break;
  117. case '': return ;
  118. };
  119. }
  120. }
  121.  
  122. void initialize_net()
  123. {
  124. int x;
  125. input = new double * [number_of_input_patterns];
  126. if(!input) { cout << endl << "memory problem!"; exit(); }
  127. for(x=; x<number_of_input_patterns; x++)
  128. {
  129. input[x] = new double [input_array_size];
  130. if(!input[x]) { cout << endl << "memory problem!"; exit(); }
  131. }
  132. hidden = new double [hidden_array_size];
  133. if(!hidden) { cout << endl << "memory problem!"; exit(); }
  134. output = new double * [number_of_input_patterns];
  135. if(!output) { cout << endl << "memory problem!"; exit(); }
  136. for(x=; x<number_of_input_patterns; x++)
  137. {
  138. output[x] = new double [output_array_size];
  139. if(!output[x]) { cout << endl << "memory problem!"; exit(); }
  140. }
  141. target = new double * [number_of_input_patterns];
  142. if(!target) { cout << endl << "memory problem!"; exit(); }
  143. for(x=; x<number_of_input_patterns; x++)
  144. {
  145. target[x] = new double [output_array_size];
  146. if(!target[x]) { cout << endl << "memory problem!"; exit(); }
  147. }
  148. bias = new double [bias_array_size];
  149. if(!bias) { cout << endl << "memory problem!"; exit(); }
  150. weight_i_h = new double * [input_array_size];
  151. if(!weight_i_h) { cout << endl << "memory problem!"; exit(); }
  152. for(x=; x<input_array_size; x++)
  153. {
  154. weight_i_h[x] = new double [hidden_array_size];
  155. if(!weight_i_h[x]) { cout << endl << "memory problem!"; exit(); }
  156. }
  157. weight_h_o = new double * [hidden_array_size];
  158. if(!weight_h_o) { cout << endl << "memory problem!"; exit(); }
  159. for(x=; x<hidden_array_size; x++)
  160. {
  161. weight_h_o[x] = new double [output_array_size];
  162. if(!weight_h_o[x]) { cout << endl << "memory problem!"; exit(); }
  163. }
  164. errorsignal_hidden = new double [hidden_array_size];
  165. if(!errorsignal_hidden) { cout << endl << "memory problem!"; exit(); }
  166. errorsignal_output = new double [output_array_size];
  167. if(!errorsignal_output) { cout << endl << "memory problem!"; exit(); }
  168. return;
  169. }
  170.  
  171. void learn()
  172. {
  173. if (file_loaded == )
  174. {
  175. cout << endl
  176. << "there is no data loaded into memory"
  177. << endl;
  178. return;
  179. }
  180. cout << endl << "learning..." << endl << "press a key to return to menu" << endl;
  181. register int y;
  182. while(!kbhit()) {
  183. for(y=; y<number_of_input_patterns; y++) {
  184. forward_pass(y);
  185. backward_pass(y);
  186. }
  187. if(compare_output_to_target()) {
  188. cout << endl << "learning successful" << endl;
  189. return;
  190. }
  191.  
  192. }
  193. cout << endl << "learning not successful yet" << endl;
  194. return;
  195. }
  196.  
  197. void load_data(char *arg) {
  198. int x, y;
  199. ifstream in(arg);
  200. if(!in) { cout << endl << "failed to load data file" << endl; file_loaded = ; return; }
  201. in >> input_array_size;
  202. in >> hidden_array_size;
  203. in >> output_array_size;
  204. in >> learning_rate;
  205. in >> number_of_input_patterns;
  206. bias_array_size = hidden_array_size + output_array_size;
  207. initialize_net();
  208. for(x = ; x < bias_array_size; x++) in >> bias[x];
  209. for(x=; x<input_array_size; x++) {
  210. for(y=; y<hidden_array_size; y++) in >> weight_i_h[x][y];
  211. }
  212. for(x = ; x < hidden_array_size; x++) {
  213. for(y=; y<output_array_size; y++) in >> weight_h_o[x][y];
  214. }
  215. for(x=; x < number_of_input_patterns; x++) {
  216. for(y=; y<input_array_size; y++) in >> input[x][y];
  217. }
  218. for(x=; x < number_of_input_patterns; x++) {
  219. for(y=; y<output_array_size; y++) in >> target[x][y];
  220. }
  221. in.close();
  222. cout << endl << "data loaded" << endl;
  223. return;
  224. }
  225.  
  226. void forward_pass(int pattern)
  227. {
  228. _control87(MCW_EM, MCW_EM);
  229. register double temp=;
  230. register int x,y;
  231.  
  232. // INPUT -> HIDDEN
  233. for(y=; y<hidden_array_size; y++) {
  234. for(x=; x<input_array_size; x++) {
  235. temp += (input[pattern][x] * weight_i_h[x][y]);
  236. }
  237. hidden[y] = (1.0 / (1.0 + exp(-1.0 * (temp + bias[y]))));
  238. temp = ;
  239. }
  240.  
  241. // HIDDEN -> OUTPUT
  242. for(y=; y<output_array_size; y++) {
  243. for(x=; x<hidden_array_size; x++) {
  244. temp += (hidden[x] * weight_h_o[x][y]);
  245. }
  246. output[pattern][y] = (1.0 / (1.0 + exp(-1.0 * (temp + bias[y + hidden_array_size]))));
  247. temp = ;
  248. }
  249. return;
  250. }
  251.  
  252. void backward_pass(int pattern)
  253. {
  254. register int x, y;
  255. register double temp = ;
  256.  
  257. // COMPUTE ERRORSIGNAL FOR OUTPUT UNITS
  258. for(x=; x<output_array_size; x++) {
  259. errorsignal_output[x] = (target[pattern][x] - output[pattern][x]);
  260. }
  261.  
  262. // COMPUTE ERRORSIGNAL FOR HIDDEN UNITS
  263. for(x=; x<hidden_array_size; x++) {
  264. for(y=; y<output_array_size; y++) {
  265. temp += (errorsignal_output[y] * weight_h_o[x][y]);
  266. }
  267. errorsignal_hidden[x] = hidden[x] * (-hidden[x]) * temp;
  268. temp = 0.0;
  269. }
  270.  
  271. // ADJUST WEIGHTS OF CONNECTIONS FROM HIDDEN TO OUTPUT UNITS
  272. double length = 0.0;
  273. for (x=; x<hidden_array_size; x++) {
  274. length += hidden[x]*hidden[x];
  275. }
  276. if (length<=0.1) length = 0.1;
  277. for(x=; x<hidden_array_size; x++) {
  278. for(y=; y<output_array_size; y++) {
  279. weight_h_o[x][y] += (learning_rate * errorsignal_output[y] *
  280. hidden[x]/length);
  281. }
  282. }
  283.  
  284. // ADJUST BIASES OF HIDDEN UNITS
  285. for(x=hidden_array_size; x<bias_array_size; x++) {
  286. bias[x] += (learning_rate * errorsignal_output[x] / length);
  287. }
  288.  
  289. // ADJUST WEIGHTS OF CONNECTIONS FROM INPUT TO HIDDEN UNITS
  290. length = 0.0;
  291. for (x=; x<input_array_size; x++) {
  292. length += input[pattern][x]*input[pattern][x];
  293. }
  294. if (length<=0.1) length = 0.1;
  295. for(x=; x<input_array_size; x++) {
  296. for(y=; y<hidden_array_size; y++) {
  297. weight_i_h[x][y] += (learning_rate * errorsignal_hidden[y] *
  298. input[pattern][x]/length);
  299. }
  300. }
  301.  
  302. // ADJUST BIASES FOR OUTPUT UNITS
  303. for(x=; x<hidden_array_size; x++) {
  304. bias[x] += (learning_rate * errorsignal_hidden[x] / length);
  305. }
  306. return;
  307. }
  308.  
  309. int compare_output_to_target()
  310. {
  311. register int y,z;
  312. register double temp, error = 0.0;
  313. temp = target[ytemp][ztemp] - output[ytemp][ztemp];
  314. if (temp < ) error -= temp;
  315. else error += temp;
  316. if(error > max_error_tollerance) return ;
  317. error = 0.0;
  318. for(y=; y < number_of_input_patterns; y++) {
  319. for(z=; z < output_array_size; z++) {
  320. temp = target[y][z] - output[y][z];
  321. if (temp < ) error -= temp;
  322. else error += temp;
  323. if(error > max_error_tollerance) {
  324. ytemp = y;
  325. ztemp = z;
  326. return ;
  327. }
  328. error = 0.0;
  329. }
  330. }
  331. return ;
  332. }
  333.  
  334. void save_data(char *argres) {
  335. int x, y;
  336. ofstream out;
  337. out.open(argres);
  338. if(!out) { cout << endl << "failed to save file" << endl; return; }
  339. out << input_array_size << endl;
  340. out << hidden_array_size << endl;
  341. out << output_array_size << endl;
  342. out << learning_rate << endl;
  343. out << number_of_input_patterns << endl << endl;
  344. for(x=; x<bias_array_size; x++) out << bias[x] << ' ';
  345. out << endl << endl;
  346. for(x=; x<input_array_size; x++) {
  347. for(y=; y<hidden_array_size; y++) out << weight_i_h[x][y] << ' ';
  348. }
  349. out << endl << endl;
  350. for(x=; x<hidden_array_size; x++) {
  351. for(y=; y<output_array_size; y++) out << weight_h_o[x][y] << ' ';
  352. }
  353. out << endl << endl;
  354. for(x=; x<number_of_input_patterns; x++) {
  355. for(y=; y<input_array_size; y++) out << input[x][y] << ' ';
  356. out << endl;
  357. }
  358. out << endl;
  359. for(x=; x<number_of_input_patterns; x++) {
  360. for(y=; y<output_array_size; y++) out << target[x][y] << ' ';
  361. out << endl;
  362. }
  363. out.close();
  364. cout << endl << "data saved" << endl;
  365. return;
  366. }
  367.  
  368. void make()
  369. {
  370. int x, y, z;
  371. double inpx, bias_array_size, input_array_size, hidden_array_size, output_array_size;
  372. char makefilename[];
  373. cout << endl << "enter name of new data file: ";
  374. cin >> makefilename;
  375. ofstream out;
  376. out.open(makefilename);
  377. if(!out) { cout << endl << "failed to open file" << endl; return;}
  378. cout << "how many input units? ";
  379. cin >> input_array_size;
  380. out << input_array_size << endl;
  381. cout << "how many hidden units? ";
  382. cin >> hidden_array_size;
  383. out << hidden_array_size << endl;
  384. cout << "how many output units? ";
  385. cin >> output_array_size;
  386. out << output_array_size << endl;
  387. bias_array_size = hidden_array_size + output_array_size;
  388. cout << endl << "Learning rate: ";
  389. cin >> inpx;
  390. out << inpx << endl;
  391. cout << endl << "Number of input patterns: ";
  392. cin >> z;
  393. out << z << endl << endl;
  394. for(x=; x<bias_array_size; x++) out << (1.0 - (2.0 * bedlam((long*)(gaset)))) << ' ';
  395. out << endl << endl;
  396. for(x=; x<input_array_size; x++) {
  397. for(y=; y<hidden_array_size; y++) out << (1.0 - (2.0 * bedlam((long*)(gaset)))) << ' ';
  398. }
  399. out << endl << endl;
  400. for(x=; x<hidden_array_size; x++) {
  401. for(y=; y<output_array_size; y++) out << (1.0 - (2.0 * bedlam((long*)(gaset)))) << ' ';
  402. }
  403. out << endl << endl;
  404. for(x=; x < z; x++) {
  405. cout << endl << "input pattern " << (x + ) << endl;
  406. for(y=; y<input_array_size; y++) {
  407. cout << (y+) << ": ";
  408. cin >> inpx;
  409. out << inpx << ' ';
  410. }
  411. out << endl;
  412. }
  413. out << endl;
  414. for(x=; x < z; x++) {
  415. cout << endl << "target output pattern " << (x+) << endl;
  416. for(y=; y<output_array_size; y++) {
  417. cout << (y+) << ": ";
  418. cin >> inpx;
  419. out << inpx << ' ';
  420. }
  421. out << endl;
  422. }
  423. out.close();
  424. cout << endl << "data saved, to work with this new data file you first have to load it" << endl;
  425. return;
  426. }
  427.  
  428. float bedlam(long *idum)
  429. {
  430. int xj;
  431. long xk;
  432. static long iy=;
  433. static long iv[NTAB];
  434. float temp;
  435.  
  436. if(*idum <= || !iy)
  437. {
  438. if(-(*idum) < )
  439. {
  440. *idum = + *idum;
  441. }
  442. else
  443. {
  444. *idum = -(*idum);
  445. }
  446. for(xj = NTAB+; xj >= ; xj--)
  447. {
  448. xk = (*idum) / IQ;
  449. *idum = IA * (*idum - xk * IQ) - IR * xk;
  450. if(*idum < )
  451. {
  452. *idum += IM;
  453. }
  454. if(xj < NTAB)
  455. {
  456. iv[xj] = *idum;
  457. }
  458. }
  459. iy = iv[];
  460. }
  461.  
  462. xk = (*idum) / IQ;
  463. *idum = IA * (*idum - xk * IQ) - IR * xk;
  464. if(*idum < )
  465. {
  466. *idum += IM;
  467. }
  468. xj = iy / NDIV;
  469. iy = iv[xj];
  470. iv[xj] = *idum;
  471.  
  472. if((temp=AM*iy) > RNMX)
  473. {
  474. return(RNMX);
  475. }
  476. else
  477. {
  478. return(temp);
  479. }
  480. }
  481.  
  482. void test()
  483. {
  484. pattern = ;
  485. while(pattern == ) {
  486. cout << endl << endl << "There are " << number_of_input_patterns << " input patterns in the file," << endl << "enter a number within this range: ";
  487. pattern = getnumber();
  488. }
  489. pattern--;
  490. forward_pass(pattern);
  491. output_to_screen();
  492. return;
  493. }
  494.  
  495. void output_to_screen()
  496. {
  497. int x;
  498. cout << endl << "Output pattern:" << endl;
  499. for(x=; x<output_array_size; x++) {
  500. cout << endl << (x+) << ": " << output[pattern][x] << " binary: ";
  501. if(output[pattern][x] >= 0.9) cout << "";
  502. else if(output[pattern][x]<=0.1) cout << "";
  503. else cout << "intermediate value";
  504. }
  505. cout << endl;
  506. return;
  507. }
  508.  
  509. int getnumber()
  510. {
  511. int a, b = ;
  512. char c, d[];
  513. while(b<) {
  514. do { c = getch(); } while (c != '' && c != '' && c != '' && c != '' && c != '' && c != '' && c != '' && c != '' && c != '' && c != '' && toascii(c) != );
  515. if(toascii(c)==) break;
  516. if(toascii(c)==) return ;
  517. d[b] = c;
  518. cout << c;
  519. b++;
  520. }
  521. d[b] = '\0';
  522. a = atoi(d);
  523. if(a < || a > number_of_input_patterns) a = ;
  524. return a;
  525. }
  526.  
  527. void get_file_name()
  528. {
  529. cout << endl << "enter name of file to load: ";
  530. cin >> filename;
  531. return;
  532. }
  533.  
  534. void print_data()
  535. {
  536. char choice;
  537. if (file_loaded == )
  538. {
  539. cout << endl
  540. << "there is no data loaded into memory"
  541. << endl;
  542. return;
  543. }
  544. cout << endl << "1. print data to screen" << endl;
  545. cout << "2. print data to file" << endl;
  546. cout << "3. return to main menu" << endl << endl;
  547. cout << "Enter your choice (1-3)" << endl;
  548. do { choice = getch(); } while (choice != '' && choice != '' && choice != '');
  549. switch(choice) {
  550. case '': print_data_to_screen();
  551. break;
  552. case '': print_data_to_file();
  553. break;
  554. case '': return;
  555. };
  556. return;
  557. }
  558.  
  559. void print_data_to_screen() {
  560. register int x, y;
  561. cout << endl << endl << "DATA FILE: " << filename << endl;
  562. cout << "learning rate: " << learning_rate << endl;
  563. cout << "input units: " << input_array_size << endl;
  564. cout << "hidden units: " << hidden_array_size << endl;
  565. cout << "output units: " << output_array_size << endl;
  566. cout << "number of input and target output patterns: " << number_of_input_patterns << endl << endl;
  567. cout << "INPUT AND TARGET OUTPUT PATTERNS:";
  568. for(x=; x<number_of_input_patterns; x++) {
  569. cout << endl << "input pattern: " << (x+) << endl;
  570. for(y=; y<input_array_size; y++) cout << input[x][y] << " ";
  571. cout << endl << "target output pattern: " << (x+) << endl;
  572. for(y=; y<output_array_size; y++) cout << target[x][y] << " ";
  573. }
  574. cout << endl << endl << "BIASES:" << endl;
  575. for(x=; x<hidden_array_size; x++) {
  576. cout << "bias of hidden unit " << (x+) << ": " << bias[x];
  577. if(x<output_array_size) cout << " bias of output unit " << (x+) << ": " << bias[x+hidden_array_size];
  578. cout << endl;
  579. }
  580. cout << endl << "WEIGHTS:" << endl;
  581. for(x=; x<input_array_size; x++) {
  582. for(y=; y<hidden_array_size; y++) cout << "i_h[" << x << "][" << y << "]: " << weight_i_h[x][y] << endl;
  583. }
  584. for(x=; x<hidden_array_size; x++) {
  585. for(y=; y<output_array_size; y++) cout << "h_o[" << x << "][" << y << "]: " << weight_h_o[x][y] << endl;
  586. }
  587. return;
  588. }
  589.  
  590. void print_data_to_file()
  591. {
  592. char printfile[];
  593. cout << endl << "enter name of file to print data to: ";
  594. cin >> printfile;
  595. ofstream out;
  596. out.open(printfile);
  597. if(!out) { cout << endl << "failed to open file"; return; }
  598. register int x, y;
  599. out << endl << endl << "DATA FILE: " << filename << endl;
  600. out << "input units: " << input_array_size << endl;
  601. out << "hidden units: " << hidden_array_size << endl;
  602. out << "output units: " << output_array_size << endl;
  603. out << "learning rate: " << learning_rate << endl;
  604. out << "number of input and target output patterns: " << number_of_input_patterns << endl << endl;
  605. out << "INPUT AND TARGET OUTPUT PATTERNS:";
  606. for(x=; x<number_of_input_patterns; x++) {
  607. out << endl << "input pattern: " << (x+) << endl;
  608. for(y=; y<input_array_size; y++) out << input[x][y] << " ";
  609. out << endl << "target output pattern: " << (x+) << endl;
  610. for(y=; y<output_array_size; y++) out << target[x][y] << " ";
  611. }
  612. out << endl << endl << "BIASES:" << endl;
  613. for(x=; x<hidden_array_size; x++) {
  614. out << "bias of hidden unit " << (x+) << ": " << bias[x];
  615. if(x<output_array_size) out << " bias of output unit " << (x+) << ": " << bias[x+hidden_array_size];
  616. out << endl;
  617. }
  618. out << endl << "WEIGHTS:" << endl;
  619. for(x=; x<input_array_size; x++) {
  620. for(y=; y<hidden_array_size; y++) out << "i_h[" << x << "][" << y << "]: " << weight_i_h[x][y] << endl;
  621. }
  622. for(x=; x<hidden_array_size; x++) {
  623. for(y=; y<output_array_size; y++) out << "h_o[" << x << "][" << y << "]: " << weight_h_o[x][y] << endl;
  624. }
  625. out.close();
  626. cout << endl << "data has been printed to " << printfile << endl;
  627. return;
  628. }
  629.  
  630. void change_learning_rate()
  631. {
  632. if (file_loaded == )
  633. {
  634. cout << endl
  635. << "there is no data loaded into memory"
  636. << endl;
  637. return;
  638. }
  639. cout << endl << "actual learning rate: " << learning_rate << " new value: ";
  640. cin >> learning_rate;
  641. return;
  642. }
  643.  
  644. void compute_output_pattern()
  645. {
  646. if (file_loaded == )
  647. {
  648. cout << endl
  649. << "there is no data loaded into memory"
  650. << endl;
  651. return;
  652. }
  653. char choice;
  654. cout << endl << endl << "1. load trained input pattern into network" << endl;
  655. cout << "2. load custom input pattern into network" << endl;
  656. cout << "3. go back to main menu" << endl << endl;
  657. cout << "Enter your choice (1-3)" << endl;
  658. do { choice = getch(); } while (choice != '' && choice != '' && choice != '');
  659. switch(choice) {
  660. case '': test();
  661. break;
  662. case '': custom();
  663. break;
  664. case '': return;
  665. };
  666. }
  667.  
  668. void custom()
  669. {
  670. _control87 (MCW_EM, MCW_EM);
  671. char filename[];
  672. register double temp=;
  673. register int x,y;
  674. double *custom_input = new double [input_array_size];
  675. if(!custom_input)
  676. {
  677. cout << endl << "memory problem!";
  678. return;
  679. }
  680. double *custom_output = new double [output_array_size];
  681. if(!custom_output)
  682. {
  683. delete [] custom_input;
  684. cout << endl << "memory problem!";
  685. return;
  686. }
  687. cout << endl << endl << "enter file that contains test input pattern: ";
  688. cin >> filename;
  689. ifstream in(filename);
  690. if(!in) { cout << endl << "failed to load data file" << endl; return; }
  691. for(x = ; x < input_array_size; x++) {
  692. in >> custom_input[x];
  693. }
  694. for(y=; y<hidden_array_size; y++) {
  695. for(x=; x<input_array_size; x++) {
  696. temp += (custom_input[x] * weight_i_h[x][y]);
  697. }
  698. hidden[y] = (1.0 / (1.0 + exp(-1.0 * (temp + bias[y]))));
  699. temp = ;
  700. }
  701. for(y=; y<output_array_size; y++) {
  702. for(x=; x<hidden_array_size; x++) {
  703. temp += (hidden[x] * weight_h_o[x][y]);
  704. }
  705. custom_output[y] = (1.0 / (1.0 + exp(-1.0 * (temp + bias[y + hidden_array_size]))));
  706. temp = ;
  707. }
  708. cout << endl << "Input pattern:" << endl;
  709. for(x = ; x < input_array_size; x++) {
  710. cout << "[" << (x + ) << ": " << custom_input[x] << "] ";
  711. }
  712. cout << endl << endl << "Output pattern:";
  713. for(x=; x<output_array_size; x++) {
  714. cout << endl << (x+) << ": " << custom_output[x] << " binary: ";
  715. if(custom_output[x] >= 0.9) cout << "";
  716. else if(custom_output[x]<=0.1) cout << "";
  717. else cout << "intermediate value";
  718. }
  719. cout << endl;
  720. delete [] custom_input;
  721. delete [] custom_output;
  722. return;
  723. }
  724.  
  725. void clear_memory()
  726. {
  727. int x;
  728. for(x=; x<number_of_input_patterns; x++)
  729. {
  730. delete [] input[x];
  731. }
  732. delete [] input;
  733. delete [] hidden;
  734. for(x=; x<number_of_input_patterns; x++)
  735. {
  736. delete [] output[x];
  737. }
  738. delete [] output;
  739. for(x=; x<number_of_input_patterns; x++)
  740. {
  741. delete [] target[x];
  742. }
  743. delete [] target;
  744. delete [] bias;
  745. for(x=; x<input_array_size; x++)
  746. {
  747. delete [] weight_i_h[x];
  748. }
  749. delete [] weight_i_h;
  750. for(x=; x<hidden_array_size; x++)
  751. {
  752. delete [] weight_h_o[x];
  753. }
  754. delete [] weight_h_o;
  755. delete [] errorsignal_hidden;
  756. delete [] errorsignal_output;
  757. file_loaded = ;
  758. return;
  759. }

初始化的神经网络的数据文件:

  1.  
  2. 0.5
  3.  
  4. 5.747781 -6.045236 1.206744 -41.245163 -0.249886 -0.35452 0.0718
  5.  
  6. -8.446443 9.25553 -6.50087 7.357942 7.777944 1.238442
  7.  
  8. 15.957281 0.452741 -8.19198 9.140881 29.124746 9.806898 5.859479 -5.09182 -3.475694 -4.896269 6.320669 0.213897
  9.  
  10. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  11. !!!!explanation of datafile. Can be deleted. Not necessary for network to work!!!!
  12. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  13.  
  14. (number of input units)
  15. (number of hidden units)
  16. (number of output units)
  17. 0.5 (learning rate)
  18. (number of input and target output patterns) (has to correspond to the amount of patterns at the end of the datafile)
  19. 5.747781 -6.045236 1.206744 -41.245163 -0.249886 -0.35452 0.0718 (biases of hidden and output units, first three are biases of the hidden units, last four are biases of the output units)
  20.  
  21. -8.446443 9.25553 -6.50087 7.357942 7.777944 1.238442 (values of weights from input to hidden units)
  22.  
  23. 15.957281 0.452741 -8.19198 9.140881 29.124746 9.806898 5.859479 -5.09182 -3.475694 -4.896269 6.320669 0.213897 (values of weights from hidden to output units)
  24.  
  25. (input pattern #)
  26. (input pattern #)
  27. (input pattern #)
  28. (input pattern #)
  29.  
  30. (target output pattern #)
  31. (target output pattern #)
  32. (target output pattern #)
  33. (target output pattern #)
  34.  
  35. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  36. !!!! end of explanation of datafile. !!!!
  37. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

按照数据输入说明,可以再b.txt文件中保存输入数据[0, 1],对应的输入结果如下:

可以看到,输入[0,1]得到的结果为0110,与训练时候的结果一直。

最后,本代码没有深入测试过,同时也只有一个隐层,所以建议只用来配合梳理算法原理用。

deep learning(1)BP神经网络原理与练习的更多相关文章

  1. 机器学习(4):BP神经网络原理及其python实现

    BP神经网络是深度学习的重要基础,它是深度学习的重要前行算法之一,因此理解BP神经网络原理以及实现技巧非常有必要.接下来,我们对原理和实现展开讨论. 1.原理  有空再慢慢补上,请先参考老外一篇不错的 ...

  2. Deep Learning In NLP 神经网络与词向量

    0. 词向量是什么 自然语言理解的问题要转化为机器学习的问题,第一步肯定是要找一种方法把这些符号数学化. NLP 中最直观,也是到目前为止最常用的词表示方法是 One-hot Representati ...

  3. BP神经网络原理及python实现

    [废话外传]:终于要讲神经网络了,这个让我踏进机器学习大门,让我读研,改变我人生命运的四个字!话说那么一天,我在乱点百度,看到了这样的内容: 看到这么高大上,这么牛逼的定义,怎么能不让我这个技术宅男心 ...

  4. 机器学习入门学习笔记:(一)BP神经网络原理推导及程序实现

    机器学习中,神经网络算法可以说是当下使用的最广泛的算法.神经网络的结构模仿自生物神经网络,生物神经网络中的每个神经元与其他神经元相连,当它“兴奋”时,想下一级相连的神经元发送化学物质,改变这些神经元的 ...

  5. BP神经网络原理详解

    转自博客园@编程De: http://www.cnblogs.com/jzhlin/archive/2012/07/28/bp.html  http://blog.sina.com.cn/s/blog ...

  6. BP神经网络原理及在Matlab中的应用

    一.人工神经网络 关于对神经网络的介绍和应用,请看如下文章 ​ 神经网络潜讲 ​ 如何简单形象又有趣地讲解神经网络是什么 二.人工神经网络分类 按照连接方式--前向神经网络.反馈(递归)神经网络 按照 ...

  7. Deep Learning - 3 改进神经网络的学习方式

    反向传播算法是大多数神经网络的基础,我们应该多花点时间掌握它. 还有一些技术能够帮助我们改进反向传播算法,从而改进神经网络的学习方式,包括: 选取更好的代价函数 正则化方法 初始化权重的方法 如何选择 ...

  8. bp神经网络原理

    bp(back propagation)修改每层神经网络向下一层传播的权值,来减少输出层的实际值和理论值的误差 其实就是训练权值嘛 训练方法为梯度下降法 其实就是高等数学中的梯度,将所有的权值看成自变 ...

  9. Coursera Deep Learning笔记 卷积神经网络基础

    参考1 参考2 1. 计算机视觉 使用传统神经网络处理机器视觉的一个主要问题是输入层维度很大.例如一张64x64x3的图片,神经网络输入层的维度为12288. 如果图片尺寸较大,例如一张1000x10 ...

随机推荐

  1. Linux下18b20温度传感器驱动代码及测试实例

    驱动代码: #include <linux/module.h> #include <linux/fs.h> #include <linux/kernel.h> #i ...

  2. opengl多重采样

    效果图如下,两幅图效果是一样的,只是换了个背景.两幅图均是左侧使用了多重采样,右侧的没有使用多重采样.

  3. hdu 5172 GTY's gay friends

    GTY's gay friends 题意:给n个数和m次查询:(1<n,m<1000,000);之后输入n个数值(1 <= ai <= n):问下面m次查询[L,R]中是否存在 ...

  4. STL set_difference set_intersection set_union 操作

    以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的. set_difference 这个是求得在第一个容器中有,第 ...

  5. iOS 页面间传值 之 属性传值,代理传值

    手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...

  6. Hanoi

    递归解决 汉诺塔 class Han{ int num; int steps; Han(int num){ this.num=num; } void total() { System.out.prin ...

  7. 图像混合学习。运用加权函数,学习opencv基础操作

               {          cout<<     }           {          cout<<     }       ,,logoImage.c ...

  8. 简单方法打包.net程序集脱离framework

    最近业余捣鼓monogame,自然而然就关注到了.net程序脱离framework发布的问题上了, 度娘,谷歌娘 都经过一番查找,无非分为如下几类方法: 1.直接使用mono运行时,附带 bin.li ...

  9. [转载]用c写PHP的扩展接口(php5,c++)

    原文[http://bugs.tutorbuddy.com/php5cpp/php5cpp/] 第1节. 开始之前 开始前,我要说明:这篇文章所描述的主要是在UNIX的PHP环境上的. 另外一点我要说 ...

  10. 介绍一个超好用的HICHARTS扩展插件

    因为需要,所以HIGHCHARTS了解一下是很有必要的. 但原始应用确实效率不行. 刚好,现在有个需求是从一系列的JSON里抽出表格数据,再显示图形. jquery.highchartsTable.j ...