Skip to content
Snippets Groups Projects
BPNet.java 15.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    package cz.vsb.mro0010.neuralnetworks;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class BPNet extends MultiLayeredNet {
    
    	protected float error;
    	
    	
    	public float getError() {
    		return error;
    	}
    
    
    	public void setError(float error) {
    		this.error = error;
    	}
    
    
    	protected float tolerance;
    	protected String neuronType;
    	protected float learnCoeff;
    	
    
    	
    	public BPNet( float tolerance, int nrOfLayers, int nrOfInputs, ArrayList<Integer> nrOfNeuronsPerLayer, float slope, float learnCoeff ) {
    		super(nrOfInputs, nrOfLayers, nrOfNeuronsPerLayer);
    		this.neuronType = "SigmoidalNeuron";
    		this.tolerance = tolerance;
    		this.learnCoeff = learnCoeff;
    		
    		for (int i = 0; i < nrOfLayers; i++) {
    			for (int j = 0; j < nrOfNeuronsPerLayer.get(i); j++) {
    				this.neuronLayers.get(i).add(new SigmoidalNeuron(slope));
    			}
    		}
    		for (int i = 0; i < nrOfLayers; i++) {
    			this.interconnectionsLayers.add(new InterconnectionsBP(this.learnCoeff));
    		}
    		for (Neuron neuronIn : this.inputNeuronLayer) {
    			for (Neuron neuronFirstLevel : this.neuronLayers.get(0)) {
    				this.interconnectionsLayers.get(0).addConnection(new Connection(neuronIn, neuronFirstLevel, (float) (Math.random())));
    			}
    		}
    		for (int i = 1; i < nrOfLayers; i++) {
    			for (Neuron neuronIn : this.neuronLayers.get(i-1)) {
    				for (Neuron neuronOut : this.neuronLayers.get(i)) {
    					this.interconnectionsLayers.get(i).addConnection(new Connection(neuronIn, neuronOut, (float) (Math.random())));
    				}
    				
    			}
    		}
    		
    		
    	}
    	
    	
    	public float getTolerance() {
    		return tolerance;
    	}
    
    	public void setTolerance(float tolerance) {
    		this.tolerance = tolerance;
    	}
    
    	
    	@Override
    	public String getNeuronType() {
    		return this.neuronType;
    	}
    
    	@Override
    	public int learn(String trainingSet) {
    		boolean learned = false;
    		int iter = 0;
    		ArrayList<String> trainingElements = new ArrayList<String>(Arrays.asList(trainingSet.split("\n")));
    		while(!learned) {
    			learned = true;
    			this.error = 0;
    			for (int i = 0; i < trainingElements.size(); i++) {
    				learned &= learnStep(trainingElements.get(i));
    			}
    			iter++;
    //			System.out.println(iter);
    		}
    		return  iter;
    //		System.out.println("Learned in " + iter + " whole training set iterations.");
    	}
    	
    	public boolean learnStep(String trainingElement) {
    		// Run training Element
    		String[] splitedTrainingElement = trainingElement.split(" ");
    		StringBuffer inputString = new StringBuffer();
    		for (int i = 0; i < this.nrOfInputs; i++) { //Input values
    			inputString.append(splitedTrainingElement[i]);
    			inputString.append(" ");
    		}
    		ArrayList<Float> expectedValues = new ArrayList<Float>();
    		for (int i = this.nrOfInputs; i < splitedTrainingElement.length; i++) { //Expected values
    			expectedValues.add(Float.parseFloat(splitedTrainingElement[i]));
    		}
    		this.run(inputString.substring(0, inputString.length() - 1));
    		// Calculate error
    		float error = 0;
    		for (int i = 0; i < expectedValues.size(); i++) {
    			float y = this.neuronLayers.get(this.nrOfLayers-1).get(i).getState(); //output of ith neuron
    			float o = expectedValues.get(i);
    			error += (float)( 0.5 * Math.pow((y-o), 2));
    		}
    		if (this.error < error) {
    			this.error = error;
    		}
    		if (error > this.tolerance) { //Error is too high -> modify weights
    			// Calculate deltas
    			for (int i = this.nrOfLayers - 1; i >= 0; i -= 1) {
    				for (Neuron n : this.neuronLayers.get(i)) {
    					SigmoidalNeuron neuron = (SigmoidalNeuron)n;
    					if (i == this.nrOfLayers - 1) { //Top layer
    						float y = neuron.getState();
    						float o = expectedValues.get(this.neuronLayers.get(i).indexOf(neuron));
    						float delta = y - o;
    						neuron.setError(delta);
    					} else { //Other layers
    						ArrayList<Connection> connectionsToUpperLayerFromNeuron = new ArrayList<Connection>();
    						// Find all connections, that have "neuron" as input
    						for (Connection c : this.interconnectionsLayers.get(i+1).getConnections()) { 
    							if (c.getInputNeuron().equals(neuron))
    								connectionsToUpperLayerFromNeuron.add(c);
    						}
    						float delta = 0;
    						for (Connection c : connectionsToUpperLayerFromNeuron) {
    							float deltaUpper = ((SigmoidalNeuron)c.getOutputNeuron()).getError();
    							float lambdaUpper = ((SigmoidalNeuron)c.getOutputNeuron()).getSlope();
    							float yUpper = c.getOutputNeuron().getState();
    							float w = c.getWeight();
    							delta += deltaUpper*lambdaUpper*yUpper*(1-yUpper)*w;
    						}
    						neuron.setError(delta);
    					}
    				}
    			}
    			// Adjust weights
    			for (Interconnections interconnectionsLayer : this.interconnectionsLayers) {
    				interconnectionsLayer.adjustWeights();
    			}
    			return false;
    		} else {
    			return true;
    		}
    		
    		
    	}
    	
    	public String getOutput() {
    		StringBuffer output = new StringBuffer();
    		ArrayList<Neuron> outputLayer = this.neuronLayers.get(this.nrOfLayers-1);
    		for (int i = 0; i < outputLayer.size(); i++) {
    			output.append(String.valueOf(outputLayer.get(i).getState()));
    			output.append(" ");
    		}
    
    		return output.toString();
    	}
    	
    
    
    
    	public void changeSlopeTo(float slope) {
    		for (ArrayList<Neuron> neuronLayer : this.neuronLayers) {
    			for (Neuron neuron : neuronLayer) {
    				((SigmoidalNeuron)neuron).setSlope(slope);
    			}
    		}
    	}
    
    
    	public void changeLearnCoeffTo(float learnCoeff) {
    		for (Interconnections layer : interconnectionsLayers) {
    			((InterconnectionsBP)layer).setLearningRate(learnCoeff);
    		}
    		
    	}
    
    
    	public void resetWeights() {
    		for (Interconnections layer : interconnectionsLayers) {
    			for (Connection connection : layer.getConnections()) {
    				connection.setWeight((float)Math.random());
    			}
    		}
    		
    	}
    	
    	public void addNeuron(int layerIndex, float slope) {
    		SigmoidalNeuron newNeuron = new SigmoidalNeuron(slope);
    		neuronLayers.get(layerIndex).add(newNeuron);
    		if ((layerIndex < nrOfLayers) && (layerIndex >= 0)) {
    			Interconnections inputConnectionLayer = this.interconnectionsLayers.get(layerIndex);
    			if (layerIndex == 0) {
    				ArrayList<InputLayerPseudoNeuron> inputNeurons = this.inputNeuronLayer;
    				for (Neuron inputNeuron : inputNeurons) {
    					inputConnectionLayer.addConnection(new Connection(inputNeuron, newNeuron, (float)Math.random()));
    				}
    			} else {
    				ArrayList<Neuron> inputNeurons = this.neuronLayers.get(layerIndex - 1);
    				for (Neuron inputNeuron : inputNeurons) {
    					inputConnectionLayer.addConnection(new Connection(inputNeuron, newNeuron, (float)Math.random()));
    				}
    			} 
    			
    			if (layerIndex < nrOfLayers - 1) {
    				Interconnections outputConnectionLayer = this.interconnectionsLayers.get(layerIndex + 1);
    				ArrayList<Neuron> outputNeurons = this.neuronLayers.get(layerIndex + 1);
    				for (Neuron outputNeuron : outputNeurons) {
    					outputConnectionLayer.addConnection(new Connection(newNeuron, outputNeuron, (float)Math.random()));
    				}
    			}
    			this.nrOfNeuronsPerLayer.set(layerIndex, this.nrOfNeuronsPerLayer.get(layerIndex) + 1 );
    			
    			
    		}	else {
    			
    			throw new InvalidLayerNumberException();
    			
    		}
    	}
    	
    	public void removeNeuron(int layerIndex) {
    		int nrOfNeuronsInThisLayer = this.nrOfNeuronsPerLayer.get(layerIndex);
    		if ((layerIndex < nrOfLayers) && (layerIndex >= 0)) {
    			if (nrOfNeuronsInThisLayer == 1) {
    				
    				removeNeuronLayer(layerIndex);
    				
    			} else {
    				Neuron removedNeuron = this.neuronLayers.get(layerIndex).get(nrOfNeuronsInThisLayer - 1);
    				Interconnections inputConnectionLayer = this.interconnectionsLayers.get(layerIndex);
    				ArrayList<Connection> removedConnections = new ArrayList<Connection>();
    				for (Connection connection : inputConnectionLayer.getConnections()) {
    					if (connection.getOutputNeuron().equals(removedNeuron)) {
    						removedConnections.add(connection);
    					}
    				}
    				for (Connection connection : removedConnections) {
    					inputConnectionLayer.getConnections().remove(connection);
    				}
    				removedConnections = new ArrayList<Connection>();
    				if (layerIndex < nrOfLayers - 1) {
    					Interconnections outputConnectionLayer = this.interconnectionsLayers.get(layerIndex + 1);
    					for (Connection connection : outputConnectionLayer.getConnections()) {
    						if (connection.getInputNeuron().equals(removedNeuron)) {
    							removedConnections.add(connection);
    						}
    					}
    					for (Connection connection : removedConnections) {
    						outputConnectionLayer.getConnections().remove(connection);
    					}
    				}
    				
    				this.neuronLayers.get(layerIndex).remove(removedNeuron);
    				this.nrOfNeuronsPerLayer.set(layerIndex, this.nrOfNeuronsPerLayer.get(layerIndex) - 1 );
    			} 
    					
    		} else {
    			throw new InvalidLayerNumberException();
    		}
    	}
    	
    	public void addNeuronLayer(int nrOfNeurons, int layerIndex, float slope) {
    		if ((layerIndex < nrOfLayers + 1) && (layerIndex >= 0) && (nrOfNeurons > 0)) {
    			
    			this.nrOfLayers++;
    			this.nrOfNeuronsPerLayer.add(layerIndex, nrOfNeurons);
    			// new layer creation
    			ArrayList<Neuron> newNeuronLayer = new ArrayList<Neuron>();
    			for (int i = 0; i < nrOfNeurons; i++) {
    				newNeuronLayer.add(new SigmoidalNeuron(slope));
    			}
    			// old connections removal
    			if (layerIndex < nrOfLayers - 1) { // only if inner layer is added
    				this.interconnectionsLayers.remove(layerIndex);
    			}
    			// new layer adding
    			this.neuronLayers.add(layerIndex, newNeuronLayer);
    			// new connections creation
    			// input
    			Interconnections inputConnLayer = new InterconnectionsBP(learnCoeff);
    			if (layerIndex == 0) {
    				ArrayList<InputLayerPseudoNeuron> inputNeurons = this.inputNeuronLayer;
    				ArrayList<Neuron> outputNeurons = newNeuronLayer; //Layers already shifted
    				for (Neuron inputNeuron : inputNeurons) {
    					for (Neuron outputNeuron : outputNeurons) {
    						inputConnLayer.addConnection(new Connection(inputNeuron, outputNeuron, (float)Math.random()));
    					}
    				}
    			} else {
    				ArrayList<Neuron> inputNeurons = this.neuronLayers.get(layerIndex - 1);
    				ArrayList<Neuron> outputNeurons = newNeuronLayer; //Layers already shifted, this is new layer
    				for (Neuron inputNeuron : inputNeurons) {
    					for (Neuron outputNeuron : outputNeurons) {
    						inputConnLayer.addConnection(new Connection(inputNeuron, outputNeuron, (float)Math.random()));
    					}
    				}
    			}
    			this.interconnectionsLayers.add(layerIndex, inputConnLayer);
    			// output
    			Interconnections outputConnLayer = new InterconnectionsBP(learnCoeff);
    			if (layerIndex < nrOfLayers - 1) {
    				ArrayList<Neuron> inputNeurons = newNeuronLayer;
    				ArrayList<Neuron> outputNeurons = this.neuronLayers.get(layerIndex + 1); //Layers already shifted
    				for (Neuron inputNeuron : inputNeurons) {
    					for (Neuron outputNeuron : outputNeurons) {
    						outputConnLayer.addConnection(new Connection(inputNeuron, outputNeuron, (float)Math.random()));
    					}
    				}
    				this.interconnectionsLayers.add(layerIndex + 1, outputConnLayer);
    			}
    			
    			
    		} else {
    			throw new InvalidLayerNumberException();
    		}
    		
    	}
    	
    	
    	
    	public void removeNeuronLayer(int layerIndex) {
    		if ((layerIndex < nrOfLayers ) && (layerIndex >= 0) && (nrOfLayers > 1)) {
    			// delete output connections
    			if (layerIndex < nrOfLayers - 1) {
    				this.interconnectionsLayers.remove(layerIndex + 1);
    			}
    			// delete input connections
    			this.interconnectionsLayers.remove(layerIndex);
    			// delete neurons on layer
    			this.neuronLayers.remove(layerIndex);
    			this.nrOfNeuronsPerLayer.remove(layerIndex);
    			this.nrOfLayers--;
    			// create new connections
    			if (layerIndex < nrOfLayers + 1) {
    				Interconnections connLayer = new InterconnectionsBP(learnCoeff);
    				if (layerIndex == 0) {
    					ArrayList<InputLayerPseudoNeuron> inputNeurons = this.inputNeuronLayer;
    					ArrayList<Neuron> outputNeurons = this.neuronLayers.get(0);
    					for (Neuron inputNeuron : inputNeurons) {
    						for (Neuron outputNeuron : outputNeurons) {
    							connLayer.addConnection(new Connection(inputNeuron, outputNeuron, (float)Math.random()));
    						}
    					}
    				} else {
    					ArrayList<Neuron> inputNeurons = this.neuronLayers.get(layerIndex - 1);
    					ArrayList<Neuron> outputNeurons = this.neuronLayers.get(layerIndex); 
    					for (Neuron inputNeuron : inputNeurons) {
    						for (Neuron outputNeuron : outputNeurons) {
    							connLayer.addConnection(new Connection(inputNeuron, outputNeuron, (float)Math.random()));
    						}
    					}
    				}
    				this.interconnectionsLayers.add(layerIndex, connLayer);
    				
    			}
    			
    		} else {
    			throw new InvalidLayerNumberException();
    		}
    	}
    	
    	@Override
    	public String toString() {
    		return getNeuronMap();
    	}
    	
    	public String getNeuronMap() {
    		StringBuffer map = new StringBuffer();
    		for (int i = 0; i < nrOfLayers; i++) {
    			map.append(String.valueOf(nrOfNeuronsPerLayer.get(i)));
    			map.append(" ");
    		}
    		map.deleteCharAt(map.length() - 1);
    		return map.toString();
    	}
    	
    	public static void main(String[] args) {
    		ArrayList<Integer> nrOfNeuronsPerLayer = new ArrayList<Integer>();
    		nrOfNeuronsPerLayer.add(10);
    		nrOfNeuronsPerLayer.add(7);
    		nrOfNeuronsPerLayer.add(2);
    		BPNet net = new BPNet( (float)0.01, 3, 5, nrOfNeuronsPerLayer, (float)1.8, (float)0.7); // bigger slope = better resolution
    		
    		String trainingSet = "0.4 0.5 1 0.5 1 0 1\n0 0 0 0 0 1 1\n0.1 0.2 0.3 0.4 0.5 0 0\n1 0 1 0 1 1 0\n0.2 0.4 0 0 0.9 0 1";
    		net.learn(trainingSet);
    		net.run("0.4 0.5 1 0.5 1"); //expected 0 1
    		System.out.println(net.getOutput());
    		net.run("0 0 0 0 0"); // 1 1
    		System.out.println(net.getOutput());
    		net.run("0.1 0.2 0.3 0.4 0.5"); // 0 0
    		System.out.println(net.getOutput());
    		net.run("1 0 1 0 1"); // 1 0
    		System.out.println(net.getOutput());
    		net.run("0.2 0.4 0 0 0.9"); // 0 1
    		System.out.println(net.getOutput());
    		
    		System.out.println("Not trained elements:");
    		net.run("0.9 0.1 0.9 0.1 0.9"); // expected 1 0
    		System.out.println(net.getOutput());
    		net.run("0.01 0.01 0.01 0.01 0.01"); // expected 1 1
    		System.out.println(net.getOutput());
    		net.run("0.15 0.15 0.35 0.35 0.5"); // 0 0
    		System.out.println(net.getOutput());
    		
    		System.out.println(net.getNeuronMap());
    		net.addNeuron(0, 1.8f);
    		System.out.println(net.getNeuronMap());
    		net.addNeuron(1, 1.8f);
    		System.out.println(net.getNeuronMap());
    		net.addNeuron(2, 1.8f);
    		System.out.println(net.getNeuronMap());
    		net.removeNeuron(0);
    		System.out.println(net.getNeuronMap());
    		net.removeNeuron(1);
    		System.out.println(net.getNeuronMap());
    		net.removeNeuron(2);
    		System.out.println(net.getNeuronMap());
    		
    		net.addNeuronLayer(5, 0, 1.8f);
    		System.out.println(net.getNeuronMap());
    		net.addNeuronLayer(5, 2, 1.8f);
    		System.out.println(net.getNeuronMap());
    		net.addNeuronLayer(5, 5, 1.8f);
    		System.out.println(net.getNeuronMap());
    		
    		net.removeNeuronLayer(5);
    		System.out.println(net.getNeuronMap());
    		net.removeNeuronLayer(2);
    		System.out.println(net.getNeuronMap());
    		net.removeNeuronLayer(0);
    		System.out.println(net.getNeuronMap());
    		
    		net.learn(trainingSet);
    		net.run("0.4 0.5 1 0.5 1"); //expected 0 1
    		System.out.println(net.getOutput());
    		net.run("0 0 0 0 0"); // 1 1
    		System.out.println(net.getOutput());
    		net.run("0.1 0.2 0.3 0.4 0.5"); // 0 0
    		System.out.println(net.getOutput());
    		net.run("1 0 1 0 1"); // 1 0
    		System.out.println(net.getOutput());
    		net.run("0.2 0.4 0 0 0.9"); // 0 1
    		System.out.println(net.getOutput());
    		
    		System.out.println("Not trained elements:");
    		net.run("0.9 0.1 0.9 0.1 0.9"); // expected 1 0
    		System.out.println(net.getOutput());
    		net.run("0.01 0.01 0.01 0.01 0.01"); // expected 1 1
    		System.out.println(net.getOutput());
    		net.run("0.15 0.15 0.35 0.35 0.5"); // 0 0
    		System.out.println(net.getOutput());
    	}
    
    }