Neuroph感知機實現記憶邏輯與
阿新 • • 發佈:2019-02-06
1.建立感知機
private void creatNetwork(int inputNeuronsCount){ //設定網路類別為感知機 this.setNetworkType(NeuralNetworkType.PERCEPTRON); //建立輸入神經元,表示輸入的刺激 NeuronProperties inputNeuron = new NeuronProperties(); inputNeuron.setProperty("neuronType", InputNeuron.class); //由輸入神經元構成的底層 Layer inputLayer = LayerFactory.createLayer(inputNeuronsCount, inputNeuron); this.addLayer(inputLayer); //在輸入層增加貝葉斯神經,表示神經元偏置 inputLayer.addNeuron(new BiasNeuron()); //設定傳遞函式為step()函式 NeuronProperties outputProperties = new NeuronProperties(); outputProperties.setProperty("transferFunction", TransferFunctionType.STEP); Layer outputLayer = LayerFactory.createLayer(1, outputProperties); this.addLayer(outputLayer); //將輸入層和輸出層進行全連線(輸入節點和每個神經元都兩兩連線) ConnectionFactory.fullConnect(inputLayer, outputLayer); NeuralNetworkFactory.setDefaultIO(this); Neuron n = outputLayer.getNeuronAt(0); System.out.println(n); //設定每個連線的權重,1和1是輸入節點到神經元的權值,-1.5是神經元的偏置 n.getInputConnections()[0].getWeight().setValue(1); n.getInputConnections()[1].getWeight().setValue(1); n.getInputConnections()[2].getWeight().setValue(-1.5); }
2.使用感知機記憶邏輯與
public static void main(String[] args) {
//建立學習資料集
DataSet trainingSet = new DataSet(2,1); //兩個輸入,一個輸出
trainingSet.addRow(new DataSetRow(new double[] {0,0},new double[] {Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[] {0,1},new double[] {Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[] {1,0},new double[] {Double.NaN}));
trainingSet.addRow(new DataSetRow(new double[] {1,1},new double[] {Double.NaN}));
simpleDemo perceptron = new simpleDemo(2);
for(DataSetRow row : trainingSet.getRows()){
perceptron.setInput(row.getInput());
perceptron.calculate();
double[] networkOutput = perceptron.getOutput();
System.out.println(Arrays.toString(row.getInput())+"="+Arrays.toString(networkOutput));
}
}