本篇只给出实现的代码,下一篇将讲一讲实现的原理,及其Adline网络中的LMS算法原理。

包含两个类:

  1. package com.cgjr.com;
  2.  
  3. import java.security.DigestInputStream;
  4. import java.util.Arrays;
  5.  
  6. import org.neuroph.core.data.DataSet;
  7. import org.neuroph.core.data.DataSetRow;
  8. import org.neuroph.core.events.LearningEvent;
  9. import org.neuroph.core.events.LearningEventListener;
  10. import org.neuroph.util.TransferFunctionType;
  11.  
  12. public class AdalineDemo implements LearningEventListener {
  13. public final static int CHAR_WIDTH = 5;
  14. public final static int CHAR_HEIGHT = 7;
  15. public static String[][] DIGITS = {
  16. {
  17. " 000 ",
  18. "0 0",
  19. "0 0",
  20. "0 0",
  21. "0 0",
  22. "0 0",
  23. " 000 "
  24. },
  25. {
  26. " 0 ",
  27. " 00 ",
  28. "0 0 ",
  29. " 0 ",
  30. " 0 ",
  31. " 0 ",
  32. " 0 "
  33.  
  34. }, {
  35.  
  36. " 000 ",
  37. "0 0",
  38. " 0",
  39. " 0 ",
  40. " 0 ",
  41. " 0 ",
  42. "00000"
  43. }, {
  44.  
  45. " 000 ",
  46. "0 0",
  47. " 0",
  48. " 000 ",
  49. " 0",
  50. "0 0",
  51. " 000 "
  52.  
  53. }, {
  54.  
  55. " 0 ",
  56. " 00 ",
  57. " 0 0 ",
  58. "0 0 ",
  59. "00000",
  60. " 0 ",
  61. " 0 "
  62.  
  63. }, {
  64. "00000",
  65. "0 ",
  66. "0 ",
  67. "0000 ",
  68. " 0",
  69. "0 0",
  70. " 000 "
  71.  
  72. }, {
  73. " 000 ",
  74. "0 0",
  75. "0 ",
  76. "0000 ",
  77. "0 0",
  78. "0 0",
  79. " 000 "
  80.  
  81. }, {
  82. "00000",
  83. " 0",
  84. " 0",
  85. " 0 ",
  86. " 0 ",
  87. " 0 ",
  88. "0 "
  89.  
  90. }, {
  91.  
  92. " 000 ",
  93. "0 0",
  94. "0 0",
  95. " 000 ",
  96. "0 0",
  97. "0 0",
  98. " 000 "
  99.  
  100. }, {
  101.  
  102. " 000 ",
  103. "0 0",
  104. "0 0",
  105. " 0000",
  106. " 0",
  107. "0 0",
  108. " 000 "
  109.  
  110. }
  111. };
  112.  
  113. public static void main(String[] args) {
  114. Adaline ada = new Adaline(CHAR_WIDTH * CHAR_HEIGHT,DIGITS.length,0.01d,TransferFunctionType.LINEAR);
  115. DataSet ds = new DataSet(CHAR_WIDTH * CHAR_HEIGHT, DIGITS.length);
  116. for (int i = 0; i < DIGITS.length; i++) {
  117. //一个数字符号就是一个训练的数据,第0个数字的的期望输出为0,第一个数字的期望输出为1等等。
  118. ds.addRow(createTrainDataRow(DIGITS[i],i));
  119. }
  120. //ada.getLearningRule().addListener(new AdalineDemo());
  121. ada.learn(ds);
  122. for (int i = 0; i < DIGITS.length; i++) {
  123. ada.setInput(image2data(DIGITS[i]));
  124. ada.calculate();
  125. printDIGITS(DIGITS[i]);
  126. System.out.println(maxIndex(ada.getOutput()));
  127. System.out.println(Arrays.toString(ada.getOutput()));
  128. System.out.println();
  129. }
  130. }
  131.  
  132. private static int maxIndex(double[] output) {
  133. //这其实就是选出最接近一的那个
  134. double maxData=output[0];
  135. int maxIndex=0;
  136. for (int i = 0; i < output.length; i++) {
  137. if(maxData<output[i]){
  138. maxData=output[i];
  139. maxIndex=i;
  140. }
  141. }
  142. return maxIndex;
  143. }
  144.  
  145. private static void printDIGITS(String[] image) {
  146.  
  147. for (int i = 0; i < image.length; i++) {
  148. System.out.println(image[i]);
  149. }
  150. System.out.println("\n");
  151. }
  152.  
  153. private static DataSetRow createTrainDataRow(String[] image, int idealValue) {
  154. //设置所有的为输出为负一,只有当那个等于
  155. double[] output=new double[DIGITS.length];
  156. for (int i = 0; i < output.length; i++) {
  157. output[i]=-1;
  158. }
  159. double[] input=image2data(image);
  160. output[idealValue]=1;
  161. DataSetRow dsr=new DataSetRow(input,output);
  162. return dsr;
  163. }
  164.  
  165. //将图像转换为数字,空格的地方为-1,不空格的地方为1
  166.  
  167. private static double[] image2data(String[] image) {
  168. double[] input=new double[CHAR_WIDTH*CHAR_HEIGHT];
  169. //行的长度,即为字符的长度,为整个字体的高度
  170. for (int row = 0; row < CHAR_HEIGHT; row++) {
  171. //有多少个列
  172. for (int col = 0; col < CHAR_WIDTH; col++) {
  173. int index=(row*CHAR_WIDTH)+col;
  174. char ch=image[row].charAt(col);
  175. input[index]=ch=='0'?1:-1;
  176. }
  177. }
  178.  
  179. return input;
  180. }
  181.  
  182. @Override
  183. public void handleLearningEvent(LearningEvent event) {
  184. // TODO Auto-generated method stub
  185.  
  186. }
  187.  
  188. }

网络类:

  1. package com.cgjr.com;
  2.  
  3. import org.neuroph.core.Layer;
  4. import org.neuroph.core.NeuralNetwork;
  5. import org.neuroph.nnet.comp.neuron.BiasNeuron;
  6. import org.neuroph.nnet.learning.LMS;
  7. import org.neuroph.util.ConnectionFactory;
  8. import org.neuroph.util.LayerFactory;
  9. import org.neuroph.util.NeuralNetworkFactory;
  10. import org.neuroph.util.NeuralNetworkType;
  11. import org.neuroph.util.NeuronProperties;
  12. import org.neuroph.util.TransferFunctionType;
  13.  
  14. public class Adaline extends NeuralNetwork {
  15.  
  16. /**
  17. * The class fingerprint that is set to indicate serialization compatibility
  18. * with a previous version of the class.
  19. */
  20. private static final long serialVersionUID = 1L;
  21.  
  22. /**
  23. * Creates new Adaline network with specified number of neurons in input
  24. * layer
  25. *
  26. * @param inputNeuronsCount
  27. * number of neurons in input layer
  28. */
  29. public Adaline(int inputNeuronsCount, int outputNeuronsCount, double learnRate, TransferFunctionType transferFunction) {
  30. this.createNetwork(inputNeuronsCount, outputNeuronsCount, learnRate,transferFunction);
  31. }
  32.  
  33. /**
  34. * Creates adaline network architecture with specified number of input
  35. * neurons
  36. *
  37. * @param inputNeuronsCount
  38. * number of neurons in input layer
  39. */
  40. private void createNetwork(int inputNeuronsCount, int outputNeuronsCount, double learnRate,
  41. TransferFunctionType transferFunction) {
  42. // set network type code
  43. this.setNetworkType(NeuralNetworkType.ADALINE);
  44.  
  45. // create input layer neuron settings for this network
  46. NeuronProperties inNeuronProperties = new NeuronProperties();
  47. inNeuronProperties.setProperty("transferFunction", TransferFunctionType.LINEAR);
  48.  
  49. // createLayer input layer with specified number of neurons
  50. Layer inputLayer = LayerFactory.createLayer(inputNeuronsCount, inNeuronProperties);
  51. inputLayer.addNeuron(new BiasNeuron()); // add bias neuron (always 1,
  52. // and it will act as bias input
  53. // for output neuron)
  54. this.addLayer(inputLayer);
  55.  
  56. // create output layer neuron settings for this network
  57. NeuronProperties outNeuronProperties = new NeuronProperties();
  58. if (transferFunction == TransferFunctionType.LINEAR) {
  59. outNeuronProperties.setProperty("transferFunction", TransferFunctionType.LINEAR);
  60. } else {
  61. outNeuronProperties.setProperty("transferFunction", TransferFunctionType.RAMP);
  62. outNeuronProperties.setProperty("transferFunction.slope", new Double(1));
  63. outNeuronProperties.setProperty("transferFunction.yHigh", new Double(1));
  64. outNeuronProperties.setProperty("transferFunction.xHigh", new Double(1));
  65. outNeuronProperties.setProperty("transferFunction.yLow", new Double(-1));
  66. outNeuronProperties.setProperty("transferFunction.xLow", new Double(-1));
  67. }
  68. // createLayer output layer (only one neuron)
  69. Layer outputLayer = LayerFactory.createLayer(outputNeuronsCount, outNeuronProperties);
  70. this.addLayer(outputLayer);
  71.  
  72. // createLayer full conectivity between input and output layer
  73. ConnectionFactory.fullConnect(inputLayer, outputLayer);
  74.  
  75. // set input and output cells for network
  76. NeuralNetworkFactory.setDefaultIO(this);
  77.  
  78. // set LMS learning rule for this network
  79. LMS l = new LMS();
  80. l.setLearningRate(learnRate);
  81. this.setLearningRule(l);
  82. }
  83.  
  84. }

运行的结果截图:

