📄 xor.java
字号:
/* Copyright 2006, 2007 Brian Greer This file is part of the Java NN Trainer. Java NN Trainer is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Java NN Trainer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Java NN Trainer; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*/package problems;import javax.swing.DefaultListModel;import java.util.HashMap;import algorithms.Trainer;import algorithms.TrainerListener;import algorithms.NeuralNetwork;public class XOR extends Problem{ public XOR(int numHidden, double minError, HashMap outputLists){ super(numHidden, minError, outputLists); this.outputLists = outputLists; numPatterns = 1000; inputs = new double[numPatterns][2]; outputs = new double[numPatterns][1]; for(int i = 0; i < numPatterns; i++){ inputs[i][0] = random.nextInt(2); inputs[i][1] = random.nextInt(2); outputs[i][0] = (int)inputs[i][0] ^ (int)inputs[i][1]; } } public String testPattern(double [] input, NeuralNetwork nn){ int result = (int)input[0] ^ (int)input[1]; double [] output = new double[1]; nn.activate(input, output); double nnResult = 0; if(output[0] > 0.7) nnResult = 1; else if(output[0] > 0.3) nnResult = 0.5; String outputText = "[" + input[0] + ", " + input[1] + ", " + result + "] -> " + output[0] + " (" + nnResult + ")"; return outputText; } public void trainingGenerationComplete(NeuralNetwork nn, Trainer trainer){ if(nn != null && trainer != null){ DefaultListModel listModel = (DefaultListModel)outputLists.get(new Integer(trainer.getType())); listModel.setSize(6); double [] input = new double[2]; input[0] = 0; input[1] = 0; listModel.setElementAt(testPattern(input, nn), 0); input[0] = 0; input[1] = 1; listModel.setElementAt(testPattern(input, nn), 1); input[0] = 1; input[1] = 0; listModel.setElementAt(testPattern(input, nn), 2); input[0] = 1; input[1] = 1; listModel.setElementAt(testPattern(input, nn), 3); listModel.setElementAt(Double.toString(nn.getFitness()), 4); if(trainer != null) listModel.setElementAt(Integer.toString(trainer.getNumGenerations()), 5); else listModel.setElementAt("0", 5); } }}// vim:noet:ts=3:sw=3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -