Skip to content

Commit e0a1b50

Browse files
committed
adding nature of code examples
1 parent 2458be0 commit e0a1b50

615 files changed

Lines changed: 229203 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// An animated drawing of a Neural Network
2+
// Daniel Shiffman <http://www.shiffman.net>
3+
// Nature of Code
4+
5+
class Connection {
6+
// Connection is from Neuron A to B
7+
Neuron a;
8+
Neuron b;
9+
10+
// Connection has a weight
11+
float weight;
12+
13+
// Variables to track the animation
14+
boolean sending = false;
15+
PVector sender;
16+
17+
// Need to store the output for when its time to pass along
18+
float output = 0;
19+
20+
Connection(Neuron from, Neuron to, float w) {
21+
weight = w;
22+
a = from;
23+
b = to;
24+
}
25+
26+
27+
// The Connection is active
28+
void feedforward(float val) {
29+
output = val*weight; // Compute output
30+
sender = a.location.get(); // Start animation at Neuron A
31+
sending = true; // Turn on sending
32+
}
33+
34+
// Update traveling sender
35+
void update() {
36+
if (sending) {
37+
// Use a simple interpolation
38+
sender.x = lerp(sender.x, b.location.x, 0.1);
39+
sender.y = lerp(sender.y, b.location.y, 0.1);
40+
float d = PVector.dist(sender, b.location);
41+
// If we've reached the end
42+
if (d < 1) {
43+
// Pass along the output!
44+
b.feedforward(output);
45+
sending = false;
46+
}
47+
}
48+
}
49+
50+
// Draw line and traveling circle
51+
void display() {
52+
stroke(0);
53+
strokeWeight(1+weight*4);
54+
line(a.location.x, a.location.y, b.location.x, b.location.y);
55+
56+
if (sending) {
57+
fill(0);
58+
strokeWeight(1);
59+
ellipse(sender.x, sender.y, 16, 16);
60+
}
61+
}
62+
}
63+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// An animated drawing of a Neural Network
2+
// Daniel Shiffman <http://www.shiffman.net>
3+
// Nature of Code
4+
5+
Network network;
6+
7+
void setup() {
8+
size(750,200);
9+
smooth();
10+
11+
// Create the Network object
12+
network = new Network(width/2, height/2);
13+
14+
int layers = 3;
15+
int inputs = 2;
16+
17+
Neuron output = new Neuron(250, 0);
18+
for (int i = 0; i < layers; i++) {
19+
for (int j = 0; j < inputs; j++) {
20+
float x = map(i, 0, layers, -300, 300);
21+
float y = map(j, 0, inputs-1, -75, 75);
22+
Neuron n = new Neuron(x, y);
23+
if (i > 0) {
24+
for (int k = 0; k < inputs; k++) {
25+
Neuron prev = network.neurons.get(network.neurons.size()-inputs+k-j);
26+
network.connect(prev, n, random(1));
27+
}
28+
}
29+
if (i == layers-1) {
30+
network.connect(n, output, random(1));
31+
}
32+
network.addNeuron(n);
33+
}
34+
}
35+
network.addNeuron(output);
36+
}
37+
38+
void draw() {
39+
background(255);
40+
// Update and display the Network
41+
network.update();
42+
network.display();
43+
44+
// Every 30 frames feed in an input
45+
if (frameCount % 30 == 0) {
46+
network.feedforward(random(1),random(1));
47+
}
48+
}
49+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// An animated drawing of a Neural Network
2+
// Daniel Shiffman <http://www.shiffman.net>
3+
// Nature of Code
4+
5+
class Network {
6+
7+
// The Network has a list of neurons
8+
ArrayList<Neuron> neurons;
9+
10+
// The Network now keeps a duplicate list of all Connection objects.
11+
// This makes it easier to draw everything in this class
12+
ArrayList<Connection> connections;
13+
PVector location;
14+
15+
Network(float x, float y) {
16+
location = new PVector(x, y);
17+
neurons = new ArrayList<Neuron>();
18+
connections = new ArrayList<Connection>();
19+
}
20+
21+
// We can add a Neuron
22+
void addNeuron(Neuron n) {
23+
neurons.add(n);
24+
}
25+
26+
// We can connection two Neurons
27+
void connect(Neuron a, Neuron b, float weight) {
28+
Connection c = new Connection(a, b, weight);
29+
a.addConnection(c);
30+
// Also add the Connection here
31+
connections.add(c);
32+
}
33+
34+
// Sending an input to the first Neuron
35+
// We should do something better to track multiple inputs
36+
void feedforward(float input1, float input2) {
37+
Neuron n1 = neurons.get(0);
38+
n1.feedforward(input1);
39+
40+
Neuron n2 = neurons.get(1);
41+
n2.feedforward(input2);
42+
43+
}
44+
45+
// Update the animation
46+
void update() {
47+
for (Connection c : connections) {
48+
c.update();
49+
}
50+
}
51+
52+
// Draw everything
53+
void display() {
54+
pushMatrix();
55+
translate(location.x, location.y);
56+
for (Neuron n : neurons) {
57+
n.display();
58+
}
59+
60+
for (Connection c : connections) {
61+
c.display();
62+
}
63+
popMatrix();
64+
}
65+
}
66+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// An animated drawing of a Neural Network
2+
// Daniel Shiffman <http://www.shiffman.net>
3+
// Nature of Code
4+
5+
class Neuron {
6+
// Neuron has a location
7+
PVector location;
8+
9+
// Neuron has a list of connections
10+
ArrayList<Connection> connections;
11+
12+
// We now track the inputs and sum them
13+
float sum = 0;
14+
15+
// The Neuron's size can be animated
16+
float r = 32;
17+
18+
Neuron(float x, float y) {
19+
location = new PVector(x, y);
20+
connections = new ArrayList<Connection>();
21+
}
22+
23+
// Add a Connection
24+
void addConnection(Connection c) {
25+
connections.add(c);
26+
}
27+
28+
// Receive an input
29+
void feedforward(float input) {
30+
// Accumulate it
31+
sum += input;
32+
// Activate it?
33+
if (sum > 1) {
34+
fire();
35+
sum = 0; // Reset the sum to 0 if it fires
36+
}
37+
}
38+
39+
// The Neuron fires
40+
void fire() {
41+
r = 64; // It suddenly is bigger
42+
43+
// We send the output through all connections
44+
for (Connection c : connections) {
45+
c.feedforward(sum);
46+
}
47+
}
48+
49+
// Draw it as a circle
50+
void display() {
51+
stroke(0);
52+
strokeWeight(1);
53+
// Brightness is mapped to sum
54+
float b = map(sum,0,1,255,0);
55+
fill(b);
56+
ellipse(location.x, location.y, r, r);
57+
58+
// Size shrinks down back to original dimensions
59+
r = lerp(r,32,0.1);
60+
}
61+
}
62+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mode=Standard
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Connection {
2+
float weight;
3+
Neuron a;
4+
Neuron b;
5+
6+
Connection(Neuron from, Neuron to,float w) {
7+
weight = w;
8+
a = from;
9+
b = to;
10+
}
11+
12+
void display() {
13+
stroke(0);
14+
strokeWeight(weight*4);
15+
line(a.location.x, a.location.y, b.location.x, b.location.y);
16+
}
17+
}
18+
19+
20+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
Network network;
3+
4+
void setup() {
5+
size(640, 360);
6+
smooth();
7+
network = new Network(4,3,1);
8+
}
9+
10+
void draw() {
11+
background(255);
12+
network.display();
13+
}
14+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
3+
class Network {
4+
ArrayList<Neuron> neurons;
5+
PVector location;
6+
Network(int layers, int inputs, int outputs) {
7+
location = new PVector(width/2, height/2);
8+
9+
neurons = new ArrayList<Neuron>();
10+
11+
Neuron output = new Neuron(250, 0);
12+
for (int i = 0; i < layers; i++) {
13+
for (int j = 0; j < inputs; j++) {
14+
float x = map(i, 0, layers, -200, 200);
15+
float y = map(j, 0, inputs-1, -100, 100);
16+
println(j + " " + y);
17+
Neuron n = new Neuron(x, y);
18+
19+
if (i > 0) {
20+
for (int k = 0; k < inputs; k++) {
21+
Neuron prev = neurons.get(neurons.size()-inputs+k-j);
22+
prev.connect(n);
23+
}
24+
}
25+
26+
if (i == layers-1) {
27+
n.connect(output);
28+
}
29+
neurons.add(n);
30+
}
31+
}
32+
neurons.add(output);
33+
}
34+
35+
36+
void display() {
37+
pushMatrix();
38+
translate(location.x, location.y);
39+
for (Neuron n : neurons) {
40+
n.display();
41+
}
42+
popMatrix();
43+
}
44+
}
45+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
class Neuron {
4+
PVector location;
5+
6+
ArrayList<Connection> connections;
7+
8+
Neuron(float x, float y) {
9+
location = new PVector(x, y);
10+
connections = new ArrayList<Connection>();
11+
}
12+
13+
void connect(Neuron n) {
14+
Connection c = new Connection(this, n, random(1));
15+
connections.add(c);
16+
}
17+
18+
void display() {
19+
stroke(0);
20+
strokeWeight(1);
21+
fill(0);
22+
ellipse(location.x, location.y, 16, 16);
23+
24+
for (Connection c : connections) {
25+
c.display();
26+
}
27+
}
28+
}
29+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mode=Standard

0 commit comments

Comments
 (0)