MCPcopy Create free account
hub / github.com/apache/singa / _div_broadcast_helper

Method _div_broadcast_helper

test/python/test_operation.py:2402–2445  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

2400 self._mul_broadcast_helper(gpu_dev)
2401
2402 def _div_broadcast_helper(self, dev):
2403 cases = [
2404 ([3, 4, 5], [5]), # 3d vs 1d
2405 ([3, 4, 5], [4, 5]), # 3d vs 2d
2406 ([3, 4, 5, 6], [5, 6]), # 4d vs 2d
2407 ([3, 4, 5, 6], [4, 5, 6]), # 4d vs 3d
2408 ([1, 4, 1, 6], [3, 1, 5, 6]) # 4d vs 4d
2409 ]
2410 for in1, in2 in cases:
2411 x = np.random.randn(*in1).astype(np.float32)
2412 x1 = np.random.randn(*in2).astype(np.float32) + 1.0
2413 y = x / x1
2414
2415 dy = np.random.randn(*y.shape).astype(np.float32)
2416 grad0 = np.sum(np.power(x1, -1) * dy,
2417 axis=axis_helper(y.shape, x.shape)).reshape(x.shape)
2418 grad1 = np.sum(x * -np.power(x1, -2) * dy,
2419 axis=axis_helper(y.shape,
2420 x1.shape)).reshape(x1.shape)
2421
2422 x = tensor.from_numpy(x)
2423 x1 = tensor.from_numpy(x1)
2424 dy = tensor.from_numpy(dy)
2425 x.to_device(dev)
2426 x1.to_device(dev)
2427 dy.to_device(dev)
2428
2429 result = autograd.div(x, x1)
2430 dx0, dx1 = result.creator.backward(dy.data)
2431 # use realtive and total error instead of demical number
2432 np.testing.assert_allclose(tensor.to_numpy(result),
2433 y,
2434 rtol=1e-4,
2435 atol=1e-4)
2436 np.testing.assert_allclose(tensor.to_numpy(
2437 tensor.from_raw_tensor(dx0)),
2438 grad0,
2439 rtol=1e-4,
2440 atol=1e-4)
2441 np.testing.assert_allclose(tensor.to_numpy(
2442 tensor.from_raw_tensor(dx1)),
2443 grad1,
2444 rtol=1e-4,
2445 atol=1e-4)
2446
2447 def test_div_broadcast_cpu(self):
2448 self._div_broadcast_helper(cpu_dev)

Callers 2

Calls 4

axis_helperFunction · 0.70
reshapeMethod · 0.45
to_deviceMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected