Beispiel #1
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]
Beispiel #2
0
    def __init__(self, dim, rank, lag, L1=0.1, L2=0.1, data=None,
                 SGD=AdaGrad(), initFactor=0.1):

        self.M = dim
        self.K = rank
        self.D = lag
        self.n_var = self.M * self.K + self.D * self.K
        self.data = None
        self.t = 0
        if data is not None:
            self.data = data
            self.T = data.T
            assert self.data.D == self.D, "lag of data not the same with DyDPP"
            assert self.data.M == self.M, "number of choices not the same"

        self.variables = dict()

        # INITIALIZATION WITH RANDOM VECTORS
        np.random.seed(0)
        self.variables["B"] = np.random.uniform(low=-0.1, high=0.1,
                                                size=(self.M, self.K))
        self.variables["W"] = np.zeros((self.K, self.D))

        # This is a vector for regulazing rows of V(t) and B
        self.regularizer = np.ones(self.M, dtype=float)

        self.init_state()

        self.SGD = SGD.set_shape(self.variables)
        self.L2 = dict()
        self.L2["B"] = 0
        self.L2["W"] = L2

        self.maxGamma = 1e-3
        self.minGamma = 1e-9
Beispiel #3
0
 def setUp(self):
     self.in_dim = 4
     self.out_dim = 2
     self.dim_hidden = 1
     self.order = 3
     self.model = VectorRegressionWithHidden(in_dim=self.in_dim,
                                             out_dim=self.out_dim,
                                             dim_hidden=self.dim_hidden,
                                             order=self.order, SGD=AdaGrad(),
                                             L1=0., L2=0., use_bias=True,
                                             sigma=0.1)
     pass
Beispiel #4
0
    def learn_dataset_backtrack(self, trainset, isResetB=False, minIteration=5,
                                maxIteration=10, eps=-1.0e-2, SGD=AdaGrad(),
                                alpha=0.1, isRegulazingB=False):

        variables = deepcopy(self.variables)

        bestLL = self.get_average_LL(trainset)
        print("bestLL", bestLL)

        nIters = 0
        for nIters in range(maxIteration):
            delta = dict()
            for data in trainset:
                self.set_data(data)
                for t in range(data.T):
                    gradient = self.get_gradient(t, applyL2=False, alpha=alpha,
                                                 isRegulazingB=False)
                    for key in gradient:
                        if key in delta:
                            delta[key] = delta[key] + gradient[key]
                        else:
                            delta[key] = gradient[key]

            if isRegulazingB:
                delta["B"] = delta["B"] - alpha * np.diag(self.regularizer).dot(variables["B"])
            gradient["W"] = gradient["W"] - self.L2["W"] * self.variables["W"]

            gamma = self.maxGamma
            while gamma > self.minGamma:
                self.variables["B"] = variables["B"] + gamma * delta["B"]
                self.variables["W"] = variables["W"] + gamma * delta["W"]
                LL = self.get_average_LL(trainset)
                print("nIters:", nIters,
                      "gamma:", gamma,
                      "LL:", LL,
                      "bestLL:", bestLL)
                if LL > bestLL:
                    print("breaking while with", LL, bestLL)
                    variables = deepcopy(self.variables)
                    bestLL = LL

                    if gamma == self.maxGamma:
                        self.maxGamma *= 10
                    break
                gamma = gamma / 10.

            print(nIters, bestLL)
            if gamma <= self.minGamma:
                print("breaking for with", gamma, self.minGamma)
                break

        return bestLL
