Download code

Jump to: navigation, search

Back to Perceptron_(Java)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

Perceptron.java

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Perceptron_(Java)?oldid=19184
 14 */
 15 
 16 
 17 import java.text.DecimalFormat;
 18 import java.text.NumberFormat;
 19 
 20 import org.junit.Test;
 21 
 22 public class Perceptron {
 23 	
 24 	int[][] patterns = { 
 25 	    { 0, 0, 0, 0 }, 
 26 	    { 0, 0, 0, 1 }, 
 27 	    { 0, 0, 1, 0 },
 28 	    { 0, 0, 1, 1 }, 
 29 	    { 0, 1, 0, 0 }, 
 30 	    { 0, 1, 0, 1 }, 
 31 	    { 0, 1, 1, 0 },
 32 	    { 0, 1, 1, 1 }, 
 33 	    { 1, 0, 0, 0 }, 
 34 	    { 1, 0, 0, 1 } };
 35 
 36 	
 37 	int[][] teachingOutput = { 
 38 	    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
 39 	    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
 40 	    { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
 41 	    { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, 
 42 	    { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 },
 43 	    { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, 
 44 	    { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
 45 	    { 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 }, 
 46 	    { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
 47 	    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 } };
 48 	
 49 	int numberOfInputNeurons = patterns[0].length;
 50 	int numberOfOutputNeurons = teachingOutput.length;
 51 	int numberOfPatterns = patterns.length;
 52 	double[][] weights;
 53 
 54 	public Perceptron() {
 55 	    weights = new double[numberOfInputNeurons][numberOfOutputNeurons];
 56 	}
 57 	public void deltaRule() {
 58 		boolean allCorrect = false;
 59 		boolean error = false;
 60 		double learningFactor = 0.2;
 61 		while (!allCorrect) {
 62 			error = false;
 63 			for (int i = 0; i < numberOfPatterns; i++) {
 64 				
 65 				int[] output = setOutputValues(i);
 66 				for (int j = 0; j < numberOfOutputNeurons; j++) {
 67 				    if (teachingOutput[i][j] != output[j]) {
 68 				        for (int k = 0; k < numberOfInputNeurons; k++) {
 69 				            weights[k][j] = weights[k][j] + learningFactor
 70 				                    * patterns[i][k]
 71 				                    * (teachingOutput[i][j] - output[j]);
 72 				        }
 73 				    }
 74 				}
 75 				for (int z = 0; z < output.length; z++) {
 76 				    if (output[z] != teachingOutput[i][z])
 77 				        error = true;
 78 				}
 79 			 }
 80 			if (!error) {
 81 				allCorrect = true;
 82 			}
 83 		}
 84 	}
 85 	
 86 	int[] setOutputValues(int patternNo) {
 87 		double bias = 0.7;
 88 		int[] result = new int[numberOfOutputNeurons];
 89 		int[] toImpress = patterns[patternNo];
 90 		for (int i = 0; i < toImpress.length; i++) {
 91 			
 92 			for (int j = 0; j < result.length; j++) {
 93 			    double net = weights[0][j] * toImpress[0] + weights[1][j]
 94 			            * toImpress[1] + weights[2][j] * toImpress[2]
 95 			            + weights[3][j] * toImpress[3];
 96 			    if (net > bias)
 97 			        result[j] = 1;
 98 			    else
 99 			        result[j] = 0;
100 			}
101 		}
102 		return result;
103 	}
104 	
105 	public void printMatrix(double[][] matrix) {
106 		
107 		for (int i = 0; i < matrix.length; i++) {
108 		    for (int j = 0; j < matrix[i].length; j++) {
109 		        NumberFormat f = NumberFormat.getInstance();
110 		        if (f instanceof DecimalFormat) {
111 		            DecimalFormat decimalFormat = ((DecimalFormat) f);
112 		            decimalFormat.setMaximumFractionDigits(1);
113 		            decimalFormat.setMinimumFractionDigits(1);
114 		            System.out.print("(" + f.format(matrix[i][j]) + ")");
115 		        }
116 		    }
117 		    System.out.println();
118 		}
119 	}
120 	
121 	@Test
122 	public void testPerceptron() {
123 		
124 		Perceptron p = new Perceptron();
125 		System.out.println("Weights before training: ");
126 		p.printMatrix(p.weights);
127 		p.deltaRule();
128 		System.out.println("Weights after training: ");
129 		p.printMatrix(p.weights);
130 	}
131 }


hijacker
hijacker
hijacker
hijacker