-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
83 lines (72 loc) · 2.56 KB
/
Copy pathMain.java
File metadata and controls
83 lines (72 loc) · 2.56 KB
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
import javax.swing.*;
import java.util.Random;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.util.concurrent.CountDownLatch;
public class Main extends JFrame{
private static final long serialVersionUID = 1L;
static int width = 400;
static int height = 400;
public static void main(String[] args){
JFrame window = new JFrame();
window.setSize(width*2, height*2);
window.setTitle("Feed Forward Supervised Learning");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
Neuron neo= new Neuron();
Random r= new Random();
Point2D.Float[] pointsArray= new Point2D.Float[100];
int[] guessArray = new int[pointsArray.length];
int[] calculatedAnswers = new int[pointsArray.length];
for(int i=0;i<pointsArray.length;i++){
//Initialize points in Cartesian Plane
pointsArray[i]= new Point2D.Float((r.nextFloat()*2-1)*width,(r.nextFloat()*2-1)*height);
guessArray[i]=neo.guess(pointsArray[i]);
calculatedAnswers[i]=f(pointsArray[i].x)>=pointsArray[i].y?1:-1;
}
paint p = new paint(pointsArray,calculatedAnswers,guessArray,neo.weights);
window.add(p);
while(!(p.ans.equals(p.gs))){
System.out.println(neo.weights[0]+" "+neo.weights[1]);
waitForSpace();
for(int i=0;i<pointsArray.length;i++){
neo.train(pointsArray[i],calculatedAnswers[i]);
guessArray[i]=neo.guess(pointsArray[i]);
p.gs[i]=guessArray[i];
p.w=neo.weights.clone();
window.update(window.getGraphics());
}
}
System.out.println("Weights Tweaked!!!");
return;
}
public static Point2D.Float toPixelPlane(Point2D.Float p){
return new Point2D.Float(p.x+width,mod(p.y-height));
}
public static float mod(float x){
return x<0?-x:x;
}
public static float f(float x){
return 0.2f*x-0.364f;
}
public static void waitForSpace() {
final CountDownLatch latch = new CountDownLatch(1);
KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
// Anonymous class invoked from EDT
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE)
latch.countDown();
return false;
}
};
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
try {
latch.await();
} catch (InterruptedException e1) {
e1.printStackTrace();
} // current thread waits here until countDown() is called
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);
}
}