コード例 #1
0
    def run(self, iterations):
        ''' Run the Gibbs sampler. '''
        self.all_U = numpy.zeros((iterations, self.I, self.K))
        self.all_V = numpy.zeros((iterations, self.J, self.K))
        self.all_tau = numpy.zeros(iterations)
        self.all_lambdak = numpy.zeros((iterations, self.K))
        self.all_times = []  # to plot performance against time

        self.all_performances = {}  # for plotting convergence of metrics
        for metric in ALL_METRICS:
            self.all_performances[metric] = []

        time_start = time.time()
        for it in range(iterations):
            # Update lambdak
            if self.ARD:
                for k in range(self.K):
                    self.lambdak[k] = gamma_draw(self.alphak_s(k),
                                                 self.betak_s(k))

            # Update U
            for k in range(0, self.K):
                tauUk = self.tauU(k)
                muUk = self.muU(tauUk, k)
                self.U[:, k] = TN_vector_draw(muUk, tauUk)

            # Update V
            for k in range(0, self.K):
                tauVk = self.tauV(k)
                muVk = self.muV(tauVk, k)
                self.V[:, k] = TN_vector_draw(muVk, tauVk)

            # Update tau
            self.tau = gamma_draw(self.alpha_s(), self.beta_s())

            # Store draws
            self.all_U[it], self.all_V[it], self.all_tau[it] = numpy.copy(
                self.U), numpy.copy(self.V), self.tau
            if self.ARD:
                self.all_lambdak[it] = numpy.copy(self.lambdak)

            # Store and print performances
            perf = self.predict_while_running()
            for metric in ALL_METRICS:
                self.all_performances[metric].append(perf[metric])

            print "Iteration %s. MSE: %s. R^2: %s. Rp: %s." % (
                it + 1, perf['MSE'], perf['R^2'], perf['Rp'])

            # Store time taken for iteration
            time_iteration = time.time()
            self.all_times.append(time_iteration - time_start)
コード例 #2
0
    def initialise(self, init_UV='random'):
        ''' Initialise U, V, tau, and lambda (if ARD). '''
        assert init_UV in OPTIONS_INIT_UV, "Unknown initialisation option: %s. Should be in %s." % (
            init_UV, OPTIONS_INIT_UV)

        self.U = numpy.zeros((self.I, self.K))
        self.V = numpy.zeros((self.J, self.K))
        self.lambdak = numpy.zeros(self.K)

        # Initialise lambdak
        if self.ARD:
            for k in range(self.K):
                self.lambdak[k] = self.alpha0 / self.beta0

        # Initialise U, V
        for i, k in itertools.product(range(self.I), range(self.K)):
            hyperparam = self.lambdak[k] if self.ARD else self.lambdaU[i, k]
            self.U[i, k] = exponential_draw(
                hyperparam) if init_UV == 'random' else 1.0 / hyperparam
        for j, k in itertools.product(range(self.J), range(self.K)):
            hyperparam = self.lambdak[k] if self.ARD else self.lambdaV[j, k]
            self.V[j, k] = exponential_draw(
                hyperparam) if init_UV == 'random' else 1.0 / hyperparam

        # Initialise tau
        self.tau = gamma_draw(self.alpha_s(), self.beta_s())
コード例 #3
0
ファイル: updates.py プロジェクト: changchunli/BMF_Priors
def update_hU_poisson_gamma_hierarchical(ap, bp, a, U):
    """ Update hU (vector) for Poisson + Gamma + hierarchical model. """
    I, K = U.shape
    hU = numpy.zeros(I)
    for i in range(I):
        (a_s, b_s) = gamma_hierarchical_hUi_a_b(ap=ap, bp=bp, a=a, Ui=U[i,:])
        hU[i] = gamma_draw(alpha=a_s, beta=b_s)
    return hU
コード例 #4
0
ファイル: updates.py プロジェクト: changchunli/BMF_Priors
def update_U_poisson_gamma(a, b, M, V, Z):
    """ Update U for Poisson + Gamma model. """
    I, J, K = Z.shape
    assert V.shape == (J,K) and M.shape == (I,J)
    U = numpy.zeros((I,K))
    for i,k in itertools.product(range(I),range(K)):
        (a_s, b_s) = poisson_gamma_a_b(a=a, b=b, Mi=M[i,:], Vk=V[:,k], Zik=Z[i,:,k]) 
        U[i,k] = gamma_draw(alpha=a_s, beta=b_s)
    return U
コード例 #5
0
ファイル: updates.py プロジェクト: changchunli/BMF_Priors
def update_tauU_gaussian_truncatednormal_hierarchical(a, b, U, muU):
    """ Update tauU (matrix) for Gaussian + Truncated Normal + hierarchical model. """
    I, K = U.shape
    assert U.shape == muU.shape
    (a_s, b_s) = tn_hierarchical_tau_a_b(a=a, b=b, U=U, muU=muU)
    new_tauU = numpy.zeros((I,K))
    for i,k in itertools.product(range(I),range(K)):
        new_tauU[i,k] = gamma_draw(alpha=a_s[i,k], beta=b_s[i,k])
    return new_tauU
コード例 #6
0
ファイル: updates.py プロジェクト: changchunli/BMF_Priors
def update_lambda_gaussian_exponential_ard(alpha0, beta0, U, V):
    """ Update lambda (vector) for Gaussian + Exponential + ARD model. """
    K = U.shape[1]
    new_lambda = numpy.zeros(K)
    for k in range(K):
        alpha_s, beta_s = exponential_ard_alpha_beta(
            alpha0=alpha0, beta0=beta0, Uk=U[:,k], Vk=V[:,k])
        new_lambda[k] = gamma_draw(alpha=alpha_s, beta=beta_s)
    return new_lambda
コード例 #7
0
ファイル: updates.py プロジェクト: changchunli/BMF_Priors
def update_U_poisson_gamma_hierarchical(a, hU, M, V, Z):
    """ Update U for Poisson + Gamma + hierarchical model. """
    I, J, K = Z.shape
    assert hU.shape == (I,) and V.shape == (J,K)
    U = numpy.zeros((I,K))
    for i,k in itertools.product(range(I),range(K)):
        (a_s, b_s) = poisson_gamma_hierarchical_a_b(
            a=a, hUi=hU[i], Mi=M[i,:], Vk=V[:,k], Zik=Z[i,:,k])
        U[i,k] = gamma_draw(alpha=a_s, beta=b_s)
    return U
コード例 #8
0
ファイル: bnmtf_gibbs.py プロジェクト: zshwuhan/BNMTF_ARD
    def initialise(self, init_FG='random', init_S='random'):
        ''' Initialise F, S, G, tau, and lambdaFk, lambdaGl (if ARD). '''
        assert init_FG in OPTIONS_INIT_FG, "Unknown initialisation option for F and G: %s. Should be in %s." % (
            init_FG, OPTIONS_INIT_FG)
        assert init_S in OPTIONS_INIT_S, "Unknown initialisation option for S: %s. Should be in %s." % (
            init_S, OPTIONS_INIT_S)

        self.F = numpy.zeros((self.I, self.K))
        self.S = numpy.zeros((self.K, self.L))
        self.G = numpy.zeros((self.J, self.L))
        self.lambdaFk = numpy.zeros(self.K)
        self.lambdaGl = numpy.zeros(self.L)

        # Initialise lambdaFk, lambdaGl
        if self.ARD:
            for k in range(self.K):
                self.lambdaFk[k] = self.alpha0 / self.beta0
            for l in range(self.L):
                self.lambdaGl[l] = self.alpha0 / self.beta0

        # Initialise F, G
        if init_FG == 'kmeans':
            print "Initialising F using KMeans."
            kmeans_F = KMeans(self.R, self.M, self.K)
            kmeans_F.initialise()
            kmeans_F.cluster()
            self.F = kmeans_F.clustering_results + 0.2

            print "Initialising G using KMeans."
            kmeans_G = KMeans(self.R.T, self.M.T, self.L)
            kmeans_G.initialise()
            kmeans_G.cluster()
            self.G = kmeans_G.clustering_results + 0.2
        else:
            # 'random' or 'exp'
            for i, k in itertools.product(range(self.I), range(self.K)):
                hyperparam = self.lambdaFk[k] if self.ARD else self.lambdaF[i,
                                                                            k]
                self.F[i, k] = exponential_draw(
                    hyperparam) if init_FG == 'random' else 1.0 / hyperparam
            for j, l in itertools.product(range(self.J), range(self.L)):
                hyperparam = self.lambdaGl[l] if self.ARD else self.lambdaG[j,
                                                                            l]
                self.G[j, l] = exponential_draw(
                    hyperparam) if init_FG == 'random' else 1.0 / hyperparam

        # Initialise S
        for k, l in itertools.product(range(self.K), range(self.L)):
            hyperparam = self.lambdaS[k, l]
            self.S[k, l] = exponential_draw(
                hyperparam) if init_S == 'random' else 1.0 / hyperparam

        # Initialise tau
        self.tau = gamma_draw(self.alpha_s(), self.beta_s())
コード例 #9
0
    def run(self, iterations):
        self.all_F = numpy.zeros((iterations, self.I, self.K))
        self.all_S = numpy.zeros((iterations, self.K, self.L))
        self.all_G = numpy.zeros((iterations, self.J, self.L))
        self.all_tau = numpy.zeros(iterations)
        self.all_times = []  # to plot performance against time

        metrics = ['MSE', 'R^2', 'Rp']
        self.all_performances = {}  # for plotting convergence of metrics
        for metric in metrics:
            self.all_performances[metric] = []

        time_start = time.time()
        for it in range(0, iterations):
            for k in range(0, self.K):
                tauFk = self.tauF(k)
                muFk = self.muF(tauFk, k)
                self.F[:, k] = TN_vector_draw(muFk, tauFk)

            for k, l in itertools.product(xrange(0, self.K), xrange(0,
                                                                    self.L)):
                tauSkl = self.tauS(k, l)
                muSkl = self.muS(tauSkl, k, l)
                self.S[k, l] = TN_draw(muSkl, tauSkl)

            for l in range(0, self.L):
                tauGl = self.tauG(l)
                muGl = self.muG(tauGl, l)
                self.G[:, l] = TN_vector_draw(muGl, tauGl)

            self.tau = gamma_draw(self.alpha_s(), self.beta_s())

            self.all_F[it], self.all_S[it], self.all_G[it], self.all_tau[
                it] = numpy.copy(self.F), numpy.copy(self.S), numpy.copy(
                    self.G), self.tau

            perf = self.predict_while_running()
            for metric in metrics:
                self.all_performances[metric].append(perf[metric])

            print "Iteration %s. MSE: %s. R^2: %s. Rp: %s." % (
                it + 1, perf['MSE'], perf['R^2'], perf['Rp'])

            time_iteration = time.time()
            self.all_times.append(time_iteration - time_start)

        return (self.all_F, self.all_S, self.all_G, self.all_tau)
