def getHessianEstimate(self, feature): # Estimate gradient of state action value function w.r.t. parameters. n = self.paramdim # state-action value # self.qvalue = self.stateActionValue(feature, self.r) # gradient of the state-action value function w.r.t. the parameters qgradient = zeros((n,)) for i in xrange(n): qgradient[i] = self.stateActionValue(feature, self.T[:, i]) # The first n elements in the first-order basis (i.e., \nabla # \log(\mu)), the following n^2 elements are the second-order basis # (i.e., \nabla^2 \log(\mu)). # self.loglhgrad = self.module.decodeFeature(feature, 'first_order') # self.loglhgrad = self.cacheFeature loglhhessian = self.module.decodeFeature(feature, 'second_order') term1 = self.qvalue * (loglhhessian - outer(self.loglhgrad, self.loglhgrad)) term2 = outer(qgradient, self.loglhgrad) # ATTENTION! This algorighm is designed only for maximization problems # in which the Hessian matrix is negative semidefinite in the optimal # point. As a result, the scaling matrix should be -1 * inverse of the # Hessian to move in the right direction return - 1 * (term1 + term2 + term2.T)
def svm_gradient_batch_fast(X_pred, X_exp, y, X_pred_ids, X_exp_ids, w, C=.0001, sigma=1.): # sample Kernel rnpred = X_pred_ids#sp.random.randint(low=0,high=len(y),size=n_pred_samples) rnexpand = X_exp_ids#sp.random.randint(low=0,high=len(y),size=n_expand_samples) #K = GaussKernMini_fast(X_pred.T,X_exp.T,sigma) X1 = X_pred.T X2 = X_exp.T if sp.sparse.issparse(X1): G = sp.outer(X1.multiply(X1).sum(axis=0), sp.ones(X2.shape[1])) else: G = sp.outer((X1 * X1).sum(axis=0), sp.ones(X2.shape[1])) if sp.sparse.issparse(X2): H = sp.outer(X2.multiply(X2).sum(axis=0), sp.ones(X1.shape[1])) else: H = sp.outer((X2 * X2).sum(axis=0), sp.ones(X1.shape[1])) K = sp.exp(-(G + H.T - 2. * fast_dot(X1.T, X2)) / (2. * sigma ** 2)) # K = sp.exp(-(G + H.T - 2.*(X1.T.dot(X2)))/(2.*sigma**2)) if sp.sparse.issparse(X1) | sp.sparse.issparse(X2): K = sp.array(K) # compute predictions yhat = fast_dot(K,w[rnexpand]) # compute whether or not prediction is in margin inmargin = (yhat * y[rnpred]) <= 1 # compute gradient G = C * w[rnexpand] - fast_dot((y[rnpred] * inmargin), K) return G,rnexpand
def sample_moments( X, k ): """Get the sample moments from data""" N, d = X.shape # Partition X into two halves to independently estimate M2 and M3 X1, X2 = X[:N/2], X[N/2:] # Get the moments M1 = X1.mean(0) M1_ = X2.mean(0) M2 = Pairs( X1, X1 ) M3 = lambda theta: TriplesP( X2, X2, X2, theta ) #M3 = Triples( X2, X2, X2 ) # TODO: Ah, not computing sigma2! # Estimate \sigma^2 = k-th eigenvalue of M2 - mu mu^T sigma2 = svdvals( M2 - outer( M1, M1 ) )[k-1] assert( sc.isreal( sigma2 ) and sigma2 > 0 ) # P (M_2) is the best kth rank apprximation to M2 - sigma^2 I P = approxk( M2 - sigma2 * eye( d ), k ) B = matrix_tensorify( eye(d), M1_ ) T = lambda theta: M3(theta) - sigma2 * ( M1_.dot(theta) * eye( d ) + outer( M1_, theta ) + outer( theta, M1_ ) ) #T = M3 - sigma2 * ( B + B.swapaxes(2, 1) + B.swapaxes(2, 0) ) return P, T
def calcInvFisher(sigma, invSigma=None, factorSigma=None): """ Efficiently compute the exact inverse of the FIM of a Gaussian. Returns a list of the diagonal blocks. """ if invSigma == None: invSigma = inv(sigma) if factorSigma == None: factorSigma = cholesky(sigma) dim = sigma.shape[0] invF = [mat(1 / (invSigma[-1, -1] + factorSigma[-1, -1] ** -2))] invD = 1 / invSigma[-1, -1] for k in reversed(list(range(dim - 1))): v = invSigma[k + 1:, k] w = invSigma[k, k] wr = w + factorSigma[k, k] ** -2 u = dot(invD, v) s = dot(v, u) q = 1 / (w - s) qr = 1 / (wr - s) t = -(1 + q * s) / w tr = -(1 + qr * s) / wr invF.append(blockCombine([[qr, tr * u], [mat(tr * u).T, invD + qr * outer(u, u)]])) invD = blockCombine([[q , t * u], [mat(t * u).T, invD + q * outer(u, u)]]) invF.append(sigma) invF.reverse() return invF
def diffmat(x): # x is an ordered array of grid points n = sp.size(x) e = sp.ones((n,1)) Xdiff = sp.outer(x,e)-sp.outer(e,x)+sp.identity(n) xprod = -reduce(mul,Xdiff) # product of rows W = sp.outer(1/xprod,e) D = W/sp.multiply(W.T,Xdiff) d = 1-sum(D) for k in range(0,n): # Set diagonal elements D[k,k] = d[k] return -D.T
def diffmat(x): n= sp.size(x) e= sp.ones((n,1)) Xdiff= sp.outer(x,e)-sp.outer(e,x)+sp.identity(n) xprod= -reduce(mul, Xdiff) W= sp.outer(1/xprod,e) D= W/sp.multiply(W.T,Xdiff) d= 1-sum(D) for k in range(0,n): D[k,k] = d[k] return -D.T
def rnd_cov(N_d,sigma=0.1): if type(sigma)==int or type(sigma)==float: sigma=sigma*sp.rand(N_d) else: pass cov=sp.outer(sigma,sigma) #correlation matrix r=2*sp.rand(N_d)-1 rho=sp.outer(r,r)+(1-r**2)*sp.eye(N_d) cov=rho*cov return cov
def bptt(self, x, t): """Back propagation throuth time of a sample. Reference: [1] Deep Learning, Ian Goodfellow, Yoshua Bengio and Aaron Courville, P385. """ dU = sp.zeros_like(self.U) dW = sp.zeros_like(self.W) db = sp.zeros_like(self.b) dV = sp.zeros_like(self.V) dc = sp.zeros_like(self.c) tau = len(x) cells = self.forward_propagation(x) dh = sp.zeros(self.n_hiddens) for i in range(tau - 1, -1, -1): # FIXME: # 1. Should not use cell[i] since there maybe multiple hidden layers. # 2. Using exponential family as output should not be specified. time_input = x[i] one_hot_t = sp.zeros(self.n_features) one_hot_t[t[i]] = 1 # Cell of time i cell = cells[i] # Hidden layer of current cell hidden = cell[0] # Output layer of current cell output = cell[1] # Hidden layer of time i + 1 prev_hidden = cells[i - 1][0] if i - 1 >= 0 else None # Hidden layer of time i - 1 next_hidden = cells[i + 1][0] if i + 1 < tau else None # Error of current time i da = hidden.backward() next_da = next_hidden.backward() if next_hidden is not None else sp.zeros(self.n_hiddens) prev_h = prev_hidden.h if prev_hidden is not None else sp.zeros(self.n_hiddens) # FIXME: The error function should not be specified here # do = sp.dot(output.backward().T, -one_hot_t / output.y) do = output.y - one_hot_t dh = sp.dot(sp.dot(self.W.T, sp.diag(next_da)), dh) + sp.dot(self.V.T, do) # Gradient back propagation through time dc += do db += da * dh dV += sp.outer(do, hidden.h) dW += sp.outer(da * dh, prev_h) dU[:, time_input] += da * dh return (dU, dW, db, dV, dc)
def GaussKernMini_fast(X1,X2,sigma): if sp.sparse.issparse(X1): G = sp.outer(X1.multiply(X1).sum(axis=0),sp.ones(X2.shape[1])) else: G = sp.outer((X1 * X1).sum(axis=0),sp.ones(X2.shape[1])) if sp.sparse.issparse(X2): H = sp.outer(X2.multiply(X2).sum(axis=0),sp.ones(X1.shape[1])) else: H = sp.outer((X2 * X2).sum(axis=0),sp.ones(X1.shape[1])) K = sp.exp(-(G + H.T - 2.*fast_dot(X1.T,X2))/(2.*sigma**2)) # K = sp.exp(-(G + H.T - 2.*(X1.T.dot(X2)))/(2.*sigma**2)) if sp.sparse.issparse(X1) | sp.sparse.issparse(X2): K = sp.array(K) return K
def test_matrix_tensorify(): """Test whether this tensorification routine works""" A = sc.eye( 3 ) x = sc.random.rand(3) y = sc.ones( 3 ) B = matrix_tensorify( A, x ) assert ( B.dot( y ) == A * x.dot(y) ).all() C = B.swapaxes( 2, 0 ) assert ( C.dot( y ) == sc.outer(x, y) ).all() D = B.swapaxes( 2, 1 ) assert ( D.dot( y ) == sc.outer(y, x) ).all()
def diffmat(x): """Compute the differentiation matrix for x is an ordered array of grid points. Uses barycentric formulas for stability. """ n = sp.size(x) e = sp.ones((n,1)) Xdiff = sp.outer(x,e)-sp.outer(e,x)+sp.identity(n) xprod = -reduce(mul,Xdiff) # product of rows W = sp.outer(1/xprod,e) D = W/sp.multiply(W.T,Xdiff) d = 1-sum(D) for k in range(0,n): # Set diagonal elements D[k,k] = d[k] return -D.T
def K(eta, g, h, y, n2, pre_s1, pre_s2, pre_s3, qp, phix): phi = phix[:, 0] scaled_quadratures = phix[:, 1]/sqrt(eta) z = (outer(cos(phi), qp[:, 0]) + outer(sin(phi), qp[:, 1]) - scaled_quadratures[:, None]) / h zy = z/y zy2 = zy**2 f_denom = 1./(n2 + zy2[:, :, None]) v = zy*sin(z)*dot(f_denom, pre_s3) cos_z = cos(z) v += zy2*(dot(f_denom, pre_s1) - cos_z*dot(f_denom, pre_s2)) del f_denom v /= sqrt(pi) v += cos_z*(exp(y**2)-1./(2.*sqrt(pi)))+(1./(2.*sqrt(pi))-1.) v /= 4.*pi*g return v
def xNES(f, x0, maxEvals=1e6, verbose=False, targetFitness= -1e-10): """ Exponential NES (xNES), as described in Glasmachers, Schaul, Sun, Wierstra and Schmidhuber (GECCO'10). Maximizes a function f. Returns (best solution found, corresponding fitness). """ dim = len(x0) I = eye(dim) learningRate = 0.6 * (3 + log(dim)) / dim / sqrt(dim) batchSize = 4 + int(floor(3 * log(dim))) center = x0.copy() A = eye(dim) # sqrt of the covariance matrix numEvals = 0 bestFound = None bestFitness = -Inf while numEvals + batchSize <= maxEvals and bestFitness < targetFitness: # produce and evaluate samples samples = [randn(dim) for _ in range(batchSize)] fitnesses = [f(dot(A, s) + center) for s in samples] if max(fitnesses) > bestFitness: bestFitness = max(fitnesses) bestFound = samples[argmax(fitnesses)] numEvals += batchSize if verbose: print "Step", numEvals / batchSize, ":", max(fitnesses), "best:", bestFitness #print A # update center and variances utilities = computeUtilities(fitnesses) center += dot(A, dot(utilities, samples)) covGradient = sum([u * (outer(s, s) - I) for (s, u) in zip(samples, utilities)]) A = dot(A, expm2(0.5 * learningRate * covGradient)) return bestFound, bestFitness
def coulomb_mat_eigvals(atoms, at_idx, r_cut, do_calc_connect=True, n_eigs=20): if do_calc_connect: atoms.set_cutoff(8.0) atoms.calc_connect() pos = sp.vstack((sp.asarray([sp.asarray(a.diff) for a in atoms.neighbours[at_idx]]), sp.zeros(3))) Z = sp.hstack((sp.asarray([atoms.z[a.j] for a in atoms.neighbours[at_idx]]), atoms.z[at_idx])) M = sp.outer(Z, Z) / (sp.spatial.distance_matrix(pos, pos) + np.eye(pos.shape[0])) sp.fill_diagonal(M, 0.5 * Z ** 2.4) # data = [[atoms.z[a.j], sp.asarray(a.diff)] for a in atoms.neighbours[at_idx]] # data.append([atoms.z[at_idx], sp.array([0,0,0])]) # central atom # M = sp.zeros((len(data), len(data))) # for i, atom1 in enumerate(data): # M[i,i] = 0.5 * atom1[0] ** 2.4 # for j, atom2 in enumerate(data[i+1:]): # j += i+1 # M[i,j] = atom1[0] * atom2[0] / LA.norm(atom1[1] - atom2[1]) # M = 0.5 * (M + M.T) eigs = (LA.eigh(M, eigvals_only=True))[::-1] if n_eigs == None: return eigs # all elif eigs.size >= n_eigs: return eigs[:n_eigs] # only first few eigenvectors else: return sp.hstack((eigs, sp.zeros(n_eigs - eigs.size))) # zero-padded extra fields
def _updateWeights(self, state, action, reward, next_state, learned_policy=None): """ Policy is a function that returns a probability vector for all actions, given the current state(-features). """ if learned_policy is None: learned_policy = self._greedyPolicy self._updateEtraces(state, action) phi = zeros((self.num_actions, self.num_features)) phi[action] += state phi_n = outer(learned_policy(next_state), next_state) self._A += outer(ravel(self._etraces), ravel(phi - self.rewardDiscount * phi_n)) self._b += reward * ravel(self._etraces) self._theta = dot(pinv2(self._A), self._b).reshape(self.num_actions, self.num_features)
def metric (V): #XXX WRONG V_V = dot(V,V) u2 = 1 - V_V M = 1/(1.0 - 2 * u2) W = outer(V,V) / u2 I = identity(len(V)) return M * (I + W)
def _learnStep(self): """ Main part of the algorithm. """ I = eye(self.numParameters) self._produceSamples() utilities = self.shapingFunction(self._currentEvaluations) utilities /= sum(utilities) # make the utilities sum to 1 if self.uniformBaseline: utilities -= 1./self.batchSize samples = array(map(self._base2sample, self._population)) dCenter = dot(samples.T, utilities) covGradient = dot(array([outer(s,s) - I for s in samples]).T, utilities) covTrace = trace(covGradient) covGradient -= covTrace/self.numParameters * I dA = 0.5 * (self.scaleLearningRate * covTrace/self.numParameters * I +self.covLearningRate * covGradient) self._lastLogDetA = self._logDetA self._lastInvA = self._invA self._center += self.centerLearningRate * dot(self._A, dCenter) self._A = dot(self._A, expm2(dA)) self._invA = dot(expm2(-dA), self._invA) self._logDetA += 0.5 * self.scaleLearningRate * covTrace if self.storeAllDistributions: self._allDistributions.append((self._center.copy(), self._A.copy()))
def problem_params(lr, gam, memories, inpst, neurons): """ Return the lowest eigenvector of the classical Hamiltonian constructed by the learning rule, gamma, memories, and input. """ # Bias Hamiltonian alpha = gam * np.array(inpst) # Memory Hamiltonian beta = np.zeros((qubits, qubits)) if lr == "hebb": # Hebb rule memMat = sp.matrix(memories).T beta = sp.triu(memMat * memMat.T) / float(neurons) elif lr == "stork": # Storkey rule Wm = sp.zeros((neurons, neurons)) for m, mem in enumerate(memories): Am = sp.outer(mem, mem) - sp.eye(neurons) Wm += (Am - Am * Wm - Wm * Am) / float(neurons) beta = sp.triu(Wm) elif lr == "proj": # Moore-Penrose pseudoinverse rule memMat = sp.matrix(memories).T beta = sp.triu(memMat * sp.linalg.pinv(memMat)) # Find the eigenvectors evals, evecs = sp.linalg.eig(np.diag(alpha) + beta) idx = evals.argsort() return evals[idx], evecs[:, idx], np.diag(alpha), beta
def critic(self, lastreward, lastfeature, reward, feature): TDLearner.critic(self, lastreward, lastfeature, reward, feature) zeta = self.zeta() ff = self.module.decodeFeature(feature, 'first_order') # Cache q value to boost speed self.qvalue = self.stateActionValue(feature) preward = self.qvalue * ff # cache the first order feature to boost speed lff = self.module.decodeFeature(lastfeature, 'first_order') lastpreward = self.stateActionValue(lastfeature) * lff self.scaledfeature = lastpreward # Estimate of "avg reward". rweight = self.rdecay * self.gamma() self.eta = (1 - rweight) * self.eta + rweight * preward fd = lastfeature - feature rd = lastpreward - self.eta self.D = rd - scipy.dot(fd.reshape((1, -1)), self.T).reshape(-1) self.T += zeta * scipy.outer(self.z, self.D) # Update estimate of Hessian self.U = self.getHessianEstimate(feature) K = self.hessiansamplenumber + 1 # self.H = (1-1.0/K) * self.H + (1.0 / K) * self.U self.H = (1-1.0/K) * self.H + (1.0 / K) * self.U self.hessiansamplenumber += 1
def __iter__(self): dim = self.wrt.shape[0] I = scipy.eye(dim) # Square root of covariance matrix. A = scipy.eye(dim) center = self.wrt.copy() n_evals = 0 best_wrt = None best_x = float("-inf") for i, (args, kwargs) in enumerate(self.args): # Draw samples, evaluate and update best solution if a better one # was found. samples = scipy.random.standard_normal((self.batch_size, dim)) samples = scipy.dot(samples, A) + center fitnesses = [self.f(samples[j], *args, **kwargs) for j in range(samples.shape[0])] fitnesses = scipy.array(fitnesses).flatten() if fitnesses.max() > best_x: best_loss = fitnesses.max() self.wrt[:] = samples[fitnesses.argmax()] # Update center and variances. utilities = self.compute_utilities(fitnesses) center += scipy.dot(scipy.dot(utilities, samples), A) # TODO: vectorize this cov_gradient = sum([u * (scipy.outer(s, s) - I) for (s, u) in zip(samples, utilities)]) update = scipy.linalg.expm2(A * cov_gradient * self.step_rate * 0.5) A[:] = scipy.dot(A, update) yield dict(loss=-best_x, n_iter=i)
def compute_coloured_loading(mat, svd, target_cond=SUFFICIENT_CONDITION, overwrite_mat=False): """tries to condition :mat: by inflating the badly conditioned subspace of :mat: using a spherical constraint. :type mat: ndarray :param mat: input matrix :type svd: tuple :param svd: return tuple of svd(:mat:) - consistency will not be checked! :type target_cond: float :param target_cond: condition number to archive after loading :type overwrite_mat: bool :param overwrite_mat: if True, operate inplace and overwrite :mat: :returns: ndarray - matrix like :mat: conditioned s.t. cond = target_cond """ U, sv = svd[0], svd[1] if target_cond == 1.0: return sp.eye(mat.shape[0]) if target_cond > compute_matrix_cond(sv): return mat if overwrite_mat is True: rval = mat else: rval = mat.copy() min_s = sv[0] / target_cond for i in xrange(sv.size): col_idx = -1 - i if sv[col_idx] < min_s: alpha = min_s - sv[col_idx] rval += alpha * sp.outer(U[:, col_idx], U[:, col_idx]) return rval
def compute_loocv_gmm(variable,model,x,y,ids,K_u,alpha,beta,log_prop_u): """ Function that computes the estimation of the loocv for the GMM model with variables ids + variable(i) Inputs: model : the GMM model x,y : the training samples and the corresponding label ids : the pool of selected variables variable : the variable to be tested from the set of available variable K_u : the initial prediction values computed with all the samples alpha, beta and log_prop_u : constant that are computed outside of the loop to increased speed Outputs: loocv_temp : the loocv Used in GMM.forward_selection() """ n = x.shape[0] ids.append(variable) # Iteratively add one of the remaining variables Kp = model.predict_gmm(x,ids=ids)[1]# Predict with all the samples with ids loocv_temp=0.0; # Initialization of the temporary loocv for j in range(n): # Predict the class with the model ids_t Kloo = Kp[j,:] + K_u # Initialization of the decision rule for sample "j" #--- Change for only not C---# c = int(y[j]-1) # Update of parameter of class c m = (model.ni[c]*model.mean[c,ids] -x[j,ids])*alpha[c] # Update the mean value xb = x[j,ids] - m # x centered cov_u = (model.cov[c,ids,:][:,ids] - sp.outer(xb,xb)*alpha[c])*beta # Update the covariance matrix logdet,rcond = safe_logdet(cov_u) Kloo[c] = logdet - 2*log_prop_u[c] + sp.vdot(xb,mylstsq(cov_u,xb.T,rcond)) # Compute the new decision rule del cov_u,xb,m,c yloo = sp.argmin(Kloo)+1 loocv_temp += float(yloo==y[j]) # Check the correct/incorrect classification rule ids.pop() # Remove the current variable return loocv_temp/n # Compute loocv for variable
def critic(self, lastreward, lastfeature, reward, feature): LSTDLearner.critic(self, lastreward, lastfeature, reward, feature) self.updateCriticPara() self.qvalue = self.stateActionValue(feature) self.loglhgrad = self.module.decodeFeature(feature, 'first_order') # Update Q-tilde critic. Note that different with the notation in the # papar, we have minus here because of difference definition of # featureDifference. self.T = dot(self.invA, self.V) # Update estimate of Hessian self.U = self.getHessianEstimate(feature) rweight = 1.0 / (self.k + 1) self.H = rweight * self.H + self.U self.hessiansamplenumber += 1 # Update estimates self.scaledfeature = self.stateActionValue(feature) * self.loglhgrad self.eta += self.zeta() * self.scaledfeature vupdate = outer(self.z, self.scaledfeature - self.eta) self.V += self.zeta() * (vupdate - self.V)
def LSTD_Qvalues(Ts, policy, R, fMap, discountFactor): """ LSTDQ is like LSTD, but with features replicated once for each possible action. Returns Q-values in a 2D array. """ numA = len(Ts) dim = len(Ts[0]) numF = len(fMap) fMapRep = zeros((numF * numA, dim * numA)) for a in range(numA): fMapRep[numF * a:numF * (a + 1), dim * a:dim * (a + 1)] = fMap statMatrix = zeros((numF * numA, numF * numA)) statResidual = zeros(numF * numA) for sto in range(dim): r = R[sto] fto = zeros(numF * numA) for nextA in range(numA): fto += fMapRep[:, sto + nextA * dim] * policy[sto][nextA] for sfrom in range(dim): for a in range(numA): ffrom = fMapRep[:, sfrom + a * dim] prob = Ts[a][sfrom, sto] statMatrix += outer(ffrom, ffrom - discountFactor * fto) * prob statResidual += ffrom * r * prob Qs = zeros((dim, numA)) w = lstsq(statMatrix, statResidual)[0] for a in range(numA): Qs[:,a] = dot(w[numF*a:numF*(a+1)], fMap) return Qs
def sample_moments( X1, X2, X3, k, a0 ): """Get the sample moments from data Assumes every row of the document corresponds to one data point """ #N, W = X1.shape # Get three uncorrelated sections of the data M1 = X1.mean(0) M2 = Pairs( X1, X2 ) M3 = Triples( X1, X2, X3 ) P = M2 - a0/(a0+1) * outer(M1, M1) T = lambda theta: (M3(theta) - a0/(a0+2) * (M2.dot( outer(theta, M1)) + outer( M1, theta ).dot( M2 ) + theta.dot(M1) * M2 ) + 2 * a0**2/((a0+2)*(a0+1)) * (theta.dot(M1) * outer(M1, M1))) return P, T
def _updateSigmas(self, updateSize, lastSample): for c in range(self.numberOfCenters): self.sigmas[c] *= (1. - updateSize[c]) dif = self.mus[c] - lastSample if self.diagonalOnly: self.sigmas[c] += updateSize[c] * multiply(dif, dif) else: self.sigmas[c] += updateSize[c] * 1.2 * outer(dif, dif)
def Kgrad_theta(self, theta, x1, d): K = SP.zeros([x1.shape[0], x1.shape[0]]) ic = 0 for i in xrange(self.n_groups): for j in xrange(i + 1): # select corresponding parameter if ic == d: A = 1.0 # if (i==j): # A = 2*SP.exp(2*theta[ic]) x1_ = x1 == i x2_ = x1 == j K0_ = SP.outer(x1_, x2_) | SP.outer(x2_, x1_) K = A * K0_ return K ic += 1 return K
def K(self, theta, x1, x2=None): # get input data: x1, x2 = self._filter_input_dimensions(x1, x2) # 2. exponentiate params: # cycle through components of the full matrix K = SP.zeros([x1.shape[0], x2.shape[0]]) ic = 0 for i in xrange(self.n_groups): for j in xrange(i + 1): A = theta[ic] # if (i==j): # A = SP.exp(2*theta[ic]) x1_ = x1 == i x2_ = x2 == j K0_ = SP.outer(x1_, x2_) | SP.outer(x2_, x1_) K += A * K0_ ic += 1 return K
def calc_mutual_information(probability_mat): """ Calculates the mutual information of stimulus and predicted stimulus from a probability matrix. :rtype: float """ marginals = sp.outer( sp.sum(probability_mat, axis=1), sp.sum(probability_mat, axis=0)) p = probability_mat[probability_mat != 0.0] m = marginals[probability_mat != 0.0] return sp.sum(p * sp.log(p / m))
def _logDerivsFactorSigma(self, samples, mu, invSigma, factorSigma): """ Compute the log-derivatives w.r.t. the factorized covariance matrix components. This implementation should be faster than the one in Vanilla. """ res = zeros((len(samples), self.numDistrParams - self.numParameters)) invA = inv(factorSigma) diagInvA = diag(diag(invA)) for i, sample in enumerate(samples): s = dot(invA.T, (sample - mu)) R = outer(s, dot(invA, s)) - diagInvA res[i] = triu2flat(R) return res
def compute_divKL(direction, variables, model, idx): """ Function that computes the Kullback–Leibler divergence of the model_cv using the variables : idx +/- one of variables Inputs: variables: the variable to add to idx model_cv: the model build with all the variables idx: the pool of retained variables Output: divKL: the estimated Kullback–Leibler divergence Used in GMM.forward_selection() and GMM.backward_selection() """ # Get machine precision eps = sp.finfo(sp.float64).eps # Initialization divKL = sp.zeros(variables.size) invCov = sp.empty((model.C,len(idx),len(idx))) if len(idx)==0: for k,var in enumerate(variables): for i in xrange(model.C): alphaI = 1/float(model.cov[i,var,var]) if alphaI < eps: alphaI = eps for j in xrange(i+1,model.C): alphaJ = 1/float(model.cov[j,var,var]) if alphaJ < eps: alphaJ = eps md = (model.mean[i,var]-model.mean[j,var]) divKL[k] += 0.5*( alphaI*model.cov[j,var,var] + alphaJ*model.cov[i,var,var] + md*(alphaI + alphaJ)*md ) * model.prop[i]*model.prop[j] else: # Compute invcov de idx for c in xrange(model.C): vp,Q,_ = model.decomposition(model.cov[c,idx,:][:,idx]) invCov[c,:,:] = sp.dot(Q,((1/vp)*Q).T) del vp,Q if direction=='forward': invCov_update = sp.empty((model.C,len(idx)+1,len(idx)+1)) elif direction=='backward': invCov_update = sp.empty((model.C,len(idx)-1,len(idx)-1)) for k,var in enumerate(variables): if direction=='forward': id_t = list(idx) id_t.append(var) elif direction=='backward': id_t = list(idx) id_t.remove(var) mask = sp.ones(len(idx), dtype=bool) mask[k] = False if direction=='forward': for c in xrange(model.C): alpha = model.cov[c,var,var] - sp.dot(model.cov[c,var,:][idx], sp.dot(invCov[c,:,:],model.cov[c,var,:][idx].T) ) if alpha < eps: alpha = eps invCov_update[c,:-1,:][:,:-1] = invCov[c,:,:] + 1/alpha * sp.outer( sp.dot(invCov[c,:,:],model.cov[c,var,:][idx].T) , sp.dot(model.cov[c,var,:][idx],invCov[c,:,:]) ) invCov_update[c,:-1,-1] = - 1/alpha * sp.dot(invCov[c,:,:],model.cov[c,var,:][idx].T) invCov_update[c,-1,:-1] = - 1/alpha * sp.dot(model.cov[c,var,:][idx],invCov[c,:,:]) invCov_update[c,-1,-1] = 1/alpha elif direction=='backward': for c in xrange(model.C): invCov_update[c,:,:] = invCov[c,mask,:][:,mask] - 1/invCov[c,k,k] * sp.outer(model.cov[c,var,:][id_t] , model.cov[c,var,:][id_t]) for i in xrange(model.C): for j in xrange(i+1,model.C): md = (model.mean[i,id_t]-model.mean[j,id_t]) divKL[k] += 0.5*( sp.trace( sp.dot( invCov_update[j,:,:],model.cov[i,id_t,:][:,id_t] ) + sp.dot( invCov_update[i,:,:],model.cov[j,id_t,:][:,id_t] ) ) + sp.dot(md,sp.dot(invCov_update[j,:,:]+invCov_update[i,:,:],md.T)) ) * model.prop[i]*model.prop[j] return divKL
def selection(self, direction, samples, labels, criterion='accuracy', varNb=0.2, nfold=5, ncpus=None, random_state=0): """ Function which selects the most discriminative variables according to a given search method Inputs: direction: 'backward' or 'forward' or 'SFFS' samples, labels: the training samples and their labels criterion: the criterion function to use for selection (accuracy, kappa, F1Mean, JM, divKL). Default: 'accuracy' varNb: maximum number of extracted variables. Default value: 20% of the original number nfold: number of folds for the cross-validation. Default value: 5 ncpus: number of cpus to use for parallelization. Default: all Outputs: idx: the selected variables criterionEvolution: values of the criterion function at each selection step bestSets: all the sets of selected variables (usefull only with SFFS) """ # Get some information from the variables n = samples.shape[0] # Number of samples # Cast to speed processing time labels = labels.ravel().astype(int) if criterion == 'accuracy' or criterion == 'F1Mean' or criterion == 'kappa': # Creation of folds kfold = StratifiedKFold(n_splits=nfold,shuffle=True,random_state=random_state) # kfold.split() is a generator ## Pre-update the models model_pre_cv = [GMMFeaturesSelection(d=self.d, C=self.C) for i in xrange(nfold)] for k, (trainInd,testInd) in enumerate(kfold.split(samples,labels.ravel())): # Get training data for this cv round testSamples,testLabels = samples[testInd,:], labels[testInd] nk = float(testLabels.size) # Update the model for each class for c in xrange(self.C): classInd = sp.where(testLabels==(c+1))[0] nk_c = float(classInd.size) mean_k = sp.mean(testSamples[classInd,:],axis=0) cov_k = compute_cov(testSamples[classInd,:],mean_k,nk_c) model_pre_cv[k].nbSpl[c] = self.nbSpl[c] - nk_c model_pre_cv[k].mean[c,:] = (self.nbSpl[c]*self.mean[c,:]-nk_c*mean_k)/(self.nbSpl[c]-nk_c) model_pre_cv[k].cov[c,:] = ((self.nbSpl[c]-1)*self.cov[c,:,:] - (nk_c-1)*cov_k - nk_c*self.nbSpl[c]/model_pre_cv[k].nbSpl[c]*sp.outer(self.mean[c,:]-mean_k,self.mean[c,:]-mean_k))/(model_pre_cv[k].nbSpl[c]-1) del classInd,nk_c,mean_k,cov_k # Update proportion model_pre_cv[k].prop = model_pre_cv[k].nbSpl/(n-nk) # Precompute cst model_pre_cv[k].logprop = 2*sp.log(model_pre_cv[k].prop) del testSamples,testLabels,nk else: kfold = None model_pre_cv = None if direction == 'forward': idx, criterionEvolution = self.forward_selection(samples, labels, criterion, varNb, kfold, model_pre_cv, ncpus) return idx, criterionEvolution, [] elif direction == 'backward': idx, criterionEvolution = self.backward_selection(samples, labels, criterion, varNb, kfold, model_pre_cv, ncpus) return idx, criterionEvolution, [] elif direction == 'SFFS': return self.floating_forward_selection(samples, labels, criterion, varNb, kfold, model_pre_cv, ncpus)
def parameters(cmdargs): """ cmdargs: -q, qubits -k, lrule -f, nmems """ # The Hopfield parameters hparams = { 'numNeurons': cmdargs['qubits'], 'inputState': [ 2 * sp.random.random_integers(0, 1) - 1 for k in xrange(cmdargs['qubits']) ], 'learningRule': cmdargs['simtype'], 'numMemories': int(cmdargs['farg']) } # Construct memories memories = [[ 2 * sp.random.random_integers(0, 1) - 1 for k in xrange(hparams['numNeurons']) ] for j in xrange(hparams['numMemories'])] # At least one pattern must be one Hamming unit away from the input memories[0] = list(hparams['inputState']) memories[0][sp.random.random_integers(0, hparams['numNeurons'] - 1)] *= -1 # Make sure all other patterns have Hamming distance > 1 def hamdist(a, b): """ Calculate Hamming distance. """ return sp.sum(abs(sp.array(a) - sp.array(b)) / 2.0) # Loop over additional memories, if there are any for imem, mem in enumerate(memories[1:]): while hamdist(mem, hparams['inputState']) < 2.0: # Flip a random spin rndbit = sp.random.random_integers(0, hparams['numNeurons'] - 1) memories[imem + 1][rndbit] *= -1 # Basic simulation params nQubits = hparams['numNeurons'] T = 1000.0 # sp.arange(0.1, 15, 0.5) # T = sp.array([10.0, 20.0, 50.0, 100.0]) dt = 0.01 * T # Define states for which to track probabilities in time # import statelabels # label_list = statelabels.GenerateLabels(nQubits) # stateoverlap = [] # for mem in memories: # # Convert spins to bits # bitstr = ''.join([ '0' if k == 1 else '1' for k in mem ]) # # Get the index of the current (converted) memory and add it to list # stateoverlap.append([ label_list.index(bitstr), bitstr ]) stateoverlap = None # Output parameters binary = 1 # Save output files as binary Numpy format progressout = 0 # Output simulation progress over anneal timesteps eigspecdat = 1 # Output data for eigspec eigspecplot = 0 # Plot eigspec eigspecnum = 2**nQubits # Number of eigenvalues to output fidelplot = 0 # Plot fidelity fideldat = 0 # Output fidelity data fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates overlapdat = 0 # Output overlap data overlapplot = 0 # Plot overlap solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM' # Output directory stuff probdir = 'data/hopfield_exp1_tuned/n'+str(nQubits)+'p'+\ str(hparams['numMemories'])+hparams['learningRule'] if isinstance(T, collections.Iterable): probdir += 'MultiT' if os.path.isdir(probdir): outlist = sorted( [int(name) for name in os.listdir(probdir) if name.isdigit()]) else: outlist = [] outnum = outlist[-1] + 1 if outlist else 0 outputdir = probdir + '/' + str(outnum) + '/' probshow = 0 # Print final state probabilities to screen probout = 1 # Output probabilities to file mingap = 0 # Record the minimum spectral gap errchk = 0 # Error-checking on/off (for simulation accuracy) eps = 0.01 # Numerical error in normalization condition (1 - norm < eps) # Specify a QUBO (convert to Ising = True), or alpha, beta directly # (convert = False), and also specify the signs on the Ising Hamiltonian # terms (you can specify coefficients too for some problems if needed) isingConvert = 0 isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1} # Construct network Ising parameters neurons = nQubits # This is gamma, the appropriate weighting on the input vector # isingSigns['hz'] *= 1 - (len(hparams['inputState']) - # hparams['inputState'].count(0))/(2*neurons) # isingSigns['hz'] *= 1.0/(5*neurons) isingSigns['hz'] *= 0.2 alpha = sp.array(hparams['inputState']) beta = sp.zeros((neurons, neurons)) delta = sp.array([]) # Construct the memory matrix according to a learning rule if hparams['learningRule'] == 'hebb': # Hebb rule isingSigns['hz'] *= 0.7 memMat = sp.matrix(memories).T beta = sp.triu(memMat * memMat.T) / float(neurons) elif hparams['learningRule'] == 'stork': # Storkey rule isingSigns['hz'] *= 0.15 Wm = sp.zeros((neurons, neurons)) for m, mem in enumerate(memories): Am = sp.outer(mem, mem) - sp.eye(neurons) Wm += (Am - Am * Wm - Wm * Am) / float(neurons) beta = sp.triu(Wm) elif hparams['learningRule'] == 'proj': isingSigns['hz'] *= 0.15 # Moore-Penrose pseudoinverse rule memMat = sp.matrix(memories).T beta = sp.triu(memMat * sp.linalg.pinv(memMat)) # Some outputs outputs = { 'nQubits': nQubits, 'learningRule': hparams['learningRule'], 'outdir': probdir, 'inputState': hparams['inputState'], 'memories': memories, 'answer': memories[0], 'annealTime': list(T) if isinstance(T, collections.Iterable) else T } ############################################################################ ######## All variables must be specified here, do NOT change the keys ###### ############################################################################ return { 'nQubits': nQubits, 'Q': None, 'T': T, 'dt': dt, 'outputdir': outputdir, 'errchk': errchk, 'eps': eps, 'isingConvert': isingConvert, 'isingSigns': isingSigns, 'outputs': outputs, 'alpha': alpha, 'beta': beta, 'delta': delta, 'eigdat': eigspecdat, 'eigplot': eigspecplot, 'eignum': eigspecnum, 'fiddat': fideldat, 'fidplot': fidelplot, 'fidnumstates': fidelnumstates, 'overlapdat': overlapdat, 'overlapplot': overlapplot, 'outdir': outputdir, 'binary': binary, 'progressout': progressout, 'probshow': probshow, 'probout': probout, 'mingap': mingap, 'stateoverlap': stateoverlap, 'hzscale': None, 'hzzscale': None, 'hxscale': None, 'solveMethod': solveMethod }
float(p) for i, p in enumerate(f.readline().split()) if i > 0 ] # construct molecule from species x y z charge molecule = [] try: for j in range(natoms): # molecule.append([float(x) for i, x in enumerate(f.readline().split()) if i > 0]) molecule.append( [x for i, x in enumerate(f.readline().split())]) molecule = sp.array(molecule) nuc_charge = species_to_nuc_charge(molecule[:, 0]) molecule = sp.array(molecule[:, 1:], dtype='float64') pos = molecule[:, :-1] # construct Coulomb matrix X = sp.outer(nuc_charge, nuc_charge) / ( dist.squareform(dist.pdist(pos)) + sp.eye(natoms)) sp.fill_diagonal(X, 0.5 * sp.absolute(nuc_charge)**2.4) # add all properties of current molecule to dataset [ dataset[k].append(properties[ordering[i]]) for i, k in enumerate(dataset.keys()) ] dataset['idx'][-1] = int(dataset['idx'][-1]) # index is an integer X_all.append(X) charge.append(molecule[:, -1]) except Exception as err: print "Molecule %s skipped, malformed file" % file print err pass
def _logDerivFactorSigma(self, sample, x, invSigma, factorSigma): logDerivSigma = 0.5 * dot(dot(invSigma, outer(sample - x, sample - x)), invSigma) - 0.5 * invSigma if self.vanillaScale: logDerivSigma = multiply(outer(diag(abs(self.factorSigma)), diag(abs(self.factorSigma))), logDerivSigma) return triu2flat(dot(factorSigma, (logDerivSigma + logDerivSigma.T)))
def parameters(cmdargs): """ """ # The Hopfield parameters hparams = { 'numNeurons': 4, 'inputState': [1, -1, 1, -1], 'learningRule': cmdargs['simtype'], 'bias': float(cmdargs['farg']) } # Construct memories # memories = sp.linalg.hadamard(hparams['numNeurons']).tolist() # memories = memories[:1] memories = [[1, -1, 1, -1], [-1, 1, 1, 1], [-1, -1, 1, 1], [1, -1, 1, 1]] memories = [memories[0]] # Basic simulation params nQubits = hparams['numNeurons'] T = 1000.0 # dt = 0.01*T dt = 0.001 * T # Output parameters binary = 1 # Save output files as binary Numpy format progressout = 0 # Output simulation progress over anneal timesteps eigspecdat = 1 # Output data for eigspec eigspecplot = 0 # Plot eigspec eigspecnum = 2**(nQubits - 1) # Number of eigenvalues to output fidelplot = 0 # Plot fidelity fideldat = 0 # Output fidelity data fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates overlapdat = 0 # Output overlap data overlapplot = 0 # Plot overlap solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM' # Output directory stuff probdir = 'data/testcases_fixed/ortho/n'+str(nQubits)+'p'+\ str(len(memories))+hparams['learningRule'] if isinstance(T, collections.Iterable): probdir += 'MultiT' if os.path.isdir(probdir): outlist = sorted( [int(name) for name in os.listdir(probdir) if name.isdigit()]) else: outlist = [] outnum = outlist[-1] + 1 if outlist else 0 outputdir = probdir + '/' + str(outnum) + '/' probshow = 0 # Print final state probabilities to screen probout = 1 # Output probabilities to file mingap = 1 # Record the minimum spectral gap errchk = 0 # Error-checking on/off (for simulation accuracy) eps = 0.01 # Numerical error in normalization condition (1 - norm < eps) # Specify a QUBO (convert to Ising = True), or alpha, beta directly # (convert = False), and also specify the signs on the Ising Hamiltonian # terms (you can specify coefficients too for some problems if needed) isingConvert = 0 isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1} # Construct network Ising parameters neurons = nQubits # This is gamma, the appropriate weighting on the input vector # isingSigns['hz'] *= 1 - (len(hparams['inputState']) - # hparams['inputState'].count(0))/(2*neurons) isingSigns['hz'] *= hparams['bias'] alpha = sp.array(hparams['inputState']) beta = sp.zeros((neurons, neurons)) delta = sp.array([]) # Construct the memory matrix according to a learning rule if hparams['learningRule'] == 'hebb': # Construct pattern matrix according to Hebb's rule memMat = sp.matrix(memories).T beta = sp.triu(memMat * memMat.T) / float(neurons) elif hparams['learningRule'] == 'stork': # Construct the memory matrix according to the Storkey learning rule Wm = sp.zeros((neurons, neurons)) for m, mem in enumerate(memories): Am = sp.mat((sp.outer(mem, mem) - sp.eye(neurons))) Wm += (Am - Am * Wm - Wm * Am) / float(neurons) beta = sp.triu(Wm) elif hparams['learningRule'] == 'proj': # Construct memory matrix according to the Moore-Penrose pseudoinverse rule memMat = sp.matrix(memories).T beta = sp.triu(memMat * sp.linalg.pinv(memMat)) # Some outputs outputs = { 'nQubits': nQubits, 'learningRule': hparams['learningRule'], 'outdir': probdir, 'inputState': hparams['inputState'], 'memories': memories, 'answer': memories[0], 'bias': hparams['bias'], 'annealTime': list(T) if isinstance(T, collections.Iterable) else T } ############################################################################ ######## All variables must be specified here, do NOT change the keys ###### ############################################################################ return { 'nQubits': nQubits, 'Q': None, 'T': T, 'dt': dt, 'outputdir': outputdir, 'errchk': errchk, 'eps': eps, 'isingConvert': isingConvert, 'isingSigns': isingSigns, 'outputs': outputs, 'alpha': alpha, 'beta': beta, 'delta': delta, 'eigdat': eigspecdat, 'eigplot': eigspecplot, 'eignum': eigspecnum, 'fiddat': fideldat, 'fidplot': fidelplot, 'fidnumstates': fidelnumstates, 'overlapdat': overlapdat, 'overlapplot': overlapplot, 'outdir': outputdir, 'binary': binary, 'progressout': progressout, 'probshow': probshow, 'probout': probout, 'mingap': mingap, 'stateoverlap': None, 'hzscale': None, 'hzzscale': None, 'hxscale': None, 'solveMethod': solveMethod }
def updateTau(x1, x2, w1, w2, y, tau_prior_strength, tau_pr_den): b_star = y**2 - 2 * y * (SP.outer(x1, w1)) + SP.outer(x2, w2) tau = (tau_prior_strength + 0.5) / (tau_pr_den + 0.5 * b_star) return tau
def forward_selection(self, x, y, delta=0.1, maxvar=None, v=5, ncpus=None): """ Function that selects the most discriminative variables according to a forward search Inputs: x,y : the training samples and their labels delta : the minimal improvement in percentage when a variable is added to the pool, the algorithm stops if the improvement is lower than delta. Default value 0.1% maxvar: maximum number of extracted variables. Default valule: 20% of the origianl number v: number of folds for the cross-validation. Default value: None -> do loocv and use fast estimation of the updated model. Otherwise, do fold-fold cross-validation with conventionnal learning of the model ncpus= Outputs: ids: the selected variable OA: the accuracy estimated for each ids by loocv or v-fold cv """ ## Get some information from the variable C = int(y.max(0)) # Number of classes n = x.shape[0] # Number of samples d = x.shape[1] # Number of variables if ncpus is None: ncpus = mp.cpu_count() # Get the number of core ## Initialization r = 0 # Initialization of the counter variable = sp.arange(d) # At step zero: d variables available ids = [] # and no selected variable OA = [] # list of the evolution the OA estimation if maxvar is None: maxvar = sp.floor( d / 5) # Select at max 20 % of the original number of variables if v is None: # LOOCV estimation of the error ## Precompute the proportion and some others constants log_prop_u = [ sp.log((n * self.prop[c] - 1.0) / (n - 1.0)) for c in range(C) ] K_u = 2 * sp.log((n - 1.0) / n) beta = self.ni[c] / (self.ni[c] - 1.0 ) # Constant for the rank one downdate alpha = [1 / (self.ni[c] - 1) for c in range(C)] ## Start the forward search while (r < maxvar): loocv = sp.zeros(variable.size) pool = mp.Pool(processes=ncpus) processes = [ pool.apply(compute_loocv_gmm, args=(v, self, x, y, ids, K_u, alpha, beta, log_prop_u)) for v in variable ] pool.close() pool.join() for i, p in enumerate(processes): loocv[i] = p ## Select the variable that provides the highest loocv t = sp.argmax(loocv) # get the indice of the maximum of loocv OA.append(loocv[t]) # add the value to loo if r == 0: ids.append( variable[t]) # add the selected variable to the pool variable = sp.delete( variable, t) # remove the selected variable from the initial set elif (variable.size == 0) or (( (OA[r] - OA[r - 1]) / OA[r - 1] * 100) < delta): OA.pop() break else: ids.append(variable[t]) variable = sp.delete(variable, t) r = r + 1 else: cv = CV() # Initialize the CV sets cv.split_data_class(y, v=v) # Generate split indices for the data ## Pre-update the models model_pre_cv = [] for i in range(v): model_pre_cv.append(GMM(size=C, d=d)) # List of updated GMM models X, Y = x[cv.iT[i], :], y[cv.iT[i]] nu = float(Y.size) for j in range(C): #Update the model for each class k = sp.where(Y == (j + 1))[0] nu_c = float(k.size) mean_t = sp.mean(X[k, :], axis=0) cov_t = sp.cov(X[k, :], bias=1, rowvar=0) model_pre_cv[i].ni[j] = self.ni[j] - nu_c model_pre_cv[i].prop[j] = model_pre_cv[i].ni[j] / (n - nu) model_pre_cv[i].mean[ j, :] = (self.ni[j] * self.mean[j, :] - nu_c * mean_t) / (self.ni[j] - nu_c) model_pre_cv[i].cov[j, :] = ( self.ni[j] * self.cov[j, :, :] - nu_c * cov_t - nu_c * self.ni[j] / model_pre_cv[i].ni[j] * sp.outer( self.mean[j, :] - mean_t, self.mean[j, :] - mean_t) ) / model_pre_cv[i].ni[j] del k, nu_c, mean_t, cov_t del X, Y, nu ## Start the forward search while (r < maxvar): err = sp.zeros(variable.size) pool = mp.Pool(processes=ncpus) processes = [ pool.apply_async(compute_v_cv_gmm, args=(variable, model_pre_cv[i], x[cv.iT[i], :], y[cv.iT[i]], ids)) for i in xrange(v) ] pool.close() pool.join() for p in processes: err += p.get() err /= v del processes, pool ## Select the variable that provides the highest loocv t = sp.argmax(err) # get the indice of the maximum of loocv OA.append(err[t]) # add the value to loo if r == 0: ids.append( variable[t]) # add the selected variable to the pool variable = sp.delete( variable, t) # remove the selected variable from the initial set elif (variable.size == 0) or (( (OA[r] - OA[r - 1]) / OA[r - 1] * 100) < delta): OA.pop() break else: ids.append(variable[t]) variable = sp.delete(variable, t) r += 1 ## Return the final value return ids, OA
def hebb(neurons, memories): W = [] for m, mem in enumerate(memories): W.append(sp.triu(sp.outer(mem, mem)-sp.eye(neurons))) return np.triu(np.sum(W, axis=0))/float(neurons)
def removeInactiveFactors(self, by_norm=0, by_r2=0): """Method to remove inactive factors PARAMETERS ---------- by_norm: float threshold to shut down factors based on the norm of the latent variable CURRENTLY NOT IMPLEMENTED by_r2: float threshold to shut down factors based on the coefficient of determination """ drop_dic = {} # Shut down based on norm of latent variable vectors if by_norm > 0: Z = self.nodes["Z"].getExpectation() drop_dic["by_norm"] = s.where((Z**2).mean(axis=0) < by_norm)[0] if len(drop_dic["by_norm"]) > 0: print("\n...A Factor has a norm smaller than {0}, dropping it and recomputing ELBO...\n".format(by_norm)) drop_dic["by_norm"] = [ s.random.choice(drop_dic["by_norm"]) ] # Shut down based on coefficient of determination with respect to the residual variance # Advantages: it takes into account both weights and latent variables, is based on how well the model fits the data # Disadvantages: slow, doesnt work with non-gaussian data if by_r2>0: Z = self.nodes['Z'].getExpectation() Y = self.nodes["Y"].getExpectation() W = self.nodes["SW"].getExpectation() all_r2 = s.zeros([self.dim['M'], self.dim['K']]) initial_k = 0 for m in range(self.dim['M']): # Fetch the mask for missing vlaues mask = self.nodes["Y"].getNodes()[m].getMask() # Fetch the data Ym = Y[m].data.copy() # If there is an intercept term, regress it out if s.all(Z[:,0]==1.): Ym -= W[m][:,0] all_r2[:,0] = 1. initial_k = 1 # Subtract mean and compute sum of squares (denominator) # Ym[mask] = 0. # Ym -= s.mean(Ym, axis=0) # Ym[mask] = 0. # Compute R2 SS = (Ym**2.).sum() for k in range(initial_k,self.dim['K']): Ypred_mk = s.outer(Z[:,k], W[m][:,k]) Ypred_mk[mask] = 0. Res = ((Ym - Ypred_mk)**2.).sum() all_r2[m,k] = 1. - Res/SS # Select factor to remove. If multiple, then just pick one at random. drop_dic["by_r2"] = s.where( (all_r2>by_r2).sum(axis=0) == 0)[0] # drop_dic["by_r2"] = s.where( ((all_r2)>by_r2+1e-16).sum(axis=0) == 0)[0] if len(drop_dic["by_r2"]) > 0: print("\n...A Factor explains less than {0}% of variance, dropping it and recomputing ELBO...\n".format(by_r2*100)) drop_dic["by_r2"] = [ s.random.choice(drop_dic["by_r2"]) ] # drop one factor at a time # Drop the factors drop = s.unique(s.concatenate(list(drop_dic.values()))) if len(drop) > 0: for node in self.nodes.keys(): self.nodes[node].removeFactors(drop) self.dim['K'] -= len(drop) # TO-DO: THIS ALSO COUNTS COVARIATES if self.dim['K']==0: print("Shut down all components, no structure found in the data.") sys.stdout.flush() exit() pass
def _calcBatchUpdate(self, fitnesses): samples = self.allSamples[-self.batchSize:] d = self.numParameters invA = inv(self.factorSigma) invSigma = inv(self.sigma) diagInvA = diag(diag(invA)) # efficient computation of V, which corresponds to inv(Fisher)*logDerivs V = zeros((self.numDistrParams, self.batchSize)) # u is used to compute the uniform baseline u = zeros(self.numDistrParams) for i in range(self.batchSize): s = dot(invA.T, (samples[i] - self.x)) R = outer(s, dot(invA, s)) - diagInvA flatR = triu2flat(R) u[:d] += fitnesses[i] * (samples[i] - self.x) u[d:] += fitnesses[i] * flatR V[:d, i] += samples[i] - self.x V[d:, i] += flatR j = self.numDistrParams - 1 D = 1 / invSigma[-1, -1] # G corresponds to the blocks of the inv(Fisher) G = 1 / (invSigma[-1, -1] + invA[-1, -1]**2) u[j] = dot(G, u[j]) V[j, :] = dot(G, V[j, :]) j -= 1 for k in reversed(list(range(d - 1))): p = invSigma[k + 1:, k] w = invSigma[k, k] wg = w + invA[k, k]**2 q = dot(D, p) c = dot(p, q) r = 1 / (w - c) rg = 1 / (wg - c) t = -(1 + r * c) / w tg = -(1 + rg * c) / wg G = blockCombine([[rg, tg * q], [mat(tg * q).T, D + rg * outer(q, q)]]) D = blockCombine([[r, t * q], [mat(t * q).T, D + r * outer(q, q)]]) u[j - (d - k - 1):j + 1] = dot(G, u[j - (d - k - 1):j + 1]) V[j - (d - k - 1):j + 1, :] = dot(G, V[j - (d - k - 1):j + 1, :]) j -= d - k # determine the update vector, according to different baselines. if self.baselineType == self.BLOCKBASELINE: update = zeros(self.numDistrParams) vsquare = multiply(V, V) j = self.numDistrParams - 1 for k in reversed(list(range(self.numParameters))): b0 = sum(vsquare[j - (d - k - 1):j + 1, :], 0) b = dot(b0, fitnesses) / sum(b0) update[j - (d - k - 1):j + 1] = dot( V[j - (d - k - 1):j + 1, :], (fitnesses - b)) j -= d - k b0 = sum(vsquare[:j + 1, :], 0) b = dot(b0, fitnesses) / sum(b0) update[:j + 1] = dot(V[:j + 1, :], (fitnesses - b)) elif self.baselineType == self.SPECIFICBASELINE: update = zeros(self.numDistrParams) vsquare = multiply(V, V) for j in range(self.numDistrParams): b = dot(vsquare[j, :], fitnesses) / sum(vsquare[j, :]) update[j] = dot(V[j, :], (fitnesses - b)) elif self.baselineType == self.UNIFORMBASELINE: v = sum(V, 1) update = u - dot(v, u) / dot(v, v) * v elif self.baselineType == self.NOBASELINE: update = dot(V, fitnesses) else: raise NotImplementedError('No such baseline implemented') return update / self.batchSize
def _backwardImplementation(self, outerr, inerr, inbuf): p = reshape(self.params, (self.outdim, self.indim)) * (1 - eye(self.outdim)) inerr += dot(p.T, outerr) ds = self.derivs ds += outer(inbuf, outerr).T.flatten()
def stork(neurons, memories): Wm = sp.zeros((neurons,neurons)) for m, mem in enumerate(memories): Am = sp.outer(mem,mem) - sp.eye(neurons) Wm += (Am - Am*Wm - Wm*Am)/float(neurons) return sp.triu(Wm)
plt.figure() for n in range(numComp): plt.subplot(numpy.ceil(numComp), 2, n + 1) plt.gca().set_color_cycle(['blue']) plt.plot(G[n]) plt.ylim(0, G.max()) plt.xlim(0, G.shape[1]) plt.ylabel('Componente %d' % n) plt.savefig('bonfacomp.png') G1 = G # Reproduzir o sinal de cada componente reconstructed_signal = scipy.zeros(len(x)) for n in range(numComp): Y = scipy.outer(B[:, n], G[n]) * numpy.exp(1j * numpy.angle(S)) y = librosa.istft(Y) reconstructed_signal[:len(y)] += y ipd.display(ipd.Audio(y, rate=sr)) # Em seguida, repare os modelos aprendidos e aplique-os ao suspeito de plágio mais alguns componentes extras storeB = B # componentes extras numCompFixed = numComp numComp = 2 # carregar amostra suspeita (x, sr) = librosa.load('Gotye - Somebody That I Used To Know.wav' ) # x = a matriz de áudio e sr=taxa de amostragem ipd.Audio(x, rate=sr)
# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import scipy box_l = 10 data = scipy.genfromtxt("coulomb_mixed_periodicity_system.data") n = data.shape[0] pos = data[:, 1:4] q = data[:, 4] forces = scipy.zeros((n, 3)) energy = 0. q1q2 = scipy.outer(q, q) images = 2000 for i in range(n): for x in range(-images, images + 1, 1): for y in range(-images, images + 1, 1): if x**2 + y**2 > images**2: continue pos_diff = pos[i] - (pos + scipy.array((x, y, 0)) * box_l) r = scipy.sqrt(scipy.sum(pos_diff**2, 1)) r3 = r**3 qq = q1q2[i, :] tmp = qq / r tmp[abs(tmp) == scipy.inf] = 0 energy += scipy.sum(tmp)
def lowerBound(x1, x2, w1, w2, y, tau): xw = SP.outer(x1, w1) return SP.nansum(tau * (y**2 + SP.outer(x2, w2) - 2 * xw * y))
print("\n\n Tri3 \n") Printer(IP, N, dN, w, K) if example == "3" or example == "all": #==Example 3=================================================# """ Line 3 Shape Stiffness Matrix """ coords = np.array([2, 5, 8]) line3 = Line3() N = line3.getNmatrix() IP = line3.getGlobalPoints(coords) [dN, w] = line3.getGlobalGradients(coords) K = np.zeros((3, 3)) for ip in range(line3.nIP): K += w[ip] * np.outer(dN[ip], dN[ip]) print("\n\n Line3 \n") Printer(IP, N, dN, w, K) if example == "4" or example == "all": #==Example 5=================================================# """ Line 2 Boundary Element """ coords = np.array([[0.2, 0.3, 0.5], [0.8, 0.9, 0.5]]) x = np.array([0.5, 0.7, 0.5]) line2 = Line2(scheme="Gauss2") N = line2.getNmatrix() dN = K = "N/A"
def inferJACKSGene(data, data_err, ctrl, ctrl_err, n_iter, tol=0.1, mu0_x=1, var0_x=1.0, mu0_w=0.0, var0_w=1e4, tau_prior_strength=0.5, fixed_x=None, apply_w_hp=False): #Adjust estimated variances if needed data_err[SP.isnan(data_err)] = 2.0 # very uncertain if a single replicate #The control can be specified once for each sample, or common across all cell lines if len(ctrl.shape) == 1 or ctrl.shape[1] == 1: #If only 1 control replicate, use mean variance from data across cell lines for that guide ctrl_err[SP.isnan(ctrl_err)] = SP.nanmean(data_err, axis=1)[SP.isnan(ctrl_err)] y = (data.T - ctrl).T tau_pr_den = tau_prior_strength * 1.0 * ( (data_err**2).T + ctrl_err**2 + 1e-2).T else: #If only 1 control replicate, use data variances for ctrls as well ctrl_err[SP.isnan(ctrl_err)] = data_err[SP.isnan(ctrl_err)] y = data - ctrl tau_pr_den = tau_prior_strength * 1.0 * (data_err**2 + ctrl_err**2 + 1e-2) #Run the inference G, L = y.shape if fixed_x is None: x1 = mu0_x * SP.ones(G) x2 = x1**2 else: x1 = fixed_x['X1'] x2 = fixed_x['X2'] w1 = np.nanmedian(y, axis=0) tau = tau_prior_strength * 1.0 / tau_pr_den w2 = w1**2 bound = lowerBound(x1, x2, w1, w2, y, tau) LOG.debug("Initially, mean absolute error=%.3f" % (SP.nanmean(abs(y)).mean())) LOG.debug( "After init, mean absolute error=%.3f, <x>=%.1f <w>=%.1f lower bound=%.1f" % (SP.nanmean( abs(y.T - SP.outer(w1, x1))).mean(), x1.mean(), w1.mean(), bound)) for i in range(n_iter): last_bound = bound if fixed_x is None: x1, x2 = updateX(w1, w2, tau, y, mu0_x, var0_x) if apply_w_hp and len(w1) > 1: mu0_w, var0_w = w1.mean(), w1.var( ) * 3 + 1e-4 # hierarchical update on w (to encourage w's together - use with caution!) w1, w2 = updateW(x1, x2, tau, y, mu0_w, var0_w) tau = updateTau(x1, x2, w1, w2, y, tau_prior_strength, tau_pr_den) bound = lowerBound(x1, x2, w1, w2, y, tau) LOG.debug( "Iter %d/%d. lb: %.1f err: %.3f x:%.2f+-%.2f w:%.2f+-%.2f xw:%.2f" % (i + 1, n_iter, bound, SP.nanmean( abs(y.T - SP.outer(w1, x1))).mean(), x1.mean(), SP.median( (x2 - x1**2)**0.5), w1.mean(), SP.median( (w2 - w1**2)**0.5), x1.mean() * w1.mean())) if abs(last_bound - bound) < tol: break return y, tau, x1, x2, w1, w2
def Problem3(x): numbers = sp.arange(x) return sp.outer(number, numbers)