from MatrixVector import Matrix, Vector

##### 2 Layer Neural Net

# input data

X = Matrix([Vector([0.0, 0.0, 1.0]), Vector([0.0, 1.0, 1.0]), Vector([1.0, 0.0, 1.0]), Vector([1.0, 1.0, 1.0])])

Xones = Matrix(Matrix.ones(1, X.getRowLen())._matrix + X.transpose()._matrix).transpose()
y = Matrix([Vector([0.0, 1.0, 1.0, 0.0])]).transpose()

# randomly initialize our weights with mean 0 (entry + 1 for bias unit)
syn0 = 2 * Matrix.random(4, 4) - 1
syn1 = 2 * Matrix.random(5, 1) - 1

# set the alpha
alpha = 10

for j in xrange(1000):

    # Feed forward through layers 0, 1, and 2
    l0 = Xones
    l1 = l0.dotProduct(syn0).nonlin()
    l1 = Matrix(Matrix.ones(1, l0.getRowLen())._matrix + l1.transpose()._matrix).transpose()
    l2 = l1.dotProduct(syn1).nonlin()

    # how much did we miss the target value?
    l2_error = y - l2

    if (j % 100) == 0:
        print "Error:" + str(l2_error.abs().mean())
def append_bias(mtx):
    return Matrix(Matrix.ones(1, mtx.getRowLen())._matrix + mtx.transpose()._matrix).transpose()


def remove_bias(mtx):
    return Matrix(mtx.transpose()._matrix[1:]).transpose()


##### 4 Layer Neural Net

# input data

X = Matrix([Vector([0.0, 0.0, 1.0]), Vector([0.0, 1.0, 1.0]), Vector([1.0, 0.0, 1.0]), Vector([1.0, 1.0, 1.0])])

Xones = Matrix(Matrix.ones(1, X.getRowLen())._matrix + X.transpose()._matrix).transpose()
y = Matrix([Vector([0.0, 1.0, 1.0, 0.0])]).transpose()

# randomly initialize our weights with mean 0 (entry + 1 for bias unit)
syn0 = 2 * Matrix.random(4, 4) - 1
syn1 = 2 * Matrix.random(5, 4) - 1
syn2 = 2 * Matrix.random(5, 1) - 1

# set the alpha
alpha = 0.07

for j in xrange(10000):

    # Feed forward through layers 0, 1, 2 and 3
    l0 = Xones
    l1 = l0.dotProduct(syn0).nonlin()