コード例 #10
0
 def run(self,iterations):
     self.all_F = numpy.zeros((iterations,self.I,self.K))  
     self.all_S = numpy.zeros((iterations,self.K,self.L))   
     self.all_G = numpy.zeros((iterations,self.J,self.L))  
     self.all_tau = numpy.zeros(iterations)
     self.all_times = [] # to plot performance against time
     
     metrics = ['MSE','R^2','Rp']
     self.all_performances = {} # for plotting convergence of metrics
     for metric in metrics:
         self.all_performances[metric] = []
     
     time_start = time.time()
     for it in range(0,iterations):            
         for k in range(0,self.K):
             tauFk = self.tauF(k)
             muFk = self.muF(tauFk,k)
             self.F[:,k] = TN_vector_draw(muFk,tauFk)
             
         for k,l in itertools.product(xrange(0,self.K),xrange(0,self.L)):
             tauSkl = self.tauS(k,l)
             muSkl = self.muS(tauSkl,k,l)
             self.S[k,l] = TN_draw(muSkl,tauSkl)
             
         for l in range(0,self.L):
             tauGl = self.tauG(l)
             muGl = self.muG(tauGl,l)
             self.G[:,l] = TN_vector_draw(muGl,tauGl)
             
         self.tau = gamma_draw(self.alpha_s(),self.beta_s())
         
         self.all_F[it], self.all_S[it], self.all_G[it], self.all_tau[it] = numpy.copy(self.F), numpy.copy(self.S), numpy.copy(self.G), self.tau
         
         perf = self.predict_while_running()
         for metric in metrics:
             self.all_performances[metric].append(perf[metric])
             
         print "Iteration %s. MSE: %s. R^2: %s. Rp: %s." % (it+1,perf['MSE'],perf['R^2'],perf['Rp'])
     
         time_iteration = time.time()
         self.all_times.append(time_iteration-time_start)            
         
     return (self.all_F, self.all_S, self.all_G, self.all_tau)
コード例 #11
0
ファイル: bnmf_gibbs_optimised.py プロジェクト: MXDC/BNMTF
    def run(self, iterations):
        self.all_U = numpy.zeros((iterations, self.I, self.K))
        self.all_V = numpy.zeros((iterations, self.J, self.K))
        self.all_tau = numpy.zeros(iterations)
        self.all_times = []  # to plot performance against time

        metrics = ['MSE', 'R^2', 'Rp']
        self.all_performances = {}  # for plotting convergence of metrics
        for metric in metrics:
            self.all_performances[metric] = []

        time_start = time.time()
        for it in range(0, iterations):
            for k in range(0, self.K):
                tauUk = self.tauU(k)
                muUk = self.muU(tauUk, k)
                self.U[:, k] = TN_vector_draw(muUk, tauUk)

            for k in range(0, self.K):
                tauVk = self.tauV(k)
                muVk = self.muV(tauVk, k)
                self.V[:, k] = TN_vector_draw(muVk, tauVk)

            self.tau = gamma_draw(self.alpha_s(), self.beta_s())

            self.all_U[it], self.all_V[it], self.all_tau[it] = numpy.copy(
                self.U), numpy.copy(self.V), self.tau

            perf = self.predict_while_running()
            for metric in metrics:
                self.all_performances[metric].append(perf[metric])

            print "Iteration %s. MSE: %s. R^2: %s. Rp: %s." % (
                it + 1, perf['MSE'], perf['R^2'], perf['Rp'])

            time_iteration = time.time()
            self.all_times.append(time_iteration - time_start)

        return (self.all_U, self.all_V, self.all_tau)
コード例 #12
0
    def run(self, iterations):
        self.all_U = numpy.zeros((iterations, self.I, self.K))
        self.all_V = numpy.zeros((iterations, self.J, self.K))
        self.all_tau = numpy.zeros(iterations)
        self.all_times = []  # to plot performance against time

        metrics = ["MSE", "R^2", "Rp"]
        self.all_performances = {}  # for plotting convergence of metrics
        for metric in metrics:
            self.all_performances[metric] = []

        time_start = time.time()
        for it in range(0, iterations):
            for k in range(0, self.K):
                tauUk = self.tauU(k)
                muUk = self.muU(tauUk, k)
                self.U[:, k] = TN_vector_draw(muUk, tauUk)

            for k in range(0, self.K):
                tauVk = self.tauV(k)
                muVk = self.muV(tauVk, k)
                self.V[:, k] = TN_vector_draw(muVk, tauVk)

            self.tau = gamma_draw(self.alpha_s(), self.beta_s())

            self.all_U[it], self.all_V[it], self.all_tau[it] = numpy.copy(self.U), numpy.copy(self.V), self.tau

            perf = self.predict_while_running()
            for metric in metrics:
                self.all_performances[metric].append(perf[metric])

            print "Iteration %s. MSE: %s. R^2: %s. Rp: %s." % (it + 1, perf["MSE"], perf["R^2"], perf["Rp"])

            time_iteration = time.time()
            self.all_times.append(time_iteration - time_start)

        return (self.all_U, self.all_V, self.all_tau)
