Skip to content

Instantly share code, notes, and snippets.

View bdhammel's full-sized avatar

Ben Hammel bdhammel

View GitHub Profile
@bdhammel
bdhammel / inverse_problem.py
Created June 3, 2020 03:53
Solving the inverse problem with MCMC
import numpy as onp
from scipy.optimize import minimize
from scipy.stats import gaussian_kde
import matplotlib.pyplot as plt
import jax.numpy as np
from jax import random, lax
import numpyro
import numpyro.distributions as dist
from numpyro.infer import MCMC, NUTS
@bdhammel
bdhammel / mle-map-example.py
Last active January 30, 2020 14:29
Example of MLE and MAP
import numpy as np
from scipy.stats import norm, invgamma
# The barrel of apples
# The average apples is between 70-100 g
BARREL = np.random.normal(loc=85, scale=20, size=100)
# Grid
WEIGHT_GUESSES = np.linspace(1, 200, 100)
ERROR_GUESSES = np.linspace(.1, 50, 100)
@bdhammel
bdhammel / logging_python.py
Last active July 28, 2019 02:03
Short example for instantiating logging in python
# logging_example.py
"""
With the logging module imported, you can use something called a “logger” to log messages that you want to see.
By default, there are 5 standard levels indicating the severity of events.
Each has a corresponding method that can be used to log events at that level of severity.
The defined levels, in order of increasing severity, are the following:
- DEBUG
- INFO
- WARNING
- ERROR
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from tqdm import tqdm
import argparse
class LinearRegression:
def __init__(self, order):
self.W = np.random.randn((order+1))
def fit(self, X, Y, alpha=1e-5, epochs=1000):
X = np.vstack((X, np.ones_like(X))).T
Y = Y.T
for _ in range(epochs):
import numpy as np
import matplotlib.pyplot as plt
BASE_LR = 1e-7
EPOCHS = 500
TRIALS = 3
class LRscanner: