(self, dev)
| 2306 | self._add_broadcast_helper(gpu_dev) |
| 2307 | |
| 2308 | def _sub_broadcast_helper(self, dev): |
| 2309 | cases = [ |
| 2310 | ([3, 4, 5], [5]), # 3d vs 1d |
| 2311 | ([3, 4, 5], [4, 5]), # 3d vs 2d |
| 2312 | ([3, 4, 5, 6], [5, 6]), # 4d vs 2d |
| 2313 | ([3, 4, 5, 6], [4, 5, 6]), # 4d vs 3d |
| 2314 | ([1, 4, 1, 6], [3, 1, 5, 6]) # 4d vs 4d |
| 2315 | ] |
| 2316 | for in1, in2 in cases: |
| 2317 | x = np.random.randn(*in1).astype(np.float32) |
| 2318 | x1 = np.random.randn(*in2).astype(np.float32) |
| 2319 | y = x - x1 |
| 2320 | |
| 2321 | dy = np.random.randn(*y.shape) |
| 2322 | grad0 = np.sum(dy, axis=axis_helper(y.shape, |
| 2323 | x.shape)).reshape(x.shape) |
| 2324 | grad1 = np.sum(-dy, axis=axis_helper(y.shape, |
| 2325 | x1.shape)).reshape(x1.shape) |
| 2326 | |
| 2327 | x = tensor.from_numpy(x) |
| 2328 | x1 = tensor.from_numpy(x1) |
| 2329 | dy = tensor.from_numpy(dy) |
| 2330 | x.to_device(dev) |
| 2331 | x1.to_device(dev) |
| 2332 | dy.to_device(dev) |
| 2333 | |
| 2334 | result = autograd.sub(x, x1) |
| 2335 | dx0, dx1 = result.creator.backward(dy.data) |
| 2336 | np.testing.assert_array_almost_equal(tensor.to_numpy(result), |
| 2337 | y, |
| 2338 | decimal=5) |
| 2339 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2340 | tensor.from_raw_tensor(dx0)), |
| 2341 | grad0, |
| 2342 | decimal=5) |
| 2343 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2344 | tensor.from_raw_tensor(dx1)), |
| 2345 | grad1, |
| 2346 | decimal=5) |
| 2347 | |
| 2348 | def test_sub_broadcast_cpu(self): |
| 2349 | self._sub_broadcast_helper(cpu_dev) |
no test coverage detected