def second_moments(i, j, M2, beta, iA, sn2, sf2M, sr, srdotSx, srdotSxdotsr_c, srdotSxdotsr_r, sin_srdotx, cos_srdotx, *args): # compute the second moments of the spectrum feature vectors siSxsj = srdotSx[i].dot(sr[j].T) # Ms x Ms sijSxsij = -0.5*(srdotSxdotsr_c[i] + srdotSxdotsr_r[j]) em = tt.exp(sijSxsij+siSxsj) # MsxMs ep = tt.exp(sijSxsij-siSxsj) # MsxMs si = sin_srdotx[i] # Msx1 ci = cos_srdotx[i] # Msx1 sj = sin_srdotx[j] # Msx1 cj = cos_srdotx[j] # Msx1 sicj = tt.outer(si, cj) # MsxMs cisj = tt.outer(ci, sj) # MsxMs sisj = tt.outer(si, sj) # MsxMs cicj = tt.outer(ci, cj) # MsxMs sm = (sicj-cisj)*em sp = (sicj+cisj)*ep cm = (sisj+cicj)*em cp = (cicj-sisj)*ep # Populate the second moment matrix of the feature vector Q_up = tt.concatenate([cm-cp, sm+sp], axis=1) Q_lo = tt.concatenate([sp-sm, cm+cp], axis=1) Q = tt.concatenate([Q_up, Q_lo], axis=0) # Compute the second moment of the output m2 = 0.5*matrix_dot(beta[i], Q, beta[j].T) m2 = theano.ifelse.ifelse( tt.eq(i, j), m2 + sn2[i]*(1.0 + sf2M[i]*tt.sum(self.iA[i]*Q)) + 1e-6, m2) M2 = tt.set_subtensor(M2[i, j], m2) return M2
def dimemsion_transform(self, X): self.mean = T.mean(X, axis=0) X -= self.mean U, s, V = linalg.svd(X, full_matrices=False) self.principal = V[:self.dim] return linalg.matrix_dot(X, T.transpose(self.principal))
def inverse_transform(self, X): """ Perform an approximation of the input matrix of observations to the original dimensionality space :param X: The matrix of observations, where the training samples populate the rows, and the features populate the columns :return: Xhat, the dimensionality increased representation of the data """ return linalg.matrix_dot(X, self.principal) + self.mean
def second_moments(i, j, M2, beta, R, logk_c, logk_r, z_, Sx, *args): # This comes from Deisenroth's thesis ( Eqs 2.51- 2.54 ) Rij = R[i, j] n2 = logk_c[i] + logk_r[j] n2 += utils.maha(z_[i], -z_[j], 0.5 * solve(Rij, Sx)) Q = tt.exp(n2) / tt.sqrt(det(Rij)) # Eq 2.55 m2 = matrix_dot(beta[i], Q, beta[j]) m2 = theano.ifelse.ifelse(tt.eq(i, j), m2 + 1e-6, m2) M2 = tt.set_subtensor(M2[i, j], m2) return M2
def grad(self, inputs, g_outputs): r"""The gradient function should return .. math:: V\frac{\partial X^{-1}}{\partial X}, where :math:`V` corresponds to ``g_outputs`` and :math:`X` to ``inputs``. Using the `matrix cookbook <http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=3274>`_, one can deduce that the relation corresponds to .. math:: (X^{-1} \cdot V^{T} \cdot X^{-1})^T. """ x, = inputs xi = self(x) gz, = g_outputs # TT.dot(gz.T,xi) return [-matrix_dot(xi, gz.T, xi).T]
def R_op(self, inputs, eval_points): r"""The gradient function should return .. math:: \frac{\partial X^{-1}}{\partial X}V, where :math:`V` corresponds to ``g_outputs`` and :math:`X` to ``inputs``. Using the `matrix cookbook <http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=3274>`_, one can deduce that the relation corresponds to .. math:: X^{-1} \cdot V \cdot X^{-1}. """ x, = inputs xi = self(x) ev, = eval_points if ev is None: return [None] return [-matrix_dot(xi, ev, xi)]
def test_matrix_dot(): rng = np.random.RandomState(utt.fetch_seed()) n = rng.randint(4) + 2 rs = [] xs = [] for k in xrange(n): rs += [rng.randn(4, 4).astype(theano.config.floatX)] xs += [tensor.matrix()] sol = matrix_dot(*xs) theano_sol = function(xs, sol)(*rs) numpy_sol = rs[0] for r in rs[1:]: numpy_sol = np.dot(numpy_sol, r) assert _allclose(numpy_sol, theano_sol)
def test_matrix_dot(): rng = np.random.RandomState(utt.fetch_seed()) n = rng.randint(4) + 2 rs = [] xs = [] for k in xrange(n): rs += [rng.randn(4, 4).astype(theano.config.floatX)] xs += [tensor.matrix()] sol = matrix_dot(*xs) theano_sol = function(xs, sol)(*rs) numpy_sol = rs[0] for r in rs[1:]: numpy_sol = np.dot(numpy_sol, r) assert _allclose(numpy_sol, theano_sol)
def inverse_transform(self, X): return linalg.matrix_dot(X, self.principal) + self.mean