Ejemplo n.º 1
0
 def _compute_rmse(self, predictions, targets):
     rmse = 0.0
     for i, pred in enumerate(predictions):
         #print(type(amath.array(pred.to_list())), amath.array(pred.to_list()))
         #print(type(amath.array(targets[i])), amath.array(targets[i]))
         a = amath.array(pred.to_list(), dtype=float)
         b = amath.array(targets[i])
         rmse += amath.sqrt(amath.mean((a - b)**2))
     return rmse
Ejemplo n.º 2
0
    def testOneDimensional(self):
        eps = 1e-2

        length = 10000
        dim = 1
        for low, high in product(range(3), range(3)):
            print("UniformTestCase.testOneDimensional Uniform[%d, %d]" %
                  (low, high))
            gen = Uniform(length, low, high, dim)

            sequence = list()
            for pattern in gen:
                sequence.append(pattern[0])
            sequence = amath.array(sequence, dtype=float)

            print("Mean: %f, Median: %f, Variance: %f" %
                  (amath.mean(sequence), amath.median(sequence),
                   amath.var(sequence)))

            self.assertLessEqual(
                amath.abs(amath.mean(sequence) - 0.5 * (low + high)),
                amath.abs(high - low) * eps)
            self.assertLessEqual(
                amath.abs(amath.median(sequence) - 0.5 * (low + high)),
                amath.abs(high - low) * eps)
            self.assertLessEqual(
                amath.abs(amath.var(sequence) - (high - low)**2 / 12.),
                (high - low)**2 * eps)

            sequence = amath.to_numpy(sequence)
            self.assertLessEqual(
                amath.abs(np.mean(sequence) - 0.5 * (low + high)),
                amath.abs(high - low) * eps)
            self.assertLessEqual(
                amath.abs(np.median(sequence) - 0.5 * (low + high)),
                amath.abs(high - low) * eps)
            self.assertLessEqual(
                amath.abs(np.var(sequence) - (high - low)**2 / 12.),
                (high - low)**2 * eps)
Ejemplo n.º 3
0
 def _compute_rmse(self, prediction, target):
     a = amath.array(prediction.to_list(), dtype=float)
     b = amath.array(target)
     rmse = amath.sqrt(amath.mean((a - b)**2))
     return rmse
Ejemplo n.º 4
0
def test_binary_model(model, max_repeat=1000, generator=False):
    """
    minimal test with learning a constant with noise
    """

    in_dim = model.get_input_dimension()
    out_dim = model.get_target_dimension()
    batch = in_dim
    eps = 1e-2

    in_seq = amath.array([[i % in_dim == j % in_dim for j in range(in_dim)]
                          for i in range(batch)],
                         dtype=int)

    if in_dim == out_dim:
        print("Testing generative learning")
        out_seq = in_seq
    else:
        print("Testing discriminative learning")
        out_seq = amath.array(
            [[i % out_dim == j % out_dim for j in range(out_dim)]
             for i in range(batch)],
            dtype=int)

    in_gen = SequenceGenerator(in_seq)
    out_gen = SequenceGenerator(out_seq)

    i = 0
    for i in xrange(max_repeat):
        if in_dim == out_dim:
            if generator:
                in_gen.reset()
                model.learn(in_gen)
            else:
                model._learn_sequence(in_seq)
        else:
            if generator:
                in_gen.reset()
                out_gen.reset()
                model.learn(in_gen, out_gen)
            else:
                model._learn_sequence(in_seq, out_seq)

        if i % 1000 == 0:
            if in_dim == out_dim:
                if generator:
                    in_gen.reset()
                    predictions = model.get_predictions(in_gen)
                else:
                    predictions = model.get_predictions(in_seq)
            else:
                if generator:
                    in_gen.reset()
                    predictions = model.get_predictions(in_gen)
                else:
                    predictions = model.get_predictions(in_seq)
            """
            diffs = predictions - out_seq
            SE = [np.dot(diff, diff) for diff in diffs]
            RMSE2 = np.sqrt(np.mean(SE))
            """

            rmse = RMSE(predictions, out_seq)

            print("%d\t%1.3f" % (i, rmse))
            if rmse < eps * out_dim:
                print("Successfully completed in %d iterations with RMSE: %f" %
                      (i + 1, rmse))
                break

    if in_dim == out_dim:
        LL = model.get_LL_sequence(in_seq)
    else:
        LL = model.get_LL_sequence(in_seq, out_seq)
    print("LL: %f" % amath.mean(LL))

    return i + 1
Ejemplo n.º 5
0
def test_complex_model(model, max_repeat=100):

    dim = [layer.get_input_dimension() for layer in model.layers]
    activations = model.activations

    random = amath.random.RandomState(0)
    mean = 1.0
    batch = np.prod(dim)
    d = 0.01

    seqs = list()
    for dimension, activation in zip(dim, activations):
        if activation == "linear":
            seq = random.uniform(low=mean - d,
                                 high=mean + d,
                                 size=(batch, dimension))
        elif activation == "sigmoid":
            seq = amath.array(
                [[i % dimension == j % dimension for j in range(dimension)]
                 for i in range(batch)],
                dtype=int)
            seq = amath.array(
                [[i % dimension == j % dimension for j in range(dimension)]
                 for i in range(batch)],
                dtype=float)
        seqs.append(seq)
    in_seq = list()

    i = 0
    for i in xrange(batch):
        pattern = [s[i] for s in seqs]
        in_seq.append(pattern)

    for i in xrange(max_repeat):
        model._learn_sequence(in_seq)

        predictions = model.get_predictions(in_seq)
        predictions = np.concatenate(predictions).reshape(
            (len(predictions), len(predictions[0])))

        rmse = 0
        start = 0
        j = 0
        for j, (dimension, sequence) in enumerate(zip(dim, seqs)):
            sub_prediction = predictions[:, j]
            sub_prediction = amath.concatenate(sub_prediction).reshape(
                (len(sub_prediction), len(sub_prediction[0])))
            start += dimension
            rmse += amath.sqrt(amath.mean((sub_prediction - sequence)**2))

        if i % 1000 == 0:
            print("rmse at %d:\t%1.4f" % (i, rmse))
        if rmse < d * sum(dim):
            print("Successfully completed in %d iterations with rmse: %f" %
                  (i + 1, rmse))
            break

    LL = model.get_LL_sequence(in_seq)
    print("LL: ", LL)

    return i + 1
Ejemplo n.º 6
0
def test_real_model(model, max_repeat=1000, generator=False):
    """
    minimal test with learning a constant with noise
    """

    in_dim = model.get_input_dimension()
    out_dim = model.get_target_dimension()
    batch = 3
    in_mean = 1.0
    out_mean = 2.0
    d = 0.001

    random = amath.random.RandomState(0)
    in_seq = random.uniform(low=in_mean - d,
                            high=in_mean + d,
                            size=(batch, in_dim))
    in_gen = Uniform(length=batch,
                     low=in_mean - d,
                     high=in_mean + d,
                     dim=in_dim)

    if in_dim == out_dim:
        print("Testing generative learning for a real model")
        out_seq = in_seq
    elif out_dim.__class__ is list:
        random = amath.random.RandomState(0)
        out_seq = list()
        for i in xrange(batch):
            patterns = [
                random.uniform(low=out_mean - d, high=out_mean + d, size=dim)
                for dim in out_dim
            ]
            out_seq.append(patterns)
        out_gens = [
            Uniform(length=batch, low=out_mean - d, high=out_mean + d, dim=dim)
            for dim in out_dim
        ]
        out_gen = ListGenerator(out_gens)
    else:
        print("Testing discriminative learning for a real model")
        random = amath.random.RandomState(0)
        out_seq = random.uniform(low=out_mean - d,
                                 high=out_mean + d,
                                 size=(batch, out_dim))
        out_gen = Uniform(length=batch,
                          low=out_mean - d,
                          high=out_mean + d,
                          dim=out_dim)

    print("Input dimension: %d" % in_dim)
    if out_dim.__class__ is list:
        print("Target dimension: " + str(out_dim))
    else:
        print("Target dimension: %d" % out_dim)

    i = 0
    for i in xrange(max_repeat):
        if in_dim == out_dim:
            if generator:
                # print("Predicting with input generator of length: %d"
                # % in_gen.limit)
                in_gen.reset()
                model.learn(in_gen)
                in_gen.reset()
                predictions = model.get_predictions(in_gen)
            else:
                # print("Predicting with input sequence")
                model._learn_sequence(in_seq)
                predictions = model.get_predictions(in_seq)
        else:
            if generator:
                # print("Predicting with input generator and target generator")
                in_gen.reset()
                out_gen.reset()
                model.learn(in_gen, out_gen)
            else:
                # print("Predicting with input sequence and target sequence")
                model._learn_sequence(in_seq, out_seq)
            predictions = model.get_predictions(in_seq)
        """
        diffs = predictions - out_seq
        SE = [amath.dot(diff, diff) for diff in diffs]
        rmse = amath.sqrt(amath.mean(SE))
        """
        if out_dim.__class__ is list:
            predictions = [amath.concatenate(pred) for pred in predictions]
            rmse = RMSE(predictions,
                        [amath.concatenate(pat) for pat in out_seq])
        else:
            rmse = RMSE(predictions, out_seq)

        if i % 1000 == 0:
            print("%d\t%1.4f" % (i, rmse))
        if rmse < d:
            print("Successfully completed in %d iterations with RMSE: %f" %
                  (i + 1, rmse))
            break

    op = getattr(model, "get_LL_sequence", None)
    if callable(op):
        if in_dim == out_dim:
            LL = model.get_LL_sequence(in_seq)
        else:
            LL = model.get_LL_sequence(in_seq, out_seq)
            if LL is not None:
                print("LL: %f" % amath.mean(LL))

    return i + 1