# Perceptron

Model was written in NetLogo 5.0.3
•
Viewed 368 times
•
Downloaded 27 times
•
Run 1 time

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)

## Comments and Questions

Please start the discussion about this model!
(You'll first need to log in.)

Click to Run Model

globals [ epoch-error ;; average error in this epoch perceptron ;; a single output-node input-node-1 ;; keep the input nodes in globals so we can refer input-node-2 ;; to them directly and distinctly ] ;; A perceptron is modeled by input-node and bias-node agents ;; connected to an output-node agent. ;; Connections from input nodes to output nodes ;; in a perceptron. links-own [ weight ] ;; all nodes have an activation ;; input nodes have a value of 1 or -1 ;; bias-nodes are always 1 turtles-own [activation] breed [ input-nodes input-node ] ;; bias nodes are input-nodes whose activation ;; is always 1. breed [ bias-nodes bias-node ] ;; output nodes compute the weighted sum of their ;; inputs and then set their activation to 1 if ;; the sum is greater than their threshold. An ;; output node can also be the input-node for another ;; perceptron. breed [ output-nodes output-node ] output-nodes-own [threshold] ;; ;; Setup Procedures ;; to setup clear-all ;; set our background to something more viewable than black ask patches [ set pcolor white - 3 ] set-default-shape input-nodes "circle" set-default-shape bias-nodes "bias-node" set-default-shape output-nodes "output-node" create-output-nodes 1 [ set activation random-activation set xcor 6 set size 2 set threshold 0 set perceptron self ] create-bias-nodes 1 [ set activation 1 setxy 3 7 set size 1.5 my-create-link-to perceptron ] create-input-nodes 1 [ setup-input-node set label "Node 1" setxy -6 5 set input-node-1 self ] create-input-nodes 1 [ setup-input-node set label "Node 2" setxy -6 0 set input-node-2 self ] ask perceptron [ compute-activation ] reset-ticks end to setup-input-node set activation random-activation set size 1.5 my-create-link-to perceptron set label-color magenta end ;; links an input or bias node to an output node to my-create-link-to [ anode ] ;; input or bias node procedure create-link-to anode [ set color red + 1 ;; links start with a random weight set weight random-float 0.1 - 0.05 ] end ;; ;; Runtime Procedures ;; ;; train sets the input nodes to a random input ;; it then computes the output ;; it determines the correct answer and back propagates the weight changes to train ;; observer procedure set epoch-error 0 repeat examples-per-epoch [ ;; set the input nodes randomly ask input-nodes [ set activation random-activation ] ;; distribute error ask perceptron [ compute-activation update-weights target-answer recolor ] ] ;; plot stats set epoch-error epoch-error / examples-per-epoch set epoch-error epoch-error * 0.5 tick end ;; compute activation by summing the inputs * weights ;; and run through sign function which determines whether ;; the computed value is above or below the threshold to compute-activation ;; output-node procedure set activation sign sum [ [activation] of end1 * weight ] of my-in-links recolor end to update-weights [ answer ] ;; output-node procedure let output-answer activation ;; calculate error for output nodes let output-error answer - output-answer ;; update the epoch-error set epoch-error epoch-error + (answer - sign output-answer) ^ 2 ;; examine input output edges and set their new weight ;; increasing or decreasing it by a value determined by the learning-rate ask my-in-links [ set weight weight + learning-rate * output-error * [activation] of end1 ] end ;; computes the sign function given an input value to-report sign [input] ;; output-node procedure ifelse input > threshold [ report 1 ] [ report -1 ] end to-report random-activation ;; observer procedure ifelse random 2 = 0 [ report 1 ] [ report -1 ] end to-report target-answer ;; observer procedure let a [activation] of input-node-1 = 1 let b [activation] of input-node-2 = 1 report ifelse-value (run-result (word "my-" target-function " a b")) [1][-1] end to-report my-or [a b];; output-node procedure report (a or b) end to-report my-xor [a b] ;; output-node procedure report (a xor b) end to-report my-and [a b] ;; output-node procedure report (a and b) end to-report my-nor [a b] ;; output-node procedure report not (a or b) end to-report my-nand [a b] ;; output-node procedure report not (a and b) end ;; test runs one instance and computes the output to test ;; observer procedure ask input-node-1 [ set activation test-input-node-1-value ] ask input-node-2 [ set activation test-input-node-2-value ] ;; compute the correct answer let correct-answer target-answer ;; color the nodes ask perceptron [ compute-activation ] ;; compute the answer let output-answer [activation] of perceptron ;; output the result ifelse output-answer = correct-answer [ user-message (word "Output: " output-answer "\\nTarget: " correct-answer "\\nCorrect Answer!") ] [ user-message (word "Output: " output-answer "\\nTarget: " correct-answer "\\nIncorrect Answer!") ] end ;; Sets the color of the perceptron's nodes appropriately ;; based on activation to recolor ;; output, input, or bias node procedure ifelse activation = 1 [ set color white ] [ set color black ] ask in-link-neighbors [ recolor ] ifelse show-weights? [ resize-recolor-links ] [ ask my-in-links [ set thickness 0 set label "" set color red + 1 ] ] end ;; resize and recolor the edges ;; resize to indicate weight ;; recolor to indicate positive or negative to resize-recolor-links ask links [ set label precision weight 4 set thickness 0.1 + 4 * abs weight ifelse weight > 0 [ set color red + 1 ] [ set color blue ] ] end

There are 9 versions of this model.

## Attached files

File | Type | Description | Last updated | |
---|---|---|---|---|

Perceptron.png | preview | Preview for 'Perceptron' | almost 10 years ago, by Uri Wilensky | Download |

This model does not have any ancestors.

This model does not have any descendants.