def test_predict(self):
     np.random.seed(100)
     total_length = 6
     features = np.random.uniform(0, 1, (total_length, 2))
     label = (features).sum() + 0.4
     predict_data = self.sc.parallelize(range(0, total_length)).map(
         lambda i: Sample.from_ndarray(features[i], label))
     model = Linear(2, 1, "Xavier").set_name("linear1").set_seed(1234).reset()
     predict_result = model.predict(predict_data)
     p = predict_result.take(6)
     ground_label = np.array([[-0.47596836], [-0.37598032], [-0.00492062],
                              [-0.5906958], [-0.12307882], [-0.77907401]], dtype="float32")
     for i in range(0, total_length):
         self.assertTrue(np.allclose(p[i], ground_label[i], atol=1e-6, rtol=0))
    def test_forward_multiple(self):
        from nn.layer import Linear
        rng = RNG()
        rng.set_seed(100)

        input = [rng.uniform(0.0, 0.1, [2]),
                 rng.uniform(0.0, 0.1, [2]) + 0.2]

        grad_output = [rng.uniform(0.0, 0.1, [3]),
                       rng.uniform(0.0, 0.1, [3]) + 0.2]

        linear1 = Linear(2, 3)
        linear2 = Linear(2, 3)

        module = ParallelTable()
        module.add(linear1)
        module.add(linear2)
        module.forward(input)
        module.backward(input, grad_output)
    def test_forward_backward(self):
        from nn.layer import Linear
        rng = RNG()
        rng.set_seed(100)

        linear = Linear(4, 5)
        input = rng.uniform(0.0, 1.0, [4])
        output = linear.forward(input)
        self.assertTrue(np.allclose(output,
                                    np.array([0.41366524,
                                              0.009532653,
                                              -0.677581,
                                              0.07945433,
                                              -0.5742568]),
                                    atol=1e-6, rtol=0))
        mse = MSECriterion()
        target = rng.uniform(0.0, 1.0, [5])
        loss = mse.forward(output, target)
        print("loss: " + str(loss))
        grad_output = mse.backward(output, rng.uniform(0.0, 1.0, [5]))
        l_grad_output = linear.backward(input, grad_output)
 def test_set_seed(self):
     l1 = Linear(
         10, 20,
         "Xavier").set_name("linear1").set_seed(1234).reset()  # noqa
     l2 = Linear(
         10, 20,
         "Xavier").set_name("linear2").set_seed(1234).reset()  # noqa
     p1 = l1.parameters()
     p2 = l2.parameters()
     self.assertTrue(
         (p1["linear1"]["weight"] == p2["linear2"]["weight"]).all())  # noqa
    def test_simple_flow(self):
        FEATURES_DIM = 2
        data_len = 100
        batch_size = 32
        epoch_num = 5

        def gen_rand_sample():
            features = np.random.uniform(0, 1, (FEATURES_DIM))
            label = (2 * features).sum() + 0.4
            return Sample.from_ndarray(features, label)

        trainingData = self.sc.parallelize(range(
            0, data_len)).map(lambda i: gen_rand_sample())

        model_test = Sequential()
        l1_test = Linear(3, 1, "Xavier").set_name("linear1_test")
        self.assertEqual("linear1_test", l1_test.name())
        model_test.add(l1_test)
        model_test.add(Sigmoid())

        model = Sequential()
        l1 = Linear(FEATURES_DIM, 1, "Xavier").set_name("linear1")
        self.assertEqual("linear1", l1.name())
        model.add(l1)

        optim_method = SGD(learningrate=0.01,
                           learningrate_decay=0.0002,
                           weightdecay=0.0,
                           momentum=0.0,
                           dampening=0.0,
                           nesterov=False,
                           leaningrate_schedule=Poly(
                               0.5, int((data_len / batch_size) * epoch_num)))
        optimizer = Optimizer(model=model_test,
                              training_rdd=trainingData,
                              criterion=MSECriterion(),
                              optim_method=optim_method,
                              end_trigger=MaxEpoch(epoch_num),
                              batch_size=batch_size)
        optimizer.set_validation(batch_size=batch_size,
                                 val_rdd=trainingData,
                                 trigger=EveryEpoch(),
                                 val_method=["Top1Accuracy"])

        optimizer.optimize()

        optimizer.set_model(model=model)
        tmp_dir = tempfile.mkdtemp()
        optimizer.set_checkpoint(SeveralIteration(1), tmp_dir)
        train_summary = TrainSummary(log_dir=tmp_dir, app_name="run1")
        train_summary.set_summary_trigger("LearningRate", SeveralIteration(1))
        val_summary = ValidationSummary(log_dir=tmp_dir, app_name="run1")
        optimizer.set_train_summary(train_summary)
        optimizer.set_val_summary(val_summary)

        trained_model = optimizer.optimize()
        lr_result = train_summary.read_scalar("LearningRate")
        top1_result = val_summary.read_scalar("Top1Accuracy")

        # TODO: add result validation
        parameters = trained_model.parameters()

        self.assertIsNotNone(parameters["linear1"])
        print("parameters %s" % parameters["linear1"])
        predict_result = trained_model.predict(trainingData)
        p = predict_result.take(2)
        print("predict predict: \n")
        for i in p:
            print(str(i) + "\n")
        print(len(p))

        test_results = trained_model.test(trainingData, 32, ["Top1Accuracy"])
        for test_result in test_results:
            print(test_result)
import numpy as np
import matplotlib.pyplot as plt

from data.test_datasets import load_easy_dataset_extended
from nn.layer import Linear
from nn.activation import Sigmoid, ReLU
from nn.model import Sequential
from nn.loss import MeanSquaredError

if __name__ == '__main__':

    X, Y = load_easy_dataset_extended()

    model_definition = [Linear(2, 4), ReLU(), Linear(4, 1), Sigmoid()]

    model = Sequential(model_definition)
    try:
        model.train(X, Y, n_h=4, epochs=100000)
    except KeyboardInterrupt:
        pass

    heatmap = np.mgrid[-1:1:200j, -1:1:200j]
    heatmap_x = heatmap[0].flatten()
    heatmap_y = heatmap[1].flatten()
    heatmap = np.array([heatmap_x, heatmap_y])
    heatmap_class = model.forward(heatmap)

    plt.plot(list(range(len(model._hist['loss']))), model._hist['loss'])
    plt.show()

    plt.scatter(heatmap[0, :],