Example #1
0
    def test_graph(self):
        # define common values  first
        groups = 3
        bottom = np.random.rand(3, 6, 5, 5).astype(aesara.config.floatX)
        kern = np.random.rand(9, 2, 3, 3).astype(aesara.config.floatX)
        bottom_sym = tensor4("bottom")
        kern_sym = tensor4("kern")

        # grouped convolution graph
        conv_group = self.conv(num_groups=groups)(bottom_sym, kern_sym)
        gconv_func = aesara.function([bottom_sym, kern_sym],
                                     conv_group,
                                     mode=self.mode)

        # Graph for the normal hard way
        kern_offset = kern_sym.shape[0] // groups
        bottom_offset = bottom_sym.shape[1] // groups
        split_conv_output = [
            self.conv()(
                bottom_sym[:, i * bottom_offset:(i + 1) * bottom_offset, :, :],
                kern_sym[i * kern_offset:(i + 1) * kern_offset, :, :, :],
            ) for i in range(groups)
        ]
        concatenated_output = at.concatenate(split_conv_output, axis=1)
        conv_func = aesara.function([bottom_sym, kern_sym],
                                    concatenated_output,
                                    mode=self.mode)

        # calculate outputs for each graph
        gconv_output = gconv_func(bottom, kern)
        conv_output = conv_func(bottom, kern)

        # compare values
        utt.assert_allclose(gconv_output, conv_output)
Example #2
0
 def setup_method(self):
     self.input = tensor4("input", dtype=self.dtype)
     self.input.name = "default_V"
     self.filters = tensor4("filters", dtype=self.dtype)
     self.filters.name = "default_filters"
     # This tests can run even when aesara.config.blas__ldflags is empty.
     super().setup_method()
Example #3
0
def burn():
    sz = 128
    img_shp = [sz, sz, sz, sz]
    kern_shp = [sz // 2, sz, 3, 3]
    out_shp = get_conv_output_shape(img_shp, kern_shp, "valid", (1, 1))
    img = tensor4("img")
    kern = tensor4("kern")
    out = tensor4("out")

    def rand(shp):
        return np.random.rand(*shp).astype(config.floatX)

    img = aesara.shared(rand(img_shp))
    kern = aesara.shared(rand(kern_shp))
    out = aesara.shared(rand(out_shp))
    # beta 1 is needed to force the reuse of out, otherwise, it is
    # replaced by a GpuAllocEmpty
    o1 = dnn._dnn_conv(img, kern, conv_mode="conv", out=out, beta=1.0)
    mode = aesara.compile.get_default_mode().including(
        "local_remove_all_assert")
    f = aesara.function([], [o1], mode=mode)
    aesara.printing.debugprint(f)
    print("Start computation")
    for i in range(10000):
        f.fn()
    print("Computation stopped")
Example #4
0
    def test_dtype_upcast(self):
        # Checks dtype upcast for CorrMM methods.

        def rand(shape, dtype="float64"):
            r = np.asarray(np.random.rand(*shape), dtype=dtype)
            return r * 2 - 1

        ops = [corr.CorrMM, corr.CorrMM_gradWeights, corr.CorrMM_gradInputs]
        a_shapes = [[4, 5, 6, 3], [1, 5, 6, 3], [1, 5, 6, 3]]
        b_shapes = [[7, 5, 3, 2], [1, 5, 3, 1], [7, 1, 3, 1]]
        dtypes = ["float32", "float64"]

        for op, a_shape, b_shape in zip(ops, a_shapes, b_shapes):
            for a_dtype in dtypes:
                for b_dtype in dtypes:
                    c_dtype = aesara.scalar.upcast(a_dtype, b_dtype)
                    a_tens = tensor4(dtype=a_dtype)
                    b_tens = tensor4(dtype=b_dtype)
                    a_tens_val = rand(a_shape, dtype=a_dtype)
                    b_tens_val = rand(b_shape, dtype=b_dtype)

                    c_tens = op()(a_tens, b_tens)
                    f = aesara.function([a_tens, b_tens],
                                        c_tens,
                                        mode=self.mode)
                    assert f(a_tens_val, b_tens_val).dtype == c_dtype
Example #5
0
    def test_perform(self, axis):
        x = tensor4("x")
        rng = np.random.default_rng(utt.fetch_seed())
        xv = rng.standard_normal((2, 3, 4, 5)).astype(config.floatX)

        f = aesara.function([x], softmax(x, axis=axis))
        assert np.allclose(f(xv), sp.softmax(xv, axis=axis))
Example #6
0
    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 = tensor4("img")
        patches = nnet.neighbours.images2neibs(img, [16, 16])
        extractPatches = aesara.function([img], patches, mode=self.mode)

        patsRecovery = matrix("patsRecovery")
        original_size = 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))
Example #7
0
 def setup_method(self):
     super().setup_method()
     self.A = tensor4("A", dtype=config.floatX)
     self.B = tensor3("B", dtype=config.floatX)
     self.a = np.random.rand(4, 6, 8, 3).astype(config.floatX)
     self.b = np.random.rand(2, 15, 30).astype(config.floatX)
     self.b1 = np.random.rand(30, 2, 15).astype(
         config.floatX
     )  # for ind=1 since we need prod(b1.shape[:ind]) == prod(b1.shape[ind:])
