Пример #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_mode(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_mode(muUk)
             self.U[:,k] = numpy.maximum(self.U[:,k],MINIMUM_TN*numpy.ones(self.I))
             
         # Update V
         for k in range(0,self.K):
             tauVk = self.tauV(k)
             muVk = self.muV(tauVk,k)
             self.V[:,k] = TN_vector_mode(muVk)
             self.V[:,k] = numpy.maximum(self.V[:,k],MINIMUM_TN*numpy.ones(self.J))
             
         # Update tau
         self.tau = gamma_mode(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_S='random',init_FG='random'):
        assert init_S in ['random','exp'], "Unknown initialisation option for S: %s. Should be 'random' or 'exp'." % init_S
        assert init_FG in ['random','exp','kmeans'], "Unknown initialisation option for S: %s. Should be 'random', 'exp', or 'kmeans." % init_FG
        
        self.S = 1./self.lambdaS
        if init_S == 'random':
            for k,l in itertools.product(xrange(0,self.K),xrange(0,self.L)):  
                self.S[k,l] = exponential_draw(self.lambdaS[k,l])
                
        self.F, self.G = 1./self.lambdaF, 1./self.lambdaG
        if init_FG == 'random':
            for i,k in itertools.product(xrange(0,self.I),xrange(0,self.K)):        
                self.F[i,k] = exponential_draw(self.lambdaF[i,k])
            for j,l in itertools.product(xrange(0,self.J),xrange(0,self.L)):
                self.G[j,l] = exponential_draw(self.lambdaG[j,l])
        elif 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

        self.tau = gamma_mode(self.alpha_s(), self.beta_s())
Пример #3
0
    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_mode(self.alpha_s(), self.beta_s())
Пример #4
0
    def run(self, iterations, minimum_TN=0.):
        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_mode(muFk)
                self.F[:, k] = numpy.maximum(self.F[:, k],
                                             minimum_TN * numpy.ones(self.I))

            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_mode(muSkl)
                self.S[k, l] = max(self.S[k, l], minimum_TN)

            for l in range(0, self.L):
                tauGl = self.tauG(l)
                muGl = self.muG(tauGl, l)
                self.G[:, l] = TN_vector_mode(muGl)
                self.G[:, l] = numpy.maximum(self.G[:, l],
                                             minimum_TN * numpy.ones(self.J))

            self.tau = gamma_mode(self.alpha_s(), self.beta_s())
            self.all_tau[it] = self.tau

            perf = self.predict(self.M)
            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
Пример #5
0
 def initialise(self,init='random'):
     assert init in ['random','exp'], "Unknown initialisation option: %s. Should be 'random' or 'exp'." % init
     self.U = numpy.zeros((self.I,self.K))
     self.V = numpy.zeros((self.J,self.K))
     
     if init == 'random':
         for i,k in itertools.product(xrange(0,self.I),xrange(0,self.K)):
             self.U[i,k] = exponential_draw(self.lambdaU[i][k])
         for j,k in itertools.product(xrange(0,self.J),xrange(0,self.K)):
             self.V[j,k] = exponential_draw(self.lambdaV[j][k])
         
     elif init == 'exp':
         for i,k in itertools.product(xrange(0,self.I),xrange(0,self.K)):
             self.U[i,k] = 1.0/self.lambdaU[i][k]
         for j,k in itertools.product(xrange(0,self.J),xrange(0,self.K)):
             self.V[j,k] = 1.0/self.lambdaV[j][k]
     
     self.tau = gamma_mode(self.alpha_s(), self.beta_s())
Пример #6
0
 def run(self,iterations,minimum_TN=0.):  
     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_mode(muFk)
             self.F[:,k] = numpy.maximum(self.F[:,k],minimum_TN*numpy.ones(self.I))
             
         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_mode(muSkl)
             self.S[k,l] = max(self.S[k,l],minimum_TN)
             
         for l in range(0,self.L):
             tauGl = self.tauG(l)
             muGl = self.muG(tauGl,l)
             self.G[:,l] = TN_vector_mode(muGl)
             self.G[:,l] = numpy.maximum(self.G[:,l],minimum_TN*numpy.ones(self.J))
             
         self.tau = gamma_mode(self.alpha_s(),self.beta_s())
         self.all_tau[it] = self.tau
         
         perf = self.predict(self.M)
         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 
