import tiny_dnn
import numpy as np
import pylab as pl


# copy from Andrew Ng's Course
def load_planar_dataset():
    np.random.seed(1)
    m = 400  # number of examples
    n = int(m / 2)  # number of points per class
    d = 2  # dimensionality
    x = np.zeros((m, d))  # data matrix where each row is a single example
    y = np.zeros((m, 1), dtype=np.uint8)  # labels vector (0 for red, 1 for blue)
    a = 4  # maximum ray of the flower

    for j in range(2):
        ix = range(n * j, n * (j + 1))
        t = np.linspace(j * 3.12, (j + 1) * 3.12, n) + np.random.randn(n) * 0.2  # theta
        r = a * np.sin(4 * t) + np.random.randn(n) * 0.2  # radius
        x[ix] = np.c_[r * np.sin(t), r * np.cos(t)]
        y[ix] = j

    x = x.T
    y = y.T

    return x, y


def main():
    x, y = load_planar_dataset()

    net = tiny_dnn.net.Net()
    net.layers.append(tiny_dnn.layer.InputLayer(x, y))
    net.layers.append(tiny_dnn.layer.FullConnectLayer(8, 'tanh'))
    net.layers.append(tiny_dnn.layer.FullConnectLayer(4, 'tanh'))
    net.layers.append(tiny_dnn.layer.FullConnectLayer(1, 'sigmoid'))

    lr = 0.1
    costs = []
    for i in range(5000):
        net.forward()
        cost = net.cost()
        costs.append(cost)

        if i % 100 == 0:
            print(cost)

        if i % 1000 == 0:
            lr *= 0.8

        net.backward()
        net.update(lr)

    predict = np.round(net.data_out)
    print(predict.max(), predict.min())
    accuracy = 1.0 - np.mean(np.abs(predict - y))
    print('Accuracy: {}'.format(accuracy))

    pl.plot(costs)
    pl.show()


if __name__ == '__main__':
    main()