コード例 #13
0
ファイル: bnmtf_gibbs.py プロジェクト: zshwuhan/BNMTF_ARD
    def run(self, iterations):
        ''' Run the Gibbs sampler. '''
        self.all_F = numpy.zeros((iterations, self.I, self.K))
        self.all_S = numpy.zeros((iterations, self.K, self.L))
        self.all_G = numpy.zeros((iterations, self.J, self.L))
        self.all_tau = numpy.zeros(iterations)
        self.all_lambdaFk = numpy.zeros((iterations, self.K))
        self.all_lambdaGl = numpy.zeros((iterations, self.L))
        self.all_times = []  # to plot performance against time

        self.all_performances = {}  # for plotting convergence of metrics
        for metric in ALL_METRICS:
            self.all_performances[metric] = []

        time_start = time.time()
        for it in range(0, iterations):
            # Update lambdaFk, lambdaGl
            if self.ARD:
                for k in range(self.K):
                    self.lambdaFk[k] = gamma_draw(self.alphaFk_s(k),
                                                  self.betaFk_s(k))
                for l in range(self.L):
                    self.lambdaGl[l] = gamma_draw(self.alphaGl_s(l),
                                                  self.betaGl_s(l))

            # Update F
            for k in range(0, self.K):
                tauFk = self.tauF(k)
                muFk = self.muF(tauFk, k)
                self.F[:, k] = TN_vector_draw(muFk, tauFk)

            # Update S
            for k, l in itertools.product(xrange(0, self.K), xrange(0,
                                                                    self.L)):
                tauSkl = self.tauS(k, l)
                muSkl = self.muS(tauSkl, k, l)
                self.S[k, l] = TN_draw(muSkl, tauSkl)

            # Update G
            for l in range(0, self.L):
                tauGl = self.tauG(l)
                muGl = self.muG(tauGl, l)
                self.G[:, l] = TN_vector_draw(muGl, tauGl)

            # Update tau
            self.tau = gamma_draw(self.alpha_s(), self.beta_s())

            # Store draws
            self.all_F[it], self.all_S[it], self.all_G[it], self.all_tau[
                it] = numpy.copy(self.F), numpy.copy(self.S), numpy.copy(
                    self.G), self.tau
            if self.ARD:
                self.all_lambdaFk[it] = numpy.copy(self.lambdaFk)
                self.all_lambdaGl[it] = numpy.copy(self.lambdaGl)

            # Store and print performances
            perf = self.predict_while_running()
            for metric in ALL_METRICS:
                self.all_performances[metric].append(perf[metric])

            print "Iteration %s. MSE: %s. R^2: %s. Rp: %s." % (
                it + 1, perf['MSE'], perf['R^2'], perf['Rp'])

            # Store time taken for iteration
            time_iteration = time.time()
            self.all_times.append(time_iteration - time_start)
コード例 #14
0
ファイル: updates.py プロジェクト: changchunli/BMF_Priors
def update_tau_gaussian(alpha, beta, R, M, U, V):
    """ Update tau (noise) in Gaussian models. """
    alpha_s, beta_s = gaussian_tau_alpha_beta(alpha, beta, R, M, U, V)
    new_tau = gamma_draw(alpha=alpha_s, beta=beta_s)
    return new_tau