Perceptron Demo
Model was written in NetLogo 6.4.0
•
Viewed 6 times
•
Downloaded 2 times
•
Run 0 times
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
Info tab cannot be displayed because of an encoding error
Comments and Questions
Please start the discussion about this model!
(You'll first need to log in.)
Click to Run Model
; NetLogo script to illustrate a simple Perceptron globals [ weights ; List of weights [w1 w2] for inputs x and y bias ; Bias term epoch-error ; list of errors for each epoch initial-error ; cumulative initial-error ; Learning rate for weight updates is an interface control ] breed [red-points red-point] ; Breed for red points (class 1) breed [green-points green-point] ; Breed for green points (class 0) breed [test-points test-point] ; Breed for test points patches-own [ original-color ] ; To store original patch color for visualization to setup clear-all clear-plot set epoch-error [] set initial-error 0 ask patches [ set pcolor white ] ; Set background color to white initialize-weights ; Initialize perceptron weights and bias create-training-data ; Generate training dataset ; Compute initial error ask red-points [ set initial-error initial-error + compute-error self "red" ] ask green-points [ set initial-error initial-error + compute-error self "green" ] ; Initialize epoch-error with the first error value set epoch-error lput initial-error epoch-error print (word "Initial Error: " (initial-error)) reset-ticks ; Reset the tick counter update-display ; Update the visualization plot-error ; Plot initial error end to go ; Training procedure to be called repeatedly train ; Perform one training epoch update-display ; Update the display after training plot-error ; Update the error plot end to start-test-mode ;; Enable test mode interaction create-test-point end to initialize-weights ; Initialize weights and bias to small random values set weights (list random-float 1 random-float 1) set weights replace-item 1 weights (-1 * item 1 weights) set bias random-float 1 end to create-training-data ; Create linearly separable training data in distinct quadrants ; Red points in the upper right quadrant create-red-points N_data_points [ setxy (random 20) (random 20) set color red set shape "dot" ; Set shape to dot set original-color red ] ; Green points in the lower left quadrant create-green-points N_data_points [ setxy (random 20) - 20 (random 20) - 20 set color green set shape "dot" ; Set shape to dot set original-color green ] end to train ; Train the perceptron for one epoch let total-error 0 ; Train on all red points ask red-points [ let derr train-perceptron self "red" set total-error total-error + derr ] ; Train on all green points ask green-points [ let derr train-perceptron self "green" set total-error total-error + derr ] set epoch-error lput total-error epoch-error print (word "Epoch: " (length epoch-error - 1) " | Error: " total-error) update-display ; Update display after training end to-report compute-error [point expected-label] let output calculate-output [xcor] of point [ycor] of point let target-output ifelse-value (expected-label = "red") [1] [0] report abs (target-output - output) end to-report train-perceptron [point expected-label] ; Perceptron learning rule let err compute-error point expected-label ; compute error ; Update weights and bias if there is an error if err != 0 [ let new-w1 item 0 weights + learning-rate * err * [xcor] of point let new-w2 item 1 weights + learning-rate * err * [ycor] of point set weights (list new-w1 new-w2) set bias bias + learning-rate * err ] report abs err end to-report calculate-output [input-x input-y] ; Perceptron output calculation let s (item 0 weights) * input-x + (item 1 weights) * input-y + bias ; Linear combination if s > 0 [ report 1 ] ; Activation function (step function): 1 if sum > 0, else 0 report 0 end to-report classify [x y] ; Classify a point (x, y) let output calculate-output x y ; Get perceptron output if output = 1 [ report "red" ] ; If output is 1, classify as "red" report "green" ; Otherwise, classify as "green" end to create-test-point if mouse-down? [ let x mouse-xcor let y mouse-ycor ;; Check if the click is within world bounds if (x >= min-pxcor and x <= max-pxcor and y >= min-pycor and y <= max-pycor) [ create-test-points 1 [ setxy x y set color black ;; Default test point color set shape "dot" update-test-point-color self ] ] ] end to update-display ; Update the display: clear drawing and redraw separator line clear-drawing draw-separator-line ; Draw the line representing the perceptron decision boundary ask test-points [ update-test-point-color self ] ; Update color of test points based on classification end to draw-separator-line ; Ensure the entire world is updated ask patches [ set pcolor white ] ;; Reset all patches to white ifelse item 1 weights != 0 [ ; Avoid division by zero let m (- (item 0 weights) / (item 1 weights)) let b (- bias / (item 1 weights)) ask patches [ if (pycor - (m * pxcor + b)) < 0 [ set pcolor rgb 80 80 80 ] ;; Set color for one side if (pycor - (m * pxcor + b)) >= 0 [ set pcolor white ] ;; Ensure other side is updated ] ] [ print "Division by zero! (w2 too small)" ] end to update-test-point-color [testPoint] ; Update the color of a test point based on perceptron classification ask testPoint [ let classification classify xcor ycor ; Classify the test point if classification = "red" [ set color red ] ; Set color to red if classified as red if classification = "green" [ set color green ] ; Set color to green if classified as green ] end to plot-error set-current-plot "Training Error" ; Plot initial error in a different color set-current-plot-pen "Initial Error" plotxy 0 initial-error ;; Plot the line graph (default pen behavior) set-current-plot-pen "Error" plot last epoch-error ;; Standard plot (connects points with a line) end
There is only one version of this model, created 2 days ago by Marco Giordano.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Perceptron Demo.png | preview | Preview for 'Perceptron Demo' | 2 days ago, by Marco Giordano | Download |
This model does not have any ancestors.
This model does not have any descendants.