Exemplo n.º 1
0
    def test_extend_inplace(self):
        mySymbolicMatricesList1 = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()

        mySymbolicMatricesList2 = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()

        z = Extend()(mySymbolicMatricesList1, mySymbolicMatricesList2)
        m = aesara.compile.mode.get_default_mode().including(
            "typed_list_inplace_opt")
        f = aesara.function(
            [
                In(mySymbolicMatricesList1, borrow=True, mutable=True),
                mySymbolicMatricesList2,
            ],
            z,
            mode=m,
        )
        assert f.maker.fgraph.toposort()[0].op.inplace

        x = rand_ranged(-1000, 1000, [100, 101])

        y = rand_ranged(-1000, 1000, [100, 101])

        assert np.array_equal(f([x], [y]), [x, y])
Exemplo n.º 2
0
    def test_conv(self):
        for conv_op in [conv.conv2d, conv2d]:
            for border_mode in ["valid", "full"]:
                image_shape = (2, 2, 4, 5)
                filter_shape = (2, 2, 2, 3)
                image_dim = len(image_shape)
                filter_dim = len(filter_shape)
                input = tensor.TensorType(aesara.config.floatX, [False] * image_dim)(
                    name="input"
                )
                filters = tensor.TensorType(aesara.config.floatX, [False] * filter_dim)(
                    name="filter"
                )
                ev_input = tensor.TensorType(aesara.config.floatX, [False] * image_dim)(
                    name="ev_input"
                )
                ev_filters = tensor.TensorType(
                    aesara.config.floatX, [False] * filter_dim
                )(name="ev_filters")

                def sym_conv2d(input, filters):
                    return conv_op(input, filters, border_mode=border_mode)

                output = sym_conv2d(input, filters).flatten()
                yv = tensor.Rop(output, [input, filters], [ev_input, ev_filters])
                mode = None
                if aesara.config.mode == "FAST_COMPILE":
                    mode = "FAST_RUN"
                rop_f = function(
                    [input, filters, ev_input, ev_filters],
                    yv,
                    on_unused_input="ignore",
                    mode=mode,
                )
                sy, _ = aesara.scan(
                    lambda i, y, x1, x2, v1, v2: (tensor.grad(y[i], x1) * v1).sum()
                    + (tensor.grad(y[i], x2) * v2).sum(),
                    sequences=tensor.arange(output.shape[0]),
                    non_sequences=[output, input, filters, ev_input, ev_filters],
                    mode=mode,
                )
                scan_f = function(
                    [input, filters, ev_input, ev_filters],
                    sy,
                    on_unused_input="ignore",
                    mode=mode,
                )
                dtype = aesara.config.floatX
                image_data = np.random.random(image_shape).astype(dtype)
                filter_data = np.random.random(filter_shape).astype(dtype)
                ev_image_data = np.random.random(image_shape).astype(dtype)
                ev_filter_data = np.random.random(filter_shape).astype(dtype)
                v1 = rop_f(image_data, filter_data, ev_image_data, ev_filter_data)
                v2 = scan_f(image_data, filter_data, ev_image_data, ev_filter_data)
                assert np.allclose(v1, v2), "Rop mismatch: {} {}".format(v1, v2)
Exemplo n.º 3
0
 def test_tensorvariable(self):
     # Get counter value
     autoname_id = next(Variable.__count__)
     Variable.__count__ = count(autoname_id)
     r1 = tensor.TensorType(dtype="int32", broadcastable=())("myvar")
     r2 = tensor.TensorVariable(
         tensor.TensorType(dtype="int32", broadcastable=()))
     r3 = shared(np.random.randn(3, 4))
     assert r1.auto_name == "auto_" + str(autoname_id)
     assert r2.auto_name == "auto_" + str(autoname_id + 1)
     assert r3.auto_name == "auto_" + str(autoname_id + 2)
