Skip to content

Instantly share code, notes, and snippets.

@johnhw
johnhw / trace_frame.py
Created July 8, 2019 16:09
Determine source of function calls in Python
import inspect
class tracked:
"""Class to remember the execution frame
that a function call was made in."""
def __init__(self, fn):
self.fn = fn
import numpy as np
def softmax(a,b):
return np.log(np.exp(a)+np.exp(b))
def softmin(a,b):
return -softmax(-a, -b)
def softrank(a, b):
return softmin(a,b), softmax(a,b)
from tqdm.auto import trange, tqdm
def tqdm_train_loop(train_fn, num_epochs, train_loader, val_fn=None):
# each epoch
with trange(num_epochs, unit="epoch") as epoch_t:
for epoch in epoch_t:
# each batch
with tqdm(train_loader, leave=False, unit='batch', postfix='') as batch_t:
for batch_idx, (features, targets) in enumerate(batch_t):
cost = train_fn(features, labels)
batch_t.postfix = f"cost {cost.item():.2f}"
@johnhw
johnhw / prime_dance.py
Created March 17, 2019 11:07
Animate the "flower" pattern of primes, using the Hungarian algorithm to ensure smooth interpolation
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from collections import defaultdict
import colorsys
import munkres # provides Hungarian algorithm
import scipy.spatial.distance as dist
import functools
import os
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# This implements the ideas in the paper "Neural Ordinary Differential Equations"
# in as simple a form as possible, using only autograd. It is not efficient.
# It is not useful for any practical purpose.
# Use [torchdiffeq](https://github.com/rtqichen/torchdiffeq) for any real use.
#
# > [1] Ricky T. Q. Chen, Yulia Rubanova, Jesse Bettencourt, David Duvenaud.
# "Neural Ordinary Differential Equations." *Advances in Neural Processing Information Systems.* 2018.
# [[arxiv]](https://arxiv.org/abs/1806.07366)
#
# The implementation is based on the
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

A complete list of books, articles, blog posts, videos and neat pages that support Data Fundamentals (H), organised by Unit.

Formatting

If the resource is available online (legally) I have included a link to it. Each entry has symbols following it.

  • ⨕⨕⨕ indicates difficulty/depth, from ⨕ (easy to pick up intro, no background required) through ⨕⨕⨕⨕⨕ (graduate level textbook, maths heavy, expect equations)
  • ⭐ indicates a particularly recommended resource; 🌟 is a very strongly recommended resource and you should look at it.
# BSD 3-Clause License
# Copyright (c) 2018, John H. Williamson
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
@johnhw
johnhw / lecture_tex_header.tex
Created October 7, 2018 09:35
Preamble for formatting math in lecture notes
$$\newcommand{\vec}[1]{{\bf #1} }
\newenvironment{examinable}{\hspace{16em}}{{\hspace{16em}\LARGE[\spadesuit]}}
\newcommand{\real}{\mathbb{R}}
\newcommand{\expect}[1]{\mathbb{E}[#1]}
\DeclareMathOperator*{\argmin}{arg\,min}
\begin{examinable}
\vec{x}
\real
\end{examinable}
$$