parser.set(th, 'betas', .01 * K_chol.dot(np.random.randn(Vspec, NUM_BASES)).T) parser.set(th, 'omegas', .01 * npr.randn(Nspec, NUM_BASES)) parser.set(th, 'mus', .01 * npr.randn(Nspec)) ### if file exists, at least pull basis out #if os.path.exists("basis_fit_K-%d_V-2728.pkl"%NUM_BASES): # print "basis fit file exists! - checking basis" # th_disk, lam0_disk, lam0_delta_disk, parser_disk = \ # qfb.load_basis_fit("basis_fit_K-%d_V-2728.pkl"%NUM_BASES) # if np.all(lam0_disk == lam0) and : # print "basis size is consistent - initializing..." # parser.set(th, 'betas', parser_disk.get('betas')) ## sanity check gradient check_grad(fun = lambda th: loss_fun(th) + prior_loss(th), # X, Lam), jac = lambda th: loss_grad(th) + prior_loss_grad(th), #, X, Lam), th = th) obj_vals = [] min_val = np.inf min_x = None def callback(x, i, g): global min_val global min_x global obj_vals if i % 10 == 0: loss_val = loss_fun(x) + prior_loss(x) if loss_val < min_val: min_x = x min_val = loss_val print " %d, loss = %2.4g, grad = %2.4g " % \
.01 * K_chol.dot(np.random.randn(Vspec, NUM_BASES)).T) parser.set(th, 'omegas', .01 * npr.randn(Nspec, NUM_BASES)) parser.set(th, 'mus', .01 * npr.randn(Nspec)) ### if file exists, at least pull basis out #if os.path.exists("basis_fit_K-%d_V-2728.pkl"%NUM_BASES): # print "basis fit file exists! - checking basis" # th_disk, lam0_disk, lam0_delta_disk, parser_disk = \ # qfb.load_basis_fit("basis_fit_K-%d_V-2728.pkl"%NUM_BASES) # if np.all(lam0_disk == lam0) and : # print "basis size is consistent - initializing..." # parser.set(th, 'betas', parser_disk.get('betas')) ## sanity check gradient check_grad( fun=lambda th: loss_fun(th) + prior_loss(th), # X, Lam), jac=lambda th: loss_grad(th) + prior_loss_grad(th), #, X, Lam), th=th) obj_vals = [] min_val = np.inf min_x = None def callback(x, i, g): global min_val global min_x global obj_vals if i % 1 == 0: loss_val = loss_fun(x) + prior_loss(x) if loss_val < min_val: min_x = x min_val = loss_val
([srcs[0].theta, srcs[0].sigma, srcs[0].phi, srcs[0].rho], srcs[0].u, [srcs[0].fluxes[b] for b in BANDS])) Z_s = [img.nelec for img in imgs] ll = galaxy_source_like(th, Z_s, imgs, check_overlap=True, unconstrained=True) ll_grad = galaxy_source_like_grad(th, Z_s, imgs) print ll print ll_grad # check to make sure the gradient is correct check_grad(fun=lambda (th): galaxy_source_like(th, Z_s, imgs), jac=lambda (th): galaxy_source_like_grad(th, Z_s, imgs), th=th, compwise=True) # do quick gradient ascent from scipy.optimize import minimize res = minimize(fun=lambda (th): -galaxy_source_like(th, Z_s, imgs), jac=lambda (th): -galaxy_source_like_grad(th, Z_s, imgs), x0=res.x, method='L-BFGS-B') print galaxy_source_like(res.x, Z_s, imgs) check_grad(fun=lambda (th): galaxy_source_like(th, Z_s, imgs), jac=lambda (th): galaxy_source_like_grad(th, Z_s, imgs), th=res.x, compwise=True)