Last active
October 28, 2016 01:13
-
-
Save igul222/2ffcb54a8613193e70357bdcfee68ac4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
debug tools for theano | |
Ishaan Gulrajani | |
""" | |
import numpy as np | |
import theano | |
from theano import gof | |
class DebugOp(gof.Op): | |
def __init__(self, name, fn): | |
super(DebugOp, self).__init__() | |
self._name = name | |
self._fn = fn | |
def make_node(self, x): | |
return gof.Apply(self, [x], [x.type()]) | |
def perform(self, node, inputs, output_storage): | |
self._fn(self._name, inputs[0]) | |
output_storage[0][0] = np.copy(inputs[0]) | |
def grad(self, inputs, output_gradients): | |
return [DebugOp(self._name+'.grad', self._fn)(output_gradients[0])] | |
def print_shape(name, x): | |
def fn(_name, _x): | |
print "{} shape: {}".format(_name, _x.shape) | |
return DebugOp(name, fn)(x) | |
def print_stats(name, x): | |
def fn(_name, _x): | |
mean = np.mean(_x) | |
std = np.std(_x) | |
percentiles = np.percentile(_x, [0,25,50,75,100]) | |
# percentiles = "skipping" | |
print "{}\tmean:{}\tstd:{}\tpercentiles:{}\t".format(_name, mean, std, percentiles) | |
return DebugOp(name, fn)(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment