Beispiel #1
0
    def test_extend_inplace(self):
        mySymbolicMatricesList1 = TypedListType(
            TensorType(aesara.config.floatX, (False, False)))()

        mySymbolicMatricesList2 = TypedListType(
            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 = random_ranged(-1000, 1000, [100, 101])

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

        assert np.array_equal(f([x], [y]), [x, y])
Beispiel #2
0
 def test_dtype_failure(self):
     arr = TensorType(FLOATX, [False] * 3)("arr")
     indices = TensorType(FLOATX, [False] * 3)("indices")
     arr.tag.test_value = np.zeros((1,) * arr.ndim, dtype=FLOATX)
     indices.tag.test_value = np.zeros((1,) * indices.ndim, dtype=FLOATX)
     with pytest.raises(IndexError):
         take_along_axis(arr, indices)
Beispiel #3
0
def test_convert_variable():
    test_type = TensorType(config.floatX, [False, False])
    test_var = test_type()

    test_type2 = TensorType(config.floatX, [True, False])
    test_var2 = test_type2()

    res = test_type.convert_variable(test_var)
    assert res is test_var

    res = test_type.convert_variable(test_var2)
    assert res is test_var2

    res = test_type2.convert_variable(test_var)
    assert res.type == test_type2

    test_type3 = TensorType(config.floatX, [True, False, True])
    test_var3 = test_type3()

    res = test_type2.convert_variable(test_var3)
    assert res is None

    const_var = at.as_tensor([[1, 2], [3, 4]], dtype=config.floatX)
    res = test_type.convert_variable(const_var)
    assert res is const_var
Beispiel #4
0
def test__getitem__AdvancedSubtensor_bool():
    x = matrix("x")
    i = TensorType("bool", (False, False))("i")

    z = x[i]
    op_types = [
        type(node.op) for node in aesara.graph.basic.io_toposort([x, i], [z])
    ]
    assert op_types[-1] == AdvancedSubtensor

    i = TensorType("bool", (False, ))("i")
    z = x[:, i]
    op_types = [
        type(node.op) for node in aesara.graph.basic.io_toposort([x, i], [z])
    ]
    assert op_types[-1] == AdvancedSubtensor

    i = TensorType("bool", (False, ))("i")
    z = x[..., i]
    op_types = [
        type(node.op) for node in aesara.graph.basic.io_toposort([x, i], [z])
    ]
    assert op_types[-1] == AdvancedSubtensor

    with pytest.raises(TypeError):
        z = x[[True, False], i]

    z = x[ivector("b"), i]
    op_types = [
        type(node.op) for node in aesara.graph.basic.io_toposort([x, i], [z])
    ]
    assert op_types[-1] == AdvancedSubtensor
Beispiel #5
0
 def make_node(self, x):
     x = aet.as_tensor_variable(x)
     self_axis = self.axis
     if self_axis is None:
         broadcastable = [False]
     else:
         if self_axis < 0:
             self_axis += len(x.broadcastable)
         if self_axis < 0 or self_axis >= len(x.broadcastable):
             raise RuntimeError(
                 "Unique axis `{}` is outside of input ndim = "
                 "{}.".format(self.axis, len(x.broadcastable)))
         broadcastable = [
             b if axis != self_axis else False
             for axis, b in enumerate(x.broadcastable)
         ]
     outputs = [TensorType(broadcastable=broadcastable, dtype=x.dtype)()]
     typ = TensorType(broadcastable=[False], dtype="int64")
     if self.return_index:
         outputs.append(typ())
     if self.return_inverse:
         outputs.append(typ())
     if self.return_counts:
         outputs.append(typ())
     return Apply(self, [x], outputs)
Beispiel #6
0
 def test_fill_grad(self):
     # Fix bug reported at
     # https://groups.google.com/d/topic/theano-users/nQshB8gUA6k/discussion
     x = TensorType(config.floatX, (False, True, False))("x")
     y = TensorType(config.floatX, (False, True, False))("y")
     e = second(x, y)
     aesara.grad(e.sum(), y)
Beispiel #7
0
    def test_infer_shape(self):

        for s_left, s_right in [
            ((5, 6), (5, 6)),
            ((5, 6), (5, 1)),
            ((5, 6), (1, 6)),
            ((5, 1), (5, 6)),
            ((1, 6), (5, 6)),
            ((2, 3, 4, 5), (2, 3, 4, 5)),
            ((2, 3, 4, 5), (2, 3, 1, 5)),
            ((2, 3, 4, 5), (1, 3, 4, 5)),
            ((2, 1, 4, 5), (2, 3, 4, 5)),
            ((2, 3, 4, 1), (2, 3, 4, 5)),
        ]:
            dtype = aesara.config.floatX
            t_left = TensorType(dtype, [(entry == 1) for entry in s_left])()
            t_right = TensorType(dtype, [(entry == 1) for entry in s_right])()
            t_left_val = np.zeros(s_left, dtype=dtype)
            t_right_val = np.zeros(s_right, dtype=dtype)
            self._compile_and_check(
                [t_left, t_right],
                [Elemwise(aes.add)(t_left, t_right)],
                [t_left_val, t_right_val],
                Elemwise,
            )
Beispiel #8
0
 def _input_tensors(self, shape):
     ndim = len(shape)
     arr = TensorType(FLOATX, [False] * ndim)("arr")
     indices = TensorType(INTX, [False] * ndim)("indices")
     arr.tag.test_value = np.zeros(shape, dtype=FLOATX)
     indices.tag.test_value = np.zeros(shape, dtype=INTX)
     return arr, indices
Beispiel #9
0
    def test_ctors(self):

        if PYTHON_INT_BITWIDTH == 32:
            assert shared(7).type == iscalar, shared(7).type
        else:
            assert shared(7).type == lscalar, shared(7).type
        assert shared(7.0).type == dscalar
        assert shared(np.float32(7)).type == fscalar

        # test tensor constructor
        b = shared(np.zeros((5, 5), dtype="int32"))
        assert b.type == TensorType("int32", broadcastable=[False, False])
        b = shared(np.random.rand(4, 5))
        assert b.type == TensorType("float64", broadcastable=[False, False])
        b = shared(np.random.rand(5, 1, 2))
        assert b.type == TensorType("float64",
                                    broadcastable=[False, False, False])

        assert shared([]).type == generic

        def badfunc():
            shared(7, bad_kw=False)

        with pytest.raises(TypeError):
            badfunc()
Beispiel #10
0
def test_fixed_shape_clone():
    t1 = TensorType("float64", (1,))

    t2 = t1.clone(dtype="float32", shape=(2, 4))
    assert t2.shape == (2, 4)

    t2 = t1.clone(dtype="float32", shape=(False, False))
    assert t2.shape == (None, None)
Beispiel #11
0
def test_get_vector_length():
    x = TensorVariable(TensorType("int64", (4, )))
    res = get_vector_length(x)
    assert res == 4

    x = TensorVariable(TensorType("int64", (None, )))
    with pytest.raises(ValueError):
        get_vector_length(x)
Beispiel #12
0
    def test_fixed_partial_shapes(self):
        x = TensorType("floatX", (None, None))("x")
        y = specify_shape(x, (None, 5))
        assert y.type.shape == (None, 5)

        x = TensorType("floatX", (3, None))("x")
        y = specify_shape(x, (None, 5))
        assert y.type.shape == (3, 5)
Beispiel #13
0
 def _input_tensors(self, shape, floatX):
     intX = str(_conversion_map[floatX])
     ndim = len(shape)
     arr = TensorType(floatX, [False] * ndim)("arr")
     indices = TensorType(intX, [False] * ndim)("indices")
     arr.tag.test_value = np.zeros(shape, dtype=floatX)
     indices.tag.test_value = np.zeros(shape, dtype=intX)
     return arr, indices
Beispiel #14
0
 def test_dtype_failure(self, floatX):
     with aesara.config.change_flags(floatX=floatX):
         arr = TensorType(floatX, [False] * 3)("arr")
         indices = TensorType(floatX, [False] * 3)("indices")
         arr.tag.test_value = np.zeros((1, ) * arr.ndim, dtype=floatX)
         indices.tag.test_value = np.zeros((1, ) * indices.ndim,
                                           dtype=floatX)
         with pytest.raises(IndexError):
             take_along_axis(arr, indices)
Beispiel #15
0
 def test_ndim_failure(self, floatX):
     with aesara.config.change_flags(floatX=floatX):
         intX = str(_conversion_map[floatX])
         arr = TensorType(floatX, [False] * 3)("arr")
         indices = TensorType(intX, [False] * 2)("indices")
         arr.tag.test_value = np.zeros((1, ) * arr.ndim, dtype=floatX)
         indices.tag.test_value = np.zeros((1, ) * indices.ndim, dtype=intX)
         with pytest.raises(ValueError):
             take_along_axis(arr, indices)
Beispiel #16
0
    def test_repeatOp(self):
        for ndim in [1, 3]:
            x = TensorType(config.floatX, [False] * ndim)()
            a = np.random.random((10, ) * ndim).astype(config.floatX)

            for axis in self._possible_axis(ndim):
                for dtype in integer_dtypes:
                    r_var = 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 = 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 = 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()
                        ])
 def make_node(self, x, y):
     x = aet.as_tensor_variable(x)
     y = aet.as_tensor_variable(y)
     outdim = x.ndim
     output1 = TensorType(dtype=aesara.scalar.upcast(x.dtype, y.dtype),
                          broadcastable=[False] * outdim)()
     output2 = TensorType(dtype=aesara.scalar.upcast(x.dtype, y.dtype),
                          broadcastable=[False] * outdim)()
     return Apply(self, inputs=[x, y], outputs=[output1, output2])
Beispiel #18
0
def test_may_share_memory():
    a = np.array(2)
    b = np.broadcast_to(a, (2, 3))

    res = TensorType.may_share_memory(a, b)
    assert res

    res = TensorType.may_share_memory(a, None)
    assert res is False
Beispiel #19
0
def test_deprecated_kwargs():
    with pytest.warns(DeprecationWarning, match=".*broadcastable.*"):
        res = TensorType("float64", broadcastable=(True, False))

    assert res.shape == (1, None)

    with pytest.warns(DeprecationWarning, match=".*broadcastable.*"):
        new_res = res.clone(broadcastable=(False, True))

    assert new_res.shape == (None, 1)
Beispiel #20
0
 def test_tensorvariable(self):
     # Get counter value
     autoname_id = next(Variable.__count__)
     Variable.__count__ = count(autoname_id)
     r1 = TensorType(dtype="int32", shape=())("myvar")
     r2 = TensorVariable(TensorType(dtype="int32", shape=()))
     r3 = shared(np.random.standard_normal((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)
Beispiel #21
0
def test_is_super():
    test_type = TensorType(config.floatX, [False, False])
    test_type2 = TensorType(config.floatX, [False, True])

    assert test_type.is_super(test_type)
    assert test_type.is_super(test_type2)
    assert not test_type2.is_super(test_type)

    test_type3 = TensorType(config.floatX, [False, False, False])
    assert not test_type3.is_super(test_type)
Beispiel #22
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 = TensorType(aesara.config.floatX,
                                   [False] * image_dim)(name="input")
                filters = TensorType(aesara.config.floatX,
                                     [False] * filter_dim)(name="filter")
                ev_input = TensorType(aesara.config.floatX,
                                      [False] * image_dim)(name="ev_input")
                ev_filters = 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 = 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: (grad(y[i], x1) * v1).sum() +
                    (grad(y[i], x2) * v2).sum(),
                    sequences=aet.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), f"Rop mismatch: {v1} {v2}"
Beispiel #23
0
def test_filter_memmap():
    r"""Make sure `TensorType.filter` can handle NumPy `memmap`\s subclasses."""
    data = np.arange(12, dtype=config.floatX)
    data.resize((3, 4))
    filename = path.join(mkdtemp(), "newfile.dat")
    fp = np.memmap(filename, dtype=config.floatX, mode="w+", shape=(3, 4))

    test_type = TensorType(config.floatX, [False, False])

    res = test_type.filter(fp)
    assert res is fp
Beispiel #24
0
def test_fixed_shape_variable_basic():
    x = TensorVariable(TensorType("int64", (4, )))
    assert isinstance(x.shape, Constant)
    assert np.array_equal(x.shape.data, (4, ))

    x = TensorConstant(TensorType("int64", (False, False)),
                       np.array([[1, 2], [2, 3]]))
    assert x.type.shape == (2, 2)

    with pytest.raises(ValueError):
        TensorConstant(TensorType("int64", (True, False)),
                       np.array([[1, 2], [2, 3]]))
Beispiel #25
0
    def test_nested_list_arg(self):
        # test for the 'depth' optional argument

        myNestedType = TypedListType(
            TensorType(aesara.config.floatX, (False, False)), 3
        )

        myType = TypedListType(TensorType(aesara.config.floatX, (False, False)))

        myManualNestedType = TypedListType(TypedListType(TypedListType(myType)))

        assert myNestedType == myManualNestedType
Beispiel #26
0
    def test_var_interface(self, shape, broadcast):
        # same as test_op, but use a_aesara_var.squeeze.
        data = np.random.random(size=shape).astype(config.floatX)
        variable = TensorType(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)
Beispiel #27
0
def test_filter_ndarray_subclass():
    """Make sure `TensorType.filter` can handle NumPy `ndarray` subclasses."""
    test_type = TensorType(config.floatX, [False])

    class MyNdarray(np.ndarray):
        pass

    test_val = np.array([1.0], dtype=config.floatX).view(MyNdarray)
    assert isinstance(test_val, MyNdarray)

    res = test_type.filter(test_val)
    assert isinstance(res, MyNdarray)
    assert res is test_val
Beispiel #28
0
    def test_type_equality(self):
        # Typed list types should only be equal
        # when they contains the same aesara
        # variables

        # list of matrices
        myType1 = TypedListType(TensorType(aesara.config.floatX, (False, False)))
        # list of matrices
        myType2 = TypedListType(TensorType(aesara.config.floatX, (False, False)))
        # list of scalars
        myType3 = TypedListType(TensorType(aesara.config.floatX, ()))

        assert myType2 == myType1
        assert myType3 != myType1
Beispiel #29
0
    def test_axis(self):
        variable = TensorType(config.floatX, [False, True, False])()
        res = squeeze(variable, axis=1)

        assert res.broadcastable == (False, False)

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

        assert res.broadcastable == (False, False)

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

        assert res.broadcastable == (False, False)
Beispiel #30
0
    def test_non_tensor_type(self):
        mySymbolicNestedMatricesList = TypedListType(
            TensorType(aesara.config.floatX, (False, False)), 1)()
        mySymbolicMatricesList = TypedListType(
            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