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维像素矩阵的像素数据.而手写识别不一样,手写可以把用户写字的笔画时间顺序,抽象成一个维度 ...
随机推荐
- 相机标定:kalibr标定工具箱使用总结
1 多相机标定 1.1采集图像和IMU 1.2制作Bag包 1)组织文件结构 ~/kalibr_workspace/test/stereo_calib bagsrc cam0 (1+time(0))* ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- ASP.NET Core MVC和Visual Studio入门
本教程将教你使用Visual Studio 2017创建 ASP.NET Core MVC web应用程序的基础知识. 安装Visual Studio 2017 和.Net Core 安装Visual ...
- linux sort命令详解(转)
sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参数格式: sort [-bcfMnrtk][源文件][-o 输出文件] 补充说明:sort可针对文本文件的内容,以行为单位来排序. 参 数 ...
- CSS 画三角形、圆
<div class="square"></div> <style> .square { height: 0px; width: 0px; bo ...
- 用jQuery模拟淘宝购物车
首先我们要实现的内容的需求有如下几点: 1.在购物车页面中,当选中"全选"复选框时,所有商品前的复选框被选中,否则所有商品的复选框取消选中. 2.当所有商品前的复选框选中时,&qu ...
- .NET遇上Docker - 使用Docker Compose组织Ngnix和.NETCore运行
本文工具准备: Docker for Windows Visual Studio 2015 与 Visual Studio Tools for Docker 或 Visual Studio 2017 ...
- ABCD多选正则表达式
正则表达式: 4个选项,可单选可多选不允许重复 ABCD正则: regexp : /^(?!.*((A.*){2,}|(B.*){2,}|(C.*){2,}|(D.*){2,})$)[A-D]{1,4 ...
- position relative
position的默认值是static,(也就是说对于任意一个元素,如果没有定义它的position属性,那么它的position:static) 如果你想让这个#demo里的一个div#sub相对于 ...
- 【web】之 jquery上传插件的Plupload的使用
首先下载plupload->http://www.plupload.com 因为Plupload可配置参数比较多,所以这里讲解最常用的,结合jquery-ui展示的界面!如下: Plupload ...