(self, dev)
| 2076 | self._negative_helper(gpu_dev) |
| 2077 | |
| 2078 | def _reciprocal_helper(self, dev): |
| 2079 | X = np.array([0.1, 0, 0.4, 1. - 4, 0.9, |
| 2080 | -2.0]).reshape(3, 2).astype(np.float32) |
| 2081 | DY = np.ones((3, 2), dtype=np.float32) |
| 2082 | |
| 2083 | x = tensor.from_numpy(X) |
| 2084 | dy = tensor.from_numpy(DY) |
| 2085 | x.to_device(dev) |
| 2086 | dy.to_device(dev) |
| 2087 | |
| 2088 | result = autograd.reciprocal(x) |
| 2089 | dx = result.creator.backward(dy.data) |
| 2090 | #dy/dx = -1/x**2 |
| 2091 | with np.errstate(divide='ignore'): |
| 2092 | XT = np.reciprocal(X) |
| 2093 | DX = -1 / np.square(X) |
| 2094 | |
| 2095 | np.testing.assert_array_almost_equal(tensor.to_numpy(result), |
| 2096 | XT, |
| 2097 | decimal=5) |
| 2098 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2099 | tensor.from_raw_tensor(dx)), |
| 2100 | DX, |
| 2101 | decimal=5) |
| 2102 | |
| 2103 | def test_reciprocal_cpu(self): |
| 2104 | self._reciprocal_helper(cpu_dev) |
no test coverage detected