Skip to content

Instantly share code, notes, and snippets.

@jotterbach
jotterbach / plot_gaussian_process.py
Last active September 15, 2018 18:52
Snippet to demonstrate the conditioning procedure of a Gaussian Process and how to plot it.
import seaborn as sns
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
from scipy.spatial import distance as dist
import scipy.linalg as la
import itertools as it
%matplotlib inline
@jotterbach
jotterbach / es.ml
Last active February 13, 2018 16:55
Evolution Strategy Optimizer
(*
The Evolution Strategy Optimizer is a Hill-Climbing routine based on stochastic gradient
estimation (not to be confused with stochastic gradient descent optimization). The basic idea
is to perturb the current best parameter value using Gaussian noise and explore the function
values of the blackbox objective in the vicinity of the parameters. From these explorations one
can estimate the gradient and perform a gradient update step.
More information can be found in:
- T. Salimans et al. Evolution Strategies as a Scalable Alternative to Reinforcement Learning,
arXiv:1703.03864, https://arxiv.org/pdf/1703.03864.pdf
@jotterbach
jotterbach / rle.ml
Last active February 13, 2018 18:14
Run-Length Encoding in OCaml
(*
This is a simple solution to the Run-Length Encoding problem described
in Watrophy #24 (http://www.watrophy.com/posts/24-Run-Length-Encoding.html)
*)
open Core
let rle int_list =
let increment tup n =
match tup with
@jotterbach
jotterbach / topological_sort.ml
Created March 6, 2018 06:45
Topological Sort using functional style and fixpoints
(** The fundamental idea behind this sort algorithm is to iteratively remove nodes from a DAG
* who do not possess ancestors and hence can be considered roots. We add them to an ordered list
* and get a topological sort of the dependency graph. *)
open Core
open Graph.Pack.Digraph
(** Simple helper to create a OCamlGraph with directionality *)
let create_digraph nodes edges =
@jotterbach
jotterbach / erdos_renyi.ml
Created April 10, 2018 05:53
Using ocamlgraph to create a summary statistic of Erdös-Renyi graphs
open Graph
open Core
open Owl
module G = Persistent.Graph.Concrete(
struct
type t = int
let equal a b = phys_equal a b
let compare a b = Pervasives.compare a b
let hash a = a
@jotterbach
jotterbach / compute_jacobians.py
Created March 25, 2020 16:02
Compute the jacobian of some output w.r.t. some inputs. This can be really useful in debugging autoregressive models
from torch.autograd.gradcheck import zero_gradients
def compute_jacobian(inputs, output):
"""
:param inputs: Batch X Size (e.g. Depth X Width X Height)
:param output: Batch X Classes
:return: jacobian: Batch X Classes X Size
"""
assert inputs.requires_grad
@jotterbach
jotterbach / make_grid.py
Created February 12, 2021 00:24
Make a grid of images that are stored individually in a numpy array
def make_grid(arr: np.ndarray, nrows: int, pad_size: int = 2, pad_value: int = 0):
"""
Expected array layout is (N, C, H, W)
"""
arr = np.pad(arr,
((0,0),
(0,0),
(pad_size // 2, pad_size // 2),
(pad_size // 2, pad_size // 2)),
constant_values=(pad_value,))
@jotterbach
jotterbach / flop_counts.py
Last active February 17, 2021 18:40
Utility to compute the flop count of various elementary neural network modules
import numpy as np
def conv_out_shape(in_shape: tuple, kernel_size:int, dilation:int, stride:int, padding:int):
"""
Convenience function for computing the output shape of an image tensor, when applying symmetric kernels, strides, etc.
"""
out_dim = []
for d in in_shape:
_d_out = d + 2 * padding - dilation * (kernel_size - 1) - 1
@jotterbach
jotterbach / cuda_11.8_installation_on_Ubuntu_22.04
Created November 22, 2023 20:49 — forked from MihailCosmin/cuda_11.8_installation_on_Ubuntu_22.04
Instructions for CUDA v11.8 and cuDNN 8.7 installation on Ubuntu 22.04 for PyTorch 2.0.0
#!/bin/bash
### steps ####
# verify the system has a cuda-capable gpu
# download and install the nvidia cuda toolkit and cudnn
# setup environmental variables
# verify the installation
###
### to verify your gpu is cuda enable check