Skip to content

Commit b6c574c

Browse files
committed
Perceptron and Driver pushed
1 parent f876b3c commit b6c574c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Driver.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Driver {
2+
public static void main(String args[])
3+
{
4+
int[][][] data = Perceptron.anddata;
5+
double[] weights = Perceptron.INTIAL_WEIGHTS;
6+
Driver driver = new Driver();
7+
Perceptron perceptron = new Perceptron();
8+
int epochnumber = 0;
9+
boolean errorflag = true;
10+
double error = 0;
11+
double[] adjustedweights = null;
12+
while(errorflag){
13+
driver.printHeading(epochnumber++);
14+
errorflag = false ;
15+
error = 0;
16+
for(int x =0; x< data.length;x++){
17+
double weightedsum = perceptron.calculateWeightedsum(data[x][0], weights);
18+
int result = perceptron.applyActivationFunction(weightedsum);
19+
error= data[x][1][0] - result ;
20+
if(error !=0 ) errorflag = true;
21+
adjustedweights = perceptron.adjustweight(data[x][0], weights, error);
22+
driver.printvector(data[x],weights, result , error, weightedsum, adjustedweights);
23+
weights = adjustedweights;
24+
25+
26+
}
27+
28+
}
29+
30+
}
31+
public void printHeading( int epochNumber)
32+
{
33+
System.out.println("\n===========================================Epoch #" + epochNumber+"======================================================");
34+
System.out.println(" w1 | w2 | x1 | x2 | Target Result | Result | error | Weighted sum | adjusted w1 | adjusted w2 ");
35+
System.out.println("--------------------------------------------------------------------------------------------------------------------");
36+
}
37+
public void printvector(int[][] data, double[] weights , int result , double error , double weightedsum , double[] adjustedweights ) {
38+
System.out.println(" "+String.format("%.2f",weights[0])+"|"+String.format("%.2f",weights[1])+"|"+data[0][0]+"|"+data[0][1]+
39+
" | "+data[1][0]+" | "+result+" | "+error+" | "+String.format("%.2f", weightedsum)+
40+
" | "+String.format("%.2f",adjustedweights[0]) + " | "+String.format("%.2f",adjustedweights[1]));
41+
42+
}
43+
}

src/Perceptron.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Perceptron {
2+
public static final int[][][] anddata = {{{0,0},{0}},
3+
{{0,1},{0}},
4+
{{1,0},{0}},
5+
{{1,1},{1}}};
6+
public static final double LEARNING_RATE = 0.05;
7+
public static final double[] INTIAL_WEIGHTS = {0.33, 0.65};//{Math.random(),Math.random()};
8+
public double calculateWeightedsum(int[] data, double[] weights){
9+
double weightedsum = 0;
10+
for(int x=0;x< data.length; x++) weightedsum += data[x]*weights[x];
11+
return weightedsum;
12+
}
13+
public int applyActivationFunction(double weightedsum){
14+
int result = 0;
15+
if(weightedsum>1)result = 1;
16+
return result;
17+
}
18+
public double[] adjustweight(int[] data, double[] weights, double error){
19+
double[] adjustedweights = new double[weights.length];
20+
for(int x= 0; x< weights.length;x++) adjustedweights[x]= LEARNING_RATE * error * data[x] + weights[x];
21+
return adjustedweights;
22+
}
23+
24+
25+
26+
}

0 commit comments

Comments
 (0)