Skip to content

Instantly share code, notes, and snippets.

View paulhendricks's full-sized avatar

Paul Hendricks paulhendricks

View GitHub Profile
@paulhendricks
paulhendricks / hmm.py
Created August 22, 2016 18:03 — forked from fonnesbeck/hmm.py
Hidden Markov model in PyMC
import numpy as np
import pymc
import pdb
def unconditionalProbability(Ptrans):
"""Compute the unconditional probability for the states of a
Markov chain."""
m = Ptrans.shape[0]
# good
hello_world <- 1L
# bad - can I call hello(world)? It's unclear.
hello.world <- 1L
@paulhendricks
paulhendricks / setup.py
Last active October 28, 2016 14:14
A setup.py file for OpenAI's rllab
"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
https://github.com/pypa/sampleproject
"""
# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
n <- 10000
alpha <- rnorm(n, 20, 5) # Customers spend on average $20 per transaction
plot(density(alpha))
b <- 10 # Customers who are petite spend on average $10 more per transaction
petite <- rbinom(n, size = 1, prob = 0.25) # Let's say 25% of customers are petite
eps <- rnorm(n, 0, 5) # Add some error
plot(density(eps))
y_true <- alpha + b * petite + eps
plot(density(y_true))
y_true[y_true < 0] <- 0
# Good - I can customize the way the file is read in
df <- read.csv("foo.csv", stringsAsFactors = FALSE)
df <- read.table("foo.txt", delimiter = "\t", stringsAsFactors = FALSE)
df <- read.table("foo.txt", delimiter = "|", stringsAsFactors = FALSE)
analyze(df)
# Bad - arguments are needed to customize how foo.csv is read in
analyze("foo.csv", stringsAsFactors = FALSE)
analyze("foo.txt", delimiter = "\t", stringsAsFactors = FALSE)
import numpy as np
import theano; import theano.tensor as T; import theano.tensor.nnet as nnet
X_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
y_train = np.array([0, 1, 1, 0])
x = T.dvector('x')
y = T.dscalar('y')
theta_0 = theano.shared(np.array(np.random.rand(3, 3), dtype=theano.config.floatX), name='theta_0')
theta_1 = theano.shared(np.array(np.random.rand(4, 1), dtype=theano.config.floatX), name='theta_1')
layer_1 = nnet.sigmoid(T.dot(theta_0.T, T.concatenate([np.array([1], dtype=theano.config.floatX), x])))
layer_2 = T.sum(nnet.sigmoid(T.dot(theta_1.T, T.concatenate([np.array([1], dtype=theano.config.floatX), layer_1]))))
#!/usr/bin/env julia
using Luxor
Drawing(500, 500, "/tmp/julia-flowers.png")
background("white")
function juliaflowers(cpos::Point, radius; petals=30, outercircleratio=0.75, innercircleratio=0.65)
# clockwise, from bottom LEFT...
points3 = ngon(cpos, radius, 3, pi/6, vertices=true)
'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import keras
from keras.datasets import mnist
@paulhendricks
paulhendricks / latency.txt
Created May 28, 2018 03:45 — forked from understeer/latency.txt
HPC-oriented Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference/hit 1.5 ns 4 cycles
Floating-point add/mult/FMA operation 1.5 ns 4 cycles
L2 cache reference/hit 5 ns 12 ~ 17 cycles
Branch mispredict 6 ns 15 ~ 20 cycles
L3 cache hit (unshared cache line) 16 ns 42 cycles
L3 cache hit (shared line in another core) 25 ns 65 cycles
Mutex lock/unlock 25 ns
L3 cache hit (modified in another core) 29 ns 75 cycles

Effective Modern CMake

Getting Started

For a brief user-level introduction to CMake, watch C++ Weekly, Episode 78, Intro to CMake by Jason Turner. LLVM’s CMake Primer provides a good high-level introduction to the CMake syntax. Go read it now.

After that, watch Mathieu Ropert’s CppCon 2017 talk Using Modern CMake Patterns to Enforce a Good Modular Design (slides). It provides a thorough explanation of what modern CMake is and why it is so much better than “old school” CMake. The modular design ideas in this talk are based on the book [Large-Scale C++ Software Design](https://www.amazon.de/Large-Scale-Soft