(self,
func,
param,
autograds,
h=0.0005,
df=1,
dev=cpu_dev)
| 346 | self._batchnorm2d_helper(gpu_dev) |
| 347 | |
| 348 | def gradients_check(self, |
| 349 | func, |
| 350 | param, |
| 351 | autograds, |
| 352 | h=0.0005, |
| 353 | df=1, |
| 354 | dev=cpu_dev): |
| 355 | # param: PyTensor |
| 356 | # autograds: numpy_tensor |
| 357 | p = tensor.to_numpy(param) |
| 358 | it = np.nditer(p, flags=['multi_index'], op_flags=['readwrite']) |
| 359 | while not it.finished: |
| 360 | idx = it.multi_index |
| 361 | diff = np.zeros_like(p) |
| 362 | diff[idx] += h |
| 363 | diff = tensor.from_numpy(diff) |
| 364 | diff.to_device(dev) |
| 365 | |
| 366 | param += diff |
| 367 | pos = func() |
| 368 | pos = tensor.to_numpy(pos) |
| 369 | |
| 370 | param -= diff |
| 371 | param -= diff |
| 372 | neg = func() |
| 373 | neg = tensor.to_numpy(neg) |
| 374 | |
| 375 | numerical_grad = np.sum((pos - neg) * df) / (2 * h) |
| 376 | #print((autograds[idx] - numerical_grad)/numerical_grad) |
| 377 | # threshold set as -5% to +5% |
| 378 | #self.assertAlmostEqual((autograds[idx] - numerical_grad)/(numerical_grad+0.0000001), 0., places=1) |
| 379 | self.assertAlmostEqual(autograds[idx] - numerical_grad, |
| 380 | 0., |
| 381 | places=2) |
| 382 | |
| 383 | it.iternext() |
| 384 | |
| 385 | def _vanillaRNN_gpu_tiny_ops_shape_check_helper(self, dev): |
| 386 | # gradients shape check. |
no test coverage detected