Exemplo n.º 4
0
class TestDiff(utt.InferShapeTester):
    def test_basic(self):
        rng = np.random.default_rng(4282)

        x = matrix("x")

        a = rng.random((30, 50)).astype(config.floatX)

        f = aesara.function([x], diff(x))
        assert np.allclose(np.diff(a), f(a))

    @pytest.mark.parametrize("axis", (-2, -1, 0, 1))
    @pytest.mark.parametrize("n", (0, 1, 2, 30, 31))
    def test_perform(self, axis, n):
        rng = np.random.default_rng(4282)

        x = matrix("x")

        a = rng.random((30, 50)).astype(config.floatX)

        f = aesara.function([x], diff(x))
        assert np.allclose(np.diff(a), f(a))

        g = aesara.function([x], diff(x, n=n, axis=axis))
        assert np.allclose(np.diff(a, n=n, axis=axis), g(a))

    @pytest.mark.xfail(reason="Subtensor shape cannot be inferred correctly")
    @pytest.mark.parametrize(
        "x_type",
        (
            at.TensorType("float64", (None, None)),
            at.TensorType("float64", (None, 30)),
            at.TensorType("float64", (10, None)),
            at.TensorType("float64", (10, 30)),
        ),
    )
    @pytest.mark.parametrize("axis", (-2, -1, 0, 1))
    @pytest.mark.parametrize("n", (0, 1, 2, 10, 11))
    def test_output_type(self, x_type, axis, n):
        x = x_type("x")
        x_test = np.empty((10, 30))
        out = diff(x, n=n, axis=axis)
        out_test = np.diff(x_test, n=n, axis=axis)
        for i in range(2):
            if x.type.shape[i] is None:
                assert out.type.shape[i] is None
            else:
                assert out.type.shape[i] == out_test.shape[i]
Exemplo n.º 5
0
def permutation(random_state, size=None, n=1, ndim=None, dtype="int64"):
    """
    Return permutations of the integers between 0 and n-1.

    Returns them as many times as required by size. For instance, if size=(p,q),
    p*q permutations will be generated, and the output shape will be (p,q,n),
    because each permutation is of size n.

    Aesara tries to infer the number of dimensions from the length of
    the size argument and the shape of n, but you may always specify it
    with the `ndim` parameter.

    Notes
    -----
    Note that the output will then be of dimension ndim+1.

    """
    if size is None or size == ():
        if not (ndim is None or ndim == 1):
            raise TypeError(
                "You asked for just one permutation but asked for more then 1 dimensions."
            )
        ndim = 1
        size = ()
        bcast = ()
    else:
        ndim, size, bcast = _infer_ndim_bcast(ndim, size)
    # print "NDIM", ndim, size
    op = RandomFunction(
        permutation_helper,
        tensor.TensorType(dtype=dtype, broadcastable=bcast + (False, )),
        ndim_added=1,
    )
    return op(random_state, size, n)
Exemplo n.º 6
0
Arquivo: smc.py Projeto: sthagen/pymc3
def _logp_forw(point, out_vars, in_vars, shared):
    """Compile Aesara function of the model and the input and output variables.

    Parameters
    ----------
    out_vars: List
        containing :class:`pymc.Distribution` for the output variables
    in_vars: List
        containing :class:`pymc.Distribution` for the input variables
    shared: List
        containing :class:`aesara.tensor.Tensor` for depended shared data
    """

    # Replace integer inputs with rounded float inputs
    if any(var.dtype in discrete_types for var in in_vars):
        replace_int_input = {}
        new_in_vars = []
        for in_var in in_vars:
            if in_var.dtype in discrete_types:
                float_var = at.TensorType("floatX",
                                          in_var.broadcastable)(in_var.name)
                new_in_vars.append(float_var)
                replace_int_input[in_var] = at.round(float_var)
            else:
                new_in_vars.append(in_var)

        out_vars = clone_replace(out_vars, replace_int_input, strict=False)
        in_vars = new_in_vars

    out_list, inarray0 = join_nonshared_inputs(point, out_vars, in_vars,
                                               shared)
    f = compile_pymc([inarray0], out_list[0])
    f.trust_input = True
    return f
Exemplo n.º 7
0
def choice(random_state,
           size=None,
           a=2,
           replace=True,
           p=None,
           ndim=None,
           dtype="int64"):
    """
    Choose values from `a` with or without replacement. `a` can be a 1-D array
    or a positive scalar. If `a` is a scalar, the samples are drawn from the
    range 0,...,a-1.

    If the size argument is ambiguous on the number of dimensions, ndim
    may be a plain integer to supplement the missing information.

    If size is None, a scalar will be returned.

    """
    a = tensor.as_tensor_variable(a)
    if isinstance(replace, bool):
        replace = tensor.constant(replace, dtype="int8")
    else:
        replace = tensor.as_tensor_variable(replace)
    # encode p=None as an empty vector
    p = tensor.as_tensor_variable(p or [])
    ndim, size, bcast = _infer_ndim_bcast(ndim, size)
    op = RandomFunction(choice_helper,
                        tensor.TensorType(dtype=dtype, broadcastable=bcast))
    return op(random_state, size, a, replace, p)
