Adaline网络识别印刷体数字0到9-java实现
本篇只给出实现的代码,下一篇将讲一讲实现的原理,及其Adline网络中的LMS算法原理。
包含两个类:
package com.cgjr.com; import java.security.DigestInputStream;
import java.util.Arrays; import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.core.events.LearningEvent;
import org.neuroph.core.events.LearningEventListener;
import org.neuroph.util.TransferFunctionType; public class AdalineDemo implements LearningEventListener {
public final static int CHAR_WIDTH = 5;
public final static int CHAR_HEIGHT = 7;
public static String[][] DIGITS = {
{
" 000 ",
"0 0",
"0 0",
"0 0",
"0 0",
"0 0",
" 000 "
},
{
" 0 ",
" 00 ",
"0 0 ",
" 0 ",
" 0 ",
" 0 ",
" 0 " }, { " 000 ",
"0 0",
" 0",
" 0 ",
" 0 ",
" 0 ",
"00000"
}, { " 000 ",
"0 0",
" 0",
" 000 ",
" 0",
"0 0",
" 000 " }, { " 0 ",
" 00 ",
" 0 0 ",
"0 0 ",
"00000",
" 0 ",
" 0 " }, {
"00000",
"0 ",
"0 ",
"0000 ",
" 0",
"0 0",
" 000 " }, {
" 000 ",
"0 0",
"0 ",
"0000 ",
"0 0",
"0 0",
" 000 " }, {
"00000",
" 0",
" 0",
" 0 ",
" 0 ",
" 0 ",
"0 " }, { " 000 ",
"0 0",
"0 0",
" 000 ",
"0 0",
"0 0",
" 000 " }, { " 000 ",
"0 0",
"0 0",
" 0000",
" 0",
"0 0",
" 000 " }
}; public static void main(String[] args) {
Adaline ada = new Adaline(CHAR_WIDTH * CHAR_HEIGHT,DIGITS.length,0.01d,TransferFunctionType.LINEAR);
DataSet ds = new DataSet(CHAR_WIDTH * CHAR_HEIGHT, DIGITS.length);
for (int i = 0; i < DIGITS.length; i++) {
//一个数字符号就是一个训练的数据,第0个数字的的期望输出为0,第一个数字的期望输出为1等等。
ds.addRow(createTrainDataRow(DIGITS[i],i));
}
//ada.getLearningRule().addListener(new AdalineDemo());
ada.learn(ds);
for (int i = 0; i < DIGITS.length; i++) {
ada.setInput(image2data(DIGITS[i]));
ada.calculate();
printDIGITS(DIGITS[i]);
System.out.println(maxIndex(ada.getOutput()));
System.out.println(Arrays.toString(ada.getOutput()));
System.out.println();
}
} private static int maxIndex(double[] output) {
//这其实就是选出最接近一的那个
double maxData=output[0];
int maxIndex=0;
for (int i = 0; i < output.length; i++) {
if(maxData<output[i]){
maxData=output[i];
maxIndex=i;
}
}
return maxIndex;
} private static void printDIGITS(String[] image) { for (int i = 0; i < image.length; i++) {
System.out.println(image[i]);
}
System.out.println("\n");
} private static DataSetRow createTrainDataRow(String[] image, int idealValue) {
//设置所有的为输出为负一,只有当那个等于
double[] output=new double[DIGITS.length];
for (int i = 0; i < output.length; i++) {
output[i]=-1;
}
double[] input=image2data(image);
output[idealValue]=1;
DataSetRow dsr=new DataSetRow(input,output);
return dsr;
} //将图像转换为数字,空格的地方为-1,不空格的地方为1 private static double[] image2data(String[] image) {
double[] input=new double[CHAR_WIDTH*CHAR_HEIGHT];
//行的长度,即为字符的长度,为整个字体的高度
for (int row = 0; row < CHAR_HEIGHT; row++) {
//有多少个列
for (int col = 0; col < CHAR_WIDTH; col++) {
int index=(row*CHAR_WIDTH)+col;
char ch=image[row].charAt(col);
input[index]=ch=='0'?1:-1;
}
} return input;
} @Override
public void handleLearningEvent(LearningEvent event) {
// TODO Auto-generated method stub } }
网络类:
package com.cgjr.com; import org.neuroph.core.Layer;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.nnet.comp.neuron.BiasNeuron;
import org.neuroph.nnet.learning.LMS;
import org.neuroph.util.ConnectionFactory;
import org.neuroph.util.LayerFactory;
import org.neuroph.util.NeuralNetworkFactory;
import org.neuroph.util.NeuralNetworkType;
import org.neuroph.util.NeuronProperties;
import org.neuroph.util.TransferFunctionType; public class Adaline extends NeuralNetwork { /**
* The class fingerprint that is set to indicate serialization compatibility
* with a previous version of the class.
*/
private static final long serialVersionUID = 1L; /**
* Creates new Adaline network with specified number of neurons in input
* layer
*
* @param inputNeuronsCount
* number of neurons in input layer
*/
public Adaline(int inputNeuronsCount, int outputNeuronsCount, double learnRate, TransferFunctionType transferFunction) {
this.createNetwork(inputNeuronsCount, outputNeuronsCount, learnRate,transferFunction);
} /**
* Creates adaline network architecture with specified number of input
* neurons
*
* @param inputNeuronsCount
* number of neurons in input layer
*/
private void createNetwork(int inputNeuronsCount, int outputNeuronsCount, double learnRate,
TransferFunctionType transferFunction) {
// set network type code
this.setNetworkType(NeuralNetworkType.ADALINE); // create input layer neuron settings for this network
NeuronProperties inNeuronProperties = new NeuronProperties();
inNeuronProperties.setProperty("transferFunction", TransferFunctionType.LINEAR); // createLayer input layer with specified number of neurons
Layer inputLayer = LayerFactory.createLayer(inputNeuronsCount, inNeuronProperties);
inputLayer.addNeuron(new BiasNeuron()); // add bias neuron (always 1,
// and it will act as bias input
// for output neuron)
this.addLayer(inputLayer); // create output layer neuron settings for this network
NeuronProperties outNeuronProperties = new NeuronProperties();
if (transferFunction == TransferFunctionType.LINEAR) {
outNeuronProperties.setProperty("transferFunction", TransferFunctionType.LINEAR);
} else {
outNeuronProperties.setProperty("transferFunction", TransferFunctionType.RAMP);
outNeuronProperties.setProperty("transferFunction.slope", new Double(1));
outNeuronProperties.setProperty("transferFunction.yHigh", new Double(1));
outNeuronProperties.setProperty("transferFunction.xHigh", new Double(1));
outNeuronProperties.setProperty("transferFunction.yLow", new Double(-1));
outNeuronProperties.setProperty("transferFunction.xLow", new Double(-1));
}
// createLayer output layer (only one neuron)
Layer outputLayer = LayerFactory.createLayer(outputNeuronsCount, outNeuronProperties);
this.addLayer(outputLayer); // createLayer full conectivity between input and output layer
ConnectionFactory.fullConnect(inputLayer, outputLayer); // set input and output cells for network
NeuralNetworkFactory.setDefaultIO(this); // set LMS learning rule for this network
LMS l = new LMS();
l.setLearningRate(learnRate);
this.setLearningRule(l);
} }
运行的结果截图:



Adaline网络识别印刷体数字0到9-java实现的更多相关文章
- OpenCV——识别印刷体数字
数字识别和其他的所有计算机视觉相关的应用都会分为两个步骤:ROI抽取和识别. 1. ROI抽取即将感兴趣的区域从原始图像中分离初来,这个步骤包括二值化,噪点的消除等2. 识别即通过一些分类器将第一步中 ...
- 中文价格识别为数字 java代码
运行效果: public class VoicePriceRecognition { private final static String NOT_HAS_PRICE_CONTENT="n ...
- 数据挖掘入门系列教程(十二)之使用keras构建CNN网络识别CIFAR10
简介 在上一篇博客:数据挖掘入门系列教程(十一点五)之CNN网络介绍中,介绍了CNN的工作原理和工作流程,在这一篇博客,将具体的使用代码来说明如何使用keras构建一个CNN网络来对CIFAR-10数 ...
- C/C++编程笔记:C语言NULL值和数字 0 值区别及NULL详解
在学习C语言的时候,我们常常会碰到C语言NULL值和数字 0 ,很多小伙伴搞不清楚他们之间的一个区别,今天我们就了解一下他们之间的区别,一起来看看吧! 先看下面一段代码输出什么: 输出<null ...
- 字符串怎么换行 || 字符串中使用单引号时应该怎么写 || 保留两位小数 || 数字0在if中的意思是false || 什么情况下会会报undefined || null和undefined的区别 ||
换行的字符串 "This string\nhas two lines" 字符串中使用单引号时应该怎么写 'You\'re right, it can\'t be a quote' ...
- 【编程题目】n 个数字(0,1,…,n-1)形成一个圆圈,从数字 0 开始
第 18 题(数组):题目:n 个数字(0,1,…,n-1)形成一个圆圈,从数字 0 开始,每次从这个圆圈中删除第 m 个数字(第一个为当前数字本身,第二个为当前数字的下一个数字).当一个数字删除后, ...
- Novate 网络库:Retrofit2.0和RxJava的又一次完美改进加强(Tamic博客 -CSDN)
作者/Tamic http://blog.csdn.net/sk719887916/article/details/52195428 前言 用过RxJava和Retrofit的朋友,用久了就会发现Re ...
- [转]mybatis if test非空判断数字0为什么是false
原文地址:http://blog.51cto.com/wangguangshuo/1944531 今天工作中发现一个Long类型的参数没有传到sql中去,在sql xml配置文件中是使用if test ...
- 字符串分隔 ->连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组:•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理. 输入描述: 连续输入字符串(输入2次,每个字符串长度小于100 ...
随机推荐
- HTML 部分常用属性、组成属性|...超链接、路径、锚点、列表、滚动、URL编码、表格、表单、GET和POST
URL地址 就是我们所说的网址:www.jd.com 浏览器内核,渲染引擎 Ie内核:triteent 谷歌/欧鹏:blink 火狐:gecko 苹果:webkit 渲染引擎是出现兼容性的根本问题 - ...
- 将spring cloud打包docker镜像
简介:用spring cloud构建微服务后,微服务肯定要docker镜像化然后用rancher等可视化工具管理,这就是走完了一套基本的流程,现在简单介绍下 环境:两台centos7.x的服务器,一台 ...
- nvm安装和配置详细教程
nvm是nodejs的版本管理工具,为什么要用nvm,你能百度到这篇文章相比是遇到不得不用的原因了,我们知道nodejs官方更新的速度非常快,有时候业务需要需要用某某版本,如果用的是msi安装,虽然安 ...
- JavaScript之作用域与闭包总结
博主最开始接触程序是C语言,C++,后来是java,现在是php,无论哪一种语言与javascript在机制上都还是有比较大的区别. 下面总结一下用面向对象的思想写javascript需要区分的要点: ...
- 简单分析下用yii2的yii\helpers\Html类和yii.js实现的post请求
yii2提供了很多帮助类,比如Html.Url.Json等,可以很方便的实现一些功能,下面简单说下这个Html.用yii2写view时时经常会用到它,今天在改写一个页面时又用到了它.它比较好用的地方就 ...
- 关于C++中的前置声明(附程序运行图)
实验于华中农业大学逸夫楼2017.3.10 在编写C++程序的时候,偶尔需要用到前置声明(Forward declaration).下面的程序中,带注释的那行就是类B的前置说明.这是必须的,因为类A中 ...
- APICloud框架—db数据库模块
db数据库模块 db 模块封装了手机常用数据库 sqlite 的增删改查语句,可实现数据的本地存储,极大的简化了数据持久化问题,本模块已支持同步接口. 官方文档地址 打开/新建一个数据库 functi ...
- 前端随手优化不完全篇-SEO篇
一代码优化概述 关于代码优化的知识是纯理论的知识,学习的很枯燥.在学到CSS时,不免遇到CSS+div进行代码优化的知 识,因此在网上看了一些关于这方面的知识,简单的整合一下,梳理自己所了解的代码优化 ...
- 分享一本书<<谁都不敢欺负你>>
有些人,不管在工作还是生活上,总是被人欺负. 分享这本书给大家,能给大家带来正能量.你强大了,就没人敢欺负你. 有的时候,感到为什么倒霉的总是我?为什么我的命运是这样?为什么总欺负我? 也许有很多人会 ...
- JS取消浏览器文本选中的方法
一 .问题的出现 今天在使用Easy-UI 的messager.alert()方法时候出现浏览器文本被选中,不知道其中是什么原因,如下图所示. 二 .解决思路 我最后的思路时在弹出消息框的同时,取消浏 ...