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

Method _pow_broadcast_helper

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

Source from the content-addressed store, hash-verified

2452 self._div_broadcast_helper(gpu_dev)
2453
2454 def _pow_broadcast_helper(self, dev):
2455 cases = [
2456 ([3, 4, 5], [5]), # 3d vs 1d
2457 ([3, 4, 5], [4, 5]), # 3d vs 2d
2458 ([3, 4, 5, 6], [5, 6]), # 4d vs 2d
2459 ([3, 4, 5, 6], [4, 5, 6]), # 4d vs 3d
2460 ([1, 4, 1, 6], [3, 1, 5, 6]) # 4d vs 4d
2461 ]
2462 for in1, in2 in cases:
2463 x = np.random.randint(1, 10, size=in1).astype(np.float32)
2464 x1 = np.random.randint(1, 5, size=in2).astype(np.float32)
2465 y = np.power(x, x1).astype(np.float32)
2466
2467 dy = np.random.randn(*y.shape).astype(np.float32)
2468 grad0 = np.sum(x1 * np.power(x, x1 - 1) * dy,
2469 axis=axis_helper(y.shape, x.shape)).reshape(x.shape)
2470 grad1 = np.sum(np.power(x, x1) * np.log(x) * dy,
2471 axis=axis_helper(y.shape,
2472 x1.shape)).reshape(x1.shape)
2473
2474 x = tensor.from_numpy(x)
2475 x1 = tensor.from_numpy(x1)
2476 dy = tensor.from_numpy(dy)
2477 x.to_device(dev)
2478 x1.to_device(dev)
2479 dy.to_device(dev)
2480
2481 result = autograd.pow(x, x1)
2482 dx0, dx1 = result.creator.backward(dy.data)
2483 np.testing.assert_array_almost_equal(tensor.to_numpy(result),
2484 y,
2485 decimal=2)
2486 np.testing.assert_array_almost_equal(tensor.to_numpy(
2487 tensor.from_raw_tensor(dx0)),
2488 grad0,
2489 decimal=2)
2490 np.testing.assert_array_almost_equal(tensor.to_numpy(
2491 tensor.from_raw_tensor(dx1)),
2492 grad1,
2493 decimal=2)
2494
2495 def test_pow_broadcast_cpu(self):
2496 self._pow_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