Ejemplo n.º 1
0
 def _estimate_trans(self):
     super(HMMProbBasedEstimator, self)._estimate_trans()
     self._trans[:] = 0.0
     for i in xrange(self._probs.shape[0]): # tracks
         for j in xrange(1, self._probs.shape[1], 1): # frames
             prob0 = self._probs[i, j-1, :]
             prob1 = self._probs[i, j, :]
             condprob = np.matrix(prob0).T*np.matrix(prob1)
             self._trans += condprob
     self._trans = normalize(self._trans, axis=1, eps=0.0)
Ejemplo n.º 2
0
    def _estimate_startprob(self):
        super(HMMTransitionCountEstimator, self)._estimate_startprob()
        self.startprob[:] = 0.0

        index_of = lambda label: np.where(self.states==label)[0][0]
        counts =  np.bincount(self._tracks[:, 0].flatten())
        for label in np.unique(self._tracks[:, 0]):
            self.startprob[index_of(label)] = counts[label]

        self._startprob = normalize(self._startprob, eps=0.0)
        return self._startprob
Ejemplo n.º 3
0
    def _estimate_emis(self):
        """Estimate the emission matrix by calculating the mean prediction
        probabiliy for each class. The result is normalized row wise.
        """
        mprobs = np.zeros((self.nstates, self.nstates))

        probs = self._probs.reshape((-1, self.nstates))
        tracks = self._tracks.flatten()

        for i in xrange(self.nstates):
            mprobs[i, :] = probs[tracks == i].mean(axis=0)

        self._emis = normalize(mprobs, axis=1)
Ejemplo n.º 4
0
    def _set_startprob(self, startprob):
        if startprob is None:
            startprob = np.tile(1.0 / self.n_components, self.n_components)
        else:
            startprob = np.asarray(startprob, dtype=np.float)

        if not np.alltrue(startprob):
            startprob = normalize(startprob, eps=EPS)

        if len(startprob) != self.n_components:
            raise ValueError('startprob must have length n_components')
        if not np.allclose(np.sum(startprob), 1.0):
            raise ValueError('startprob must sum to 1.0')

        self._log_startprob = np.log(np.asarray(startprob).copy())
Ejemplo n.º 5
0
Archivo: hmm.py Proyecto: raj347/cecog
    def _set_startprob(self, startprob):
        if startprob is None:
            startprob = np.tile(1.0 / self.n_components, self.n_components)
        else:
            startprob = np.asarray(startprob, dtype=np.float)

        if not np.alltrue(startprob):
            startprob = normalize(startprob, eps=EPS)

        if len(startprob) != self.n_components:
            raise ValueError('startprob must have length n_components')
        if not np.allclose(np.sum(startprob), 1.0):
            raise ValueError('startprob must sum to 1.0')

        self._log_startprob = np.log(np.asarray(startprob).copy())
Ejemplo n.º 6
0
    def _estimate_trans(self):
        """Estimates the transition probaility by counting."""
        super(HMMTransitionCountEstimator, self)._estimate_trans()
        self._trans[:] = 0.0
        index_of = lambda label: np.where(self.states==label)[0][0]
        _tracks = self._tracks.flatten()

        for i, label in enumerate(_tracks):
            for state in self.states:
                try:
                    if (_tracks[i+1] == state):
                        self._trans[index_of(label), index_of(state)] += 1.0
                        continue
                except IndexError:
                    pass

        self._trans =  normalize(self._trans, axis=1, eps=0.0)
        return self._trans
Ejemplo n.º 7
0
Archivo: hmm.py Proyecto: raj347/cecog
    def _set_transmat(self, transmat):

        if transmat is None:
            transmat = np.tile(1.0 / self.n_components,
                               (self.n_components, self.n_components))

        if not np.alltrue(transmat):
            transmat = normalize(transmat, axis=1, eps=EPS)

        if (np.asarray(transmat).shape \
            != (self.n_components, self.n_components)):
            raise ValueError('transmat must have shape '
                             '(n_components, n_components)')

        if not np.all(np.allclose(np.sum(transmat, axis=1), 1.0)):
            raise ValueError('Rows of transmat must sum to 1.0')

        self._log_transmat = np.log(np.asarray(transmat).copy())
        underflow_idx = np.isnan(self._log_transmat)
        self._log_transmat[underflow_idx] = -np.inf
Ejemplo n.º 8
0
    def _set_transmat(self, transmat):

        if transmat is None:
            transmat = np.tile(1.0 / self.n_components,
                               (self.n_components, self.n_components))

        if not np.alltrue(transmat):
            transmat = normalize(transmat, axis=1, eps=EPS)

        if (np.asarray(transmat).shape \
            != (self.n_components, self.n_components)):
            raise ValueError('transmat must have shape '
                             '(n_components, n_components)')


        if not np.all(np.allclose(np.sum(transmat, axis=1), 1.0)):
            raise ValueError('Rows of transmat must sum to 1.0')

        self._log_transmat = np.log(np.asarray(transmat).copy())
        underflow_idx = np.isnan(self._log_transmat)
        self._log_transmat[underflow_idx] = -np.inf
Ejemplo n.º 9
0
 def _estimate_emis(self):
     """This emission is an adhoc-assumtiopn!"""
     self._emis = normalize(np.eye(self.nstates) + self._emission_noise,
                            eps=0.0)
Ejemplo n.º 10
0
 def _estimate_emis(self):
     """This emission is an adhoc-assumtiopn!"""
     self._emis = normalize(np.eye(self.nstates) + self._emission_noise,
                            eps=0.0)
Ejemplo n.º 11
0
 def _estimate_startprob(self):
     super(HMMProbBasedEstimator, self)._estimate_startprob()
     self._startprob[:] = 0.0
     self._startprob = self._probs[:, 0, :].sum(axis=0)
     self._startprob = normalize(self._startprob, eps=0.0)
Ejemplo n.º 12
0
 def constrain(self, hmmc):
     self._trans = normalize(hmmc.trans*self._trans, axis=1, eps=0.0)
     self._startprob = normalize(hmmc.start*self._startprob, eps=0.0)
     self._emis = normalize(hmmc.emis*self._emis, axis=1, eps=0.0)
Ejemplo n.º 13
0
 def _estimate_emis(self):
     self._emis = np.eye(self.nstates) + self._emission_noise
     self._emis = normalize(self._emis, axis=1, eps=0.0)