⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 realnumbers.java

📁 利用JAVA分别实现了神经网络和遗传算法并对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 RealNumbers extends Problem{	private int numBits = 4;	private int numInputNumbers = 2;	private int upperBound = (int)Math.pow(2, numBits) - 1;	private int numTestCases = 4;	// calculate sum of numInputNumbers numbers...	public RealNumbers(int numHidden, double minError, HashMap outputLists){		super(numHidden, minError, outputLists);		numPatterns = 1000;		inputs = new double[numPatterns][numBits * numInputNumbers];		outputs = new double[numPatterns][numBits];		for(int i = 0; i < numPatterns; i++){			int sum = 0;			for(int j = 0; j < numInputNumbers; j++){				int num = random.nextInt(upperBound);				fillBits(num, inputs[i], j * numBits);				sum += num;			}			sum %= upperBound;			fillBits(sum, outputs[i], 0);		}	}	private void fillBits(int num, double [] inputs, int start){		int end = start + numBits;		int shiftBit = 1;		for(int i = start; i < end; i++){			inputs[i] = (num & shiftBit) > 0 ? 1 : 0;			shiftBit <<= 1;		}	}	private int getBits(double [] outputs, int start){		int num = 0;		int shiftBit = 1;		int end = start + numBits;		for(int i = start; i < end; i++){			if(outputs[i] > 0.7) //cutoff for value of 1				num |= shiftBit;			shiftBit <<= 1;		}		return num;	}	public int testPattern(double [] input, NeuralNetwork nn){		double [] output = new double[numBits];		nn.activate(input, output);		return getBits(output, 0);	}	public void trainingGenerationComplete(NeuralNetwork nn, Trainer trainer){		if(nn != null && trainer != null){			DefaultListModel listModel = (DefaultListModel)outputLists.get(new Integer(trainer.getType()));			listModel.setSize(numTestCases + 2);			double [] input = new double[numBits * numInputNumbers];			for(int i = 0; i < numTestCases; i++){				int sum = 0;				String outputText = "[";				for(int j = 0; j < numInputNumbers; j++){					int num = random.nextInt(upperBound);					if(j > 0)						outputText += ", ";					outputText += num;					fillBits(num, input, j * numBits);					sum += num;				}				sum %= upperBound;				outputText += "] -> " + testPattern(input, nn) + "[" + sum + "]";				listModel.setElementAt(outputText, i);			}			int index = numTestCases;			listModel.setElementAt(Double.toString(nn.getFitness()), index++);			if(trainer != null)				listModel.setElementAt(Integer.toString(trainer.getNumGenerations()), index++);			else				listModel.setElementAt("0", index++);		}	}}// vim:noet:ts=3:sw=3

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -