def init_model(dim, Q, cond_num): def search_optm_cond(V, cond_num_star): min_c = 0 max_c = 100 if cond_num_star == 1: return 0, 1 while True: curr_c = (min_c + max_c) / 2. M = np.eye(dim, ) + curr_c * V eigvals = np.linalg.eigvals(M) assert np.all(eigvals > 0), 'illegal input' val = np.max(eigvals) / np.min(eigvals) if val - cond_num_star > 0.3: max_c = curr_c elif val - cond_num_star < -0.3: min_c = curr_c else: return curr_c, val t0 = np.eye(dim) mu0 = sharedX(np_rng.uniform(-3, 3, size=(dim, ))) V = np.dot( np.dot(Q.T, np.diag(1. + np.random.uniform(0, 1, size=(dim, )))), Q) coeff, cond_approx = search_optm_cond(V, cond_num) M = np.eye(dim, ) + coeff * V A0 = sharedX(np.linalg.inv(M)) model_params = {'mu': mu0, 'A': A0} ### score function score_q = score_gaussian log_prob = logp_gaussian ## ground truth gt = np.random.multivariate_normal(mu0.get_value(), M, (10000, ), check_valid='raise') return model_params, score_q, log_prob, gt
def init_model(): G = nx.grid_2d_graph(args.grid, args.grid) A = np.asarray(nx.adjacency_matrix(G).todense()) #A = A * np_rng.uniform(-0.1, 0.1, size=A.shape) noise = np.tril(np_rng.uniform(-0.1, 0.1, size=A.shape)) noise = noise + noise.T A = A * noise np.fill_diagonal(A, np.sum(np.abs(A), axis=1) + .1) if not np.all(np.linalg.eigvals(A) > 0) or not np.all( np.linalg.eigvals(np.linalg.inv(A)) > 0): raise NotImplementedError b = np_rng.normal(0, 1, size=(args.grid**2, 1)) mu0 = sharedX(np.dot(np.linalg.inv(A), b).flatten()) A0 = sharedX(A) model_params = {'mu': mu0, 'A': A0} ## score function score_q = score_gaussian ## ground truth samples gt = np.random.multivariate_normal(mean=mu0.get_value().flatten(), cov=np.linalg.inv(A), size=(10000, ), check_valid='raise') ## adjacency matrix W = np.zeros(A.shape).astype(int) W[A != 0] = 1 assert np.all(np.sum(W, axis=1) > 0), 'illegal inputs' assert np.sum((W - W.T)**2) < 1e-8, 'illegal inputs' return model_params, score_q, gt, W
def random_features_kernel(x, n_features, score_q, fixed_weights=True, cos_feat_dim_scale=True, **model_params): dim = x.get_value().shape[1] H = sqr_dist(x, x) h = median_distance(H) #h = select_h_by_ksdv(x, score_q, **model_params) gamma = 1./h if fixed_weights: random_offset = sharedX(np_rng.uniform(0, 2*pi, (n_features,))) weights = sharedX(np_rng.normal(0, 1, (dim, n_features))) random_weights = T.sqrt(2*gamma) * weights else: #random_weights = T.sqrt(2 * gamma) * t_rng.normal( # (x.shape[1], n_features)) #random_offset = t_rng.uniform((n_features,), 0, 2 * pi) raise NotImplementedError if cos_feat_dim_scale: alpha = T.sqrt(2.) / T.sqrt(n_features).astype(theano.config.floatX) else: alpha = T.sqrt(2.) coff = T.dot(x, random_weights) + random_offset projection = alpha * T.cos(coff) kxy = T.dot(projection, projection.T) sinf = -alpha*T.sin(coff) wd = random_weights.T inner = T.sum(sinf, axis=0).dimshuffle(0,'x') * wd dxkxy = T.dot(projection, inner) return kxy, dxkxy
def __call__(self, shape): return sharedX(np_rng.uniform(low=-self.scale, high=self.scale, size=shape))
def __call__(self, shape, name=None): return sharedX(np_rng.uniform(low=-self.scale, high=self.scale, size=shape), name=name)