'perform gradient check.
since gradcheck can be tricky (especially with relus involved)
this function prints to console for visual inspection'

def gradCheck(self, batch, model, cost_function, **kwargs):

    num_checks = kwargs.get('num_checks', 10)
    delta = kwargs.get('delta', 1e-05)
    rel_error_thr_warning = kwargs.get('rel_error_thr_warning', 0.01)
    rel_error_thr_error = kwargs.get('rel_error_thr_error', 1)
    cg = cost_function(batch, model)
    print 'running    gradient    check...'
    for p in model.keys():
        print ('checking    gradient    on    parameter    %s    of    shape    %s...' % (p, `model[p].shape`))
        mat = model[p]
        s0 = cg['grad'][p].shape
        s1 = mat.shape
        assert (s0 == s1), ('Error    dims    dont    match:    %s    and    %s.' % (`s0`, `s1`))
        for i in xrange(num_checks):
            ri = randi(mat.size)
            old_val = mat.flat[ri]
            mat.flat[ri] = (old_val + delta)
            cg0 = cost_function(batch, model)
            mat.flat[ri] = (old_val - delta)
            cg1 = cost_function(batch, model)
            mat.flat[ri] = old_val
            grad_analytic = cg['grad'][p].flat[ri]
            grad_numerical = ((cg0['cost']['total_cost'] - cg1['cost']['total_cost']) / (2 * delta))
            if ((grad_numerical == 0) and (grad_analytic == 0)):
                rel_error = 0
                status = 'OK'
            elif ((abs(grad_numerical) < 1e-07) and (abs(grad_analytic) < 1e-07)):
                rel_error = 0
                status = 'VAL    SMALL    WARNING'
            else:
                rel_error = (abs((grad_analytic - grad_numerical)) / abs((grad_numerical + grad_analytic)))
                status = 'OK'
                if (rel_error > rel_error_thr_warning):
                    status = 'WARNING'
                if (rel_error > rel_error_thr_error):
                    status = '!!!!!    NOTOK'
            print ('%s    checking    param    %s    index    %8d    (val    =    %+8f),    analytic    =    %+8f,    numerical    =    %+8f,    relative    error    =    %+8f' % (status, p, ri, old_val, grad_analytic, grad_numerical, rel_error))
