Exemplo n.º 1
0
    def initialize(self,
                   datas,
                   inputs=None,
                   masks=None,
                   tags=None,
                   verbose=0,
                   num_init_iters=50,
                   discrete_state_init_method="random",
                   num_init_restarts=1):
        # First initialize the observation model
        self.emissions.initialize(datas, inputs, masks, tags)

        # Get the initialized variational mean for the data
        xs = [
            self.emissions.invert(data, input, mask, tag)
            for data, input, mask, tag in zip(datas, inputs, masks, tags)
        ]
        xmasks = [np.ones_like(x, dtype=bool) for x in xs]

        # Number of times to run the arhmm initialization (we'll use the one with the highest log probability as the initialization)
        pbar = ssm_pbar(num_init_restarts, verbose,
                        "ARHMM Initialization restarts", [''])

        #Loop through initialization restarts
        best_lp = -np.inf
        for i in pbar:  #range(num_init_restarts):

            # Now run a few iterations of EM on a ARHMM with the variational mean
            if verbose > 0:
                print(
                    "Initializing with an ARHMM using {} steps of EM.".format(
                        num_init_iters))

            arhmm = hmm.HMM(self.K,
                            self.D,
                            M=self.M,
                            init_state_distn=copy.deepcopy(
                                self.init_state_distn),
                            transitions=copy.deepcopy(self.transitions),
                            observations=copy.deepcopy(self.dynamics))

            arhmm.fit(xs,
                      inputs=inputs,
                      masks=xmasks,
                      tags=tags,
                      verbose=verbose,
                      method="em",
                      num_iters=num_init_iters,
                      init_method=discrete_state_init_method)

            #Keep track of the arhmm that led to the highest log probability
            current_lp = arhmm.log_probability(xs)
            if current_lp > best_lp:
                best_lp = copy.deepcopy(current_lp)
                best_arhmm = copy.deepcopy(arhmm)

        self.init_state_distn = copy.deepcopy(best_arhmm.init_state_distn)
        self.transitions = copy.deepcopy(best_arhmm.transitions)
        self.dynamics = copy.deepcopy(best_arhmm.observations)
Exemplo n.º 2
0
    def initialize(self,
                   datas,
                   inputs=None,
                   masks=None,
                   tags=None,
                   num_optimizer_iters=1000,
                   num_em_iters=25,
                   betas=None,
                   accum_log_sigmasq=None):

        # First initialize the observation model
        self.base_model = Accumulation(
            self.K,
            self.D,
            M=self.M,
            transitions=self.transitions_label,
            transition_kwargs=self.transition_kwargs,
            observation_kwargs=self.dynamics_kwargs)
        self.base_model.observations.Vs = self.dynamics.Vs
        self.base_model.observations.As = self.dynamics.As
        self.base_model.observations.Sigmas = self.dynamics.Sigmas
        self.emissions.initialize(self.base_model, datas, inputs, masks, tags)

        if self.emissions_label == "gaussian":
            # Get the initialized variational mean for the data
            xs = [
                self.emissions.invert(data, input, mask, tag)
                for data, input, mask, tag in zip(datas, inputs, masks, tags)
            ]
            xmasks = [np.ones_like(x, dtype=bool) for x in xs]

            # Now run a few iterations of EM on a ARHMM with the variational mean
            print("Initializing with an ARHMM using {} steps of EM.".format(
                num_em_iters))
            arhmm = hmm.HMM(self.K,
                            self.D,
                            M=self.M,
                            init_state_distn=copy.deepcopy(
                                self.init_state_distn),
                            transitions=copy.deepcopy(self.transitions),
                            observations=copy.deepcopy(self.dynamics))

            arhmm.fit(xs,
                      inputs=inputs,
                      masks=xmasks,
                      tags=tags,
                      method="em",
                      num_em_iters=num_em_iters)

            self.init_state_distn = copy.deepcopy(arhmm.init_state_distn)
            self.transitions = copy.deepcopy(arhmm.transitions)
            self.dynamics = copy.deepcopy(arhmm.observations)
Exemplo n.º 3
0
    def initialize(self,
                   datas,
                   inputs=None,
                   masks=None,
                   tags=None,
                   num_em_iters=25,
                   choices=None):

        C, x0 = self.emissions.initialize(datas, inputs, masks, tags, choices)

        self.dynamics.params = (self.dynamics.params[0],
                                self.dynamics.params[1], x0)

        # Get the initialized variational mean for the data
        xs = [
            self.emissions.invert(data, input, mask, tag)
            for data, input, mask, tag in zip(datas, inputs, masks, tags)
        ]
        xmasks = [np.ones_like(x, dtype=bool) for x in xs]

        # Now run a few iterations of EM on a ARHMM with the variational mean
        print("Initializing with an ARHMM using {} steps of EM.".format(
            num_em_iters))
        arhmm = hmm.HMM(self.K,
                        self.D,
                        M=self.M,
                        init_state_distn=copy.deepcopy(self.init_state_distn),
                        transitions=copy.deepcopy(self.transitions),
                        observations=copy.deepcopy(self.dynamics))

        arhmm.fit(xs,
                  inputs=inputs,
                  masks=xmasks,
                  tags=tags,
                  method="em",
                  num_em_iters=num_em_iters)

        self.init_state_distn = copy.deepcopy(arhmm.init_state_distn)
        self.transitions = copy.deepcopy(arhmm.transitions)
        self.dynamics = copy.deepcopy(arhmm.observations)
Exemplo n.º 4
0
    ys,
    inputs=us,
    method="laplace_em",
    variational_posterior="structured_meanfield",
    variational_posterior_kwargs={"initial_variance": 1e-4},
    alpha=0.0,
    num_iters=0,
    initialize=False)
q_lem_init = copy.deepcopy(q_lem)
test_ramp_init = copy.deepcopy(test_ramp)

from ssm import hmm
arhmm = hmm.HMM(K,
                1,
                M,
                init_state_distn=copy.deepcopy(
                    test_ramp_init.init_state_distn),
                transitions=copy.deepcopy(test_ramp_init.transitions),
                observations=copy.deepcopy(test_ramp_init.dynamics))

arhmm.fit(q_lem_init.mean_continuous_states,
          inputs=us,
          method="em",
          num_iters=25)
test_ramp_init.dynamics.params = arhmm.observations.params
test_ramp.dynamics.params = arhmm.observations.params

test_ramp = copy.deepcopy(test_ramp_init)
q_lem = copy.deepcopy(q_lem_init)
q_elbos, q_lem = test_ramp.fit(ys,
                               inputs=us,