Example #8
0
def test_tensorsolve():
    rng = np.random.RandomState(utt.fetch_seed())

    A = tensor4("A", dtype=config.floatX)
    B = matrix("B", dtype=config.floatX)
    X = tensorsolve(A, B)
    fn = function([A, B], [X])

    # slightly modified example from np.linalg.tensorsolve docstring
    a = np.eye(2 * 3 * 4).astype(config.floatX)
    a.shape = (2 * 3, 4, 2, 3 * 4)
    b = rng.rand(2 * 3, 4).astype(config.floatX)

    n_x = np.linalg.tensorsolve(a, b)
    t_x = fn(a, b)
    assert _allclose(n_x, t_x)

    # check the type upcast now
    C = tensor4("C", dtype="float32")
    D = matrix("D", dtype="float64")
    Y = tensorsolve(C, D)
    fn = function([C, D], [Y])

    c = np.eye(2 * 3 * 4, dtype="float32")
    c.shape = (2 * 3, 4, 2, 3 * 4)
    d = rng.rand(2 * 3, 4).astype("float64")
    n_y = np.linalg.tensorsolve(c, d)
    t_y = fn(c, d)
    assert _allclose(n_y, t_y)
    assert n_y.dtype == Y.dtype

    # check the type upcast now
    E = tensor4("E", dtype="int32")
    F = matrix("F", dtype="float64")
    Z = tensorsolve(E, F)
    fn = function([E, F], [Z])

    e = np.eye(2 * 3 * 4, dtype="int32")
    e.shape = (2 * 3, 4, 2, 3 * 4)
    f = rng.rand(2 * 3, 4).astype("float64")
    n_z = np.linalg.tensorsolve(e, f)
    t_z = fn(e, f)
    assert _allclose(n_z, t_z)
    assert n_z.dtype == Z.dtype
Example #9
0
def test_batch_normalization_train_without_running_averages():
    # compile and run batch_normalization_train without running averages
    utt.seed_rng()

    x, scale, bias, dy = (
        tensor4("x"),
        tensor4("scale"),
        tensor4("bias"),
        tensor4("dy"),
    )
    data_shape = (5, 10, 30, 25)
    param_shape = (1, 10, 30, 25)

    # forward pass
    out, x_mean, x_invstd = batchnorm.batch_normalization_train(
        x, scale, bias, "per-activation"
    )
    # backward pass
    grads = aet.grad(None, wrt=[x, scale, bias], known_grads={out: dy})
    # compile
    f = aesara.function([x, scale, bias, dy], [out, x_mean, x_invstd] + grads)
    # check if the abstract Ops have been replaced
    assert not any(
        [
            isinstance(
                n.op,
                (
                    batchnorm.AbstractBatchNormTrain,
                    batchnorm.AbstractBatchNormInference,
                    batchnorm.AbstractBatchNormTrainGrad,
                ),
            )
            for n in f.maker.fgraph.toposort()
        ]
    )
    # run
    X = 4 + 3 * np.random.randn(*data_shape).astype(aesara.config.floatX)
    Dy = -1 + 2 * np.random.randn(*data_shape).astype(aesara.config.floatX)
    Scale = np.random.randn(*param_shape).astype(aesara.config.floatX)
    Bias = np.random.randn(*param_shape).astype(aesara.config.floatX)
    f(X, Scale, Bias, Dy)
Example #10
0
def test_bn_feature_maps():
    def bn_ref(x, G, B, M, V):
        n = (x - M) / V
        return n * G + B

    rng = np.random.default_rng(1234)
    X = 1 + rng.random([2, 3, 4, 4]).astype("float32")
    B = 1 + rng.random([3]).astype("float32")
    G = 1 + rng.random([3]).astype("float32")
    M = 1 + rng.random([3]).astype("float32")
    V = 1 + rng.random([3]).astype("float32")

    x = tensor4("x")
    b = vector("b")
    g = vector("g")
    m = vector("m")
    v = vector("v")

    bn_ref_op = bn_ref(
        x,
        g.dimshuffle("x", 0, "x", "x"),
        b.dimshuffle("x", 0, "x", "x"),
        m.dimshuffle("x", 0, "x", "x"),
        v.dimshuffle("x", 0, "x", "x"),
    )
    f_ref = aesara.function([x, b, g, m, v], [bn_ref_op])
    res_ref = f_ref(X, G, B, M, V)

    for mode in ["low_mem", "high_mem"]:
        bn_op = batchnorm.batch_normalization(
            x,
            g.dimshuffle("x", 0, "x", "x"),
            b.dimshuffle("x", 0, "x", "x"),
            m.dimshuffle("x", 0, "x", "x"),
            v.dimshuffle("x", 0, "x", "x"),
            mode=mode,
        )
        f = aesara.function([x, b, g, m, v], [bn_op])
        res = f(X, G, B, M, V)
        utt.assert_allclose(res_ref, res)

        def conv_bn(inputs, gamma, beta, mean, std):
            return batchnorm.batch_normalization(
                inputs,
                gamma.dimshuffle("x", 0, "x", "x"),
                beta.dimshuffle("x", 0, "x", "x"),
                mean.dimshuffle("x", 0, "x", "x"),
                std.dimshuffle("x", 0, "x", "x"),
                mode=mode,
            )

        utt.verify_grad(conv_bn, [X, G, B, M, V])
