(self, dev)
| 1881 | self._HardSigmoid_helper(gpu_dev) |
| 1882 | |
| 1883 | def _prelu_helper(self, dev): |
| 1884 | x = np.random.randn(3, 2) |
| 1885 | slope = np.random.randn(3, 2) |
| 1886 | y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope |
| 1887 | dy = np.random.randn(3, 2) |
| 1888 | x0 = x.copy() |
| 1889 | x0[x0 > 0] = 1 |
| 1890 | x0[x0 < 1] = 0 |
| 1891 | grad0 = (x0 + (1 - x0) * slope) * dy |
| 1892 | grad1 = (1 - x0) * x * dy |
| 1893 | x = tensor.from_numpy(x) |
| 1894 | slope = tensor.from_numpy(slope) |
| 1895 | dy = tensor.from_numpy(dy) |
| 1896 | x.to_device(dev) |
| 1897 | slope.to_device(dev) |
| 1898 | dy.to_device(dev) |
| 1899 | result = autograd.prelu(x, slope) |
| 1900 | dx0, dx1 = result.creator.backward(dy.data) |
| 1901 | np.testing.assert_array_almost_equal(tensor.to_numpy(result), |
| 1902 | y, |
| 1903 | decimal=5) |
| 1904 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 1905 | tensor.from_raw_tensor(dx0)), |
| 1906 | grad0, |
| 1907 | decimal=5) |
| 1908 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 1909 | tensor.from_raw_tensor(dx1)), |
| 1910 | grad1, |
| 1911 | decimal=5) |
| 1912 | |
| 1913 | def test_prelu_cpu(self): |
| 1914 | self._prelu_helper(cpu_dev) |
no test coverage detected