(self, dev)
| 2500 | self._pow_broadcast_helper(gpu_dev) |
| 2501 | |
| 2502 | def _prelu_broadcast_helper(self, dev): |
| 2503 | cases = [ |
| 2504 | ([3, 4, 5], [5]), # 3d vs 1d |
| 2505 | ([3, 4, 5], [4, 5]), # 3d vs 2d |
| 2506 | ([3, 4, 5, 6], [5, 6]), # 4d vs 2d |
| 2507 | ([3, 4, 5, 6], [4, 5, 6]), # 4d vs 3d |
| 2508 | ([1, 4, 1, 6], [3, 1, 5, 6]) # 4d vs 4d |
| 2509 | ] |
| 2510 | for in1, in2 in cases: |
| 2511 | x = np.random.randn(*in1).astype(np.float32) |
| 2512 | slope = np.random.randn(*in2).astype(np.float32) |
| 2513 | y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope |
| 2514 | |
| 2515 | dy = np.random.randn(*y.shape).astype(np.float32) |
| 2516 | x0 = x.copy() |
| 2517 | x0[x0 > 0] = 1 |
| 2518 | x0[x0 < 1] = 0 |
| 2519 | grad0 = np.sum((x0 + (1 - x0) * slope) * dy, |
| 2520 | axis=axis_helper(y.shape, x.shape)).reshape(x.shape) |
| 2521 | grad1 = np.sum((1 - x0) * x * dy, |
| 2522 | axis=axis_helper(y.shape, |
| 2523 | slope.shape)).reshape(slope.shape) |
| 2524 | |
| 2525 | x = tensor.from_numpy(x) |
| 2526 | slope = tensor.from_numpy(slope) |
| 2527 | dy = tensor.from_numpy(dy) |
| 2528 | x.to_device(dev) |
| 2529 | slope.to_device(dev) |
| 2530 | dy.to_device(dev) |
| 2531 | |
| 2532 | result = autograd.prelu(x, slope) |
| 2533 | dx0, dx1 = result.creator.backward(dy.data) |
| 2534 | np.testing.assert_array_almost_equal(tensor.to_numpy(result), |
| 2535 | y, |
| 2536 | decimal=5) |
| 2537 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2538 | tensor.from_raw_tensor(dx0)), |
| 2539 | grad0, |
| 2540 | decimal=5) |
| 2541 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2542 | tensor.from_raw_tensor(dx1)), |
| 2543 | grad1, |
| 2544 | decimal=5) |
| 2545 | |
| 2546 | def test_prelu_broadcast_cpu(self): |
| 2547 | self._prelu_broadcast_helper(cpu_dev) |
no test coverage detected