Esempio n. 1
0
    def gradCheck(self, batch, model, cost_function, **kwargs):
        """ 
    perform gradient check.
    since gradcheck can be tricky (especially with relus involved)
    this function prints to console for visual inspection
    """

        num_checks = kwargs.get('num_checks', 10)
        delta = kwargs.get('delta', 1e-5)
        rel_error_thr_warning = kwargs.get('rel_error_thr_warning', 1e-2)
        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)

                # evluate cost at [x + delta] and [x - delta]
                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  # reset old value for this parameter

                # fetch both numerical and analytic gradient
                grad_analytic = cg['grad'][p].flat[ri]
                grad_numerical = (cg0['cost']['total_cost'] -
                                  cg1['cost']['total_cost']) / (2 * delta)

                # compare them
                if grad_numerical == 0 and grad_analytic == 0:
                    rel_error = 0  # both are zero, OK.
                    status = 'OK'
                elif abs(grad_numerical) < 1e-7 and abs(grad_analytic) < 1e-7:
                    rel_error = 0  # not enough precision to check this
                    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 stats
                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)
Esempio n. 2
0
  def gradCheck(self, batch, model, cost_function, **kwargs):
    """
    perform gradient check.
    since gradcheck can be tricky (especially with relus involved)
    this function prints to console for visual inspection
    """

    num_checks = kwargs.get('num_checks', 10)
    delta = kwargs.get('delta', 1e-5)
    rel_error_thr_warning = kwargs.get('rel_error_thr_warning', 1e-2)
    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)
        ipdb.set_trace()

        # evluate cost at [x + delta] and [x - delta]
        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 # reset old value for this parameter

        # fetch both numerical and analytic gradient
        grad_analytic = cg['grad'][p].flat[ri]
        grad_numerical = (cg0['cost']['total_cost'] - cg1['cost']['total_cost']) / ( 2 * delta )

        # compare them
        if grad_numerical == 0 and grad_analytic == 0:
          rel_error = 0 # both are zero, OK.
          status = 'OK'
        elif abs(grad_numerical) < 1e-7 and abs(grad_analytic) < 1e-7:
          rel_error = 0 # not enough precision to check this
          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 stats
        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)