def test_convolve_full(self): mode = 'full' devices = [backend.cpu_device] if config.cupy_enabled: devices.append(backend.Device(0)) for device in devices: xp = device.xp with device: for dtype in [np.float32, np.float64, np.complex64, np.complex128]: x = util.dirac([1, 3], device=device, dtype=dtype) W = xp.ones([1, 3], dtype=dtype) y = backend.to_device(conv.convolve(x, W, mode=mode), backend.cpu_device) npt.assert_allclose(y, [[0, 1, 1, 1, 0]], atol=1e-5) x = util.dirac([1, 3], device=device, dtype=dtype) W = xp.ones([1, 2], dtype=dtype) y = backend.to_device(conv.convolve(x, W, mode=mode), backend.cpu_device) npt.assert_allclose(y, [[0, 1, 1, 0]], atol=1e-5) x = util.dirac([1, 3], device=device, dtype=dtype) W = xp.ones([2, 1, 3], dtype=dtype) y = backend.to_device(conv.convolve(x, W, mode=mode, output_multi_channel=True), backend.cpu_device) npt.assert_allclose(y, [[[0, 1, 1, 1, 0]], [[0, 1, 1, 1, 0]]], atol=1e-5)
def test_convolve_valid(self): mode = 'valid' devices = [backend.cpu_device] if config.cupy_enabled: devices.append(backend.Device(0)) for D in [1, 2, 3]: for device in devices: xp = device.xp with device: for dtype in dtypes: with self.subTest(D=D, dtype=dtype, device=device): data = util.dirac([3] + [1] * (D - 1), device=device, dtype=dtype) filt = xp.ones([3] + [1] * (D - 1), dtype=dtype) output = backend.to_device(conv.convolve( data, filt, mode=mode)) npt.assert_allclose( output, np.ones([1] * D), atol=1e-5) data = util.dirac([3] + [1] * (D - 1), device=device, dtype=dtype) filt = xp.ones([2] + [1] * (D - 1), dtype=dtype) output = backend.to_device(conv.convolve( data, filt, mode=mode)) npt.assert_allclose( output, np.ones([2] + [1] * (D - 1)), atol=1e-5) data = util.dirac([1, 3] + [1] * (D - 1), device=device, dtype=dtype) filt = xp.ones([2, 1, 3] + [1] * (D - 1), dtype=dtype) output = backend.to_device( conv.convolve(data, filt, mode=mode, multi_channel=True), backend.cpu_device) npt.assert_allclose( output, np.ones([2, 1] + [1] * (D - 1)), atol=1e-5) data = util.dirac([1, 3] + [1] * (D - 1), device=device, dtype=dtype) filt = xp.ones([2, 1, 3] + [1] * (D - 1), dtype=dtype) strides = [2] + [1] * (D - 1) output = backend.to_device( conv.convolve(data, filt, mode=mode, strides=strides, multi_channel=True), backend.cpu_device) npt.assert_allclose( output, np.ones([2, 1] + [1] * (D - 1)), atol=1e-5)
def test_dirac(self): output = util.dirac([5]) truth = [0, 0, 1, 0, 0] npt.assert_allclose(output, truth) output = util.dirac([4]) truth = [0, 0, 1, 0] npt.assert_allclose(output, truth)
def test_convolve_full(self): mode = 'full' devices = [backend.cpu_device] if config.cupy_enabled: devices.append(backend.Device(0)) dtypes = [np.float32, np.float64, np.complex64, np.complex128] for device in devices: xp = device.xp with device: for dtype in dtypes: with self.subTest(dtype=dtype, device=device): data = util.dirac([1, 3], device=device, dtype=dtype) filt = xp.ones([1, 3], dtype=dtype) output = backend.to_device( conv.convolve(data, filt, mode=mode)) npt.assert_allclose(output, [[0, 1, 1, 1, 0]], atol=1e-5) data = util.dirac([1, 3], device=device, dtype=dtype) filt = xp.ones([1, 2], dtype=dtype) output = backend.to_device( conv.convolve(data, filt, mode=mode)) npt.assert_allclose(output, [[0, 1, 1, 0]], atol=1e-5) data = util.dirac([1, 1, 3], device=device, dtype=dtype) filt = xp.ones([2, 1, 1, 3], dtype=dtype) output = backend.to_device( conv.convolve(data, filt, mode=mode, multi_channel=True), backend.cpu_device) npt.assert_allclose( output, [[[0, 1, 1, 1, 0]], [[0, 1, 1, 1, 0]]], atol=1e-5) data = util.dirac([1, 1, 3], device=device, dtype=dtype) filt = xp.ones([2, 1, 1, 3], dtype=dtype) strides = [1, 2] output = backend.to_device( conv.convolve(data, filt, mode=mode, strides=strides, multi_channel=True)) npt.assert_allclose(output, [[[0, 1, 0]], [[0, 1, 0]]], atol=1e-5)