Computes the theoretical and numerical jacobian.
(x,
x_shape,
dx,
y,
y_shape,
dy,
x_init_value=None,
delta=1e-3,
extra_feed_dict=None)
| 209 | |
| 210 | |
| 211 | def _compute_gradient(x, |
| 212 | x_shape, |
| 213 | dx, |
| 214 | y, |
| 215 | y_shape, |
| 216 | dy, |
| 217 | x_init_value=None, |
| 218 | delta=1e-3, |
| 219 | extra_feed_dict=None): |
| 220 | """Computes the theoretical and numerical jacobian.""" |
| 221 | t = dtypes.as_dtype(x.dtype) |
| 222 | allowed_types = [dtypes.float16, dtypes.bfloat16, dtypes.float32, |
| 223 | dtypes.float64, dtypes.complex64, dtypes.complex128] |
| 224 | assert t.base_dtype in allowed_types, "Don't support type %s for x" % t.name |
| 225 | t2 = dtypes.as_dtype(y.dtype) |
| 226 | assert t2.base_dtype in allowed_types, "Don't support type %s for y" % t2.name |
| 227 | |
| 228 | if x_init_value is not None: |
| 229 | i_shape = list(x_init_value.shape) |
| 230 | assert(list(x_shape) == i_shape), "x_shape = %s, init_data shape = %s" % ( |
| 231 | x_shape, i_shape) |
| 232 | x_data = x_init_value |
| 233 | else: |
| 234 | x_data = np.random.random_sample(x_shape).astype(t.as_numpy_dtype) |
| 235 | if t.is_complex: |
| 236 | x_data.imag = np.random.random_sample(x_shape) |
| 237 | |
| 238 | jacob_t = _compute_theoretical_jacobian( |
| 239 | x, x_shape, x_data, dy, y_shape, dx, extra_feed_dict=extra_feed_dict) |
| 240 | jacob_n = _compute_numeric_jacobian( |
| 241 | x, x_shape, x_data, y, y_shape, delta, extra_feed_dict=extra_feed_dict) |
| 242 | return jacob_t, jacob_n |
| 243 | |
| 244 | |
| 245 | def _compute_gradient_list(x, |