Пример #7
0
    def run(self, iterations, minimum_TN=0.):
        self.all_tau = numpy.zeros(iterations)  # to plot convergence
        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_mode(muUk)
                self.U[:, k] = numpy.maximum(self.U[:, k],
                                             minimum_TN * numpy.ones(self.I))

            for k in range(0, self.K):
                tauVk = self.tauV(k)
                muVk = self.muV(tauVk, k)
                self.V[:, k] = TN_vector_mode(muVk)
                self.V[:, k] = numpy.maximum(self.V[:, k],
                                             minimum_TN * numpy.ones(self.J))

            self.tau = gamma_mode(self.alpha_s(), self.beta_s())
            self.all_tau[it] = self.tau

            perf = self.predict(self.M)
            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
Пример #8
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_mode(self.alpha_s(),self.beta_s())
Пример #9
0
    def initialise(self, init_S='random', init_FG='random'):
        assert init_S in [
            'random', 'exp'
        ], "Unknown initialisation option for S: %s. Should be 'random' or 'exp'." % init_S
        assert init_FG in [
            'random', 'exp', 'kmeans'
        ], "Unknown initialisation option for S: %s. Should be 'random', 'exp', or 'kmeans." % init_FG

        self.S = 1. / self.lambdaS
        if init_S == 'random':
            for k, l in itertools.product(xrange(0, self.K), xrange(0,
                                                                    self.L)):
                self.S[k, l] = exponential_draw(self.lambdaS[k, l])

        self.F, self.G = 1. / self.lambdaF, 1. / self.lambdaG
        if init_FG == 'random':
            for i, k in itertools.product(xrange(0, self.I), xrange(0,
                                                                    self.K)):
                self.F[i, k] = exponential_draw(self.lambdaF[i, k])
            for j, l in itertools.product(xrange(0, self.J), xrange(0,
                                                                    self.L)):
                self.G[j, l] = exponential_draw(self.lambdaG[j, l])
        elif 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

        self.tau = gamma_mode(self.alpha_s(), self.beta_s())
Пример #10
0
    def initialise(self, init='random'):
        assert init in [
            'random', 'exp'
        ], "Unknown initialisation option: %s. Should be 'random' or 'exp'." % init
        self.U = numpy.zeros((self.I, self.K))
        self.V = numpy.zeros((self.J, self.K))

        if init == 'random':
            for i, k in itertools.product(xrange(0, self.I), xrange(0,
                                                                    self.K)):
                self.U[i, k] = exponential_draw(self.lambdaU[i][k])
            for j, k in itertools.product(xrange(0, self.J), xrange(0,
                                                                    self.K)):
                self.V[j, k] = exponential_draw(self.lambdaV[j][k])

        elif init == 'exp':
            for i, k in itertools.product(xrange(0, self.I), xrange(0,
                                                                    self.K)):
                self.U[i, k] = 1.0 / self.lambdaU[i][k]
            for j, k in itertools.product(xrange(0, self.J), xrange(0,
                                                                    self.K)):
                self.V[j, k] = 1.0 / self.lambdaV[j][k]

        self.tau = gamma_mode(self.alpha_s(), self.beta_s())
Пример #11
0
 def run(self,iterations,minimum_TN=0.):   
     self.all_tau = numpy.zeros(iterations) # to plot convergence
     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_mode(muUk) 
             self.U[:,k] = numpy.maximum(self.U[:,k],minimum_TN*numpy.ones(self.I))
             
         for k in range(0,self.K):
             tauVk = self.tauV(k)
             muVk = self.muV(tauVk,k)
             self.V[:,k] = TN_vector_mode(muVk) 
             self.V[:,k] = numpy.maximum(self.V[:,k],minimum_TN*numpy.ones(self.J))
             
         self.tau = gamma_mode(self.alpha_s(),self.beta_s())
         self.all_tau[it] = self.tau
         
         perf = self.predict(self.M)
         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
Пример #12
0
    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_mode(self.alphaFk_s(k),
                                                  self.betaFk_s(k))
                for l in range(self.L):
                    self.lambdaGl[l] = gamma_mode(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_mode(muFk)
                self.F[:, k] = numpy.maximum(self.F[:, k],
                                             MINIMUM_TN * numpy.ones(self.I))

            # 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_mode(muSkl)
                self.S[k, l] = max(self.S[k, l], MINIMUM_TN)

            # Update G
            for l in range(0, self.L):
                tauGl = self.tauG(l)
                muGl = self.muG(tauGl, l)
                self.G[:, l] = TN_vector_mode(muGl)
                self.G[:, l] = numpy.maximum(self.G[:, l],
                                             MINIMUM_TN * numpy.ones(self.J))

            # Update tau
            self.tau = gamma_mode(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)