Exemplo n.º 1
0
 def compute_pder(hyperparameters, i, x, z):
     sigma, l = hyperparameters[0], hyperparameters[1]
     tao = x - z
     if i==0:
         return 2*sigma*exp(-dot(tao.T, tao)/(2*l*l))
     elif i==1:
         r_squared = dot(tao.T, tao)
         return (r_squared/l**3)*(sigma**2)*exp(-r_squared/(2*l*l))
     else:
         #TODO: controlar error
         # print 'error'
         return None
Exemplo n.º 2
0
 def compute_pder(hyperparameters, i, x, z):
     sigma, l = hyperparameters[0], hyperparameters[1]
     tao = x - z
     if i == 0:
         return 2 * sigma * exp(-dot(tao.T, tao) / (2 * l * l))
     elif i == 1:
         r_squared = dot(tao.T, tao)
         return (r_squared / l**3) * (sigma**2) * exp(-r_squared /
                                                      (2 * l * l))
     else:
         #TODO: controlar error
         # print 'error'
         return None
Exemplo n.º 3
0
        def cov_function(hyperparameters, x, z):
            # TODO:
            # Comprobar que el numero de hip. es correcto

            # TODO:
            # Una posible mejora: hacer que todos los hypp
            # puedan tomar cualquier valor real
            # (p.ej. si un hyp puede tomar cualquier valor
            # pos considerar el logaritmo de dicho hypp).

            P, Q = self.P, self.Q
            w = hyperparameters[0:Q]
            mu = [[]] * Q
            v = [[]] * Q
            for q in range(Q):
                mu[q] = hyperparameters[Q + q * P:Q + (q + 1) * P]
            for q in range(Q):
                v[q] = hyperparameters[Q * (1 + P) + q * P:Q * (1 + P) +
                                       (q + 1) * P]
            tao = x - z
            sum = 0
            for q in range(Q):
                prod = w[q]
                prod *= cos(TWOPI * dot(tao.T, mu[q]))
                arg_exp = 0
                for p in range(P):
                    arg_exp += ((tao[p, 0]**2) * v[q][p])
                arg_exp *= (MTWOPISQ)
                prod *= exp(arg_exp)
                sum += prod
            # TODO:
            # Recomprobar que la formulita
            # esta bien copiada.
            return sum
Exemplo n.º 4
0
 def gpc_find_mode(self, tasks, hyperparameters):
     try:
         num_tasks = len(tasks)
         # TODO: modificar esta linea (puede fallar)
         n = num_cols(self.X[0])
         f = np.matrix([0] * n * num_tasks).reshape((n, num_tasks))
         self.mlog_ML = 0
         K = [[]] * num_tasks
         for task in range(num_tasks):
             I = np.matrix(np.eye(n))
             y = self.Y[:, task]
             while True:
                 f_old = np.copy(f[:, task])
                 W = -self.sigmoid.hessian_log_likelihood(y, f[:, task])
                 K[task] = self.cov_function.cov_matrix(
                     hyperparameters, self.X[task])
                 sqrt_W = np.sqrt(W)
                 L = cholesky(I + sqrt_W * K[task] * sqrt_W)
                 b = W * f[:, task] + self.sigmoid.gradient_log_likelihood(
                     y, f[:, task])
                 a = b - sqrt_W * backslash(
                     L.T, backslash(L, sqrt_W * K[task] * b))
                 f[:, task] = K[task] * a
                 if norm(f[:, task] - f_old) < 0.01:
                     break
             self.mlog_ML += (0.5 * dot(a.T, f[:, task]) -
                              self.sigmoid.log_likelihood(y, f[:, task]) +
                              sum(log(np.diag(L))))
         self.f, self.a, self.sqrt_W, self.K, self.I = f, a, sqrt_W, K, I
         return f, self.mlog_ML
     except LinAlgError:
         return None, np.inf
Exemplo n.º 5
0
        def cov_function(hyperparameters, x, z):
            # TODO:
            # Comprobar que el numero de hip. es correcto

            # TODO:
            # Una posible mejora: hacer que todos los hypp
            # puedan tomar cualquier valor real
            # (p.ej. si un hyp puede tomar cualquier valor
            # pos considerar el logaritmo de dicho hypp).

            P, Q = self.P, self.Q
            w = hyperparameters[0:Q]
            mu = [[]]*Q
            v = [[]]*Q
            for q in range(Q):
                mu[q] = hyperparameters[Q+q*P:Q+(q+1)*P]
            for q in range(Q):
                v[q] = hyperparameters[Q*(1+P)+q*P:Q*(1+P)+(q+1)*P]
            tao = x - z
            sum = 0
            for q in range(Q):
                prod = w[q]
                prod *= cos(TWOPI*dot(tao.T, mu[q]))
                arg_exp = 0
                for p in range(P):
                    arg_exp += ((tao[p,0]**2)*v[q][p])
                arg_exp *= (MTWOPISQ)
                prod *= exp(arg_exp)
                sum += prod
            # TODO:
            # Recomprobar que la formulita
            # esta bien copiada.
            return sum
Exemplo n.º 6
0
 def gpc_find_mode(self, tasks, hyperparameters):
     try:
         num_tasks = len(tasks)
         # TODO: modificar esta linea (puede fallar)
         n = num_cols(self.X[0])
         f = np.matrix([0]*n*num_tasks).reshape((n, num_tasks))
         self.mlog_ML = 0
         K = [[]]*num_tasks
         for task in range(num_tasks):
             I = np.matrix(np.eye(n))
             y = self.Y[:,task]
             while True:
                 f_old = np.copy(f[:,task])
                 W = -self.sigmoid.hessian_log_likelihood(y, f[:,task])
                 K[task] = self.cov_function.cov_matrix(hyperparameters, self.X[task])
                 sqrt_W = np.sqrt(W)
                 L = cholesky(I + sqrt_W*K[task]*sqrt_W)
                 b = W*f[:,task] + self.sigmoid.gradient_log_likelihood(y, f[:,task])
                 a = b - sqrt_W*backslash(L.T, backslash(L, sqrt_W*K[task]*b))
                 f[:,task] = K[task]*a
                 if norm(f[:,task]-f_old) < 0.01:
                     break
             self.mlog_ML += (0.5*dot(a.T, f[:,task]) - self.sigmoid.log_likelihood(y, f[:,task]) + sum(log(np.diag(L))))
         self.f, self.a, self.sqrt_W, self.K, self.I = f, a, sqrt_W, K, I
         return f, self.mlog_ML
     except LinAlgError:
         return None, np.inf
Exemplo n.º 7
0
 def compute_pder(hyperparameters, i, x, z):
     w, mu, v = hyperparameters_SMK(P, Q, hyperparameters)
     tao = x - z
     if i < Q:
         # El parametro es una w_q
         # TODO: Copypasteado (se podria hacer mejor)
         q = i
         prod = cos(TWOPI * dot(tao.T, mu[q]))
         arg_exp = 0
         for p in range(P):
             arg_exp += ((tao[p, 0]**2) * v[q][p])
         arg_exp *= (MTWOPISQ)
         prod *= exp(arg_exp)
         return prod
     elif i < Q * (P + 1):
         q = (i - Q) / P
         p = (i - Q) % P
         prod = -TWOPI * (tao[p, 0]**2) * w[q]
         arg_exp = 0
         for j in range(P):
             arg_exp += ((tao[j, 0]**2) * v[q][p])
         arg_exp *= MTWOPISQ
         prod *= exp(arg_exp)
         prod *= sin(TWOPI * dot(tao.T, mu[q]))
         return prod
     elif i < Q * (2 * P + 1):
         q = (i - Q * (P + 1)) / P
         p = (i - Q) % P
         prod = MTWOPISQ * tao[p, 0] * w[q]
         prod *= cos(TWOPI * dot(tao.T, mu[q]))
         arg_exp = 0
         for j in range(P):
             arg_exp += ((tao[j, 0]**2) * v[q][p])
         arg_exp *= MTWOPISQ
         prod *= exp(arg_exp)
         return prod
     else:
         # TODO: Implementar esto
         # print 'Sin implementar (oops)'
         return None
