标签:
要求如下:
所以当神经元输出函数选择在硬极函数的时候,如果想分成上面的四个类型,则必须要2个神经元,其实至于所有的分类问题,n个神经元则可以分成2的n次方类型。
又前一节所证明出来的关系有:
从而算出了所有的权重的值。。
代码实现如下:
第一个类是用来操实际操作的类,真正核心的内容是在PerceptronClassifyNoLearn中。
package com.cgrj.com; import java.util.Arrays; import org.neuroph.core.data.DataSet; import org.neuroph.core.data.DataSetRow; import org.neuroph.nnet.Perceptron; public class MyNeturol { public static void main(String[] args) { // TODO Auto-generated method stub DataSet trainingSet=new DataSet(2,2); trainingSet.addRow(new DataSetRow(new double[]{1,2},new double[]{Double.NaN,Double.NaN})); trainingSet.addRow(new DataSetRow(new double[]{1,1},new double[]{Double.NaN,Double.NaN})); trainingSet.addRow(new DataSetRow(new double[]{2,0},new double[]{Double.NaN,Double.NaN})); trainingSet.addRow(new DataSetRow(new double[]{2,-1},new double[]{Double.NaN,Double.NaN})); trainingSet.addRow(new DataSetRow(new double[]{-1,2},new double[]{Double.NaN,Double.NaN})); trainingSet.addRow(new DataSetRow(new double[]{-2,1},new double[]{Double.NaN,Double.NaN})); trainingSet.addRow(new DataSetRow(new double[]{-1,-1},new double[]{Double.NaN,Double.NaN})); trainingSet.addRow(new DataSetRow(new double[]{-2,-2},new double[]{Double.NaN,Double.NaN})); PerceptronClassifyNoLearn perceptronClassifyNoLearn=new PerceptronClassifyNoLearn(2); for(DataSetRow row:trainingSet.getRows()){ perceptronClassifyNoLearn.setInput(row.getInput()); perceptronClassifyNoLearn.calculate(); double[] netWorkOutput=perceptronClassifyNoLearn.getOutput(); System.out.println(Arrays.toString(row.getInput())+"="+Arrays.toString(netWorkOutput)); } } }
PerceptronClassifyNoLearn规定了输入层和输出层的属性和规则,由于是无法学的,所以其判定规则是依然设定好了的,在此类中。
package com.cgrj.com; import org.neuroph.core.Layer; import org.neuroph.core.NeuralNetwork; import org.neuroph.core.Neuron; import org.neuroph.nnet.comp.neuron.BiasNeuron; import org.neuroph.nnet.comp.neuron.InputNeuron; 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 PerceptronClassifyNoLearn extends NeuralNetwork { public PerceptronClassifyNoLearn(int inputNeuronsCount){ this.createNetWork(inputNeuronsCount); } private void createNetWork(int inputNeuronsCount) { //设置网络感知机 this.setNetworkType(NeuralNetworkType.PERCEPTRON); //构建输入神经元,表示输入的刺激 NeuronProperties inputNeuronProperties=new NeuronProperties(); inputNeuronProperties.setProperty("neuronType", InputNeuron.class); //由输入神经元构成的输入层 Layer inputLayer=LayerFactory.createLayer(inputNeuronsCount,inputNeuronProperties); this.addLayer(inputLayer); //给输入层增加BiasNeron,表示神经元偏置 inputLayer.addNeuron(new BiasNeuron()); //构建输出神经元 NeuronProperties outputNeuronProperties=new NeuronProperties(); outputNeuronProperties.setProperty("transferFunction", TransferFunctionType.STEP); Layer outputLayer=LayerFactory.createLayer(2, outputNeuronProperties); this.addLayer(outputLayer); ConnectionFactory.fullConnect(inputLayer, outputLayer); NeuralNetworkFactory.setDefaultIO(this); Neuron n=outputLayer.getNeuronAt(0); n.getInputConnections()[0].getWeight().setValue(-3); n.getInputConnections()[1].getWeight().setValue(-1); n.getInputConnections()[2].getWeight().setValue(1); n=outputLayer.getNeuronAt(1); n.getInputConnections()[0].getWeight().setValue(1); n.getInputConnections()[1].getWeight().setValue(-2); n.getInputConnections()[2].getWeight().setValue(0); } }
标签:
原文地址:http://www.cnblogs.com/beigongfengchen/p/5462020.html