Skip to content

Instantly share code, notes, and snippets.

View sdwebb's full-sized avatar
:shipit:

Stephen Webb sdwebb

:shipit:
View GitHub Profile
@sdwebb
sdwebb / NormalizedCategoricalCrossentropy.py
Last active March 27, 2024 00:57
A Keras Metric for computing the scaled categorical crossentropy, as described here: https://www.swebb.io/blog/interpreting-the-categorical-cross-entropy-loss-function
import keras
import tensorflow as tf
class ScaledCrossentropy(keras.metrics.Metric):
def __init__(self, name='scaled_crossentropy', **kwargs):
super().__init__(name=name, **kwargs)
self.entropy = self.add_weight(name='entropy', initializer='zeros')
def update_state(self, y_true, y_pred, sample_weight=None):
@sdwebb
sdwebb / MH_numba.ipynb
Last active November 10, 2020 16:43
Timing comparison of Metropolis-Hastings implementations with and without numba decorators.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sdwebb
sdwebb / metropolis-hastings.py
Created June 18, 2020 19:40
A working implementation of random walk Metropolis-Hastings, complete with a nice plotting benchmark.
"""
Here is a simple implementation of Metropolis-Hastings on a 1D probability function that is not normalized.
Read more about Metropolis-Hastings, e.g., here: https://gregorygundersen.com/blog/2019/11/02/metropolis-hastings/
"""
from matplotlib import pyplot as plt
import numpy as np
from scipy import integrate
@sdwebb
sdwebb / dot-histogram.py
Last active May 29, 2020 17:49
Creating a histogram that has a dot for every data point. Variations would be to rescale the number of dots if you have too many, i.e. a dot is a thousand widget-counts, or splitting the dots into sub-columns so the peaks aren't quite so high.
from matplotlib import pyplot as plt
from matplotlib.ticker import FixedLocator
import matplotlib.font_manager as fm
from matplotlib import pyplot as plt
import numpy as np
# Use the Gill Sans font
import matplotlib.pylab as pylab
@sdwebb
sdwebb / annotated-scatter.py
Created May 27, 2020 05:13
An annotated scatter plot, using the numerical value in lieu of doing a contour plot
from matplotlib import pyplot as plt
import matplotlib.font_manager as fm
from matplotlib import pyplot as plt
font = fm.FontProperties(
family = 'Gill Sans',
fname = '/usr/share/fonts/truetype/adf/GilliusADF-Regular.otf')
import matplotlib.pylab as pylab
params = {'axes.spines.right' : False,
'axes.spines.left' : False,
@sdwebb
sdwebb / dot-dash-plot.py
Last active May 16, 2020 02:07
A Tufte style dot-dash plot
from matplotlib import pyplot as plt
from matplotlib.ticker import FixedLocator
import matplotlib.font_manager as fm
from matplotlib import pyplot as plt
font = fm.FontProperties(
family = 'Gill Sans',
fname = '/usr/share/fonts/truetype/adf/GilliusADF-Regular.otf')
import matplotlib.pylab as pylab
params = {'axes.spines.right' : False,
@sdwebb
sdwebb / invariants.py
Last active April 22, 2024 21:40
Computing the kick and invariants for nonlinear integrable optics
"""
Class for computing the invariants and kick for the nonlinear elliptic potential.
Use of this class should reference IPAC'15 proceeding number MOPMA029.
"""
import numpy as np
from numpy import sqrt, arccosh, arccos, pi
class Invariants: