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

📄 neuralnet.java

📁 All the tool for build a network able to reconize any shapes. Very complete, a good base for anythi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package neuralNetwork;
/*
Coded by Aydin Gurel, 2003
The code is free, but please contact me if you wish to use the code entirely or partially in any kind of project so that I can reference it and please don't delete these lines so that other people can reach this information. Also, please inform me if you encounter a bug.
aydingurel@hotmail.com
http://aydingurel.brinkster.com/neural
*/


import java.io.*;


public class NeuralNet {

	Neuron[] neurons;
	Synapse[] synapses;
	int nolayers; // no of layers, inc. input and output layers
	Layer[] layers;
	private Randomizer randomizer;
	
	// constructor
	// opens the configuration file and creates a net according to it.
	public NeuralNet (String path, Randomizer randomizer) {
		this.randomizer = randomizer;
		LineReader linereader = new LineReader(path);
		while (linereader.NextLineSplitted()){
			// if it declares # of objects, dimension the appropriate array
			if (linereader.column[0].compareTo("#neurons") == 0) { neurons = new Neuron[Integer.parseInt(linereader.column[1])]; }
			if (linereader.column[0].compareTo("#synapses") == 0) { synapses = new Synapse[Integer.parseInt(linereader.column[1])]; }
			// if it represents an input neuron, create a neuron object
			if (linereader.column[0].compareTo("i") == 0) { neurons[Integer.parseInt(linereader.column[1])] = new Neuron(Integer.parseInt(linereader.column[1])); }
			// if it represents a neuron, create a neuron object
			if (linereader.column[0].compareTo("n") == 0) { neurons[Integer.parseInt(linereader.column[1])] = new Neuron( Integer.parseInt(linereader.column[1]), Integer.parseInt(linereader.column[2]), Double.parseDouble(linereader.column[3]), linereader.column[4].charAt(0), Double.parseDouble(linereader.column[5]), Double.parseDouble(linereader.column[6]), randomizer ); }
			// if it represents a synapse, create a synapse object
			if (linereader.column[0].compareTo("s") == 0) { synapses[Integer.parseInt(linereader.column[1])] = 
				new Synapse(
				 	neurons[Integer.parseInt(linereader.column[2])], 
					neurons[Integer.parseInt(linereader.column[3])],
					randomizer
				); }
		}
		linereader = null;
		// first find out how many layers there are
		int temp_maxlayer = 0;
		for (int i = 0; i < neurons.length; i++) {
			if (neurons[i].layer > temp_maxlayer) {temp_maxlayer = neurons[i].layer;}
		}
		nolayers = temp_maxlayer+1;
		// then create layer objects
		layers = new Layer[nolayers];
		for (int i = 0; i < nolayers; i++) {layers[i] = new Layer(i);}
		NeuronsInOut();
	}

	// another constructor. creates a MULTILAYER PERCEPTRON with 
	// given no. of layers, no of neurons, learning rates, momentum parameters,
	// axonfamilies, flatness. except for noofneurons, all parameters are
	// ineffectual for the first layer.
	public NeuralNet (int[] noofneurons, double[] learningratecoefficient, char[] axonfamily, double[] momentumrate, double[] axonfuncflatness, Randomizer randomizer) {
		this.randomizer = randomizer;
		int temp_nooflayers = noofneurons.length;
		nolayers = noofneurons.length;
		// determine the no of neurons and create the array
		int temp_noofneurons = 0;
		for ( int i = 0; i < temp_nooflayers; i++ ) {
			temp_noofneurons += noofneurons[i];
		}
		neurons = new Neuron[temp_noofneurons];
		// determine the no of synapses and create the array
		int temp_noofsynapses = 0;
		for ( int i = 0; i < temp_nooflayers-1; i++ ) {
			temp_noofsynapses += noofneurons[i] * noofneurons[i+1];
		}
		synapses = new Synapse[temp_noofsynapses];
		// instantiate neurons:
		int temp_neuronidcounter = 0;
		// first instantiate input neurons
		for ( int i = 0; i < noofneurons[0]; i++ ) {
			neurons[temp_neuronidcounter] = new Neuron(temp_neuronidcounter);
			temp_neuronidcounter++;
		}
		// then instantiate hidden and output neurons
		for ( int i = 1; i < temp_nooflayers; i++ ) {
			for ( int j = 0; j < noofneurons[i]; j++ ) {
				neurons[temp_neuronidcounter] = new Neuron(temp_neuronidcounter, i, axonfuncflatness[i], axonfamily[i], momentumrate[i], learningratecoefficient[i], randomizer);
				temp_neuronidcounter++;
			}
		}
		// then create layer objects
		layers = new Layer[temp_nooflayers];
		for (int i = 0; i < temp_nooflayers; i++) {layers[i] = new Layer(i);}
		// instantiate synapses
		int temp_synapseidcounter = 0;
		for ( int i = 0; i < temp_nooflayers-1; i++) {
			for ( int j = 0; j < layers[i].neurons.length; j++ ) {
				for ( int k = 0; k < layers[i+1].neurons.length; k++ ) {
					synapses[temp_synapseidcounter++] = new Synapse(layers[i].neurons[j], layers[i+1].neurons[k], randomizer);
				}
			}
		}
		NeuronsInOut();
	}	

	// This method is used by constructors only.
	// It determines the incoming and outgoing neurons / synapses for each neuron 
	// and set them in the neuron. This information is to be used later during feed forward and back propagation.
	private void NeuronsInOut () {
		// and then create neuronsin, neuronsout, synapsesin, synapsesout arrays in the neuron objects
		// in order to determine relationships between neurons
		Neuron[] temp_neuronsin;
		Neuron[] temp_neuronsout;
		Synapse[] temp_synapsesin;
		Synapse[] temp_synapsesout;
		
		int incounter; int outcounter;
		for (int i = 0; i < neurons.length; i++) {
			// first determine the dimension of the arrays
			temp_neuronsin = null;
			temp_neuronsout = null;
			incounter = 0; outcounter = 0;
			for (int j = 0; j < synapses.length; j++) {
				if (synapses[j].sourceunit == neurons[i]) {outcounter++;}
				if (synapses[j].targetunit == neurons[i]) {incounter++;}
			}
			temp_neuronsin = new Neuron[incounter];
			temp_synapsesin = new Synapse[incounter];
			temp_neuronsout = new Neuron[outcounter];
			temp_synapsesout = new Synapse[outcounter];
			// then fill each array
			incounter = 0; outcounter = 0;
			for (int j = 0; j < synapses.length; j++) {
				if (synapses[j].sourceunit == neurons[i]) {
					temp_neuronsout[outcounter] = synapses[j].targetunit;
					temp_synapsesout[outcounter++] = synapses[j];
				}
				if (synapses[j].targetunit == neurons[i]) {
					temp_neuronsin[incounter] = synapses[j].sourceunit;
					temp_synapsesin[incounter++] = synapses[j];
				}
			}
			// set them in the neuron
			neurons[i].InsOuts(temp_neuronsin, temp_neuronsout, temp_synapsesin, temp_synapsesout);
		}
	}

	// saves the configuration of the net to a file
	public void SaveConfig (String path) throws IOException {
		File outputFile = new File(path);
		FileWriter out = new FileWriter(outputFile);
		out.write("// Input units:\n");
		// no of neurons
		out.write("#neurons;"+neurons.length+"\n");
		out.write("// type;ID;layer;flatness;axonfamily;momentum;learningrate\n");
		// neurons
		for (int i = 0; i < neurons.length; i++) {
			if (neurons[i].layer == 0) {
				out.write("i;"+i+";0\n");
			}
			else {
				out.write("n;"+i+";"+neurons[i].layer+";"+neurons[i].axonfuncflatness+";"+neurons[i].axonfamily+";"+neurons[i].momentumrate+";"+neurons[i].learningratecoefficient+"\n");
			}
		}
		// synapses
		out.write("#synapses;"+synapses.length+"\n");
		out.write("// type; ID; sourceunit; targetunit\n");
		for (int i = 0; i < synapses.length; i++) {
			out.write("s;"+i+";"+synapses[i].sourceunit.id+";"+synapses[i].targetunit.id+"\n");
		}
		out.close();
		
	}

	// loads weights of the net from a file
	public void LoadWeights (String path) {
		LineReader linereader = new LineReader(path);
		while (linereader.NextLineSplitted()) {
			// if it's a synapse weight 
			if (linereader.column[0].compareTo("w") == 0) { synapses[Integer.parseInt(linereader.column[1])].weight = Double.parseDouble(linereader.column[2]); }
			// if it's a neuron threshold
			if (linereader.column[0].compareTo("t") == 0) { neurons[Integer.parseInt(linereader.column[1])].threshold = Double.parseDouble(linereader.column[2]); }
		}
		linereader = null;
	}
	
	// saves weights to a file
	public void SaveWeights (String path) throws IOException {
		File outputFile = new File(path);
		FileWriter out = new FileWriter(outputFile);
		// first write weight of each synapse
		for (int i = 0; i < synapses.length; i++) {
			out.write("w; "+i+"; "+synapses[i].weight+"\n");
		}
		out.write("\n");
		// then threshold of each neuron
		for (int i = 0; i < neurons.length; i++) {
			out.write("t; "+i+"; "+neurons[i].threshold+"\n");
		}
		out.close();
	}

	// feeds the network forward and updates all the neurons.
	public void FeedForward (double[] inputs) {
		// feed input values
		for (int i = 0; i < layers[0].neurons.length; i++) {
			layers[0].neurons[i].output = inputs[i];
		}
		// begin from the first layer and propagate through layers.
		for (int i = 1; i < nolayers; i++) {
			// update the output of each neuron in this layer
			for (int j = 0; j < layers[i].neurons.length; j++) {
				layers[i].neurons[j].UpdateOutput();
			}
		}
	}
	
	// takes an array of input values, put them to the input neurons, feeds the net forward and returns the outputs of the output layer
	public double[] Output (double[] inputs) {

⌨️ 快捷键说明

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