Exemplo n.º 8
0
 def compute_pder(hyperparameters, i, x, z):
     w, mu, v = hyperparameters_SMK(P, Q, hyperparameters)
     tao = x - z
     if i < Q:
         # El parametro es una w_q
         # TODO: Copypasteado (se podria hacer mejor)
         q = i
         prod = cos(TWOPI*dot(tao.T, mu[q]))
         arg_exp = 0
         for p in range(P):
             arg_exp += ((tao[p,0]**2)*v[q][p])
         arg_exp *= (MTWOPISQ)
         prod *= exp(arg_exp)
         return prod
     elif i < Q*(P+1):
         q = (i-Q)/P
         p = (i-Q)%P
         prod = -TWOPI*(tao[p, 0]**2)*w[q]
         arg_exp = 0
         for j in range(P):
             arg_exp += ((tao[j,0]**2)*v[q][p])
         arg_exp *= MTWOPISQ
         prod *= exp(arg_exp)
         prod *= sin(TWOPI*dot(tao.T, mu[q]))
         return prod
     elif i < Q*(2*P+1):
         q = (i-Q*(P+1))/P
         p = (i-Q)%P
         prod = MTWOPISQ*tao[p, 0]*w[q]
         prod *= cos(TWOPI*dot(tao.T, mu[q]))
         arg_exp = 0
         for j in range(P):
             arg_exp += ((tao[j,0]**2)*v[q][p])
         arg_exp *= MTWOPISQ
         prod *= exp(arg_exp)
         return prod
     else:
         # TODO: Implementar esto
         # print 'Sin implementar (oops)'
         return None
Exemplo n.º 9
0
 def gpc_gradient_mlogML(self, hyperparameters, tasks):
     f, a, sqrt_W, K, I = self.f, self.a, self.sqrt_W, self.K, self.I
     num_tasks = len(tasks)
     num_hyper = len(hyperparameters)
     gradient = np.array([0.0]*num_hyper)
     for task in range(num_tasks):
         y = self.Y[:,task]
         self.L = cholesky(I + sqrt_W*K[task]*sqrt_W)
         L = self.L
         R = sqrt_W*backslash(L.T, backslash(L, sqrt_W))
         C = backslash(L, sqrt_W*K[task])
         s2 = np.matrix(np.diag(np.diag(K[task]) - np.diag(C.T*C)))*self.sigmoid.der3_log_likelihood(y, f[:,task])
         for j in range(num_hyper):
             C = self.cov_function.pder_matrix(hyperparameters, j, self.X[task])
             s1 = 0.5*(a.T*C*a - trace_of_prod(R, C))[0, 0]
             b = C*self.sigmoid.gradient_log_likelihood(y, f[:,task])
             s3 = b - K[task]*R*b
             gradient[j] += (-s1 -dot(s2.T,s3))
     return gradient
Exemplo n.º 10
0
 def gpc_gradient_mlogML(self, hyperparameters, tasks):
     f, a, sqrt_W, K, I = self.f, self.a, self.sqrt_W, self.K, self.I
     num_tasks = len(tasks)
     num_hyper = len(hyperparameters)
     gradient = np.array([0.0] * num_hyper)
     for task in range(num_tasks):
         y = self.Y[:, task]
         self.L = cholesky(I + sqrt_W * K[task] * sqrt_W)
         L = self.L
         R = sqrt_W * backslash(L.T, backslash(L, sqrt_W))
         C = backslash(L, sqrt_W * K[task])
         s2 = np.matrix(np.diag(np.diag(K[task]) - np.diag(C.T * C))
                        ) * self.sigmoid.der3_log_likelihood(y, f[:, task])
         for j in range(num_hyper):
             C = self.cov_function.pder_matrix(hyperparameters, j,
                                               self.X[task])
             s1 = 0.5 * (a.T * C * a - trace_of_prod(R, C))[0, 0]
             b = C * self.sigmoid.gradient_log_likelihood(y, f[:, task])
             s3 = b - K[task] * R * b
             gradient[j] += (-s1 - dot(s2.T, s3))
     return gradient
Exemplo n.º 11
0
 def compute_pder(hyperparameters, i, x, z):
     l = hyperparameters[0]
     return dot(x.T, z) / l
Exemplo n.º 12
0
 def cov_function(hyperparameters, x, z):
     l = hyperparameters[0]
     return log(l) * dot(x.T, z)
Exemplo n.º 13
0
 def cov_function(hyperparameters, x, z):
     sigma, l = hyperparameters[0], hyperparameters[1]
     tao = x - z
     return (sigma**2) * exp(-dot(tao.T, tao) / (2 * l * l))
Exemplo n.º 14
0
 def compute_pder(hyperparameters, i, x, z):
     l = hyperparameters[0]
     return dot(x.T, z)/l
Exemplo n.º 15
0
 def cov_function(hyperparameters, x, z):
     l = hyperparameters[0]
     return log(l)*dot(x.T, z)
Exemplo n.º 16
0
 def cov_function(hyperparameters, x, z):
     sigma, l = hyperparameters[0], hyperparameters[1]
     tao = x - z
     return (sigma**2)*exp(-dot(tao.T, tao)/(2*l*l))