You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@singa.apache.org by GitBox <gi...@apache.org> on 2019/11/29 02:24:24 UTC
[GitHub] [singa] joddiy edited a comment on issue #565: fixed broadcast div
pow
joddiy edited a comment on issue #565: fixed broadcast div pow
URL: https://github.com/apache/singa/pull/565#issuecomment-559638167
For div based cpu
```
dev = cpu_dev
cases = [
([3, 4, 5], [5]), # 3d vs 1d
([3, 4, 5], [4, 5]), # 3d vs 2d
([3, 4, 5, 6], [5, 6]), # 4d vs 2d
([3, 4, 5, 6], [4, 5, 6]), # 4d vs 3d
([1, 4, 1, 6], [3, 1, 5, 6]) # 4d vs 4d
]
for in1, in2 in cases:
x = np.random.randn(*in1).astype(np.float32)
x1 = np.random.randn(*in2).astype(np.float32) + 1.0
y = x / x1
dy = np.random.randn(*y.shape).astype(np.float32)
grad0 = np.sum(np.power(x1, -1) * dy, axis=axis_helper(y.shape, x.shape)).reshape(x.shape)
grad1 = np.sum(x * - np.power(x1, -2) * dy, axis=axis_helper(y.shape, x1.shape)).reshape(x1.shape)
x = tensor.from_numpy(x)
x1 = tensor.from_numpy(x1)
dy = tensor.from_numpy(dy)
x.to_device(dev)
x1.to_device(dev)
dy.to_device(dev)
result = autograd.div(x,x1)
dx0,dx1 = result.creator.backward(dy.data)
np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx0)), grad0, decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx1)), grad1, decimal=5)
```
For pow based gpu
```
dev = gpu_dev
cases = [
([3, 4, 5], [5]), # 3d vs 1d
([3, 4, 5], [4, 5]), # 3d vs 2d
([3, 4, 5, 6], [5, 6]), # 4d vs 2d
([3, 4, 5, 6], [4, 5, 6]), # 4d vs 3d
([1, 4, 1, 6], [3, 1, 5, 6]) # 4d vs 4d
]
for in1, in2 in cases:
x = np.random.randint(1, 10, size=in1).astype(np.float32)
x1 = np.random.randint(1, 5, size=in2).astype(np.float32)
y = np.power(x, x1).astype(np.float32)
dy = np.random.randn(*y.shape).astype(np.float32)
grad0 = np.sum(x1 * np.power(x, x1-1) * dy, axis=axis_helper(y.shape, x.shape)).reshape(x.shape)
grad1 = np.sum(np.power(x, x1) * np.log(x) * dy, axis=axis_helper(y.shape, x1.shape)).reshape(x1.shape)
x = tensor.from_numpy(x)
x1 = tensor.from_numpy(x1)
dy = tensor.from_numpy(dy)
x.to_device(dev)
x1.to_device(dev)
dy.to_device(dev)
result = autograd.pow(x,x1)
dx0,dx1 = result.creator.backward(dy.data)
np.testing.assert_array_almost_equal(tensor.to_numpy(result), y, decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx0)), grad0, decimal=5)
np.testing.assert_array_almost_equal(tensor.to_numpy(tensor.from_raw_tensor(dx1)), grad1, decimal=5)
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services