Beispiel #5
0
def experiment(period,
               std,
               delay,
               decay,
               Nh,
               repeat,
               bidirectional,
               sigma=0.01):
    """
    A run of experiment

    Parameters
    ----------
    period : int
        period of the wave
    std : float
        standard deviation of noise
    delay : int
        delay
    decay : list
        list of decay rates
    Nh : int
        number of hidden units
    repeat : int
        number of iterations of training
    bidirectional : boolean
        whether to train bidirectionally
    sigma : float
        std of random initialization
        0.01 is recommended in
        https://www.cs.toronto.edu/~hinton/absps/guideTR.pdf
    """
    """
    Prepare data generators
    """

    dim = 1  # dimension of the wave
    phase = amath.zeros(dim)  # initial phase of the wave

    # forward sequence
    wave = NoisySawtooth(0, period, std, dim, phase, False)
    wave.reset(seed=0)

    # backward sequence
    revwave = NoisySawtooth(0, period, std, dim, phase, True)
    revwave.reset(seed=1)
    """
    Prepare a Gaussian Bernoulli DyBM
    """

    Nv = dim  # number of visible units

    sgd = AdaGrad()
    dybm = GaussianBernoulliDyBM([delay, delay], [decay, decay], [Nv, Nh],
                                 [sgd, deepcopy(sgd)],
                                 sigma=sigma,
                                 insert_to_etrace="w_delay")
    dybm.layers[0].layers[1].SGD.set_learning_rate(0)
    dybm.layers[1].layers[1].SGD.set_learning_rate(0)
    """
    Learn
    """
    error = list()  # list of numpy array
    bi_end = 0.5
    bi_factor = 2
    for i in range(repeat):
        # update internal states by reading forward sequence
        wave.add_length(period)
        dybm.get_predictions(wave)

        if bidirectional and i % (bi_factor + 1) == 0 and bi_factor > 0 \
           and i < repeat * bi_end:
            # make a time-reversed DyBM
            dybm._time_reversal()

            # update internal states by reading backward sequence
            revwave.add_length(period)
            dybm.get_predictions(revwave)

            # learn backward sequence for one period
            revwave.add_length(period)
            dybm.learn(revwave, get_result=False)

            # make a non time-reversed DyBM
            dybm._time_reversal()
        else:
            # update internal states by reading forward sequence
            wave.add_length(period)
            dybm.get_predictions(wave)

            # learn forward sequence
            wave.add_length(period)
            result = dybm.learn(wave, get_result=True)

            if i % (bi_factor + 1) == bi_factor:
                rmse = RMSE(result["actual"], result["prediction"])
                rmse = amath.to_numpy(rmse)
                error.append(rmse)

    return error, dybm, wave
Beispiel #6
0
    def initialize_B_backtrack(self, trainset, isResetB=False, minIteration=5,
                               maxIteration=10, eps=-1.0e-2, SGD=AdaGrad(),
                               alpha=0.1, isRegulazingB=False):
        """
        Parameters
        ----------
        trainset: list of DataDyDPP
            list of time-series selection vectors for training
        validset: list of DataDyDPP
            list of time-series selection vectors for validation
        isResetB: if True, self.variables["B"] is reset with samples from
        uniform distribution on [-0.1,0.1] before running initialization
        """

        print("Initializing B with backtrack")
        oldW = self.initialize_W(isOne=False)  # set W=0 and store oldW

        variables = dict()
        if isResetB:
            self.variables["B"] = np.random.uniform(low=-0.1, high=0.1,
                                                    size=(self.M, self.K))
        variables["B"] = np.copy(self.variables["B"])

        if trainset is None:
            trainset = [self.data]

        if isRegulazingB:
            # Compute regularization of B according to Gartrell et al. 2017
            itemFreq = dict()
            for i in range(self.M):
                itemFreq[i] = 1.0  # to avoid division by zero
            for data in trainset:
                for t in range(data.T):
                    selectionT = data.get_selection(t)
                    for i in selectionT:
                        itemFreq[i] += 1.0
            # Set the value of self.regularizer during training of B
            self.regularizer = np.array([1.0/(1.0+itemFreq[i])
                                         for i in range(self.M)])
            del itemFreq

        bestLL = self.get_average_LL(trainset)
        print("bestLL", bestLL)

        nIters = 0
        for nIters in range(maxIteration):
            delta = dict()
            delta["B"] = np.zeros(variables["B"].shape)
            for data in trainset:
                delta["B"] = delta["B"] - data.T * variables["B"].dot(np.linalg.inv(np.eye(self.K) + variables["B"].T.dot(variables["B"])))
                for t in range(data.T):
                    selectionT = data.get_selection(t)
                    if len(selectionT) <= 0:
                        continue
                    delta["B"][selectionT, :] = delta["B"][selectionT, :] + npLinalgInv(variables["B"][selectionT, :]).T

            if isRegulazingB:
                delta["B"] = delta["B"] - alpha * np.diag(self.regularizer).dot(variables["B"])

            gamma = self.maxGamma
            while gamma > self.minGamma:
                self.variables["B"] = variables["B"] + gamma * delta["B"]
                LL = self.get_average_LL(trainset)
                print("nIters:", nIters,
                      "gamma:", gamma,
                      "LL:", LL,
                      "bestLL:", bestLL)
                if LL > bestLL:
                    print("breaking while with", LL, bestLL)
                    variables["B"] = deepcopy(self.variables["B"])
                    bestLL = LL

                    if gamma == self.maxGamma:
                        self.maxGamma *= 10
                    if gamma < self.maxGamma / 10.:
                        self.maxGamma = gamma * 10
                    break
                gamma = gamma / 10.

            print(nIters, bestLL)
            if gamma <= self.minGamma:
                print("breaking for with", gamma, self.minGamma)
                break

        self.variables["B"] = variables["B"]

        self.initialize_W(initW=oldW)  # restore oldW

        return bestLL