gingerfish 0 Light Poster

Hi

i have the following code, Single Perceptron:

import java.text.DecimalFormat;
import java.text.NumberFormat;


public class SinglePerceptron {
 	
	double[][] patterns = {           // input values (training data(
	    { 0.958, 0.003},
	    { 1.043, 0.001},
 	    {1.907, 0.003},
 	    {0.780, 0.002},
 	    {0.579, 0.001},
 	    {0.003, 0.105},
 	    {0.001, 1.748},
 	    {0.014, 1.839},
 	    {0.007, 1.021},
 	    {0.004, 0.214}};
//            {0.005, 1.215}, {0.911, 0.001}};
 
 	
 	double[][] teachingOutput = {     // target values
 	    { 1, 0},
            { 1, 0},
            { 1, 0},
            { 1, 0},
            { 1, 0},
            { 0, 1},
            { 0, 1},
            { 0, 1},
            { 0, 1},
            { 0, 1}
 	     };
 
 	
 	int numberOfInputNeurons = patterns[0].length;
 	int numberOfOutputNeurons = teachingOutput[0].length;
 	int numberOfPatterns = patterns.length;
 	double[][] weights;


 	/////////////////////////////////////////////////////////////////////////////
	public SinglePerceptron() {
 	    weights = new double[numberOfPatterns][numberOfOutputNeurons];
 	}
 
 	/////////////////////////////////////////////////////////////////////////////
	public void deltaRule() {
 		boolean allCorrect = false;
 		boolean error = false;
 		double learningRate = 0.5;
 		while (!allCorrect) {               
 			error = false;
 			for (int i = 0; i < numberOfPatterns; i++) {
 				
 				int[] output = setOutputValues(i);
 				for (int j = 0; j < numberOfOutputNeurons; j++) {
 				    if (teachingOutput[i][j] != output[j]) {
 				        for (int k = 0; k < numberOfInputNeurons; k++) {
 				            weights[k][j] = weights[k][j] + learningRate
 				                    * patterns[i][k]
 				                    * (teachingOutput[i][j] - output[j]);
 				        }
 				    }
 				}
 				for (int z = 0; z < output.length; z++) {
 				    if (output[z] != teachingOutput[i][z])
 				        error = true;
 				}
 
 			 }
 			if (!error) {
 				allCorrect = true;                // It means learning is completed       
 			}
 		}
// 	System.out.println("number of pattern: "+numberOfPatterns);
//        System.out.println("number of output neurons: "+numberOfOutputNeurons);
//        System.out.println("number of input neurons: "+numberOfInputNeurons);

        }

 	
 	/////////////////////////////////////////////////////////////////////////////
 	int[] setOutputValues(int patternNo) {
 		double bias = 0.7;                       // threshhold
 		int[] result = new int[numberOfOutputNeurons];
 		double[] toImpress = patterns[patternNo];
 		for (int i = 0; i < toImpress.length; i++) {
 			
 			for (int j = 0; j < result.length; j++) {
 			    double net = weights[0][j] * toImpress[0] + weights[1][j]* toImpress[1] ;
//                                    + weights[2][j] * toImpress[2]
// 			            + weights[3][j] * toImpress[3];
 			    if (net > bias)                          
 			        result[j] = 1;
 			    else
 			        result[j] = 0;
 			}
 
 		}
 		return result;
 	}
 	
 	/////////////////////////////////////////////////////////////////////////////
 	public void printMatrix(double[][] matrix) {
 		
 		for (int i = 0; i < matrix.length; i++) {
 		    for (int j = 0; j < matrix[i].length; j++) {
 		        NumberFormat f = NumberFormat.getInstance();
 		        if (f instanceof DecimalFormat) {
 		            DecimalFormat decimalFormat = ((DecimalFormat) f);
 		            decimalFormat.setMaximumFractionDigits(1);
 		            decimalFormat.setMinimumFractionDigits(1);
 		            System.out.print("(" + f.format(matrix[i][j]) + ")");
 		        }
 		    }
 		    System.out.println();
 		}
 
 	}
 	
 	/////////////////////////////////////////////////////////////////////////////
 	public void printTest() {          // added by Sejong
		// test perceptron using training data 
		double calcVal = 0;

		for (int i = 0; i < patterns.length; i++) {        
	        for (int j = 0; j < numberOfOutputNeurons; j++) {
				calcVal = 0;
		        for (int k = 0; k < numberOfInputNeurons; k++) {
				    calcVal += patterns[i][k] * weights[k][j];
				}
				if (calcVal > 0.7) calcVal = 1;
				else calcVal = 0;
				System.out.print((int)calcVal+",") ; 
			}
			System.out.println();

		}
		System.out.println();

  	}

	////////////////////////////////////////////////////////////////////////////
	public static void main(String args[]) {
 		
		// Training
		SinglePerceptron p = new SinglePerceptron();
 		System.out.println("Weights before training: ");
 		p.printMatrix(p.weights);
 		p.deltaRule(); 
 		System.out.println("Weights after training: ");
 		p.printMatrix(p.weights);
 
		// Test 
		System.out.println("Test ------------------------------");
 		p.printTest();


	}

 }

i got an assignment to calculate the output of new input data (pattern): {0.005, 1.215}, {0.911, 0.001}
to show like : (1, 0) or (0, 1)

please help how to calculate???

thanQ :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.