Hopfield神经网络实现污染字体的识别
这个网络的内部使用的是hebb学习规则
贴上两段代码:
package geym.nn.hopfiled; import java.util.Arrays; import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.nnet.Hopfield;
import org.neuroph.nnet.comp.neuron.InputOutputNeuron;
import org.neuroph.nnet.learning.HopfieldLearning;
import org.neuroph.util.NeuronProperties;
import org.neuroph.util.TransferFunctionType; /**
* 识别0 1 2 使用hopfield 全连接结构
* @author Administrator
*
*/
public class HopfieldSample2 { public static double[] format(double[] data){
for(int i=0;i<data.length;i++){
if(data[i]==0)data[i]=-1;
}
return data;
} public static void main(String args[]) {
NeuronProperties neuronProperties = new NeuronProperties();
neuronProperties.setProperty("neuronType", InputOutputNeuron.class);
neuronProperties.setProperty("bias", new Double(0.0D));
neuronProperties.setProperty("transferFunction", TransferFunctionType.STEP);
neuronProperties.setProperty("transferFunction.yHigh", new Double(1.0D));
neuronProperties.setProperty("transferFunction.yLow", new Double(-1.0D)); // create training set (H and T letter in 3x3 grid)
DataSet trainingSet = new DataSet(30);
trainingSet.addRow(new DataSetRow(format(new double[] {
0,1,1,1,1,0,
1,0,0,0,0,1,
1,0,0,0,0,1,
1,0,0,0,0,1,
0,1,1,1,1,0}))); // trainingSet.addRow(new DataSetRow(format(new double[] {
0,0,0,0,0,0,
1,0,0,0,0,0,
1,1,1,1,1,1,
0,0,0,0,0,0,
0,0,0,0,0,0}))); // trainingSet.addRow(new DataSetRow(format(new double[] {
1,0,0,0,0,0,
1,0,0,1,1,1,
1,0,0,1,0,1,
1,0,0,1,0,1,
0,1,1,0,0,1}))); //2 // create hopfield network
Hopfield myHopfield = new Hopfield(30, neuronProperties);
myHopfield.setLearningRule(new StandHopfieldLearning());
// learn the training set
myHopfield.learn(trainingSet); // test hopfield network
System.out.println("Testing network"); // add one more 'incomplete' H pattern for testing - it will be
// recognized as H
// DataSetRow h=new DataSetRow(new double[] { 1, 0, 0, 1, 0, 1, 1, 0, 1
// });
// DataSetRow h=new DataSetRow(new double[] { 1, 0, 0, 1, 0, 1, 1, 0, 1
// });
DataSetRow h = new DataSetRow(format(new double[] {
1,0,0,0,0,0,
1,0,0,1,1,1,
1,0,0,1,0,1,
1,0,0,1,0,0,
0,1,1,0,0,1})); // 2 bad
trainingSet.addRow(h); myHopfield.setInput(h.getInput()); double[] networkOutput = null;
double[] preNetworkOutput = null;
while (true) {
myHopfield.calculate();
networkOutput = myHopfield.getOutput();
if (preNetworkOutput == null) {
preNetworkOutput = networkOutput;
continue;
}
if (Arrays.equals(networkOutput, preNetworkOutput)) {
break;
}
preNetworkOutput = networkOutput;
} System.out.print("Input: " + Arrays.toString(h.getInput()));
System.out.println(" Output: " + Arrays.toString(networkOutput)); System.out.println(Arrays.equals(format(new double[] {
1,0,0,0,0,0,
1,0,0,1,1,1,
1,0,0,1,0,1,
1,0,0,1,0,1,
0,1,1,0,0,1}), networkOutput));
} }
下面就是StandHopfieldLearning类的实现,里面标红的地方就是hebb学习规则,权重为输入和输出的乘积:
package com.cgjr.com.hopfield; import org.neuroph.core.Connection;
import org.neuroph.core.Layer;
import org.neuroph.core.Neuron;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.core.learning.LearningRule; /**
* Learning algorithm for the Hopfield neural network.
*
* @author Zoran Sevarac <sevarac@gmail.com>
*/
public class StandHopfieldLearning extends LearningRule { /**
* 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 HopfieldLearning
*/
public StandHopfieldLearning() {
super();
} /**
* Calculates weights for the hopfield net to learn the specified training
* set
*
* @param trainingSet
* training set to learn
*/
public void learn(DataSet trainingSet) {
int M = trainingSet.size();
int N = neuralNetwork.getLayerAt(0).getNeuronsCount();
Layer hopfieldLayer = neuralNetwork.getLayerAt(0); for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (j == i)
continue;
Neuron ni = hopfieldLayer.getNeuronAt(i);
Neuron nj = hopfieldLayer.getNeuronAt(j);
Connection cij = nj.getConnectionFrom(ni);
Connection cji = ni.getConnectionFrom(nj); double wij=0;
for(int k = 0;k < M;k++){
DataSetRow row=trainingSet.getRowAt(k);
double[] inputs=row.getInput();
wij+=inputs[i]*inputs[j];//Hebb学习规则
}
cij.getWeight().setValue(wij);
cji.getWeight().setValue(wij);
}// j
} // i } }
Hopfield神经网络实现污染字体的识别的更多相关文章
- Hopfield神经网络和TSP问题
一.TSP问题 旅行商问题,又叫货郎担问题.它是指如下问题:在完全图中寻找一条最短的哈密尔顿回路. 哈密尔顿回路问题:给定一个图,判断图中是否存在哈密尔顿回路. 哈密尔顿回路:寻找一条回路,经过图中所 ...
- tensorflow神经网络与单层手写字识别
1.知识点 """ 1.基础知识: 1.神经网络结构:1.输入层 2.隐含层 3.全连接层(类别个数=全连接层神经元个数)+softmax函数 4.输出层 2.逻辑回归: ...
- 五.反馈(Hopfield)神经网络
前馈网络一般指前馈神经网络或前馈型神经网络.它是一种最简单的神经网络,各神经元分层排列.每个神经元只与前一层的神经元相连.接收前一层的输出,并输出给下一层,数据正想流动,输出仅由当前的输入和网络权值决 ...
- Hopfield神经网络
神经网络分类 多层神经网络:模式识别 相互连接型网络:通过联想记忆去除数据中的噪声 1982年提出的Hopfield神经网络是最典型的相互连结型网络. 联想记忆 当输入模式为某种状态时,输出端要给出与 ...
- BP神经网络的手写数字识别
BP神经网络的手写数字识别 ANN 人工神经网络算法在实践中往往给人难以琢磨的印象,有句老话叫“出来混总是要还的”,大概是由于具有很强的非线性模拟和处理能力,因此作为代价上帝让它“黑盒”化了.作为一种 ...
- Hopfield 神经网络及稳态性的证明
根据其提出者,John Joseph Hopfield 命名.Hopfield 在 1982 年提出的划时代的:Neural networks and physical systems with em ...
- TensorFlow卷积神经网络实现手写数字识别以及可视化
边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...
- 利用c++编写bp神经网络实现手写数字识别详解
利用c++编写bp神经网络实现手写数字识别 写在前面 从大一入学开始,本菜菜就一直想学习一下神经网络算法,但由于时间和资源所限,一直未展开比较透彻的学习.大二下人工智能课的修习,给了我一个学习的契机. ...
- [纯C#实现]基于BP神经网络的中文手写识别算法
效果展示 这不是OCR,有些人可能会觉得这东西会和OCR一样,直接进行整个字的识别就行,然而并不是. OCR是2维像素矩阵的像素数据.而手写识别不一样,手写可以把用户写字的笔画时间顺序,抽象成一个维度 ...
随机推荐
- Linux云自动化运维第五课
Linux云自动化运维第五课 一.进程定义 进程就是cpu未完成的工作 二.ps命令 ps a ###关于当前环境的所有进程 x ###与当前环境无关的所有进程 f ###显示进程从属关系 e ### ...
- Ceres Solver for android
最近开发中,需要对图片做一些处理与线性技术,这时就用到了Ceres Solver.如何把Ceres Solver集成到Android里呢? 官网给了一个解决方案,简洁明了: Downloa ...
- centos下编译phantomjs2.0
phantomjs是一个无头浏览器,可以用来做测试和爬虫,但是因为有一些问题没有解决,所以官网不提供2.0版本的binary包,所以要自己编译. 1.安装需要的依赖: sudo yum -y inst ...
- JPlayer Jquery video视频插件
近日一直在搜关于视频的jquery插件,要求功能全,跨平台,百思不得其解,偶尔找到一个插件JPlayer,国产的,很全.为什么选择JPlayer 简单:几分钟就可以上手编码.部署 可定制:可以方便地用 ...
- 老李推荐:第14章5节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态
老李推荐:第14章5节<MonkeyRunner源码剖析> HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态 poptest是国内唯一 ...
- jQ层级选择器
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- XML文档结构
<?xml version="1.0" encoding="UTF-8"?> <books> <bool id="bk1 ...
- 记一次 Newtonsoft.Json 巧妙的用法(C#)
数据添加的功能 有一个表格提交数据如下: 是否选择和文本值.分开保存到数据库太麻烦.取得时候也麻烦 想到了存成json数据.一个字段就可以了. html代码: <table class=&quo ...
- angular apply
<div ng-controller="firstController"> {{date}} </div> <script> var first ...
- 【2017-04-18】Ado.Net C#连接数据库进行增、删、改、查
一.简介 1.ado.net是一门数据库访问技术. 他可以通过程序来操作数据库 2.类库 Connection 类 和数据库交互,必须连接它.连接帮助指明数据库服务器.数据库名字.用户名.密码,和连接 ...