def fn(data, weight1, weight2, bias1, bias2): l1 = weight1.dot(data) l1 = Matrix(l1) + b1.T e1 = l1.applyfunc(sigmoid) l2 = weight2.dot(e1) l2 = Matrix(l2) + b2.T e2 = l2.applyfunc(sigmoid) return e2[0] - e2[1]
def test_bidiagonalize(): M = Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) assert M.bidiagonalize() == M assert M.bidiagonalize(upper=False) == M assert M.bidiagonalize() == M assert M.bidiagonal_decomposition() == (M, M, M) assert M.bidiagonal_decomposition(upper=False) == (M, M, M) assert M.bidiagonalize() == M import random #Real Tests for real_test in range(2): test_values = [] row = 2 col = 2 for _ in range(row * col): value = random.randint(-1000000000, 1000000000) test_values = test_values + [value] # L -> Lower Bidiagonalization # M -> Mutable Matrix # N -> Immutable Matrix # 0 -> Bidiagonalized form # 1,2,3 -> Bidiagonal_decomposition matrices # 4 -> Product of 1 2 3 M = Matrix(row, col, test_values) N = ImmutableMatrix(M) N1, N2, N3 = N.bidiagonal_decomposition() M1, M2, M3 = M.bidiagonal_decomposition() M0 = M.bidiagonalize() N0 = N.bidiagonalize() N4 = N1 * N2 * N3 M4 = M1 * M2 * M3 N2.simplify() N4.simplify() N0.simplify() M0.simplify() M2.simplify() M4.simplify() LM0 = M.bidiagonalize(upper=False) LM1, LM2, LM3 = M.bidiagonal_decomposition(upper=False) LN0 = N.bidiagonalize(upper=False) LN1, LN2, LN3 = N.bidiagonal_decomposition(upper=False) LN4 = LN1 * LN2 * LN3 LM4 = LM1 * LM2 * LM3 LN2.simplify() LN4.simplify() LN0.simplify() LM0.simplify() LM2.simplify() LM4.simplify() assert M == M4 assert M2 == M0 assert N == N4 assert N2 == N0 assert M == LM4 assert LM2 == LM0 assert N == LN4 assert LN2 == LN0 #Complex Tests for complex_test in range(2): test_values = [] size = 2 for _ in range(size * size): real = random.randint(-1000000000, 1000000000) comp = random.randint(-1000000000, 1000000000) value = real + comp * I test_values = test_values + [value] M = Matrix(size, size, test_values) N = ImmutableMatrix(M) # L -> Lower Bidiagonalization # M -> Mutable Matrix # N -> Immutable Matrix # 0 -> Bidiagonalized form # 1,2,3 -> Bidiagonal_decomposition matrices # 4 -> Product of 1 2 3 N1, N2, N3 = N.bidiagonal_decomposition() M1, M2, M3 = M.bidiagonal_decomposition() M0 = M.bidiagonalize() N0 = N.bidiagonalize() N4 = N1 * N2 * N3 M4 = M1 * M2 * M3 N2.simplify() N4.simplify() N0.simplify() M0.simplify() M2.simplify() M4.simplify() LM0 = M.bidiagonalize(upper=False) LM1, LM2, LM3 = M.bidiagonal_decomposition(upper=False) LN0 = N.bidiagonalize(upper=False) LN1, LN2, LN3 = N.bidiagonal_decomposition(upper=False) LN4 = LN1 * LN2 * LN3 LM4 = LM1 * LM2 * LM3 LN2.simplify() LN4.simplify() LN0.simplify() LM0.simplify() LM2.simplify() LM4.simplify() assert M == M4 assert M2 == M0 assert N == N4 assert N2 == N0 assert M == LM4 assert LM2 == LM0 assert N == LN4 assert LN2 == LN0 M = Matrix(18, 8, range(1, 145)) M = M.applyfunc(lambda i: Float(i)) assert M.bidiagonal_decomposition()[1] == M.bidiagonalize() assert M.bidiagonal_decomposition(upper=False)[1] == M.bidiagonalize( upper=False) a, b, c = M.bidiagonal_decomposition() diff = a * b * c - M assert abs(max(diff)) < 10**-12