-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkVisualizer.java
83 lines (73 loc) · 2.27 KB
/
NetworkVisualizer.java
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 java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class NetworkVisualizer extends JPanel{
final static int UPS = 20;//Updates per Second
final static int inputNeuronX = 50;
final static int inputNeuronY = 50;
JFrame frame;
Neuron[][] network;
public NetworkVisualizer(Neuron[][] n){
network = n;
frame = new JFrame("Brain Visualizer");
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.repaint();
}
@Override
public void paint(Graphics g){
super.paint(g);//clears screen
Graphics2D g2 = (Graphics2D) g;
//g2.setStroke(new BasicStroke(5));
g.setColor(Color.gray);
g.fillRect(0,0,1000,1000);
//seperate inputs, 1d inputs assumed
for(int x = 0; x < network[0].length; x++){
g.setColor(Color.black);
g.fillOval(inputNeuronX + x*30, inputNeuronY, 10, 10);//x,y,w,h
if(neuronIndicator(network[0][x].value)){
g.setColor(Color.white);
g.fillOval(inputNeuronX + x*30 + 1, inputNeuronY + 1, 9, 9);//x,y,w,h
}
}
//outputs
for(int y = 1; y < network.length; y++){
for(int x = 0; x < network[y].length; x++){
g.setColor(Color.black);
g.fillRect(inputNeuronX + x*30, inputNeuronY + y*30, 10, 10);//x,y,w,h
if(neuronIndicator(network[y][x].value)){
g.setColor(Color.white);
g.fillRect(inputNeuronX + x*30 + 1, inputNeuronY + y*30 + 1, 9, 9);//x,y,w,h
}
//weights
for(int z = 0; z < network[y-1].length; z++){
double i = network[y][x].weights[z];
if(weightIndicator(i)){
g.setColor(new Color(0, 255, 0, (int)(255*(sigmoid(i) )) )); // 0.1 minimum
}else{
g.setColor(new Color(255, 0, 0, (int)(255*(sigmoid(i) )) ));
}
g.drawLine(inputNeuronX + z*30, inputNeuronY + (y-1)*30, inputNeuronX + x*30, inputNeuronY + y*30);
}
}
}
}
public boolean neuronIndicator(double value){
return value > 0.5;
}
public boolean weightIndicator(double value){
return value > 0.0;
}
public static double sigmoid(double i){
double returnValue = 1/(1+Math.exp(-i));
if(Double.isInfinite(returnValue)){
return 1.0;
}
return returnValue;
}
/*public static void main(String[]args){
NetworkVisualizer nv = new NetworkVisualizer();
}*/
}