Exemplo n.º 1
0
 def test_GenerativeLinearDyBM(self):
     """ testing minimal consistency in learning a sequence
     """
     print("\nDyBMTestCase.testGenerativeGaussianDyBM")
     for delay in [1, 3]:
         for SGD in [AdaGrad, ADAM]:
             for GENERATOR in [True, False]:
                 model = LinearDyBM(self.in_dim,
                                    self.in_dim,
                                    delay,
                                    SGD=SGD())
                 model.set_learning_rate(self.rate)
                 i = tests.simple.test_real_model(model, self.max_repeat,
                                                  GENERATOR)
                 self.assertLess(i, self.max_repeat)
Exemplo n.º 2
0
    def test_vSGD(self):
        from collections import defaultdict
        from pydybm.base.sgd import vSGD
        from pydybm.time_series.dybm import LinearDyBM
        from pydybm.base.generator import Uniform

        gen = Uniform(length=1000, low=0, high=1, dim=1)

        dybm = LinearDyBM(in_dim=1,
                          delay=1,
                          decay_rates=[],
                          SGD=vSGD(hessian=defaultdict(lambda: 1)))
        dybm.learn(gen)

        self.assertEqual(float(dybm.variables['b']), 0.4932564368799297)
Exemplo n.º 3
0
 def test_DiscriminativeDyBMESN(self):
     print("\nDyBMTestCase.testDiscriminativeDyBMESN")
     esn = ESN(self.esn_dim, self.in_dim, self.out_dim)
     for delay in [1, 3]:
         for SGD in [AdaGrad]:
             for GENERATOR in [True]:
                 model = LinearDyBM(self.in_dim,
                                    self.out_dim,
                                    delay,
                                    SGD=SGD(),
                                    esn=esn)
                 model.set_learning_rate(self.rate)
                 esn.set_learning_rate(self.rate)
                 i = tests.simple.test_real_model(model, self.max_repeat,
                                                  GENERATOR)
                 self.assertLess(i, self.max_repeat)
Exemplo n.º 4
0
def learn_DyBM(trainX, trainY, testX, testY, DyBMmodel):

    plotFig = False
    RNN_dim = 10
    input_dim = 1
    max_epochs = 5
    saveResults = False
    SGD = RMSProp
    """
    Choose the DyBM model type: VAR (default), Gaussian, RNNGaussian

    Change accordinly for other DyBM models
    """
    print('*****************************************')
    print('Learning with DyBM model %s' % (DyBMmodel))
    print('*****************************************')

    trainX = np.reshape(trainX, (trainX.shape[0], input_dim))
    testX = np.reshape(testX, (testX.shape[0], input_dim))

    # default config of decay change accordinly
    decay = [0.5]

    for delay in [3]:

        print("learning with delay =", delay)

        if saveResults:
            filename = "RMSE_DyBM_" + \
                str(RNN_dim) + "delay_" + str(delay) + ".xml"
            out_file = open(filename, "wb")
            out_file.write('Decay' + '\t' + 'DyBM Train RMSE' + '\t' +
                           'DyBMTestRMSE' + '\n')

            print('Currently writing to file: %s' % (out_file.name))

        if DyBMmodel == "VAR":
            dybm = LinearDyBM(input_dim, delay=delay, decay_rates=[0.0])
            dybm.set_learning_rate(0.001)
            dybm.init_state

        elif DyBMmodel == "RNNGaussian":

            dybm = RNNGaussianDyBM(input_dim,
                                   input_dim,
                                   RNN_dim,
                                   0.3,
                                   0.1,
                                   delay,
                                   decay_rates=decay,
                                   leak=1.0,
                                   SGD=SGD())

            dybm.set_learning_rate(0.001)
            dybm.init_state

        elif DyBMmodel == "Gaussian":
            dybm = GaussianDyBM(input_dim, delay=delay, decay_rates=decay)
            dybm.set_learning_rate(0.001)
            dybm.init_state

        errs = list()
        train_errs = list()
        for epochs in range(max_epochs):

            # training
            start_time = time.time()
            dybm.init_state()
            result = dybm.learn(trainX, get_result=True)
            end_time = time.time() - start_time
            train_rmse = RMSE(trainY, result["prediction"])
            print('train error: %.5f' % (train_rmse))

            # testing
            # dybm.init_state()
            result2 = dybm.learn(testX, get_result=True)
            plt.plot(testY)
            plt.plot(result2["prediction"])
            plt.show()
            test_rmse = RMSE(testY, result2["prediction"])

            print('test error: %.5f ' % (test_rmse))

            errs.append(test_rmse)
            train_errs.append(train_rmse)

            error = np.array(errs)

            curr_mean = np.mean(error)

            print('delay = %f decay = %s curr_mean_error = %f' %
                  (delay, decay, curr_mean))

        print('Mean train error with DyBM: %.5f RMSE' %
              (np.mean(np.array(train_errs))))
        print('Mean test error with DyBM: %.5f RMSE' % (curr_mean))
        print('Per epoch time to learn: %.5f sec.' % (end_time))

    if saveResults:
        out_file.write('%s\t%s\t%s\n' %
                       (str(decay), str(train_rmse), str(test_rmse)))

        out_file.write('\n%s\t%s' %
                       ('Mean test error with DyBM', str(curr_mean)))
        out_file.close()
Exemplo n.º 5
0
    def test_UpdateState(self):
        """ testing fifo, eligibility trace, and update_state method in
        LinearDyBM
        """
        print("\n * testing fifo, eligibility trace, and update_state method"
              " in LinearDyBM \n")
        in_dim = 3
        delay = 3
        decay_rate = 0.5

        len_ts = 10

        print("testing wo_delay, single e_trace")
        model = LinearDyBM(in_dim,
                           delay=delay,
                           decay_rates=[decay_rate],
                           insert_to_etrace="wo_delay")
        random = np.random.RandomState(0)
        in_patterns = np.random.uniform(size=(len_ts, in_dim))
        fifo_test = np.zeros((delay - 1, in_dim))
        e_trace_test = np.zeros((1, in_dim))
        for i in xrange(len_ts):
            self.assertTrue(
                np.allclose(amath.to_numpy(model.fifo.to_array()), fifo_test))
            self.assertTrue(
                np.allclose(amath.to_numpy(model.e_trace), e_trace_test))
            model.learn_one_step(amath.array(in_patterns[i]))
            model._update_state(amath.array(in_patterns[i]))

            fifo_test[1:] = fifo_test[:-1]
            fifo_test[0] = in_patterns[i]
            e_trace_test = e_trace_test * decay_rate + in_patterns[i]

        print("testing w_delay, single e_trace")
        model = LinearDyBM(in_dim,
                           delay=delay,
                           decay_rates=[decay_rate],
                           insert_to_etrace="w_delay")
        random = np.random.RandomState(0)
        in_patterns = np.random.uniform(size=(len_ts, in_dim))
        fifo_test = np.zeros((delay - 1, in_dim))
        e_trace_test = np.zeros((1, in_dim))
        for i in xrange(len_ts):
            self.assertTrue(
                np.allclose(amath.to_numpy(model.fifo.to_array()), fifo_test))
            self.assertTrue(
                np.allclose(amath.to_numpy(model.e_trace), e_trace_test))
            model.learn_one_step(amath.array(in_patterns[i]))
            model._update_state(amath.array(in_patterns[i]))

            fifo_test[1:] = fifo_test[:-1]
            fifo_test[0] = in_patterns[i]
            if i < delay - 1:
                pass
            else:
                e_trace_test = e_trace_test * decay_rate \
                    + in_patterns[i - delay + 1]

        print("testing w_delay, two e_traces")
        model = LinearDyBM(in_dim,
                           delay=delay,
                           decay_rates=[decay_rate, decay_rate**2],
                           insert_to_etrace="w_delay")
        random = np.random.RandomState(0)
        in_patterns = np.random.uniform(size=(len_ts, in_dim))
        fifo_test = np.zeros((delay - 1, in_dim))
        e_trace_test = np.zeros((2, in_dim))
        for i in xrange(len_ts):
            self.assertTrue(
                np.allclose(amath.to_numpy(model.fifo.to_array()), fifo_test))
            self.assertTrue(
                np.allclose(amath.to_numpy(model.e_trace), e_trace_test))
            model.learn_one_step(amath.array(in_patterns[i]))
            model._update_state(amath.array(in_patterns[i]))

            fifo_test[1:] = fifo_test[:-1]
            fifo_test[0] = in_patterns[i]
            if i < delay - 1:
                pass
            else:
                e_trace_test[0] = e_trace_test[0] * decay_rate \
                    + in_patterns[i - delay + 1]
                e_trace_test[1] = e_trace_test[1] * (decay_rate**2) \
                    + in_patterns[i - delay + 1]

        print("testing w_delay, single e_trace, delay=1")
        delay = 1
        model = LinearDyBM(in_dim,
                           delay=delay,
                           decay_rates=[decay_rate],
                           insert_to_etrace="w_delay")
        random = np.random.RandomState(0)
        in_patterns = random.uniform(size=(len_ts, in_dim))
        e_trace_test = np.zeros((1, in_dim))
        for i in xrange(len_ts):
            self.assertTrue(
                np.allclose(amath.to_numpy(model.e_trace), e_trace_test))
            model.learn_one_step(amath.array(in_patterns[i]))
            model._update_state(amath.array(in_patterns[i]))

            if i < delay - 1:
                pass
            else:
                e_trace_test = e_trace_test * decay_rate \
                    + in_patterns[i - delay + 1]
Exemplo n.º 6
0
    def test_LearnGenerator(self):
        """ testing learning with generator
        """
        print("\nDyBMTestCase.testLearnGenerator")
        batch = 3
        in_mean = 1.0
        out_mean = 2.0
        d = 0.01
        delay = 1
        rates = [0.5, 0.8]
        L1 = 0.0
        L2 = 0.0

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

        model = LinearDyBM(self.in_dim,
                           self.in_dim,
                           delay=delay,
                           decay_rates=rates,
                           L1=L1,
                           L2=L2)
        model.set_learning_rate(0.1)
        model._learn_sequence(in_seq)

        model2 = LinearDyBM(self.in_dim,
                            self.in_dim,
                            delay=delay,
                            decay_rates=rates,
                            L1=L1,
                            L2=L2)
        model2.set_learning_rate(0.1)
        model2.learn(in_gen)

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

        random = amath.random.RandomState(0)
        out_gen = Uniform(length=batch,
                          low=out_mean - d,
                          high=out_mean + d,
                          dim=self.out_dim)
        out_seq = random.uniform(low=out_mean - d,
                                 high=out_mean + d,
                                 size=(batch, self.out_dim))

        self.assertEqual(model.variables.keys(), model2.variables.keys())
        for key in model.variables:
            self.assertTrue(
                (model.variables[key] == model2.variables[key]).all())

        model = LinearDyBM(self.in_dim,
                           self.out_dim,
                           delay=delay,
                           decay_rates=rates,
                           L1=L1,
                           L2=L2)
        model.set_learning_rate(0.1)
        model._learn_sequence(in_seq, out_seq)

        model2 = LinearDyBM(self.in_dim,
                            self.out_dim,
                            delay=delay,
                            decay_rates=rates,
                            L1=L1,
                            L2=L2)
        model2.set_learning_rate(0.1)
        model2.learn(in_gen, out_gen)

        self.assertEqual(model.variables.keys(), model2.variables.keys())
        for key in model.variables:
            self.assertTrue(
                (model.variables[key] == model2.variables[key]).all())