Skip to content

Instantly share code, notes, and snippets.

@chunsj
chunsj / AD.hs
Created March 19, 2024 03:04 — forked from ttesmer/AD.hs
Automatic Differentiation in 38 lines of Haskell using Operator Overloading and Dual Numbers. Inspired by conal.net/papers/beautiful-differentiation
{-# LANGUAGE TypeSynonymInstances #-}
data Dual d = D Float d deriving Show
type Float' = Float
diff :: (Dual Float' -> Dual Float') -> Float -> Float'
diff f x = y'
where D y y' = f (D x 1)
class VectorSpace v where
zero :: v
@chunsj
chunsj / bo.lisp
Created September 28, 2021 09:48 — forked from lukego/bo.lisp
Bayesian Optimization
(defpackage bo
(:use :common-lisp :alexandria :serapeum :trivia))
(in-package :bo)
;;;; Bayesian Optimization based on Tree-structured Parzen Estimator and naive-Bayes.
;;;
;;; based on Rust code at https://docs.rs/tpe/0.1.1/tpe/
;;; which is inspired by https://optuna.org/
;;; which is inspired by http://hyperopt.github.io/hyperopt/
;;; which is described in https://proceedings.neurips.cc/paper/2011/file/86e8f7ab32cfd12577bc2619bc635690-Paper.pdf
@chunsj
chunsj / weightings.lisp
Created January 14, 2021 05:52
Computes weights to make similar amounts
(defun weighting/same (prices)
"returns integral weights to make similar amounts"
(labels ((dp (&rest args)
(let ((ns (mapcar #'round args)))
(if (> ($sum (mapcar (lambda (n) (if (<= n 0) 1 0)) ns)) 0)
most-positive-single-float
(let ((vals (mapcar (lambda (price n) (* n price)) prices ns)))
(loop :for v :in vals
:summing ($sum (mapcar (lambda (u) ($square (- u v))) vals))))))))
(multiple-value-bind (weights dp)
(defpackage :gdrl-ch11
(:use #:common-lisp
#:mu
#:th
#:th.layers
#:th.env
#:th.env.cartpole))
(in-package :gdrl-ch11)
@chunsj
chunsj / duel-ddqn.lisp
Created July 28, 2020 11:56
Duel DDQN - The simplest form
(defpackage :gdrl-ch10
(:use #:common-lisp
#:mu
#:th
#:th.layers
#:th.env
#:th.env.cartpole-regulator))
(in-package :gdrl-ch10)
@chunsj
chunsj / cartpole.lisp
Created July 24, 2020 13:18
Neural Fitted Q-Iteration using Common Lisp
;; from https://github.com/seungjaeryanlee/implementations-nfq.git
(defpackage :cartpole2
(:use #:common-lisp
#:mu
#:th
#:th.layers
#:th.env))
(in-package :cartpole2)
@chunsj
chunsj / cartpole.lisp
Created July 24, 2020 13:18
Neural Fitted Q-Iteration using Common Lisp
;; from https://github.com/seungjaeryanlee/implementations-nfq.git
(defpackage :cartpole2
(:use #:common-lisp
#:mu
#:th
#:th.layers
#:th.env))
(in-package :cartpole2)
@chunsj
chunsj / genchars-obama-lstm.lisp
Created July 17, 2019 04:09
Obama speech generation
;; from
;; http://karpathy.github.io/2015/05/21/rnn-effectiveness/
(defpackage :genchars-obama-lstm
(:use #:common-lisp
#:mu
#:th
#:th.ex.data))
(in-package :genchars-obama-lstm)
@chunsj
chunsj / vgg16.lisp
Created September 16, 2018 07:57
vgg16 weight importing and testing.
(defpackage :vgg16-example
(:use #:common-lisp
#:mu
#:th
#:th.m.vgg16
#:th.image))
(in-package :vgg16-example)
;; load weights - takes some time
@chunsj
chunsj / cgan.lisp
Created August 26, 2018 10:51
Conditional GAN example
;; from
;; https://wiseodd.github.io/techblog/2016/12/24/conditional-gan-tensorflow/
;; https://github.com/wiseodd/generative-models/blob/master/GAN/conditional_gan/cgan_pytorch.py
(ql:quickload :opticl)
(defpackage :cgan
(:use #:common-lisp
#:mu
#:th