神经网络: 计算简单的加法

神经网络: 计算简单的加法
下面是完整的代码:
  1. //
  2. //fann_test.c:
  3. //FANN测试
  4. //
  5. //到下面的网站下载FANN库:
  6. //FastArtificialNeuralNetworkLibrary(fann)
  7. //http://leenissen.dk/fann/
  8. //下载链接:DownloadFANN--->CSourceCodeandWindowsDLLfiles
  9. //
  10. #include"../fann-2.0.0/src/include/doublefann.h"
  11. #ifdef_DEBUG
  12. #pragmacomment(lib,"../fann-2.0.0/MicrosoftWindowsDll/bin/fanndoubleMTd.lib")
  13. #else
  14. #pragmacomment(lib,"../fann-2.0.0/MicrosoftWindowsDll/bin/fanndoubleMT.lib")
  15. #endif
  16. //训练:
  17. //加法神经网络
  18. //c=a+b;
  19. voidtrain()
  20. {
  21. constunsignedintnum_input=2;//输入项个数
  22. constunsignedintnum_output=1;//输出项个数
  23. constunsignedintnum_layers=3;
  24. constunsignedintnum_neurons_hidden=3;
  25. constfloatdesired_error=(constfloat)0.00000001;
  26. constunsignedintmax_epochs=500000;//最多执行次数
  27. constunsignedintepochs_between_reports=10000;//报告频率
  28. structfann*ann;
  29. intNum=200;
  30. floatMf=Num*3.f;
  31. inti;
  32. doublea,b,c;
  33. FILE*fp;
  34. fopen_s(&fp,"add.fann","w");
  35. fprintf_s(fp,"%d21/n",Num);
  36. //生成训练文件
  37. for(i=1;i<=Num;i++){
  38. //生成2个数,要求在(0,1)之间
  39. a=i/Mf;
  40. b=(i+1)/Mf;
  41. c=a+b;//要求在(0,1)之间
  42. //输入内容写到训练文件
  43. fprintf_s(fp,"%lf%lf/n%lf/n",a,b,c);
  44. }
  45. fclose(fp);
  46. //样本训练
  47. ann=fann_create_standard(num_layers,num_input,num_neurons_hidden,num_output);
  48. fann_set_activation_function_hidden(ann,FANN_LINEAR);
  49. fann_set_activation_function_output(ann,FANN_LINEAR);
  50. fann_train_on_file(ann,"add.fann",max_epochs,epochs_between_reports,desired_error);
  51. fann_save(ann,"add.fann.net");
  52. fann_destroy(ann);
  53. }
  54. //执行:
  55. //测试
  56. voidexec(doublea,doubleb)
  57. {
  58. structfann*ann;
  59. fann_type*calc_out;
  60. fann_typeinput[2];
  61. ann=fann_create_from_file("add.fann.net");
  62. input[0]=a;
  63. input[1]=b;
  64. calc_out=fann_run(ann,input);
  65. fann_destroy(ann);
  66. printf("a=%f/nb=%f/nc=%f/n期望值c=%f/n/n",input[0],input[1],calc_out[0],input[0]+input[1]);
  67. }
  68. //
  69. //主程序
  70. //
  71. intmain()
  72. {
  73. //下面的方法只需调用一次,然后注释掉
  74. //train();
  75. exec(0.354,0.58934);
  76. exec(0.21469,0.3914968);
  77. exec(0.130,0.44);
  78. exec(-0.3654,0.58455);
  79. exec(0.365420,-0.95);
  80. return0;
  81. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值