예제 #1
0
def test_BlockMatrix_Inverse_execution():
    k, n = 2, 4
    dtype = 'float32'
    A = diofant.MatrixSymbol('A', n, k)
    B = diofant.MatrixSymbol('B', n, n)
    inputs = A, B
    output = B.I * A

    cutsizes = {
        A: [(n // 2, n // 2), (k // 2, k // 2)],
        B: [(n // 2, n // 2), (n // 2, n // 2)]
    }
    cutinputs = [diofant.blockcut(i, *cutsizes[i]) for i in inputs]
    cutoutput = output.subs(dict(zip(inputs, cutinputs)))

    dtypes = dict(zip(inputs, [dtype] * len(inputs)))
    f = theano_function(inputs, [output], dtypes=dtypes, cache={})
    fblocked = theano_function(inputs, [diofant.block_collapse(cutoutput)],
                               dtypes=dtypes,
                               cache={})

    ninputs = [np.random.rand(*x.shape).astype(dtype) for x in inputs]
    ninputs = [
        np.arange(n * k).reshape(A.shape).astype(dtype),
        np.eye(n).astype(dtype)
    ]
    ninputs[1] += np.ones(B.shape) * 1e-5

    assert np.allclose(f(*ninputs), fblocked(*ninputs), rtol=1e-5)
예제 #2
0
def test_BlockMatrix():
    n = diofant.Symbol('n', integer=True)
    A = diofant.MatrixSymbol('A', n, n)
    B = diofant.MatrixSymbol('B', n, n)
    C = diofant.MatrixSymbol('C', n, n)
    D = diofant.MatrixSymbol('D', n, n)
    At, Bt, Ct, Dt = map(theano_code, (A, B, C, D))
    Block = diofant.BlockMatrix([[A, B], [C, D]])
    Blockt = theano_code(Block)
    solutions = [
        tt.join(0, tt.join(1, At, Bt), tt.join(1, Ct, Dt)),
        tt.join(1, tt.join(0, At, Ct), tt.join(0, Bt, Dt))
    ]
    assert any(theq(Blockt, solution) for solution in solutions)
예제 #3
0
def test_MatrixSlice_2():
    n = diofant.Symbol('n', integer=True)
    X = diofant.MatrixSymbol('X', n, n)

    Y = X[1:2:3, 4:5:6]
    Yt = theano_code(Y)
    assert tuple(Yt.owner.op.idx_list) == (slice(1, 2, 3), slice(4, 5, 6))
예제 #4
0
def test_MatrixSlice():
    n = diofant.Symbol('n', integer=True)
    X = diofant.MatrixSymbol('X', n, n)

    Y = X[1:2:3, 4:5:6]
    Yt = theano_code(Y)
    # assert tuple(Yt.owner.op.idx_list) == (slice(1,2,3), slice(4,5,6))
    assert Yt.owner.inputs[0] == theano_code(X)

    k = diofant.Symbol('k')
    kt = theano_code(k, dtypes={k: 'int32'})
    start, stop, step = 4, k, 2
    Y = X[start:stop:step]
    Yt = theano_code(Y, dtypes={n: 'int32', k: 'int32'})
예제 #5
0
def test_MatAdd():
    X = diofant.MatrixSymbol('X', 4, 4)
    Y = diofant.MatrixSymbol('X', 4, 4)
    Z = diofant.MatrixSymbol('X', 4, 4)
    expr = X + Y + Z
    assert isinstance(theano_code(expr).owner.op, tt.Elemwise)
예제 #6
0
def test_Transpose():
    X = diofant.MatrixSymbol('X', 4, 4)
    assert isinstance(theano_code(X.T).owner.op, tt.DimShuffle)
예제 #7
0
def test_MatMul():
    X = diofant.MatrixSymbol('X', 4, 4)
    Y = diofant.MatrixSymbol('X', 4, 4)
    Z = diofant.MatrixSymbol('X', 4, 4)
    expr = X * Y * Z
    assert isinstance(theano_code(expr).owner.op, tt.Dot)
예제 #8
0
def test_MatrixSymbol():
    X = diofant.MatrixSymbol('X', 4, 5)
    Xt = theano_code(X)
    assert isinstance(Xt, tt.TensorVariable)
    assert Xt.broadcastable == (False, False)