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维像素矩阵的像素数据.而手写识别不一样,手写可以把用户写字的笔画时间顺序,抽象成一个维度 ...
随机推荐
- [原创]HBase学习笔记(3)- Java程序访问HBase
这里介绍使用java api来访问和操作HBase,例如create.delete.select.update等操作. 1.HBase配置 配置HBase使用的zookeeper集群地址和端口. pr ...
- VisualVM监控远程主机上的JAVA应用程序
使用VisualVM监控远程主机上JAVA应用程序时,需要开启远程主机上的远程监控访问,或者在远程JAVA应用程序启动时,开启远程监控选项,两种方法,选择其中一种就可以开启远程监控功能,配置完成后就可 ...
- Windows:将cmd命令行添加到右键中方法
win10中将命令行cmd添加到右键的方法 Windows cmd 右键 win10 命令行 最近在学python,所以会用到很多库文件,但是有的库文件需要下载whl文件再通过cmd进行安装,所以每次 ...
- Emmet 快速编写html代码
简介 快速编写HTML代码 语法简单,语法类似css选择器,30分钟内你就可以搞定它.开发商为sublime.atom.brackets.hbuilder.webstrom等编辑器或IDE提供对应的插 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列7-消息事务
上篇博文中我们介绍了Azure Messaging-ServiceBus Messaging消息回执机制. Azure Messaging-ServiceBus Messaging消息回执机制 本文中 ...
- 通过取父级for循环的i来理解闭包,iife,匿名函数
在使用for循环的时候,假如需要在循环体中添加一个匿名函数处理其他的事情,那么,在这个匿名函数内,如果需要用到对应的i,因为闭包的缘故,循环体循环结束后才返回i,所以i最终为最后一次++的数值. ...
- STM32驱动OV7725摄像头颜色识别
实验目的: 使用stm32驱动OV7725摄像头进行图像实时采集,在tft屏幕上实时显示并识别图像中的特定颜色,在颜色的周围画上框. 实验现象: 我的工程代码链接: http://download.c ...
- Java基础之IO框架
一.流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. ...
- State模式学习笔记
选用了一个假设需要用户验证的例子进行State模式学习,这个例子并不恰当.无所谓了,只要能学习到其中的内容即可. 适用性: 1,一个对象的行为取决于他的状态,并且它必须在运行时刻依据状态改变他的行为. ...
- Windows入门基础:2.vs2013中Icon显示
第一:系统小图标的显示 wndclass.hIcon = LoadIcon(NULL,IDI_WARNING); //LoadIcon函数的第一的参数要为0,第二个参数是系统自定义的ID号: IDI_ ...