예제 #1
0
def test_erf() -> None:
    try:
        from scipy.special import erf as scipy_erf
    except:
        pytest.skip("scipy not installed skipping test for erf")

    x = np.array([-1000, -100, -10] + np.linspace(-5, 5, 1001).tolist() +
                 [10, 100, 1000])
    y_scipy = scipy_erf(x)

    # Test mx.nd
    y_mxnet = util.erf(mx.nd, mx.nd.array(x)).asnumpy()
    assert np.allclose(y_mxnet, y_scipy, rtol=1e-3)

    # Test mx.sym
    X = mx.symbol.Variable("x")
    func = util.erf(mx.sym, X)
    func_exec = func.bind(ctx=mx.cpu(), args={"x": mx.nd.array(x)})
    func_exec.forward()
    y_mxnet_sym = func_exec.outputs[0].asnumpy()
    assert np.allclose(y_mxnet_sym, y_scipy, rtol=1e-3)

    # Text np
    y_np = util.erf(np, x)
    assert np.allclose(y_np, y_scipy, atol=1e-7)
예제 #2
0
def test_erf() -> None:
    try:
        from scipy.special import erf as scipy_erf
    except:
        pytest.skip("scipy not installed skipping test for erf")

    x = np.array([-1000, -100, -10] + np.linspace(-5, 5, 1001).tolist() +
                 [10, 100, 1000])
    y_mxnet = util.erf(mx.nd, mx.nd.array(x)).asnumpy()
    y_scipy = scipy_erf(x)
    assert np.allclose(y_mxnet, y_scipy)
예제 #3
0
def test_erf() -> None:
    pytest.importorskip("scipy")
    from scipy.special import erf as scipy_erf

    x = np.array([-1000, -100, -10] + np.linspace(-5, 5, 1001).tolist() +
                 [10, 100, 1000])
    y_scipy = scipy_erf(x)

    # Text np
    y_np = erf(x)
    assert np.allclose(y_np, y_scipy, atol=1e-7)
예제 #4
0
def test_erf() -> None:
    try:
        from scipy.special import erf as scipy_erf
    except:
        pytest.skip("scipy not installed skipping test for erf")

    x = np.array([-1000, -100, -10] + np.linspace(-5, 5, 1001).tolist() +
                 [10, 100, 1000])
    y_scipy = scipy_erf(x)

    # Text np
    y_np = erf(x)
    assert np.allclose(y_np, y_scipy, atol=1e-7)
예제 #5
0
    def testErfExecution(self):
        raw = np.random.rand(10, 8, 6)
        a = tensor(raw, chunk_size=3)

        r = erf(a)

        result = self.executor.execute_tensor(r, concat=True)[0]
        expected = scipy_erf(raw)

        np.testing.assert_array_equal(result, expected)

        # test sparse
        raw = sps.csr_matrix(np.array([0, 1.0, 1.01, np.nan]))
        a = tensor(raw, chunk_size=3)

        r = erf(a)

        result = self.executor.execute_tensor(r, concat=True)[0]

        data = scipy_erf(raw.data)
        expected = sps.csr_matrix((data, raw.indices, raw.indptr), raw.shape)

        np.testing.assert_array_equal(result.toarray(), expected.toarray())
예제 #6
0
def test_elf():
    raw = np.random.rand(10, 8, 5)
    t = tensor(raw, chunk_size=3)

    r = erf(t)
    expect = scipy_erf(raw)

    assert r.shape == raw.shape
    assert r.dtype == expect.dtype

    t, r = tile(t, r)

    assert r.nsplits == t.nsplits
    for c in r.chunks:
        assert isinstance(c.op, TensorErf)
        assert c.index == c.inputs[0].index
        assert c.shape == c.inputs[0].shape
예제 #7
0
    def testElf(self):
        raw = np.random.rand(10, 8, 5)
        t = tensor(raw, chunk_size=3)

        r = erf(t)
        expect = scipy_erf(raw)

        self.assertEqual(r.shape, raw.shape)
        self.assertEqual(r.dtype, expect.dtype)

        r.tiles()

        self.assertEqual(r.nsplits, t.nsplits)
        for c in r.chunks:
            self.assertIsInstance(c.op, TensorErf)
            self.assertEqual(c.index, c.inputs[0].index)
            self.assertEqual(c.shape, c.inputs[0].shape)
예제 #8
0
def chirp_region(x_in, nhalfcycles=6.5, warpexp=0.65,
                 symmetric=False, noise=None):
    """Determine which region of the chirp the x_in vectors are in
    by assigning a probability of 0 to 1.
    If the noise is non-zero the erf() is used to approximate
    the probability, otherwise it is binary 0 or 1.
    This is very close to the "best" classifier possible for the noisy chirp.
    Parameters:
        x_in -- usual set of ML X vectors with shape of (m, 2)
        others as in make_chirp().
    """
    # confirm some of the x_in dimension properties
    assert((len(x_in.shape) == 2) and (x_in.shape[1] == 2))
    # convert the input xs (similar to the x1rot,x2rot above)
    # to the diagonal coordinate system: the x1, x2 above.
    x1r = x_in[:, 0]
    x2r = x_in[:, 1]
    x1 = (x1r + x2r) / 2.0
    x2 = (x2r - x1r) / 2.0
    y_out = np.zeros(len(x1))
    # go from x1 to the x2boundary
    x1warp = (abs(x1))**warpexp * x1 / abs(x1)
    # Symmetry determines if we use sin or cos
    if symmetric:
        trigfunc = np.cos
    else:
        trigfunc = np.sin

    # determine the boundary (in x2) between the two classes (at x1)
    x2boundary = trigfunc(x1warp * nhalfcycles * np.pi) * (1.0 - abs(x1warp))
    if noise > 0:
        # Use erf() to assign a 0 to 1 probability
        zboundary = (x2 - x2boundary)/noise
        y_class = 0.5 * scipy_erf(zboundary / np.sqrt(2)) + 0.5
    else:
        # assign binary values
        y_class = 1 * (x2 > x2boundary)
    # show the assignment in the input coord.s:
    ##plt.scatter(x1r, x2r, s=10, c=y_class, cmap=plt.cm.Spectral);
    return y_class
예제 #9
0
 def __init__(self, a):
     self.a, self.b = a, None
     self.value = scipy_erf(a.value)