gradient_checker.py 文件源码

python
阅读 43 收藏 0 点赞 0 评论 0

项目:thinstack-rl 作者: hans 项目源码 文件源码
def _compute_gradient(x,
                      x_shape,
                      dx,
                      y,
                      y_shape,
                      dy,
                      x_init_value=None,
                      delta=1e-3,
                      feed_dict=None,
                      prep_fn=None,
                      limit=0):
  """Computes the theoretical and numerical jacobian."""
  t = dtypes.as_dtype(x.dtype)
  allowed_types = [dtypes.float16, dtypes.float32, dtypes.float64,
                   dtypes.complex64, dtypes.complex128]
  assert t.base_dtype in allowed_types, "Don't support type %s for x" % t.name
  t2 = dtypes.as_dtype(y.dtype)
  assert t2.base_dtype in allowed_types, "Don't support type %s for y" % t2.name

  if x_init_value is not None:
    i_shape = list(x_init_value.shape)
    assert(list(x_shape) == i_shape), "x_shape = %s, init_data shape = %s" % (
        x_shape, i_shape)
    x_data = x_init_value
  else:
    if t == dtypes.float16:
      dtype = np.float16
    elif t == dtypes.float32:
      dtype = np.float32
    else:
      dtype = np.float64
    x_data = np.asfarray(np.random.random_sample(x_shape), dtype=dtype)

  print("\ttheoretical jacobian..")
  jacob_t = _compute_theoretical_jacobian(x, x_shape, x_data, dy, y_shape, dx, feed_dict, prep_fn=prep_fn)

  print("\tnumeric jacobian..")
  jacob_n = _compute_numeric_jacobian(x, x_shape, x_data, y, y_shape, delta, feed_dict, prep_fn=prep_fn, limit=limit)
  return jacob_t, jacob_n
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号