Exemplo n.º 8
0
    def test_interface(self):
        mySymbolicMatricesList1 = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        mySymbolicMatricesList2 = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()

        z = mySymbolicMatricesList1.extend(mySymbolicMatricesList2)

        f = aesara.function([mySymbolicMatricesList1, mySymbolicMatricesList2],
                            z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        y = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert np.array_equal(f([x], [y]), [x, y])
Exemplo n.º 9
0
    def test_wrong_input(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        mySymbolicMatrix = tt.matrix()

        with pytest.raises(TypeError):
            GetItem()(mySymbolicMatricesList, mySymbolicMatrix)
Exemplo n.º 10
0
    def test_insert_inplace(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        mySymbolicIndex = tt.scalar(dtype="int64")
        mySymbolicMatrix = tt.matrix()

        z = Insert()(mySymbolicMatricesList, mySymbolicIndex, mySymbolicMatrix)
        m = aesara.compile.mode.get_default_mode().including(
            "typed_list_inplace_opt")

        f = aesara.function(
            [
                In(mySymbolicMatricesList, borrow=True, mutable=True),
                mySymbolicIndex,
                mySymbolicMatrix,
            ],
            z,
            accept_inplace=True,
            mode=m,
        )
        assert f.maker.fgraph.toposort()[0].op.inplace

        x = rand_ranged(-1000, 1000, [100, 101])

        y = rand_ranged(-1000, 1000, [100, 101])

        assert np.array_equal(f([x], np.asarray(1, dtype="int64"), y), [x, y])
Exemplo n.º 11
0
    def test_non_tensor_type(self):
        mySymbolicNestedMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)), 1)()
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()

        z = Count()(mySymbolicNestedMatricesList, mySymbolicMatricesList)

        f = aesara.function(
            [mySymbolicNestedMatricesList, mySymbolicMatricesList], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        y = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert f([[x, y], [x, y, y]], [x, y]) == 1
Exemplo n.º 12
0
    def test_axis(self):
        variable = tt.TensorType(aesara.config.floatX, [False, True, False])()
        res = squeeze(variable, axis=1)

        assert res.broadcastable == (False, False)

        variable = tt.TensorType(aesara.config.floatX, [False, True, False])()
        res = squeeze(variable, axis=(1, ))

        assert res.broadcastable == (False, False)

        variable = tt.TensorType(aesara.config.floatX,
                                 [False, True, False, True])()
        res = squeeze(variable, axis=(1, 3))

        assert res.broadcastable == (False, False)
Exemplo n.º 13
0
def poisson(random_state, size=None, lam=1.0, ndim=None, dtype="int64"):
    """
    Draw samples from a Poisson distribution.

    The Poisson distribution is the limit of the Binomial distribution for
    large N.

    Parameters
    ----------
    lam : float or ndarray-like of the same shape as size parameter
        Expectation of interval, should be >= 0.
    size: int or tuple of ints, optional
        Output shape. If the given shape is, e.g., (m, n, k), then m * n * k
        samples are drawn.
    dtype
        The dtype of the return value (which will represent counts).

    size or ndim must be given.

    """
    lam = tensor.as_tensor_variable(lam)

    ndim, size, bcast = _infer_ndim_bcast(ndim, size)

    op = RandomFunction("poisson",
                        tensor.TensorType(dtype=dtype, broadcastable=bcast))
    return op(random_state, size, lam)
Exemplo n.º 14
0
    def test_random_function_noshape_args(self):
        # Test if random_function helper works with args but without shape
        rng_R = random_state_type()

        # No shape, default args -> OK
        post_out, out = uniform(rng_R, size=None, ndim=2)
        f = compile.function(
            [
                compile.In(
                    rng_R,
                    value=np.random.RandomState(utt.fetch_seed()),
                    update=post_out,
                    mutable=True,
                )
            ],
            [out],
            accept_inplace=True,
        )
        (o, ) = f()

        # No shape, args that have to be broadcasted -> OK
        low = tensor.TensorType(dtype="float64",
                                broadcastable=(False, True, True))()
        high = tensor.TensorType(dtype="float64",
                                 broadcastable=(True, True, True, False))()
        post_out2, out2 = uniform(rng_R, size=None, ndim=2, low=low, high=high)
        assert out2.ndim == 4
        assert out2.broadcastable == (True, False, True, False)

        g = compile.function(
            [
                low,
                high,
                compile.In(
                    rng_R,
                    value=np.random.RandomState(utt.fetch_seed()),
                    update=post_out2,
                    mutable=True,
                ),
            ],
            [out2],
            accept_inplace=True,
        )
        low_v = [[[3]], [[4]], [[-5]]]
        high_v = [[[[5, 8]]]]
        (o2, ) = g(low_v, high_v)
        assert o2.shape == (1, 3, 1, 2)
Exemplo n.º 15
0
 def test_broadcastable(self):
     x = tt.TensorType(config.floatX, [False, True, False])()
     r = RepeatOp(axis=1)(x, 2)
     assert r.broadcastable == (False, False, False)
     r = RepeatOp(axis=1)(x, 1)
     assert r.broadcastable == (False, True, False)
     r = RepeatOp(axis=0)(x, 2)
     assert r.broadcastable == (False, True, False)
Exemplo n.º 16
0
    def test_infer_shape(self):
        for shape, broadcast in zip(self.shape_list, self.broadcast_list):
            data = np.random.random(size=shape).astype(aesara.config.floatX)
            variable = tt.TensorType(aesara.config.floatX, broadcast)()

            self._compile_and_check([variable], [self.op(variable)], [data],
                                    tt.DimShuffle,
                                    warn=False)
Exemplo n.º 17
0
    def test_repeatOp(self):
        for ndim in [1, 3]:
            x = tt.TensorType(config.floatX, [False] * ndim)()
            a = np.random.random((10, ) * ndim).astype(config.floatX)

            for axis in self._possible_axis(ndim):
                for dtype in tt.integer_dtypes:
                    r_var = tt.scalar(dtype=dtype)
                    r = np.asarray(3, dtype=dtype)
                    if dtype == "uint64" or (dtype
                                             in self.numpy_unsupported_dtypes
                                             and r_var.ndim == 1):
                        with pytest.raises(TypeError):
                            repeat(x, r_var, axis=axis)
                    else:
                        f = aesara.function([x, r_var],
                                            repeat(x, r_var, axis=axis))
                        assert np.allclose(np.repeat(a, r, axis=axis), f(a, r))

                        r_var = tt.vector(dtype=dtype)
                        if axis is None:
                            r = np.random.randint(1, 6,
                                                  size=a.size).astype(dtype)
                        else:
                            r = np.random.randint(1, 6,
                                                  size=(10, )).astype(dtype)

                        if dtype in self.numpy_unsupported_dtypes and r_var.ndim == 1:
                            with pytest.raises(TypeError):
                                repeat(x, r_var, axis=axis)
                        else:
                            f = aesara.function([x, r_var],
                                                repeat(x, r_var, axis=axis))
                            assert np.allclose(np.repeat(a, r, axis=axis),
                                               f(a, r))

                        # check when r is a list of single integer, e.g. [3].
                        r = np.random.randint(1, 11, size=()).astype(dtype) + 2
                        f = aesara.function([x], repeat(x, [r], axis=axis))
                        assert np.allclose(np.repeat(a, r, axis=axis), f(a))
                        assert not np.any([
                            isinstance(n.op, RepeatOp)
                            for n in f.maker.fgraph.toposort()
                        ])

                        # check when r is  aesara tensortype that broadcastable is (True,)
                        r_var = aesara.tensor.TensorType(
                            broadcastable=(True, ), dtype=dtype)()
                        r = np.random.randint(1, 6, size=(1, )).astype(dtype)
                        f = aesara.function([x, r_var],
                                            repeat(x, r_var, axis=axis))
                        assert np.allclose(np.repeat(a, r[0], axis=axis),
                                           f(a, r))
                        assert not np.any([
                            isinstance(n.op, RepeatOp)
                            for n in f.maker.fgraph.toposort()
                        ])
Exemplo n.º 18
0
    def test_interface(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        z = mySymbolicMatricesList.__len__()

        f = aesara.function([mySymbolicMatricesList], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert f([x, x]) == 2
Exemplo n.º 19
0
 def test_randomvariable(self):
     # Get counter value
     autoname_id = next(Variable.__count__)
     Variable.__count__ = count(autoname_id)
     mytype = tensor.TensorType(dtype="int32", broadcastable=())
     r1 = tensor.shared_randomstreams.RandomStateSharedVariable(
         name="x", type=mytype, value=1, strict=False)
     r2 = tensor.shared_randomstreams.RandomStateSharedVariable(
         name="x", type=mytype, value=1, strict=False)
     assert r1.auto_name == "auto_" + str(autoname_id)
     assert r2.auto_name == "auto_" + str(autoname_id + 1)
Exemplo n.º 20
0
    def test_sanity_check(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()

        z = Length()(mySymbolicMatricesList)

        f = aesara.function([mySymbolicMatricesList], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert f([x, x, x, x]) == 4
Exemplo n.º 21
0
    def test_op(self):
        for shape, broadcast in zip(self.shape_list, self.broadcast_list):
            data = np.random.random(size=shape).astype(aesara.config.floatX)
            variable = tt.TensorType(aesara.config.floatX, broadcast)()

            f = aesara.function([variable], self.op(variable))

            expected = np.squeeze(data)
            tested = f(data)

            assert tested.shape == expected.shape
            assert np.allclose(tested, expected)
Exemplo n.º 22
0
    def test_var_interface(self):
        # same as test_op, but use a_aesara_var.squeeze.
        for shape, broadcast in zip(self.shape_list, self.broadcast_list):
            data = np.random.random(size=shape).astype(aesara.config.floatX)
            variable = tt.TensorType(aesara.config.floatX, broadcast)()

            f = aesara.function([variable], variable.squeeze())

            expected = np.squeeze(data)
            tested = f(data)

            assert tested.shape == expected.shape
            assert np.allclose(tested, expected)
Exemplo n.º 23
0
    def test_interface(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()

        z = mySymbolicMatricesList.reverse()

        f = aesara.function([mySymbolicMatricesList], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        y = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert np.array_equal(f([x, y]), [y, x])
Exemplo n.º 24
0
    def test_sanity_check(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        myMatrix = tt.matrix()

        z = Remove()(mySymbolicMatricesList, myMatrix)

        f = aesara.function([mySymbolicMatricesList, myMatrix], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        y = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert np.array_equal(f([x, y], y), [x])
Exemplo n.º 25
0
    def test_sanity_check_single(self):

        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()

        mySymbolicScalar = tt.scalar(dtype="int64")

        z = GetItem()(mySymbolicMatricesList, mySymbolicScalar)

        f = aesara.function([mySymbolicMatricesList, mySymbolicScalar], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert np.array_equal(f([x], np.asarray(0, dtype="int64")), x)
Exemplo n.º 26
0
    def test_interface(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        myMatrix = tt.matrix()

        z = mySymbolicMatricesList.count(myMatrix)

        f = aesara.function([mySymbolicMatricesList, myMatrix], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        y = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert f([x, y], y) == 1
Exemplo n.º 27
0
    def test_sanity_check(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        myMatrix = tt.matrix()

        z = Count()(mySymbolicMatricesList, myMatrix)

        f = aesara.function([mySymbolicMatricesList, myMatrix], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        y = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert f([y, y, x, y], y) == 3
Exemplo n.º 28
0
    def test_interface(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        myMatrix = tt.matrix()
        myScalar = tt.scalar(dtype="int64")

        z = mySymbolicMatricesList.insert(myScalar, myMatrix)

        f = aesara.function([mySymbolicMatricesList, myScalar, myMatrix], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        y = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert np.array_equal(f([x], np.asarray(1, dtype="int64"), y), [x, y])
Exemplo n.º 29
0
    def test_inplace(self):
        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()
        myMatrix = tt.matrix()

        z = Append(True)(mySymbolicMatricesList, myMatrix)

        f = aesara.function([mySymbolicMatricesList, myMatrix],
                            z,
                            accept_inplace=True)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        y = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert np.array_equal(f([x], y), [x, y])
Exemplo n.º 30
0
    def test_sanity_check_slice(self):

        mySymbolicMatricesList = TypedListType(
            tt.TensorType(aesara.config.floatX, (False, False)))()

        mySymbolicSlice = SliceType()()

        z = GetItem()(mySymbolicMatricesList, mySymbolicSlice)

        assert not isinstance(z, tt.TensorVariable)

        f = aesara.function([mySymbolicMatricesList, mySymbolicSlice], z)

        x = rand_ranged_matrix(-1000, 1000, [100, 101])

        assert np.array_equal(f([x], slice(0, 1, 1)), [x])