Example #11
0
def test_binary_crossentropy_reshape():
    # Reported as https://github.com/Theano/Theano/issues/4086
    a = tensor4("a")
    for c in (
            binary_crossentropy(sigmoid(a.reshape((-1, 1))), 1).sum(),
            binary_crossentropy(sigmoid(a).reshape((-1, 1)), 1).sum(),
    ):

        ga = aesara.grad(c, a)
        # This only works when "specialize" options are included
        mode = aesara.compile.get_default_mode().including("fast_run")
        fga = aesara.function([a], ga, mode=mode)
        utt.assert_allclose(
            fga(np.array([[[[30.0]]]], dtype=config.floatX)),
            np.zeros((1, 1, 1, 1), dtype=config.floatX),
        )
Example #12
0
def test_broadcast_grad():
    # rng = numpy.random.RandomState(utt.fetch_seed())
    x1 = tensor4("x")
    # x1_data = rng.randn(1, 1, 300, 300)
    sigma = scalar("sigma")
    # sigma_data = 20
    window_radius = 3

    filter_1d = aet.arange(-window_radius, window_radius + 1)
    filter_1d = filter_1d.astype(aesara.config.floatX)
    filter_1d = exp(-0.5 * filter_1d ** 2 / sigma ** 2)
    filter_1d = filter_1d / filter_1d.sum()

    filter_W = filter_1d.dimshuffle(["x", "x", 0, "x"])

    y = conv2d(x1, filter_W, border_mode="full", filter_shape=[1, 1, None, None])
    aesara.grad(y.sum(), sigma)
Example #13
0
    def test_max_pool_2d_2D_same_size(self):
        rng = np.random.RandomState(utt.fetch_seed())
        test_input_array = np.array([[[[1.0, 2.0, 3.0, 4.0],
                                       [5.0, 6.0, 7.0,
                                        8.0]]]]).astype(aesara.config.floatX)
        test_answer_array = np.array([[[[0.0, 0.0, 0.0, 0.0],
                                        [0.0, 6.0, 0.0,
                                         8.0]]]]).astype(aesara.config.floatX)
        input = tensor4(name="input")
        patch_size = (2, 2)
        op = max_pool_2d_same_size(input, patch_size)
        op_output = function([input], op)(test_input_array)
        utt.assert_allclose(op_output, test_answer_array)

        def mp(input):
            return max_pool_2d_same_size(input, patch_size)

        utt.verify_grad(mp, [test_input_array], rng=rng)
Example #14
0
def test_broadcast_grad():
    x1 = tensor4("x")
    sigma = scalar("sigma")
    window_radius = 3

    filter_1d = at.arange(-window_radius, window_radius + 1)
    filter_1d = filter_1d.astype(aesara.config.floatX)
    filter_1d = exp(-0.5 * filter_1d**2 / sigma**2)
    filter_1d = filter_1d / filter_1d.sum()

    filter_W = filter_1d.dimshuffle(["x", "x", 0, "x"])

    y = conv2d(x1,
               filter_W,
               border_mode="full",
               filter_shape=[1, 1, None, None])
    # TODO FIXME: Make this a real test and `assert` something
    aesara.grad(y.sum(), sigma)
Example #15
0
 def test_tensor_float16(self):
     x = tensor4()
     np_x = np.arange(30107).reshape(7, 11, 17, 23).astype("float16")
     for offset, axis1, axis2 in [
         (1, 0, 1),
         (-1, 0, 1),
         (0, 1, 0),
         (-2, 1, 0),
         (-3, 1, 0),
         (-2, 2, 0),
         (3, 3, 0),
         (-1, 3, 2),
         (2, 2, 3),
         (-1, 2, 1),
         (1, 3, 1),
         (-1, 1, 3),
     ]:
         assert np.allclose(
             GpuExtractDiag(offset, axis1, axis2)(x).eval({x: np_x}),
             np_x.diagonal(offset, axis1, axis2),
         )
Example #16
0
    def test_perform(self, axis):
        x = tensor4("x")
        xv = np.random.randn(2, 3, 4, 5).astype(config.floatX)

        f = aesara.function([x], softmax(x, axis=axis))
        assert np.allclose(f(xv), sp.softmax(xv, axis=axis))
Example #17
0
 def setup_method(self):
     self.input = tensor4("input", dtype=self.dtype)
     self.input.name = "default_V"
     self.filters = tensor4("filters", dtype=self.dtype)
     self.filters.name = "default_filters"
     super().setup_method()