Adaline网络识别印刷体数字0到9-java实现的更多相关文章

  1. OpenCV——识别印刷体数字

    数字识别和其他的所有计算机视觉相关的应用都会分为两个步骤:ROI抽取和识别. 1. ROI抽取即将感兴趣的区域从原始图像中分离初来,这个步骤包括二值化,噪点的消除等2. 识别即通过一些分类器将第一步中 ...

  2. 中文价格识别为数字 java代码

    运行效果: public class VoicePriceRecognition { private final static String NOT_HAS_PRICE_CONTENT="n ...

  3. 数据挖掘入门系列教程(十二)之使用keras构建CNN网络识别CIFAR10

    简介 在上一篇博客:数据挖掘入门系列教程(十一点五)之CNN网络介绍中,介绍了CNN的工作原理和工作流程,在这一篇博客,将具体的使用代码来说明如何使用keras构建一个CNN网络来对CIFAR-10数 ...

  4. C/C++编程笔记:C语言NULL值和数字 0 值区别及NULL详解

    在学习C语言的时候,我们常常会碰到C语言NULL值和数字 0 ,很多小伙伴搞不清楚他们之间的一个区别,今天我们就了解一下他们之间的区别,一起来看看吧! 先看下面一段代码输出什么: 输出<null ...

  5. 字符串怎么换行 || 字符串中使用单引号时应该怎么写 || 保留两位小数 || 数字0在if中的意思是false || 什么情况下会会报undefined || null和undefined的区别 ||

    换行的字符串 "This string\nhas two lines" 字符串中使用单引号时应该怎么写 'You\'re right, it can\'t be a quote' ...

  6. 【编程题目】n 个数字(0,1,…,n-1)形成一个圆圈,从数字 0 开始

    第 18 题(数组):题目:n 个数字(0,1,…,n-1)形成一个圆圈,从数字 0 开始,每次从这个圆圈中删除第 m 个数字(第一个为当前数字本身,第二个为当前数字的下一个数字).当一个数字删除后, ...

  7. Novate 网络库:Retrofit2.0和RxJava的又一次完美改进加强(Tamic博客 -CSDN)

    作者/Tamic http://blog.csdn.net/sk719887916/article/details/52195428 前言 用过RxJava和Retrofit的朋友,用久了就会发现Re ...

  8. [转]mybatis if test非空判断数字0为什么是false

    原文地址:http://blog.51cto.com/wangguangshuo/1944531 今天工作中发现一个Long类型的参数没有传到sql中去,在sql xml配置文件中是使用if test ...

  9. 字符串分隔 ->连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

        •连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组:•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理. 输入描述: 连续输入字符串(输入2次,每个字符串长度小于100 ...

随机推荐

  1. ECMA script 6的新特性

    简单介绍下ES6的新特性: (1)箭头操作符 :简化了函数的书写 (2)类的支持:引入了class关键字,对象的创建,继承更加直观,父类方法的调用,实例化,构造函数等概念更加形象化. (3)增强的对象 ...

  2. bootstrap使用模板

    Bootstrap下载地址: - https://github.com/twbs/bootstrap/releases/download/v3.3.6/bootstrap-3.3.6-dist.zip ...

  3. Mycat中的核心概念

      Mycat中的核心概念     Mycat中的核心概念 1.数据库中间件    Mycat 是一个开源的分布式数据库系统,但是由于真正的数据库需要存储引擎,而 Mycat 并没有 存储引擎,所以并 ...

  4. POPTEST老李谈Debug和Release的区别(c#)2

    二.哪些情况下 Release 版会出错 有了上面的介绍,我们再来逐个对照这些选项看看 Release 版错误是怎样产生的 1. Runtime Library: 2. 优化:这类错误主要有以下几种: ...

  5. 老李分享:大数据测试之HDFS文件系统

    poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...

  6. [笔记]RankSVM 和 IR SVM

    之前的博客:http://www.cnblogs.com/bentuwuying/p/6681943.html中简单介绍了Learning to Rank的基本原理,也讲到了Learning to R ...

  7. POJ 2585 Window Pains 题解

    链接:http://poj.org/problem?id=2585 题意: 某个人有一个屏幕大小为4*4的电脑,他很喜欢打开窗口,他肯定打开9个窗口,每个窗口大小2*2.并且每个窗口肯定在固定的位置上 ...

  8. cocoapods安装好后repo换源

    1.pod repo 然后会出现以下内容,如下是我已经换了之后的,而你的URL还是github的 master - Type: git (master) - URL:  https://git.cod ...

  9. 基于appium的移动端自动化测试,密码键盘无法识别问题

    基于appium做自动化测试,APP密码键盘无法识别问题解决思路 这个问题的解决思路如下: 1.针对iOS无序键盘:首先,iOS的密码键盘是可识别的,但是,密码键盘一般是无序的.针对这个情况,思路是用 ...

  10. POJ 3261 出现至少K次的可重叠最长子串

    题意就是给一列数字,求最长的一个子串,并且满足子串在原数串中出现至少K次,子串可以重叠. 解法是将问题转为判定性问题,二分子串的长度,判定是否满足重复至少K次.判定方法是经典的根据子串长度将Heigh ...