Skip to content

Instantly share code, notes, and snippets.

@ppsdatta
Created April 7, 2023 15:26
Show Gist options
  • Save ppsdatta/df64b49c37ec0e22d606fce17eb3ffc1 to your computer and use it in GitHub Desktop.
Save ppsdatta/df64b49c37ec0e22d606fce17eb3ffc1 to your computer and use it in GitHub Desktop.
Perceptrons and Neural Networks in Racket - Functional + Composable
#lang racket
(require plot)
(define (sigmoid x)
(/ 1.0 (+ 1 (exp (- x)))))
(define (step x)
(if (>= x 0) 1 0))
(define (dot-product xs ys)
(for/fold ([s 0])
([i (map * xs ys)])
(+ s i)))
;; Bias is the first weight
(define ((perceptron ws activate-fn) input)
(activate-fn
(dot-product (cons 1 input)
ws)))
(define ((parallel . ps) input)
(map (λ (p) (p input)) ps))
(define ((sequential . ps) input)
(for/fold ([i input])
([p ps])
(p i)))
;; --------
;; AND gate
(define p-and (perceptron '(-15 10 10) sigmoid))
(for/list ([i '((1 0) (0 1) (0 0) (1 1))])
(cons i (p-and i)))
;; OR gate
(define p-or (perceptron '(-5 10 10) sigmoid))
(for/list ([i '((1 0) (0 1) (0 0) (1 1))])
(cons i (p-or i)))
;; NAND gate
(define p-nand (perceptron '(15 -10 -10) sigmoid))
(for/list ([i '((1 0) (0 1) (0 0) (1 1))])
(cons i (p-nand i)))
;; The XOR gate
(define p-xor (sequential (parallel p-or p-nand) p-and))
(for/list ([i '((1 0) (0 1) (0 0) (1 1))])
(cons i (p-xor i)))
;; --------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment