Beispiel #1
0
def multMatVect(v, A, m1, B, m2):
    # TODO : need description for parameter and return
    """
    Multiply the first half of v by A with a modulo of m1 and the second half
    by B with a modulo of m2.

    Notes
    -----
    The parameters of dot_modulo are passed implicitly because passing them
    explicitly takes more time than running the function's C-code.

    """
    if multMatVect.dot_modulo is None:
        A_sym = tensor.lmatrix("A")
        s_sym = tensor.ivector("s")
        m_sym = tensor.iscalar("m")
        A2_sym = tensor.lmatrix("A2")
        s2_sym = tensor.ivector("s2")
        m2_sym = tensor.iscalar("m2")
        o = DotModulo()(A_sym, s_sym, m_sym, A2_sym, s2_sym, m2_sym)
        multMatVect.dot_modulo = function(
            [A_sym, s_sym, m_sym, A2_sym, s2_sym, m2_sym], o, profile=False)

    # This way of calling the Aesara fct is done to bypass Aesara overhead.
    f = multMatVect.dot_modulo
    f.input_storage[0].storage[0] = A
    f.input_storage[1].storage[0] = v[:3]
    f.input_storage[2].storage[0] = m1
    f.input_storage[3].storage[0] = B
    f.input_storage[4].storage[0] = v[3:]
    f.input_storage[5].storage[0] = m2
    f.fn()
    r = f.output_storage[0].storage[0]

    return r
def test_multMatVect():
    A1 = tensor.lmatrix("A1")
    s1 = tensor.ivector("s1")
    m1 = tensor.iscalar("m1")
    A2 = tensor.lmatrix("A2")
    s2 = tensor.ivector("s2")
    m2 = tensor.iscalar("m2")

    g0 = rng_mrg.DotModulo()(A1, s1, m1, A2, s2, m2)
    f0 = aesara.function([A1, s1, m1, A2, s2, m2], g0)

    i32max = np.iinfo(np.int32).max

    A1 = np.random.randint(0, i32max, (3, 3)).astype("int64")
    s1 = np.random.randint(0, i32max, 3).astype("int32")
    m1 = np.asarray(np.random.randint(i32max), dtype="int32")
    A2 = np.random.randint(0, i32max, (3, 3)).astype("int64")
    s2 = np.random.randint(0, i32max, 3).astype("int32")
    m2 = np.asarray(np.random.randint(i32max), dtype="int32")

    f0.input_storage[0].storage[0] = A1
    f0.input_storage[1].storage[0] = s1
    f0.input_storage[2].storage[0] = m1
    f0.input_storage[3].storage[0] = A2
    f0.input_storage[4].storage[0] = s2
    f0.input_storage[5].storage[0] = m2

    r_a1 = rng_mrg.matVecModM(A1, s1, m1)
    r_a2 = rng_mrg.matVecModM(A2, s2, m2)
    f0.fn()
    r_b = f0.output_storage[0].value

    assert np.allclose(r_a1, r_b[:3])
    assert np.allclose(r_a2, r_b[3:])
Beispiel #3
0
def test__getitem__AdvancedSubtensor():
    # Make sure we get `AdvancedSubtensor`s for basic indexing operations
    x = tt.matrix("x")
    i = tt.ivector("i")

    # This is a `__getitem__` call that's redirected to `_tensor_py_operators.take`
    z = x[i]
    op_types = [type(node.op) for node in aesara.gof.graph.io_toposort([x, i], [z])]
    assert op_types[-1] == AdvancedSubtensor1

    # This should index nothing (i.e. return an empty copy of `x`)
    # We check that the index is empty
    z = x[[]]
    op_types = [type(node.op) for node in aesara.gof.graph.io_toposort([x, i], [z])]
    assert op_types == [AdvancedSubtensor1]
    assert isinstance(z.owner.inputs[1], TensorConstant)

    # This is also a `__getitem__` call that's redirected to `_tensor_py_operators.take`
    z = x[:, i]
    op_types = [type(node.op) for node in aesara.gof.graph.io_toposort([x, i], [z])]
    assert op_types == [DimShuffle, AdvancedSubtensor1, DimShuffle]

    z = x[..., i, None]
    op_types = [type(node.op) for node in aesara.gof.graph.io_toposort([x, i], [z])]
    assert op_types == [MakeSlice, AdvancedSubtensor]

    z = x[i, None]
    op_types = [type(node.op) for node in aesara.gof.graph.io_toposort([x, i], [z])]
    assert op_types[-1] == AdvancedSubtensor
    def test_can_not_infer_nb_dim(self):
        # Was reported in gh-5613. Test that we do not crash
        # or that we crash in a few other case found while
        # investigating that case

        img = tt.tensor4("img")
        patches = tt.nnet.neighbours.images2neibs(img, [16, 16])
        extractPatches = aesara.function([img], patches, mode=self.mode)

        patsRecovery = tt.matrix("patsRecovery")
        original_size = tt.ivector("original_size")

        for mode in ["valid", "ignore_borders"]:
            out = neibs2images(patsRecovery, (16, 16),
                               original_size,
                               mode=mode)
            f = aesara.function([patsRecovery, original_size],
                                out,
                                mode=self.mode)

            im_val = np.ones((1, 3, 320, 320), dtype=np.float32)
            neibs = extractPatches(im_val)
            f(neibs, im_val.shape)
            # Wrong number of dimensions
            with pytest.raises(ValueError):
                f(neibs, (1, 1, 3, 320, 320))
            # End up with a step of 0
            # This can lead to division by zero in DebugMode
            with pytest.raises((ValueError, ZeroDivisionError)):
                f(neibs, (3, 320, 320, 1))
Beispiel #5
0
 def test_invalid_type(self):
     a = at.ivector("a")
     a.tag.test_value = np.zeros(3, dtype=a.dtype)
     a.dshape = (3, )
     a.dsize = 3
     with pytest.raises(TypeError) as err:
         ValueGradFunction([a.sum()], [a], {}, mode="FAST_COMPILE")
     err.match("Invalid dtype")
Beispiel #6
0
    def test_givens(self):
        x = shared(0)
        assign = pfunc([], x, givens={x: 3})
        assert assign() == 3
        assert x.get_value(borrow=True) == 0

        y = tensor.ivector()
        f = pfunc([y], (y * x), givens={x: 6})
        assert np.all(f([1, 1, 1]) == [6, 6, 6])
        assert x.get_value() == 0

        z = tensor.ivector()
        c = z * y
        f = pfunc([y], (c + 7),
                  givens={z: aesara._asarray([4, 4, 4], dtype="int32")})
        assert np.all(f([1, 1, 1]) == [11, 11, 11])
        assert x.get_value() == 0
def test_local_csm_properties_csm():
    data = tensor.vector()
    indices, indptr, shape = (tensor.ivector(), tensor.ivector(), tensor.ivector())
    mode = aesara.compile.mode.get_default_mode()
    mode = mode.including("specialize", "local_csm_properties_csm")
    for CS, cast in [
        (sparse.CSC, sp.sparse.csc_matrix),
        (sparse.CSR, sp.sparse.csr_matrix),
    ]:
        f = aesara.function(
            [data, indices, indptr, shape],
            sparse.csm_properties(CS(data, indices, indptr, shape)),
            mode=mode,
        )
        assert not any(
            isinstance(node.op, (sparse.CSM, sparse.CSMProperties))
            for node in f.maker.fgraph.toposort()
        )
        v = cast(random_lil((10, 40), config.floatX, 3))
        f(v.data, v.indices, v.indptr, v.shape)
def test_local_csm_grad_c():
    data = tensor.vector()
    indices, indptr, shape = (tensor.ivector(), tensor.ivector(), tensor.ivector())
    mode = aesara.compile.mode.get_default_mode()

    if aesara.config.mode == "FAST_COMPILE":
        mode = aesara.compile.Mode(linker="c|py", optimizer="fast_compile")

    mode = mode.including("specialize", "local_csm_grad_c")
    for CS, cast in [
        (sparse.CSC, sp.sparse.csc_matrix),
        (sparse.CSR, sp.sparse.csr_matrix),
    ]:
        cost = tensor.sum(sparse.DenseFromSparse()(CS(data, indices, indptr, shape)))
        f = aesara.function(
            [data, indices, indptr, shape], tensor.grad(cost, data), mode=mode
        )
        assert not any(
            isinstance(node.op, sparse.CSMGrad) for node in f.maker.fgraph.toposort()
        )
        v = cast(random_lil((10, 40), config.floatX, 3))
        f(v.data, v.indices, v.indptr, v.shape)
def test_seed_fn():
    idx = tensor.ivector()

    for new_seed, same in [(234, True), (None, True), (23, False)]:
        random = MRG_RandomStreams(234)
        fn1 = aesara.function([], random.uniform((2, 2), dtype="float32"))
        fn2 = aesara.function([],
                              random.uniform((3, 3),
                                             nstreams=2,
                                             dtype="float32"))
        fn3 = aesara.function([idx],
                              random.uniform(idx,
                                             nstreams=3,
                                             ndim=1,
                                             dtype="float32"))

        fn1_val0 = fn1()
        fn1_val1 = fn1()
        assert not np.allclose(fn1_val0, fn1_val1)
        fn2_val0 = fn2()
        fn2_val1 = fn2()
        assert not np.allclose(fn2_val0, fn2_val1)
        fn3_val0 = fn3([4])
        fn3_val1 = fn3([4])
        assert not np.allclose(fn3_val0, fn3_val1)
        assert fn1_val0.size == 4
        assert fn2_val0.size == 9

        random.seed(new_seed)

        fn1_val2 = fn1()
        fn1_val3 = fn1()
        fn2_val2 = fn2()
        fn2_val3 = fn2()
        fn3_val2 = fn3([4])
        fn3_val3 = fn3([4])
        assert np.allclose(fn1_val0, fn1_val2) == same
        assert np.allclose(fn1_val1, fn1_val3) == same
        assert np.allclose(fn2_val0, fn2_val2) == same
        assert np.allclose(fn2_val1, fn2_val3) == same
        assert np.allclose(fn3_val0, fn3_val2) == same
        assert np.allclose(fn3_val1, fn3_val3) == same
Beispiel #10
0
def test__getitem__AdvancedSubtensor_bool():
    x = tt.matrix("x")
    i = tt.type.TensorType("bool", (False, False))("i")

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

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

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

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

    z = x[tt.ivector("b"), i]
    op_types = [type(node.op) for node in aesara.gof.graph.io_toposort([x, i], [z])]
    assert op_types[-1] == AdvancedSubtensor
Beispiel #11
0
    def test_infer_shape(self):
        rng_R = random_state_type()
        rng_R_val = np.random.RandomState(utt.fetch_seed())

        # no shape specified, default args
        post_r, out = uniform(rng_R)
        self._compile_and_check([rng_R], [out], [rng_R_val], RandomFunction)

        post_r, out = uniform(rng_R, size=None, ndim=2)
        self._compile_and_check([rng_R], [out], [rng_R_val], RandomFunction)
        """
        #infer_shape don't work for multinomial.
        #The parameter ndim_added is set to 1 and in this case, the infer_shape
        #inplementation don't know how to infer the shape
        post_r, out = multinomial(rng_R)

        self._compile_and_check([rng_R], [out], [rng_R_val],
                                RandomFunction)
        """

        # no shape specified, args have to be broadcasted
        low = tensor.TensorType(dtype="float64",
                                broadcastable=(False, True, True))()
        high = tensor.TensorType(dtype="float64",
                                 broadcastable=(True, True, True, False))()
        post_r, out = uniform(rng_R, size=None, ndim=2, low=low, high=high)
        low_val = [[[3]], [[4]], [[-5]]]
        high_val = [[[[5, 8]]]]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        # multinomial, specified shape
        """
        #infer_shape don't work for multinomial
        n = iscalar()
        pvals = dvector()
        size_val = (7, 3)
        n_val = 6
        pvals_val = [0.2] * 5
        post_r, out = multinomial(rng_R, size=size_val, n=n, pvals=pvals,
                                  ndim=2)

        self._compile_and_check([rng_R, n, pvals], [out],
                                [rng_R_val, n_val, pvals_val],
                                RandomFunction)
        """

        # uniform vector low and high
        low = dvector()
        high = dvector()
        post_r, out = uniform(rng_R, low=low, high=1)
        low_val = [-5, 0.5, 0, 1]
        self._compile_and_check([rng_R, low], [out], [rng_R_val, low_val],
                                RandomFunction)

        low_val = [0.9]
        self._compile_and_check([rng_R, low], [out], [rng_R_val, low_val],
                                RandomFunction)

        post_r, out = uniform(rng_R, low=low, high=high)
        low_val = [-4.0, -2]
        high_val = [-1, 0]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        low_val = [-4.0]
        high_val = [-1]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        # uniform broadcasting low and high
        low = dvector()
        high = dcol()
        post_r, out = uniform(rng_R, low=low, high=high)
        low_val = [-5, 0.5, 0, 1]
        high_val = [[1.0]]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        low_val = [0.9]
        high_val = [[1.0], [1.1], [1.5]]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        low_val = [-5, 0.5, 0, 1]
        high_val = [[1.0], [1.1], [1.5]]
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        # uniform with vector slice
        low = dvector()
        high = dvector()
        post_r, out = uniform(rng_R, low=low, high=high)
        low_val = [0.1, 0.2, 0.3]
        high_val = [1.1, 2.2, 3.3]
        size_val = (3, )
        self._compile_and_check(
            [rng_R, low, high],
            [out],
            [rng_R_val, low_val[:-1], high_val[:-1]],
            RandomFunction,
        )

        # uniform with explicit size and size implicit in parameters
        # NOTE 1: Would it be desirable that size could also be supplied
        # as a Aesara variable?
        post_r, out = uniform(rng_R, size=size_val, low=low, high=high)
        self._compile_and_check([rng_R, low, high], [out],
                                [rng_R_val, low_val, high_val], RandomFunction)

        # binomial with vector slice
        n = ivector()
        prob = dvector()
        post_r, out = binomial(rng_R, n=n, p=prob)
        n_val = [1, 2, 3]
        prob_val = [0.1, 0.2, 0.3]
        size_val = (3, )
        self._compile_and_check(
            [rng_R, n, prob],
            [out],
            [rng_R_val, n_val[:-1], prob_val[:-1]],
            RandomFunction,
        )

        # binomial with explicit size and size implicit in parameters
        # cf. NOTE 1
        post_r, out = binomial(rng_R, n=n, p=prob, size=size_val)
        self._compile_and_check([rng_R, n, prob], [out],
                                [rng_R_val, n_val, prob_val], RandomFunction)

        # normal with vector slice
        avg = dvector()
        std = dvector()
        post_r, out = normal(rng_R, avg=avg, std=std)
        avg_val = [1, 2, 3]
        std_val = [0.1, 0.2, 0.3]
        size_val = (3, )
        self._compile_and_check(
            [rng_R, avg, std],
            [out],
            [rng_R_val, avg_val[:-1], std_val[:-1]],
            RandomFunction,
        )

        # normal with explicit size and size implicit in parameters
        # cf. NOTE 1
        post_r, out = normal(rng_R, avg=avg, std=std, size=size_val)
        self._compile_and_check([rng_R, avg, std], [out],
                                [rng_R_val, avg_val, std_val], RandomFunction)

        # multinomial with tensor-3 probabilities
        """
Beispiel #12
0
def test_mlp():
    """
    Demonstrate stochastic gradient descent optimization for a multilayer
    perceptron

    This is demonstrated on MNIST.

    :type learning_rate: float
    :param learning_rate: learning rate used (factor for the stochastic
    gradient

    :type n_epochs: int
    :param n_epochs: maximal number of epochs to run the optimizer

    :type dataset: string
    :param dataset: the path of the MNIST dataset file from
                         http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz


    """
    datasets = gen_data()

    train_set_x, train_set_y = datasets[0]
    valid_set_x, valid_set_y = datasets[1]
    test_set_x, test_set_y = datasets[2]

    batch_size = 100  # size of the minibatch

    # compute number of minibatches for training, validation and testing
    # n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
    # n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
    # n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size

    ######################
    # BUILD ACTUAL MODEL #
    ######################
    # print '... building the model'

    # allocate symbolic variables for the data
    index = tt.lscalar()  # index to a [mini]batch
    x = tt.matrix("x")  # the data is presented as rasterized images
    y = tt.ivector("y")  # the labels are presented as 1D vector of
    # [int] labels

    rng = np.random.RandomState(1234)

    # construct the MLP class
    classifier = MLP(rng=rng, input=x, n_in=28 * 28, n_hidden=500, n_out=10)

    # the cost we minimize during training is the negative log likelihood of
    # the model.
    # We take the mean of the cost over each minibatch.
    cost = classifier.negative_log_likelihood(y).mean()

    # compute the gradient of cost with respect to theta (stored in params)
    # the resulting gradients will be stored in a list gparams
    gparams = []
    for param in classifier.params:
        gparam = tt.grad(cost, param)
        gparams.append(gparam)

    # Some optimizations needed are tagged with 'fast_run'
    # TODO: refine that and include only those
    mode = aesara.compile.get_default_mode().including("fast_run")

    updates2 = OrderedDict()

    updates2[classifier.hiddenLayer.params[0]] = tt.grad(
        cost, classifier.hiddenLayer.params[0])
    train_model = aesara.function(
        inputs=[index],
        updates=updates2,
        givens={
            x: train_set_x[index * batch_size:(index + 1) * batch_size],
            y: train_set_y[index * batch_size:(index + 1) * batch_size],
        },
        mode=mode,
    )
    # print 'MODEL 1'
    # aesara.printing.debugprint(train_model, print_type=True)
    assert any([
        isinstance(i.op, tt.nnet.CrossentropySoftmax1HotWithBiasDx)
        for i in train_model.maker.fgraph.toposort()
    ])

    # Even without FeatureShape
    train_model = aesara.function(
        inputs=[index],
        updates=updates2,
        mode=mode.excluding("ShapeOpt"),
        givens={
            x: train_set_x[index * batch_size:(index + 1) * batch_size],
            y: train_set_y[index * batch_size:(index + 1) * batch_size],
        },
    )
    # print
    # print 'MODEL 2'
    # aesara.printing.debugprint(train_model, print_type=True)
    assert any([
        isinstance(i.op, tt.nnet.CrossentropySoftmax1HotWithBiasDx)
        for i in train_model.maker.fgraph.toposort()
    ])