Skip to content

Commit

Permalink
Update comment syntax in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dougalm committed Dec 7, 2023
1 parent d750217 commit e171f3d
Show file tree
Hide file tree
Showing 38 changed files with 564 additions and 564 deletions.
32 changes: 16 additions & 16 deletions examples/bfgs.dx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def zoom(
c1:Float,
c2:Float
) -> Float =
-- Zoom line search helper; Algorithm 3.6 from Nocedal and Wright (2006).
# Zoom line search helper; Algorithm 3.6 from Nocedal and Wright (2006).

f0 = f_line 0.
g0 = grad f_line 0.
Expand All @@ -32,10 +32,10 @@ def zoom(
a_lo = get a_lo_ref
a_hi = get a_hi_ref

--Find a trial step length between a_lo and a_hi.
#Find a trial step length between a_lo and a_hi.
a_i = 0.5 * (a_lo + a_hi)

if (a_hi - a_lo) < 0.000001 -- The line search failed.
if (a_hi - a_lo) < 0.000001 # The line search failed.
then Done a_i
else
f_ai = f_line a_i
Expand All @@ -57,7 +57,7 @@ def zoom_line_search(
maxiter:Nat,
f: (Float)->Float
) -> Float =
--Algorithm 3.5 from Nocedal and Wright (2006).
#Algorithm 3.5 from Nocedal and Wright (2006).
c1 = 0.0001
c2 = 0.9
a_max = 1.
Expand Down Expand Up @@ -90,7 +90,7 @@ def backtracking_line_search(
maxiter:Nat,
f: (Float)->Float
) -> Float =
-- Algorithm 3.1 in Nocedal and Wright (2006).
# Algorithm 3.1 in Nocedal and Wright (2006).
a_init = 1.
f_0 = f 0.
g_0 = grad f 0.
Expand All @@ -113,14 +113,14 @@ struct BFGSresults(n|Ix) =
num_iter: Nat

def bfgs_minimize(
f: (n=>Float)->Float, --Objective function.
x0: n=>Float, --Initial point.
H0: n=>n=>Float, --Initial inverse Hessian approximation.
linesearch: ((Float)->Float)->Float, --Line search that returns a step size.
tol: Float, --Convergence tolerance (of the gradient L2 norm).
maxiter: Nat --Maximum number of BFGS iterations.
f: (n=>Float)->Float, #Objective function.
x0: n=>Float, #Initial point.
H0: n=>n=>Float, #Initial inverse Hessian approximation.
linesearch: ((Float)->Float)->Float, #Line search that returns a step size.
tol: Float, #Convergence tolerance (of the gradient L2 norm).
maxiter: Nat #Maximum number of BFGS iterations.
) -> BFGSresults n given (n|Ix) =
-- Algorithm 6.1 in Nocedal and Wright (2006).
# Algorithm 6.1 in Nocedal and Wright (2006).

xref <- with_state x0
Href <- with_state H0
Expand All @@ -143,7 +143,7 @@ def bfgs_minimize(
g_next = grad f x_next
grad_diff = g_next - g

-- Update the inverse Hessian approximation.
# Update the inverse Hessian approximation.
rho_inv = vdot grad_diff x_diff
if not (rho_inv == 0.)
then
Expand All @@ -157,7 +157,7 @@ def bfgs_minimize(


def rosenbrock(coord:(Fin 2)=>Float) -> Float =
--A standard optimization test case; the optimum is (1, 1).
#A standard optimization test case; the optimum is (1, 1).
x = coord[0@_]
y = coord[1@_]
pow (1 - x) 2 + 100 * pow (y - x * x) 2
Expand Down Expand Up @@ -188,8 +188,8 @@ def multiclass_logreg(
res = bfgs_minimize ob_fun w0 eye (\f. zoom_line_search maxls f) tol maxiter
res.fval

-- Define a version of `multiclass_logreg` with Int instead of Nat labels, callable from Python
-- (see benchmarks/bfgs.py).
# Define a version of `multiclass_logreg` with Int instead of Nat labels, callable from Python
# (see benchmarks/bfgs.py).
def multiclass_logreg_int(
xs:(Fin n)=>(Fin d)=>Float,
ys:(Fin n)=>Int,
Expand Down
30 changes: 15 additions & 15 deletions examples/brownian_motion.dx
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ def sample_unit_brownian_bridge(
key:Key,
t:Float
) -> v given (v|VSpace) =
-- Can only handle t between 0 and 1.
-- iteratively subdivide to desired tolerance.
# Can only handle t between 0 and 1.
# iteratively subdivide to desired tolerance.
num_iters = 10 + f_to_n (-log tolerance)
init_state = SamplerState(key, zero, 1.0, t)
result = fold init_state \i:(Fin num_iters) prev.
[key_draw, key_left, key_right] = split_key key
-- add scaled noise
# add scaled noise
t' = abs (prev.t - 0.5)
new_y = prev.y + prev.sigma * (0.5 - t') .* sampler key_draw

-- zoom in left or right
# zoom in left or right
new_key = if prev.t > 0.5
then key_left
else key_right
Expand Down Expand Up @@ -72,8 +72,8 @@ def sample_brownian_bridge(
y0:v, t1:Float,
y1:v, t:Float
) -> v given (v|VSpace) =
-- Linearly interpolate between the two bracketing samples
-- and add appropriately scaled Brownian bridge noise.
# Linearly interpolate between the two bracketing samples
# and add appropriately scaled Brownian bridge noise.
unit_bb = \t. sample_unit_brownian_bridge (tolerance / (t1 - t0)) sampler key t
bridge_val = scale_brownian_bridge unit_bb t0 t1 t
interp_val = linear_interp t0 y0 t1 y1 t
Expand All @@ -85,8 +85,8 @@ def sample_brownian_motion(
key:Key,
t':Float
) -> v given (v|VSpace) =
-- Can handle a t' anywhere on the real line.
-- Handle negative times by reflecting and using a different key.
# Can handle a t' anywhere on the real line.
# Handle negative times by reflecting and using a different key.
[neg_key', key'] = split_key key
(key, t) = if t' < 0.0
then (neg_key', -t')
Expand All @@ -95,8 +95,8 @@ def sample_brownian_motion(
[key_start, key_rest] = split_key key
first_f = sampler key_start

-- Iteratively sample a point twice as far ahead
-- until we bracket the query time.
# Iteratively sample a point twice as far ahead
# until we bracket the query time.
doublings = Fin (1 + (natlog2 (f_to_n t)))
init_state = (0.0, zero, 1.0, first_f, key_rest)
(t0, left_f, t1, right_f, key_draw) =
Expand All @@ -105,8 +105,8 @@ def sample_brownian_motion(
[key_current, key_continue] = split_key key_rest
new_right_f = left_f + (sqrt t1) .* sampler key_current
(t1, right_f, t1 * 2.0, new_right_f, key_continue)
-- The above iterative loop could be mostly parallelized, but would
-- require some care to get the random keys right.
# The above iterative loop could be mostly parallelized, but would
# require some care to get the random keys right.

sample_brownian_bridge tolerance sampler key_draw t0 left_f t1 right_f t

Expand Down Expand Up @@ -152,7 +152,7 @@ from a Gaussian process. In particular, we can re-use the
This process can be nested to arbitrarily many dimensions.

interface HasBrownianSheet(a:Type, v|HasStandardNormal)
-- tolerance -> key -> input -> output
# tolerance -> key -> input -> output
sample_brownian_sheet : (Float, Key, a) -> v

instance HasBrownianSheet(Float, v) given (v|HasStandardNormal|VSpace)
Expand All @@ -161,8 +161,8 @@ instance HasBrownianSheet(Float, v) given (v|HasStandardNormal|VSpace)

instance HasBrownianSheet((a, Float), v) given (a, v|VSpace) (HasBrownianSheet a v)
def sample_brownian_sheet(tol, k, xab) =
-- the call to `sample_brownian_sheet` below will recurse
-- until the input is one-dimensional.
# the call to `sample_brownian_sheet` below will recurse
# until the input is one-dimensional.
(xa, xb) = xab
sample_a = \k. sample_brownian_sheet tol k xa
sample_brownian_motion tol sample_a k xb
Expand Down
18 changes: 9 additions & 9 deletions examples/ctc.dx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ However, Dex's autodiff can produce the backward
pass automatically. That makes this code much shorter than
most implementations.

import stats -- for log-space representation of probabilities.
import stats # for log-space representation of probabilities.


'## Custom index sets
Expand Down Expand Up @@ -103,7 +103,7 @@ project(to=AllButFirst (Bool `Either` Bool), 0@(Bool `Either` Bool))
'### Helper functions

def interleave_blanks(blank:v, seq: m=>v) -> (FenceAndPosts m)=>v given (m|Ix, v) =
-- Turns "text" into " t e x t ".
# Turns "text" into " t e x t ".
for idx. case idx of
Fence i -> seq[i]
Posts i -> blank
Expand All @@ -127,8 +127,8 @@ def ctc_non_empty(
ilabels = interleave_blanks blank labels
label_probs = for t. normalize logits[t]

-- Initialize dynamic programming table to all zeros
-- except for starting with either a blank or the first token.
# Initialize dynamic programming table to all zeros
# except for starting with either a blank or the first token.
prob_seq_at_start = yield_state zero \lpRef.
lpRef!(Posts first_ix) := label_probs[first_ix,blank]
lpRef!(Fence first_ix) := label_probs[first_ix,labels[first_ix]]
Expand All @@ -150,14 +150,14 @@ def ctc_non_empty(
prob_transition_from_blank = safe_idx_sub prev s 1
prob_transition_from_last_token = safe_idx_sub prev s 2

-- Equation 6 from CTC paper.
# Equation 6 from CTC paper.
transition_prob = if ilabels[s] == blank || same_token_as_last s
then prob_no_transition + prob_transition_from_blank
else prob_no_transition + prob_transition_from_blank +
prob_transition_from_last_token
transition_prob * label_probs[inject t, ilabels[s]]

-- Sum over last two entries in the table. Eq 8 from paper.
# Sum over last two entries in the table. Eq 8 from paper.
end_with_label = prob_seq_final[Fence last_ix]
end_with_space = prob_seq_final[Posts last_ix]
end_with_label + end_with_space
Expand Down Expand Up @@ -201,11 +201,11 @@ Vocab = Fin 6
blank = 0@Vocab
Time = Fin 4

-- Create random logits and labels
# Create random logits and labels
logits : Time => Vocab => (LogSpace Float) = arb $ new_key 0
labels = [(3@Vocab), (4@Vocab), (1@Vocab)]

-- Evaluate probability.
# Evaluate probability.
:p ls_to_f $ ctc blank logits labels
> 0.00208998

Expand All @@ -216,7 +216,7 @@ computes `p(labels|logits)`, which should sum to 1.0 over all possible labels.
They don't sum to one. Maybe there is a bug in the above code
or the paper.

-- Fin N=>Vocab is the set of all combinations of N tokens.
# Fin N=>Vocab is the set of all combinations of N tokens.
sum for i:(Fin 3=>Vocab).
ls_to_f $ ctc blank logits i
> 0.5653746
2 changes: 1 addition & 1 deletion examples/dither.dx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ halftone = [[14.0, 10, 6, 13, 19, 23, 27, 20],

' # Dithering a real image

-- conversion from RGB to grayscale
# conversion from RGB to grayscale

def pixel(x:Char) -> Float32 =
r = w8_to_i x
Expand Down
42 changes: 21 additions & 21 deletions examples/fluidsim.dx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import png
import plot

def wrapidx(n|Ix, i:Int) -> n =
-- Index wrapping around at ends.
# Index wrapping around at ends.
asidx $ unsafe_i_to_n $ mod i $ n_to_i $ size n

def incwrap(i:n) -> n given (n|Ix) = -- Increment index, wrapping around at ends.
def incwrap(i:n) -> n given (n|Ix) = # Increment index, wrapping around at ends.
asidx $ unsafe_i_to_n $ mod ((n_to_i $ ordinal i) + 1) $ n_to_i $ size n

def decwrap(i:n) -> n given (n|Ix) = -- Decrement index, wrapping around at ends.
def decwrap(i:n) -> n given (n|Ix) = # Decrement index, wrapping around at ends.
asidx $ unsafe_i_to_n $ mod (n_to_i (ordinal i) - 1) $ n_to_i $ size n

def finite_difference_neighbours(x:n=>a) -> n=>a given (n|Ix, a|Add|Sub) =
Expand Down Expand Up @@ -42,11 +42,11 @@ def add_neighbours_2d(x:n=>m=>a) -> (n=>m=>a) given (n|Ix, m|Ix, a|Add) =
ax1 + ax2

def project_vel(v: n=>m=>(Fin 2)=>a) -> n=>m=>(Fin 2)=>a given (n|Ix, m|Ix, a|VSpace) =
-- Project_vel the velocity field to be approximately mass-conserving,
-- using a few iterations of Gauss-Seidel.
# Project_vel the velocity field to be approximately mass-conserving,
# using a few iterations of Gauss-Seidel.
h = 1.0 / n_to_f (size n)

-- unpack into two scalar fields
# unpack into two scalar fields
vx = for i j. v[i,j, from_ordinal 0]
vy = for i j. v[i,j, from_ordinal 1]

Expand All @@ -59,7 +59,7 @@ def project_vel(v: n=>m=>(Fin 2)=>a) -> n=>m=>(Fin 2)=>a given (n|Ix, m|Ix, a|VS
vx = vx - (0.5 / h) .* fdx(p)
vy = vy - (0.5 / h) .* fdy(p)

for i j. [vx[i,j], vy[i,j]] -- pack back into a table.
for i j. [vx[i,j], vy[i,j]] # pack back into a table.

def bilinear_interp(
right_weight:Float, bottom_weight:Float,
Expand All @@ -71,32 +71,32 @@ def bilinear_interp(
left + right

def advect(f: n=>m=>a, v: n=>m=>(Fin 2)=>Float) -> n=>m=>a given (n|Ix, m|Ix, a|VSpace) =
-- Move field f according to x and y velocities (u and v)
-- using an implicit Euler integrator.
# Move field f according to x and y velocities (u and v)
# using an implicit Euler integrator.

cell_xs = linspace n 0.0 $ n_to_f (size n)
cell_ys = linspace m 0.0 $ n_to_f (size m)

for i j.
-- Location of source of flow for this cell. No meshgrid!
# Location of source of flow for this cell. No meshgrid!
center_xs = cell_xs[i] - v[i,j, from_ordinal 0]
center_ys = cell_ys[j] - v[i,j, from_ordinal 1]

-- Index of source cell.
# Index of source cell.
source_col = floor center_xs
source_row = floor center_ys

-- Relative weight of right-hand and bottom cells.
# Relative weight of right-hand and bottom cells.
right_weight = center_xs - source_col
bottom_weight = center_ys - source_row

-- Cast back to indices, wrapping around edges.
# Cast back to indices, wrapping around edges.
l = wrapidx n (f_to_i source_col)
r = wrapidx n ((f_to_i source_col) + 1)
t = wrapidx m (f_to_i source_row)
b = wrapidx m ((f_to_i source_row) + 1)

-- A convex weighting of the 4 surrounding cells.
# A convex weighting of the 4 surrounding cells.
bilinear_interp right_weight bottom_weight f[l,t] f[l,b] f[r,t] f[r,b]

def fluidsim(
Expand All @@ -107,9 +107,9 @@ def fluidsim(
with_state (color_init, v) \state.
for i:(Fin num_steps).
(color, v) = get state
v = advect v v -- Move velocities
v = project_vel v -- Project to be volume-preserving
color' = advect color v -- Move color
v = advect v v # Move velocities
v = project_vel v # Project to be volume-preserving
color' = advect color v # Move color
state := (color', v)
color

Expand All @@ -118,13 +118,13 @@ def fluidsim(
N = Fin 50
M = Fin 50

-- Create random velocity field.
# Create random velocity field.
def ixkey3(k:Key, i:n, j:m, k2:o) -> Key given (n|Ix, m|Ix, o|Ix) =
hash (hash (hash k (ordinal i)) (ordinal j)) (ordinal k2)
init_velocity = for i:N j:M k:(Fin 2).
3.0 * (randn $ ixkey3 (new_key 0) i j k)

-- Create diagonally-striped color pattern.
# Create diagonally-striped color pattern.
init_color = for i:N j:M.
i' = n_to_f $ ordinal i
j' = n_to_f $ ordinal j
Expand All @@ -133,7 +133,7 @@ init_color = for i:N j:M.
g = b_to_f $ (sin $ (j' + i') / 4.0) > 0.0
[r, g, b]

-- Run fluid sim and plot it.
# Run fluid sim and plot it.
num_steps = 5
:html imseqshow $ fluidsim num_steps init_color init_velocity
> <html output>
Expand All @@ -142,7 +142,7 @@ num_steps = 5

target = transpose init_color

-- This is partial
# This is partial
def last(xs:n=>a) -> a given (n|Ix, a) = xs[(unsafe_nat_diff (size n) 1)@_]

def objective(v:N=>M=>(Fin 2)=>Float) -> Float =
Expand Down
Loading

0 comments on commit e171f3d

Please sign in to comment.