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

Method _HardSigmoid_helper

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

Source from the content-addressed store, hash-verified

1850 self._min_1inputs_helper(gpu_dev)
1851
1852 def _HardSigmoid_helper(self, dev):
1853 x = np.random.randn(3, 2)
1854 #y = max(0, min(1, alpha * x + gamma))
1855 a = 0.2
1856 g = 0.5
1857 y = np.clip(x * 0.2 + 0.5, 0, 1)
1858 dy = np.random.randn(3, 2)
1859 grad = (0 < (np.clip(x * 0.2 + 0.5, 0, 1)) *
1860 (np.clip(x * 0.2 + 0.5, 0, 1) < 1)) * 0.2 * dy
1861 x = tensor.from_numpy(x)
1862 dy = tensor.from_numpy(dy)
1863 x.to_device(dev)
1864 dy.to_device(dev)
1865
1866 result = autograd.hardsigmoid(x, a, g)
1867 dx = result.creator.backward(dy.data)
1868 np.testing.assert_array_almost_equal(tensor.to_numpy(result),
1869 y,
1870 decimal=5)
1871 np.testing.assert_array_almost_equal(tensor.to_numpy(
1872 tensor.from_raw_tensor(dx)),
1873 grad,
1874 decimal=5)
1875
1876 def test_HardSigmoid_cpu(self):
1877 self._HardSigmoid_helper(cpu_dev)

Callers 2

test_HardSigmoid_cpuMethod · 0.95
test_HardSigmoid_gpuMethod · 0.95

Calls 2

to_deviceMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected