def test_mu(self):
     """ check mu given by get_conditional_negative_energy
     """
     in_patterns = [
         amath.array([1, 0, 0, 0]),
         amath.array([0, 1, 0, 0]),
         amath.array([0, 0, 1, 0])
     ]
     self.model._update_state(in_patterns[2])
     self.model._update_state(in_patterns[1])
     self.model._update_state(in_patterns[0])
     fifo_array = self.model.fifo.to_array()
     self.assertTrue(amath.allclose(fifo_array[0], in_patterns[0]))
     self.assertTrue(amath.allclose(fifo_array[1], in_patterns[1]))
     self.assertTrue(amath.allclose(fifo_array[2], in_patterns[2]))
     mu_test = deepcopy(self.model.variables["b"])
     u_tilde = self.model._get_u_tilde()
     # naive implementation of sigmoid
     sig_u_tilde = sigmoid(u_tilde)
     for d in xrange(self.order):
         mu_test += self.model.variables["W"][d, d, :]
     mu_test += sig_u_tilde.dot(self.model.variables["V"])
     self.assertTrue(
         amath.allclose(self.model._get_conditional_negative_energy(),
                        mu_test))
     pass
Ejemplo n.º 2
0
    def testFifo(self):
        """
        testing fifo and _update_state method in VectorRegressionWithVariance
        """
        print("\n * testing fifo and update_state method "
              "in VectorRegressionWithVariance \n")
        in_dim = 3
        order = 3

        len_ts = 10

        model = VectorRegressionWithVariance(in_dim, in_dim, order)
        random = amath.random.RandomState(0)
        in_patterns = amath.random.uniform(size=(len_ts, in_dim))
        fifo_test = amath.zeros((order, in_dim))
        for i in xrange(len_ts):
            self.assertTrue(amath.allclose(model.fifo.to_array(), fifo_test))
            model.learn_one_step(in_patterns[i])
            popped_in_pattern = model._update_state(in_patterns[i])
            if i < order:
                self.assertTrue(
                    amath.allclose(popped_in_pattern, amath.zeros(in_dim)))
            else:
                self.assertTrue(
                    amath.allclose(popped_in_pattern, in_patterns[i - order]))
            fifo_test[1:] = fifo_test[:-1]
            fifo_test[0] = in_patterns[i]
Ejemplo n.º 3
0
    def testFifo(self):
        """
        testing fifo and _update_state method in MultiTargetVectorRegression
        """
        print("\n * testing fifo and update_state method "
              "in MultiTargetVectorRegression \n")
        in_dim = 3
        out_dims = [2, 4]
        SGDs = [AdaGrad(), AdaGrad()]
        order = 3

        len_ts = 10

        model = MultiTargetVectorRegression(in_dim, out_dims, SGDs, order)
        random = amath.random.RandomState(0)
        in_patterns = amath.random.uniform(size=(len_ts, in_dim))
        out_pattern_0 = amath.random.uniform(size=(len_ts, out_dims[0]))
        out_pattern_1 = amath.random.uniform(size=(len_ts, out_dims[1]))
        fifo_test = amath.zeros((order, in_dim))
        for i in xrange(len_ts):
            self.assertTrue(
                amath.allclose(model.layers[0].fifo.to_array(), fifo_test))
            model.learn_one_step([out_pattern_0[i], out_pattern_1[i]])
            popped_in_pattern = model._update_state(in_patterns[i])
            if i < order:
                self.assertTrue(
                    amath.allclose(popped_in_pattern, amath.zeros(in_dim)))
            else:
                self.assertTrue(
                    amath.allclose(popped_in_pattern, in_patterns[i - order]))
            fifo_test[1:] = fifo_test[:-1]
            fifo_test[0] = in_patterns[i]
Ejemplo n.º 4
0
    def test_update_state(self):
        print("""
        ---------------------------------
            test updating internal states
        ---------------------------------
        """)
        anc_test = amath.random.uniform(self.low, self.high,
                                        (self.n_anc, self.dim))
        model = FunctionalDyBM(
            self.dim, anc_test, self.delay, self.decay_rates)
        loc = amath.random.uniform(self.low, self.high, (self.n_obs, self.dim))

        for i in range(10):
            pattern = amath.zeros(self.n_obs)
            pattern[i % self.n_obs] = 1
            fifo_old = model.fifo[:-1]
            fifo_last = model.fifo[-1]
            e_trace_old = model.e_trace

            model._update_state(pattern, loc)

            # check fifo update
            self.assertTrue(amath.allclose(model.fifo[1:], fifo_old))

            # check e_trace update
            e_trace_new = amath.zeros((self.n_elg, self.n_anc)) + fifo_last
            for k in xrange(e_trace_new.shape[0]):
                e_trace_new[k, :] = e_trace_new[k, :] \
                    + self.decay_rates[k] * e_trace_old[k, :]
            self.assertTrue(amath.allclose(model.e_trace, e_trace_new))

            # check mu_anc update
            self.assertTrue(amath.allclose(model.mu, amath.zeros(self.n_anc)))

        print("mu(P) = {}".format(model.mu))
        return 0
Ejemplo n.º 5
0
    def test_init(self):
        print("""
        ---------------------------------------------
            test initialization of NeuralFieldDyBM
        ---------------------------------------------
        """)
        anc_test = amath.random.uniform(self.low, self.high,
                                        (self.n_anc, self.dim))

        model = FunctionalDyBM(
            self.dim, anc_test, self.delay, self.decay_rates)

        # check anchors
        amath.random.seed(self.rand_seed)
        self.assertTrue(amath.allclose(model.anc_basis, anc_test))

        # check the shapes
        self.assertTrue(model.variables["W"].shape ==
                        (self.delay - 1, self.n_anc, self.n_anc))
        self.assertTrue(model.variables["V"].shape ==
                        (self.n_elg, self.n_anc, self.n_anc))
        self.assertTrue(model.variables["b"].shape == (self.n_anc,))

        # check kernel
        X1 = amath.random.randn(10, self.dim)
        X2 = amath.random.randn(10, self.dim)
        rbf = amath.kernel_metrics['rbf'](X1, X2, gamma=1.0)
        self.assertTrue(amath.allclose(model.ker(X1, X2), rbf))

        # check anchor points
        self.assertTrue(model.anc_basis.shape == (self.n_anc, self.dim))

        # check fifo and e_trace
        self.assertTrue(model.fifo.shape == (self.delay - 1, self.n_anc))
        self.assertTrue(model.e_trace.shape == (self.n_elg, self.n_anc))
        return 0
Ejemplo n.º 6
0
 def test_u_tilde(self):
     """ check _get_u_tilde
     """
     in_patterns = [amath.array([1, 0, 0, 0]),
                    amath.array([0, 1, 0, 0]),
                    amath.array([0, 0, 1, 0])]
     self.model._update_state(in_patterns[2])
     self.model._update_state(in_patterns[1])
     self.model._update_state(in_patterns[0])
     u_tilde_test = amath.zeros(self.dim_hidden)
     for d in xrange(self.order):
         u_tilde_test += self.model.variables["U"][d, d, :]
     u_tilde_test += self.model.variables["b_h"]
     self.assertTrue(amath.allclose(u_tilde_test, self.model._get_u_tilde()))
     pass