Perceptron Demo
Model was written in NetLogo 6.4.0
•
Viewed 6 times
•
Downloaded 0 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 and highlight misclassified points ask red-points [ let err_value compute-error self "red" set initial-error initial-error + abs err_value if abs err_value > 0 [ set shape "triangle" ] ;; Highlight misclassified red points ] ask green-points [ let err_value compute-error self "green" set initial-error initial-error + abs err_value if abs err_value > 0 [ set shape "triangle" ] ;; Highlight misclassified green points ] ; 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) ; to get an initial separation line in the first-third quadrant set bias random-float 1 end to create-training-data let MARGIN 3 ;; Separation margin from y = -x ; Red points: Upper regions, ensuring a margin above y = -x create-red-points N_data_points [ let attempts 0 let max-attempts 100 ;; Avoid infinite loops let x random 40 - 20 let y random 40 - 20 while [(y < (- x + MARGIN)) and (attempts < max-attempts)] [ set x random 40 - 20 set y random 40 - 20 set attempts attempts + 1 ] if attempts < max-attempts [ ;; Place only if valid setxy x y set color red set shape "dot" set original-color red ] ] ; Green points: Lower regions, ensuring a margin below y = -x create-green-points N_data_points [ let attempts 0 let max-attempts 100 ;; Avoid infinite loops let x random 40 - 20 let y random 40 - 20 while [(y > (- x - MARGIN)) and (attempts < max-attempts)] [ set x random 40 - 20 set y random 40 - 20 set attempts attempts + 1 ] if attempts < max-attempts [ ;; Place only if valid setxy x y set color green set shape "dot" set original-color green ] ] end to train let total-error 0 print "---- STARTING TRAINING EPOCH ----" ;; Debug message ;; Train on all red points one at a time foreach sort red-points [ point -> ask point [ set shape "star" set size 3] wait training-delay / 2 ask point [set size 1] let err train-perceptron point "red" set total-error total-error + abs err update-display ;; Now called in observer context ;plot-error wait training-delay / 2 ;; Pause to visualize the change ] ;; Train on all green points one at a time foreach sort green-points [ point -> ask point [ set shape "star" set size 3] wait training-delay / 2 ask point [set size 1] let err train-perceptron point "green" set total-error total-error + abs err update-display ;; Now called in observer context ;plot-error wait training-delay / 2 ;; Pause to visualize the change ] ;; Store error and update epoch set epoch-error lput total-error epoch-error print (word "Epoch: " (length epoch-error - 1) " | Error: " total-error) end to-report compute-error [point expected-label] let input-x [xcor] of point let input-y [ycor] of point let output calculate-output input-x input-y let ground-truth ifelse-value (expected-label = "red") [1] [0] let err ground-truth - output report err end to-report train-perceptron [point expected-label] let old-w1 item 0 weights let old-w2 item 1 weights let old-bias bias ; Perceptron learning rule let err compute-error point expected-label ; compute error print (word "point (" ([xcor] of point) "," ([ycor] of point) ") Err:" err) ; Update weights and bias if there is an error if err != 0 [ ask point [set shape "triangle"] 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 ;print (word "🔄 Updating Weights: " old-w1 ", " old-w2 " → " new-w1 ", " new-w2 " | Bias: " old-bias " → " bias) ] if err = 0 [ ask point [ set shape "dot" ] ;; Restore normal shape ] 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 ifelse 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 ifelse (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 "square" 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 ask patches [ let s (item 0 weights) * pxcor + (item 1 weights) * pycor + bias ; Linear combination ifelse (s > 0) [ set pcolor rgb 80 80 80 ] [ set pcolor white ] ] 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 are 2 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Perceptron Demo.png | preview | Preview for 'Perceptron Demo' | 7 days ago, by Marco Giordano | Download |
This model does not have any ancestors.
This model does not have any descendants.