Skip to content

Instantly share code, notes, and snippets.

View nirum's full-sized avatar

Niru Maheswaranathan nirum

View GitHub Profile
@nirum
nirum / pax.py
Created April 23, 2020 00:00
Routines for loading/saving pytrees to disk as hdf5 files.
"""Save/load pytrees to disk."""
import collections
import h5py
import jax
import numpy as np
def save(filepath, tree):
"""Saves a pytree to an hdf5 file.
@nirum
nirum / lax_shape_bug.ipynb
Last active April 17, 2019 19:18
lax_shape_bug
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nirum
nirum / cell07.ipynb
Created December 6, 2018 18:56 — forked from szapp/cell07.ipynb
Attempt to recreate figure 3 from Maheswaranathan et al, 2018
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Dark mode
color0 #282828
color1 #cc241d
color2 #98971a
color3 #d79921
color4 #458588
color5 #b16286
color6 #689d6a
color7 #a89984
color8 #928374
@nirum
nirum / tf_sandbox.py
Last active June 23, 2017 03:51
monkeypatch for giving subclasses their own graph and session (tensorflow)
def tf_graph_wrapper(func):
"""Wraps a class method with a tf.Graph context manager"""
@wraps(func)
def wrapper(self, *args, **kwargs):
with self._graph.as_default():
return func(self, *args, **kwargs)
return wrapper
def tf_init(func):
@nirum
nirum / adaptiveIAF.py
Created March 8, 2017 00:11 — forked from lmcintosh/adaptiveIAF.py
adaptive integrate and (not yet fire) neuron in tensorflow
class AdaptiveIAF(tf.nn.rnn_cell.RNNCell):
def __init__(self, num_units, dt, reuse=False):
self._dt = tf.constant(dt, dtype=tf.float32)
self._num_units = num_units
self._reuse = reuse
@property
def state_size(self):
return (self._num_units, self._num_units)
@nirum
nirum / noisy_opt_demo.py
Created March 3, 2017 23:19
tensorflow optimization with added noise near first-order critical points
import numpy as np
import tensorflow as tf
# initialize variable x=10
x = tf.Variable(10.0, dtype=tf.float32)
# objective is x ** 3 which has a saddle point at x=0
f = x ** 3
# create optimizer and compute gradients
@nirum
nirum / animations.py
Last active April 7, 2019 07:19
Save a numpy array (a stack of images) as a gif using moviepy
import os
from moviepy.editor import ImageSequenceClip
def gif(filename, array, fps=10, scale=1.0):
"""Creates a gif given a stack of images using moviepy
Notes
-----
works with current Github version of moviepy (not the pip version)
@nirum
nirum / autotime.py
Last active November 23, 2015 19:22
IPython magic for automatically timing every REPL command
from __future__ import print_function
from time import perf_counter
from IPython.core.magics.execution import _format_time as fmt
class Timer(object):
"""
Timer is a simple class to keep track of elapsed time.
"""
@nirum
nirum / data_imports.py
Created October 28, 2015 02:57
python data stack standard imports
from numpy.random import rand, randn, shuffle, choice, sample
from numpy import arange, zeros, ones, eye, linspace, pi, inf, nan, cov, array
from scipy.linalg import *
from matplotlib.pyplot import *