Skip to content

Instantly share code, notes, and snippets.

View nirum's full-sized avatar

Niru Maheswaranathan nirum

View GitHub Profile
@nirum
nirum / classes.py
Last active October 28, 2015 04:54
classes in python
class Foo(object):
def __init__(self, data):
self.data = data
self.shape = self.data.shape
def __getitem__(self, index):
return self.data[index] ** 2
def __dir__(self):
@nirum
nirum / unfold.py
Last active February 3, 2018 23:58
Unfold a muli-dimensional numpy array along an arbitrary axis
import numpy as np
def unfold(arr, ax):
"""
Unfolds a given array along the given axis
"""
return np.rollaxis(arr, ax, 0).reshape(arr.shape[ax], -1)
def test_unfold():
"""
@nirum
nirum / test_numpy.py
Last active August 29, 2015 14:26 — forked from osdf/test_numpy.py
Testing numpy and scipy setups
#!/usr/bin/env python
import numpy
import sys
import timeit
try:
import numpy.core._dotblas
print 'FAST BLAS'
except ImportError:
print 'slow blas'
@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 *
@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 / 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 / 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 / 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 / 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):
# Dark mode
color0 #282828
color1 #cc241d
color2 #98971a
color3 #d79921
color4 #458588
color5 #b16286
color6 #689d6a
color7 #a89984
color8 #928374