Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Last active March 14, 2017 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixpickle/47f3effb5ff21729ed336fc01c566cbe to your computer and use it in GitHub Desktop.
Save unixpickle/47f3effb5ff21729ed336fc01c566cbe to your computer and use it in GitHub Desktop.
Wrapped Normal Distribution
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
for {
variance := rand.Float64() * math.Pi * 2
mean := randAngle()
x := randAngle()
// Bound the variance to prevent HUGE values.
if variance < 0.05 {
variance = 0.05
}
fmt.Println(mean, variance, x)
logDensity := math.Inf(-1)
for i := -15; i < 15; i++ {
sqDist := math.Pow(2*math.Pi*float64(i)+mean-x, 2)
logVal := -sqDist/(2*variance) - math.Log(2*math.Pi*variance)/2
max := math.Max(logDensity, logVal)
expSum := math.Exp(logVal-max) + math.Exp(logDensity-max)
logDensity = math.Log(expSum) + max
}
fmt.Println(logDensity)
}
}
func randAngle() float64 {
return rand.Float64()*math.Pi*2 - math.Pi
}
#!/bin/bash
#
# Plot (in polar coordinates) an example distribution.
MEAN=2.356
VARIANCE=0.05
PLOT_FILE=plot.jpg
OCTAVE=octave
SCRIPT_FILE=plot_example.m
trap "rm -rf $SCRIPT_FILE octave-workspace" EXIT
xs=$($OCTAVE --eval "[0:2*pi/100:2*pi]'" | tr $'\n' ' ' | cut -f 3- -d ' ')
echo 'data = [' >$SCRIPT_FILE
for x in $xs
do
y=$(echo $MEAN $VARIANCE $x | neurocli run -net network)
echo $x $y \; >>$SCRIPT_FILE
done
echo '];' >>$SCRIPT_FILE
echo 'set(0, "defaultlinelinewidth", 2.0); ' >>$SCRIPT_FILE
echo 'polar(data(:,1), 1+exp(data(:,2)));' >>$SCRIPT_FILE
echo "print -djpg $PLOT_FILE" >>$SCRIPT_FILE
echo "Creating $PLOT_FILE..."
octave $SCRIPT_FILE
Input(w=1, h=1, d=3)
FC(out=64)
Tanh
FC(out=64)
Tanh
FC(out=96)
Tanh
FC(out=1)
#!/bin/bash
make_samples() {
# Avoid "signal: broken pipe" output.
go run data.go 2>/dev/null
}
train_cmd="neurocli train -net network -cost mse -adam default -quiet"
if [ -f network ]; then
echo 'Using existing network.'
else
echo 'Creating network...'
neurocli new -in network.txt -out network
fi
echo 'Stage 1: big learning rate...'
make_samples | $train_cmd -step 0.001 -batch 10 -stopsamples 400000
echo 'Stage 2: small learning rate...'
make_samples | $train_cmd -step 0.0001 -batch 10 -stopsamples 200000
echo 'Stage 3: refinements...'
make_samples | $train_cmd -step 0.00001 -batch 1000 -stopsamples 100000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment