Skip to content

Instantly share code, notes, and snippets.

@crowsonkb
crowsonkb / capture.py
Created December 16, 2018 03:14
Captures and returns an argument passed to a lambda expression.
"""Captures and returns an argument passed to a lambda expression."""
class Capture:
"""Captures and returns an argument passed to a lambda expression.
:param fn: The lambda expression.
:type fn: function
:Example:
@crowsonkb
crowsonkb / ewma.py
Last active September 30, 2018 19:00
import numpy as np
class EWMA:
"""An exponentially weighted moving average with initialization bias correction."""
def __init__(self, shape=(), dtype=np.float64, beta=0.9, correct_bias=True):
self.beta = beta
self.beta_accum = 1 if correct_bias else 0
self.value = np.zeros(shape, dtype)
import pyparsing as pp
from pyparsing import pyparsing_common as ppc
from repl import repl
class Node:
def __init__(self, name, *args):
self.name = name
self.items = args
import pyparsing as pp
from pyparsing import pyparsing_common as ppc
from repl import repl
variables = {}
var = ppc.identifier.copy()
def do_var(t):
"""Converts between the RGB and CIECAM02 color spaces."""
from collections import namedtuple
from functools import partial
import colour
from colour.utilities import tsplit, tstack
import numpy as np
from scipy.optimize import fmin_l_bfgs_b
@crowsonkb
crowsonkb / hsl.py
Created May 11, 2018 20:29
Converts colors from HSL to RGB and back.
"""Converts colors from HSL to RGB and back."""
import numpy as np
from scipy import optimize
def tstack(a):
"""Stacks arrays in sequence along the last axis (tail)."""
a = np.asarray(a)
return np.concatenate([x[..., np.newaxis] for x in a], axis=-1)
import argparse
from functools import partial, reduce
import math
from pathlib import Path
import random
from keras.layers import *
from keras.models import Model
from keras.utils import io_utils
name: "VGG_ILSVRC_16_layers_conv"
force_backward: true
layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 1 dim: 3 dim: 224 dim: 224 } }
}
layer {
bottom: "data"
@crowsonkb
crowsonkb / dmsqn.py
Last active February 26, 2017 03:56
Experimental quasi-Newton optimizer for image synthesis from CNNs
import numpy as np
from scipy.linalg import blas
from scipy.ndimage import zoom
# Machine epsilon for float32
EPS = np.finfo(np.float32).eps
# pylint: disable=no-member
def dot(x, y):
--[[ An experimental quasi-Newton optimizer.
Incorporates Hessian damping, momentum, and per-feature learning rate scaling.
Also implements optional polynomial-decay averaging (similar to ASGD).
ARGS:
- 'opfunc' : a function that takes a single input (X), the point
of a evaluation, and returns f(X) and df/dX
- 'x' : the initial point
- 'config` : a table with configuration parameters for the optimizer