Example #1
0
    def init_kmean(self, init_data, niter=5):
        """ Init the model using kmean."""
        assert init_data.ndim == 1
        k = self.gm.k
        w = N.ones(k) / k

        # Init the internal state of EM
        self.cx = w * mean(init_data)
        self.cxx = w * mean(init_data**2)

        # w, mu and va init is the same that in the standard case
        (code, label)   = kmean(init_data[:, N.newaxis], \
                init_data[0:k, N.newaxis], iter = niter)
        mu = code.copy()
        va = N.zeros((k, 1))
        for i in range(k):
            va[i] = N.cov(init_data[N.where(label == i)], rowvar=0)

        self.gm.set_param(w, mu, va)
        # c* are the parameters which are computed at every step (ie
        # when a new frame is taken into account
        self.cw = self.gm.w
        self.cmu = self.gm.mu[:, 0]
        self.cva = self.gm.va[:, 0]

        # p* are the parameters used when computing gaussian densities
        # they are the same than c* in the online case
        # self.pw     = self.cw.copy()
        # self.pmu    = self.cmu.copy()
        # self.pva    = self.cva.copy()
        self.pw = self.cw
        self.pmu = self.cmu
        self.pva = self.cva
Example #2
0
    def init_kmean(self, data, niter=5):
        """ Init the model with kmean."""
        k = self.gm.k
        d = self.gm.d
        #init = data[0:k, :]

        # XXX: This is bogus initialization should do better (in kmean with CV)
        (code, label) = kmean(data, k, niter, minit='random')

        w = N.ones(k) / k
        mu = code.copy()
        if self.gm.mode == 'diag':
            va = N.zeros((k, d))
            for i in range(k):
                for j in range(d):
                    va[i, j] = N.cov(data[N.where(label == i), j], rowvar=0)
        elif self.gm.mode == 'full':
            va = N.zeros((k * d, d))
            for i in range(k):
                va[i*d:i*d+d, :] = \
                    N.cov(data[N.where(label==i)], rowvar = 0)
        else:
            raise GmmParamError("mode " + str(self.gm.mode) + \
                    " not recognized")

        self.gm.set_param(w, mu, va)

        self.isinit = True
Example #3
0
    def init_kmean(self, data, niter = 5):
        """ Init the model with kmean."""
        k = self.gm.k
        d = self.gm.d
        init = data[0:k, :]

        # XXX: This is bogus initialization should do better (in kmean with CV)
        (code, label)   = kmean(data, init, niter, minit = 'matrix')

        w   = N.ones(k) / k
        mu  = code.copy()
        if self.gm.mode == 'diag':
            va = N.zeros((k, d))
            for i in range(k):
                for j in range(d):
                    va[i, j] = N.cov(data[N.where(label==i), j], rowvar = 0)
        elif self.gm.mode == 'full':
            va  = N.zeros((k*d, d))
            for i in range(k):
                va[i*d:i*d+d, :] = \
                    N.cov(data[N.where(label==i)], rowvar = 0)
        else:
            raise GmmParamError("mode " + str(self.gm.mode) + \
                    " not recognized")

        self.gm.set_param(w, mu, va)

        self.isinit = True
    def init_kmean(self, init_data, niter = 5):
        """ Init the model using kmean."""
        assert init_data.ndim == 1
        k   = self.gm.k
        w   = N.ones(k) / k

        # Init the internal state of EM
        self.cx     = w * mean(init_data)
        self.cxx    = w * mean(init_data ** 2)

        # w, mu and va init is the same that in the standard case
        (code, label)   = kmean(init_data[:, N.newaxis], \
                init_data[0:k, N.newaxis], iter = niter)
        mu          = code.copy()
        va          = N.zeros((k, 1))
        for i in range(k):
            va[i] = N.cov(init_data[N.where(label==i)], rowvar = 0)

        self.gm.set_param(w, mu, va)
        # c* are the parameters which are computed at every step (ie
        # when a new frame is taken into account
        self.cw     = self.gm.w
        self.cmu    = self.gm.mu[:, 0]
        self.cva    = self.gm.va[:, 0]

        # p* are the parameters used when computing gaussian densities
        # they are the same than c* in the online case
        # self.pw     = self.cw.copy()
        # self.pmu    = self.cmu.copy()
        # self.pva    = self.cva.copy()
        self.pw     = self.cw
        self.pmu    = self.cmu
        self.pva    = self.cva
Example #5
0
    def init_kmean(self, init_data, niter=5):
        """ Init the model using kmean."""
        k = self.gm.k
        d = self.gm.d
        if self.gm.mode == 'diag':
            w = N.ones(k) / k

            # Init the internal state of EM
            self.cx = N.outer(w, mean(init_data, 0))
            self.cxx = N.outer(w, mean(init_data**2, 0))

            # w, mu and va init is the same that in the standard case
            (code, label) = kmean(init_data,
                                  init_data[0:k, :],
                                  iter=niter,
                                  minit='matrix')
            mu = code.copy()
            va = N.zeros((k, d))
            for i in range(k):
                for j in range(d):
                    va[i, j] = N.cov(init_data[N.where(label == i), j],
                                     rowvar=0)
        else:
            raise OnGmmParamError(
                """init_online not implemented for
                    mode %s yet""", self.gm.mode)

        self.gm.set_param(w, mu, va)
        # c* are the parameters which are computed at every step (ie
        # when a new frame is taken into account
        self.cw = self.gm.w
        self.cmu = self.gm.mu
        self.cva = self.gm.va

        # p* are the parameters used when computing gaussian densities
        # they are the same than c* in the online case
        # self.pw     = self.cw.copy()
        # self.pmu    = self.cmu.copy()
        # self.pva    = self.cva.copy()
        self.pw = self.cw
        self.pmu = self.cmu
        self.pva = self.cva
    def init_kmean(self, init_data, niter = 5):
        """ Init the model using kmean."""
        k   = self.gm.k
        d   = self.gm.d
        if self.gm.mode == 'diag':
            w  = N.ones(k) / k

            # Init the internal state of EM
            self.cx = N.outer(w, mean(init_data, 0))
            self.cxx = N.outer(w, mean(init_data ** 2, 0))

            # w, mu and va init is the same that in the standard case
            (code, label) = kmean(init_data, init_data[0:k, :], 
                    iter = niter, minit = 'matrix')
            mu = code.copy()
            va = N.zeros((k, d))
            for i in range(k):
                for j in range(d):
                    va[i, j] = N.cov(init_data[N.where(label==i), j], 
                            rowvar = 0)
        else:
            raise OnGmmParamError("""init_online not implemented for
                    mode %s yet""", self.gm.mode)

        self.gm.set_param(w, mu, va)
        # c* are the parameters which are computed at every step (ie
        # when a new frame is taken into account
        self.cw     = self.gm.w
        self.cmu    = self.gm.mu
        self.cva    = self.gm.va

        # p* are the parameters used when computing gaussian densities
        # they are the same than c* in the online case
        # self.pw     = self.cw.copy()
        # self.pmu    = self.cmu.copy()
        # self.pva    = self.cva.copy()
        self.pw     = self.cw
        self.pmu    = self.cmu
        self.pva    = self.cva