Ejemplo n.º 1
0
    def testOneDimensional(self):
        eps = 1e-2
        period = 10
        for Wave in [NoisySin, NoisySawtooth]:
            for std in [0.0, 0.1, 1.0]:
                print("NoisyWaveTestCase.testOneDimensional " + str(Wave) +
                      " with std: " + str(std))
                length = period * 10
                dim = 1
                phase = None
                reverse = False

                if Wave == NoisySin:
                    angles = 2 * amath.pi * amath.arange(length) / period
                    target = amath.sin(angles)
                elif Wave == NoisySawtooth:
                    r = 1. * amath.arange(length) / period
                    target = r % 1
                else:
                    print(Wave)

                wave = Wave(length, period, std, dim, phase, reverse)

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

                diff = target - sequence

                rmse = amath.sqrt(amath.dot(diff, diff) / len(diff))

                print("RMSE: %f" % rmse)

                self.assertLessEqual(amath.abs(rmse - std), std * eps)
Ejemplo n.º 2
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.º 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_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