`

ann xor

阅读更多
/**
 * Network
 * Copyright 2005 by Jeff Heaton(jeff@jeffheaton.com)
 *
 * Example program from Chapter 3
 * Programming Neural Networks in Java
 * http://www.heatonresearch.com/articles/series/1/
 *
 * This software is copyrighted. You may use it in programs
 * of your own, without restriction, but you may not
 * publish the source code without the author's permission.
 * For more information on distributing this code, please
 * visit:
 *    http://www.heatonresearch.com/hr_legal.php
 *
 * @author Jeff Heaton
 * @version 1.1
 */

public class Network {

  /**
   * The global error for the training.
   */
  protected double globalError;


  /**
   * The number of input neurons.
   */
  protected int inputCount;

  /**
   * The number of hidden neurons.
   */
  protected int hiddenCount;

  /**
   * The number of output neurons
   */
  protected int outputCount;

  /**
   * The total number of neurons in the network.
   */
  protected int neuronCount;

  /**
   * The number of weights in the network.
   */
  protected int weightCount;

  /**
   * The learning rate.
   */
  protected double learnRate;

  /**
   * The outputs from the various levels.
   */
  protected double fire[];

  /**
   * The weight matrix this, along with the thresholds can be
   * thought of as the "memory" of the neural network.
   */
  protected double matrix[];

  /**
   * The errors from the last calculation.
   */
  protected double error[];

  /**
   * Accumulates matrix delta's for training.
   */
  protected double accMatrixDelta[];

  /**
   * The thresholds, this value, along with the weight matrix
   * can be thought of as the memory of the neural network.
   */
  protected double thresholds[];

  /**
   * The changes that should be applied to the weight
   * matrix.
   */
  protected double matrixDelta[];

  /**
   * The accumulation of the threshold deltas.
   */
  protected double accThresholdDelta[];

  /**
   * The threshold deltas.
   */
  protected double thresholdDelta[];

  /**
   * The momentum for training.
   */
  protected double momentum;

  /**
   * The changes in the errors.
   */
  protected double errorDelta[];


  /**
   * Construct the neural network.
   *
   * @param inputCount The number of input neurons.
   * @param hiddenCount The number of hidden neurons
   * @param outputCount The number of output neurons
   * @param learnRate The learning rate to be used when training.
   * @param momentum The momentum to be used when training.
   */
  public Network(int inputCount,
                 int hiddenCount,
                 int outputCount,
                 double learnRate,
                 double momentum) {

    this.learnRate = learnRate;
    this.momentum = momentum;

    this.inputCount = inputCount;
    this.hiddenCount = hiddenCount;
    this.outputCount = outputCount;
    neuronCount = inputCount + hiddenCount + outputCount;
    weightCount = (inputCount * hiddenCount) + (hiddenCount * outputCount);

    fire        = new double[neuronCount];
    matrix      = new double[weightCount];
    matrixDelta = new double[weightCount];
    thresholds  = new double[neuronCount];
    errorDelta  = new double[neuronCount];
    error       = new double[neuronCount];
    accThresholdDelta = new double[neuronCount];
    accMatrixDelta = new double[weightCount];
    thresholdDelta = new double[neuronCount];

    reset();
  }



  /**
   * Returns the root mean square error for a complet training set.
   *
   * @param len The length of a complete training set.
   * @return The current error for the neural network.
   */
  public double getError(int len) {
    double err = Math.sqrt(globalError / (len * outputCount));
    globalError = 0;  // clear the accumulator
    return err;

  }

  /**
   * The threshold method. You may wish to override this class to provide other
   * threshold methods.
   *
   * @param sum The activation from the neuron.
   * @return The activation applied to the threshold method.
   */
  public double threshold(double sum) {
    return 1.0 / (1 + Math.exp(-1.0 * sum));
  }

  /**
   * Compute the output for a given input to the neural network.
   *
   * @param input The input provide to the neural network.
   * @return The results from the output neurons.
   */
  public double []computeOutputs(double input[]) {
    int i, j;
    final int hiddenIndex = inputCount;
    final int outIndex = inputCount + hiddenCount;

    for (i = 0; i < inputCount; i++) {
      fire[i] = input[i];
    }

    // first layer
    int inx = 0;

    for (i = hiddenIndex; i < outIndex; i++) {
      double sum = thresholds[i];

      for (j = 0; j < inputCount; j++) {
        sum += fire[j] * matrix[inx++];
      }
      fire[i] = threshold(sum);
    }

    // hidden layer

    double result[] = new double[outputCount];

    for (i = outIndex; i < neuronCount; i++) {
      double sum = thresholds[i];

      for (j = hiddenIndex; j < outIndex; j++) {
        sum += fire[j] * matrix[inx++];
      }
      fire[i] = threshold(sum);
      result[i-outIndex] = fire[i];
    }

    return result;
  }


  /**
   * Calculate the error for the recogntion just done.
   *
   * @param ideal What the output neurons should have yielded.
   */
  public void calcError(double ideal[]) {
    int i, j;
    final int hiddenIndex = inputCount;
    final int outputIndex = inputCount + hiddenCount;

    // clear hidden layer errors
    for (i = inputCount; i < neuronCount; i++) {
      error[i] = 0;
    }

    // layer errors and deltas for output layer
    for (i = outputIndex; i < neuronCount; i++) {
      error[i] = ideal[i - outputIndex] - fire[i];
      globalError += error[i] * error[i];
      errorDelta[i] = error[i] * fire[i] * (1 - fire[i]);
    }

    // hidden layer errors
    int winx = inputCount * hiddenCount;

    for (i = outputIndex; i < neuronCount; i++) {
      for (j = hiddenIndex; j < outputIndex; j++) {
        accMatrixDelta[winx] += errorDelta[i] * fire[j];
        error[j] += matrix[winx] * errorDelta[i];
        winx++;
      }
      accThresholdDelta[i] += errorDelta[i];
    }

    // hidden layer deltas
    for (i = hiddenIndex; i < outputIndex; i++) {
      errorDelta[i] = error[i] * fire[i] * (1 - fire[i]);
    }

    // input layer errors
    winx = 0;  // offset into weight array
    for (i = hiddenIndex; i < outputIndex; i++) {
      for (j = 0; j < hiddenIndex; j++) {
        accMatrixDelta[winx] += errorDelta[i] * fire[j];
        error[j] += matrix[winx] * errorDelta[i];
        winx++;
      }
      accThresholdDelta[i] += errorDelta[i];
    }
  }

  /**
   * Modify the weight matrix and thresholds based on the last call to
   * calcError.
   */
  public void learn() {
    int i;

    // process the matrix
    for (i = 0; i < matrix.length; i++) {
      matrixDelta[i] = (learnRate * accMatrixDelta[i]) + (momentum * matrixDelta[i]);
      matrix[i] += matrixDelta[i];
      accMatrixDelta[i] = 0;
    }

    // process the thresholds
    for (i = inputCount; i < neuronCount; i++) {
      thresholdDelta[i] = learnRate * accThresholdDelta[i] + (momentum * thresholdDelta[i]);
      thresholds[i] += thresholdDelta[i];
      accThresholdDelta[i] = 0;
    }
  }

  /**
   * Reset the weight matrix and the thresholds.
   */
  public void reset() {
    int i;

    for (i = 0; i < neuronCount; i++) {
      thresholds[i] = 0.5 - (Math.random());
      thresholdDelta[i] = 0;
      accThresholdDelta[i] = 0;
    }
    for (i = 0; i < matrix.length; i++) {
      matrix[i] = 0.5 - (Math.random());
      matrixDelta[i] = 0;
      accMatrixDelta[i] = 0;
    }
  }
}



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

/**
 * XorExample
 * Copyright 2005 by Jeff Heaton(jeff@jeffheaton.com)
 *
 * Example program from Chapter 3
 * Programming Neural Networks in Java
 * http://www.heatonresearch.com/articles/series/1/
 *
 * This software is copyrighted. You may use it in programs
 * of your own, without restriction, but you may not
 * publish the source code without the author's permission.
 * For more information on distributing this code, please
 * visit:
 *    http://www.heatonresearch.com/hr_legal.php
 *
 * @author Jeff Heaton
 * @version 1.1
 */
public class XorExample extends JFrame implements
ActionListener,Runnable {

  /**
   * The train button.
   */
  JButton btnTrain;

  /**
   * The run button.
   */
  JButton btnRun;

  /**
   * The quit button.
   */
  JButton btnQuit;

  /**
   * The status line.
   */
  JLabel status;

  /**
   * The background worker thread.
   */
  protected Thread worker = null;

/**
 * The number of input neurons.
 */
  protected final static int NUM_INPUT = 2;

/**
 * The number of output neurons.
 */
  protected final static int NUM_OUTPUT = 1;

/**
 * The number of hidden neurons.
 */
  protected final static int NUM_HIDDEN = 3;

/**
 * The learning rate.
 */
  protected final static double RATE = 0.5;

/**
 * The learning momentum.
 */
  protected final static double MOMENTUM = 0.7;


  /**
   * The training data that the user enters.
   * This represents the inputs and expected
   * outputs for the XOR problem.
   */
  protected JTextField data[][] = new JTextField[4][4];

  /**
   * The neural network.
   */
  protected Network network;



  /**
   * Constructor. Setup the components.
   */
  public XorExample()
  {
    setTitle("XOR Solution");
    network = new Network(
                         NUM_INPUT,
                         NUM_HIDDEN,
                         NUM_OUTPUT,
                         RATE,
                         MOMENTUM);

    Container content = getContentPane();

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    content.setLayout(gridbag);

    c.fill = GridBagConstraints.NONE;
    c.weightx = 1.0;

    // Training input label
    c.gridwidth = GridBagConstraints.REMAINDER; //end row
    c.anchor = GridBagConstraints.NORTHWEST;
    content.add(
               new JLabel(
                         "Enter training data:"),c);

    JPanel grid = new JPanel();
    grid.setLayout(new GridLayout(5,4));
    grid.add(new JLabel("IN1"));
    grid.add(new JLabel("IN2"));
    grid.add(new JLabel("Expected OUT   "));
    grid.add(new JLabel("Actual OUT"));

    for ( int i=0;i<4;i++ ) {
      int x = (i&1);
      int y = (i&2)>>1;
      grid.add(data[i][0] = new JTextField(""+y));
      grid.add(data[i][1] = new JTextField(""+x));
      grid.add(data[i][2] = new JTextField(""+(x^y)));
      grid.add(data[i][3] = new JTextField("??"));
      data[i][0].setEditable(false);
      data[i][1].setEditable(false);
      data[i][3].setEditable(false);
    }

    content.add(grid,c);

    // the button panel
    JPanel buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(btnTrain = new JButton("Train"));
    buttonPanel.add(btnRun = new JButton("Run"));
    buttonPanel.add(btnQuit = new JButton("Quit"));
    btnTrain.addActionListener(this);
    btnRun.addActionListener(this);
    btnQuit.addActionListener(this);

    // Add the button panel
    c.gridwidth = GridBagConstraints.REMAINDER; //end row
    c.anchor = GridBagConstraints.CENTER;
    content.add(buttonPanel,c);

    // Training input label
    c.gridwidth = GridBagConstraints.REMAINDER; //end row
    c.anchor = GridBagConstraints.NORTHWEST;
    content.add(
               status = new JLabel("Click train to begin training..."),c);

    // adjust size and position
    pack();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension d = toolkit.getScreenSize();
    setLocation(
               (int)(d.width-this.getSize().getWidth())/2,
               (int)(d.height-this.getSize().getHeight())/2 );
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    setResizable(false);

    btnRun.setEnabled(false);
  }

  /**
   * The main function, just display the JFrame.
   *
   * @param args No arguments are used.
   */
  public static void main(String args[])
  {
    (new XorExample()).show(true);
  }

  /**
   * Called when the user clicks one of the three
   * buttons.
   *
   * @param e The event.
   */
  public void actionPerformed(ActionEvent e)
  {
    if ( e.getSource()==btnQuit )
      System.exit(0);
    else if ( e.getSource()==btnTrain )
      train();
    else if ( e.getSource()==btnRun )
      evaluate();
  }

  /**
   * Called when the user clicks the run button.
   */
  protected void evaluate()
  {
    double xorData[][] = getGrid();
    int update=0;

    for (int i=0;i<4;i++) {
      NumberFormat nf = NumberFormat.getInstance();
      double d[] = network.computeOutputs(xorData[i]);
      data[i][3].setText(nf.format(d[0]));
    }

  }


  /**
  * Called when the user clicks the train button.
  */
  protected void train()
  {
    if ( worker != null )
      worker = null;
    worker = new Thread(this);
    worker.setPriority(Thread.MIN_PRIORITY);
    worker.start();
  }

  /**
  * The thread worker, used for training
  */
  public void run()
  {
    double xorData[][] = getGrid();
    double xorIdeal[][] = getIdeal();
    int update=0;

    int max = 10000;
    for (int i=0;i<max;i++) {
      for (int j=0;j<xorData.length;j++) {
        network.computeOutputs(xorData[j]);
        network.calcError(xorIdeal[j]);
        network.learn();
      }


      update++;
      if (update==100) {
        status.setText( "Cycles Left:" + (max-i) + ",Error:" + network.getError(xorData.length) );
        update=0;
      }
    }
    btnRun.setEnabled(true);
  }


  /**
   * Called to generate an array of doubles based on
   * the training data that the user has entered.
   *
   * @return An array of doubles
   */
  double [][]getGrid()
  {
    double array[][] = new double[4][2];

    for ( int i=0;i<4;i++ ) {
      array[i][0] =
      Float.parseFloat(data[i][0].getText());
      array[i][1] =
      Float.parseFloat(data[i][1].getText());
    }

    return array;
  }

  /**
   * Called to the the ideal values that that the neural network
   * should return for each of the grid training values.
   *
   * @return The ideal results.
   */
  double [][]getIdeal()
  {
    double array[][] = new double[4][1];

    for ( int i=0;i<4;i++ ) {
      array[i][0] =
      Float.parseFloat(data[i][2].getText());
    }

    return array;
  }


}
分享到:
评论

相关推荐

    Classifying XOR gate using ann:Classifying XOR gate using ann-matlab开发

    标题中的“Classifying XOR gate using ann”指的是使用人工神经网络(ANN)来解决异或(XOR)门的分类问题。XOR门是一个经典的逻辑运算问题,它的输出是两个输入信号的异或,即只有当输入不同时,输出才为1。在神经网络...

    ann.rar_40

    描述提到“3级神经网络实现'异或'功能”,这表明这个项目是用一个具有三个层级(输入层、隐藏层和输出层)的神经网络来模拟解决经典的逻辑运算问题——异或(XOR)。异或通常被用作检验神经网络是否具备一定程度的非...

    bp神经网络matlab源码-python-ann:用Python编程ANN

    ANN 为什么选择 Python 简单易学 非常易读的代码(易于遵循结构) 程序、面向对象和功能元素都在一定程度上存在 适用于所有平台 完全免费 广泛应用于科学计算(与 MATLAB、Mathematica 或 Maple 一样强大) 广泛用于...

    人工神经网络概述.pptx

    * 1969年Minsky 证明线性(单层)感知机不能解决 XOR 问题,ANN 进入低潮 * 1982年加州理工 Hopfield 提出结点全互联 ANN 模型(Hopfield 模型) * 1987年ICNN(International Conference on NN)召开 * 1987年加州...

    ann-playground:人工神经网络实验

    BackPropagationXOR :使用带有S形激活函数(值从0到1)的反向传播来解决XOR问题PerceptronAND :使用简单的多层感知器解决AND运算符。 反向传播无法解决这个简单的问题AdalineDigits :仅当模式识别与训练使用的...

    Artificial_neural_network:使用 XOR 测试人工神经网络。 它将成为更大事物的基础

    在本项目中,我们将探讨如何使用Java实现一个简单的ANN,并通过经典的XOR问题来验证其功能。 XOR(异或)问题在逻辑运算中是一个重要的例子,因为它无法通过单一的逻辑门(如与、或、非)直接解决,但可以通过两个...

    C++ OPENCV 神經網路範例實作收藏

    GITHUB: https://github.com/jash-git/CB_OPENCV249_ANN_EX 01.3輸入XOR- http://thomazmaia.com/pages/tutorials/mlpopencv3.html 02.分類+繪圖顯示結果- ...

    ann-backpropagation:人工神经网络反向传播的实现

    人工神经网络(ANN)是一种受到生物神经元结构启发的计算模型,用于处理复杂的数据和模式识别任务。在ANN中,反向传播算法是训练多层感知器(MLP)网络最常用的方法之一,它通过调整权重来最小化预测输出与实际输出...

    智能仪表 课件 人工神经网络.ppt

    - **1969年**:Minsky和Papert发表了一篇关于感知机局限性的文章,指出感知机无法解决某些复杂问题,如XOR问题(异或问题),这引发了对神经网络研究的质疑。 #### 结语 尽管经历了起伏,人工神经网络的研究仍然...

    神经网络

    神经网络 Haskell中的人工神经网络。...Numeric.ANN.XOR模块训练网络以学习XOR运算符。 $ stack ghci ... &gt; import Numeric.ANN.XOR &gt; net &lt;- xorNet &gt; runNet [1,0] net 0.969682524723555 执照 根据BSD3许可。

    BP神经网络

    `ann.cpp`可能包含了整个神经网络的主程序,包括读取数据、训练网络、测试网络等部分。对于XOR问题,训练数据会是四组{(0, 0), (1, 0), (0, 1), (1, 1)},对应的期望输出分别是{(0, 1), (1, 0), (1, 0), (0, 1)}。 ...

    人工神经网络之异或分类器

    人工神经网络(Artificial Neural Networks, ANN)是一种模拟生物神经元网络结构的计算模型,它在机器学习领域扮演着重要角色。在这个项目中,我们关注的是一个特定的应用——异或分类器,它是神经网络初学者常常...

    人工神经网络 适合神经网络学习的问题

    人工神经网络(Artificial Neural Networks, ANN)是受生物神经元结构启发的一种计算模型,用于模拟大脑中的信息处理过程。ANN的基本单位是人工神经元,它由三个主要部分组成:输入(树突)、处理单元(细胞体)和...

    Fast Artifi cial Neural Network Library PDF文档

    最简单的例子之一是ANN学习XOR函数,但这同样适用于学习文本的语言特性,或判断X光图像中是否可见肿瘤。 若要让人工神经网络学习某一问题,必须将其定义为具有输入和输出变量集的函数,并提供该函数应如何工作的...

    AI:面向初学者的神经网络(第3部分,共3部分)

    而"ga_ann_xor.zip"文件可能包含了一个示例,演示如何用遗传算法训练神经网络来解决异或(XOR)问题。异或问题是一个经典的非线性问题,通常用来测试神经网络的性能。在这个例子中,遗传算法可能会帮助找到一组权重...

    第三讲&#40;2&#41;人工神经网络&#40;BP算法&#41;.ppt

    单层感知机无法解决如异或(XOR)问题这样的非线性问题,而大部分有趣的问题都是非线性的。BP算法通过引入多层结构,使得网络有能力学习非线性关系,从而解决了这一问题。 BP网络的拓扑结构包括输入层、隐藏层和...

    AI:面向初学者的神经网络(第2部分,共3部分)

    "Backprop_ANN_XOR.zip"这个文件名暗示了我们将用神经网络解决异或问题(XOR)。异或问题是一个经典示例,因为它展示了单层神经网络的局限性,而多层网络如MLP能够成功解决。在XOR问题中,我们的目标是让网络学习...

    Java Encog神经网络简介

    Java Encog是一个强大的开源框架,专为在Java平台上构建和训练人工神经网络(ANN)而设计。它提供了多种类型的神经网络结构,如多层感知器、RBF网络、自组织映射(SOM)以及遗传编程和进化算法。通过Encog,开发者...

    61046606ABCNNTrain.zip_matlab例程_matlab_

    描述中提到的"Training Artificial Neural Network"是指利用MATLAB进行人工神经网络(Artificial Neural Network, ANN)的学习过程。神经网络是一种模仿人脑神经元结构的计算模型,它通过学习数据中的模式来执行各种...

Global site tag (gtag.js) - Google Analytics