def x_proj(params): params[0].data[0] = 1e-5 def grad_proj(params): params[0].grad.data[0] = 0 ## one can also simply set since nfix is globally accessible # def x_proj(*args,**kw): # nfix.data[0] = 1e-5 # def grad_proj(*args,**kw): # nfix.grad.data[0] = 0 # nfi.set_oprions(0,x_proj=x_proj,grad_proj=grad_proj) paramidx = 0 nfi.set_options(paramidx, x_proj=x_proj, grad_proj=grad_proj) """ Now we can solve this constraint optimization problem in a unconstraint manner """ print("\n\n\n\n ***************** constraint powell_bs ***************** ") x, f, d = lbfgsb(nfi.f, x0, nfi.fprime, m=100, factr=1, pgtol=1e-14, iprint=10) out, fx, its, imode, smode = slsqp(nfi.f, x0, fprime=nfi.fprime, acc=1e-16, iter=15000, iprint=1, full_output=True) """ The original output ('x' or 'out') of the optimizer may not satisfy the constraint. Recall that the nfi.flat_param will automatically do the projection in reader and setter,
f = nfi.f(a) g = nfi.fprime(a) print(g) print(a - nfi.flat_param) print(nfi.f(a) - f) print(np.linalg.norm(nfi.fprime(a) - g)) nfi.fprime(a + 1) print(nfi.f(a) - f) print(nfi.flat_param - a) nfi.flat_param = a print(nfi.fprime(a) - g) print('no frozen, no x_proj, no grad_proj, 2 param_groups') test() nfi.set_options(1, x_proj=x_proj, grad_proj=grad_proj) print('set x_proj and grad_proj in param_group[1], 2 param_groups') test() nfi.add_param_group(param_group2) print('add param_group') test() nfi.flat_param = random.randn(nfi.numel()) nfi.set_options(1, isfrozen=True) print('set frozen in param_group[1], 3 param_groups') test() nfi.set_options(1, x_proj=None, grad_proj=None) print('delete x_proj,grad_proj in param_group[1], 3 param_groups') test() nfi.set_options(0, x_proj=x_proj, grad_proj=grad_proj) print('add x_proj and grad_proj in param_group[0], 3 param_groups') test()
x, f, d = lbfgsb(nfi.f, x0, nfi.fprime, m=100, factr=1, pgtol=1e-14, iprint=10) out, fx, its, imode, smode = slsqp(nfi.f, x0, fprime=nfi.fprime, acc=1e-16, iter=15000, iprint=1, full_output=True) def x_proj(params): params[0].data[0] = 1e-5 def grad_proj(params): params[0].grad.data[0] = 0 nfi.set_options(0, x_proj=x_proj, grad_proj=grad_proj) x, f, d = lbfgsb(nfi.f, x0, nfi.fprime, m=100, factr=1, pgtol=1e-14, iprint=10) x = nfi.flat_param out, fx, its, imode, smode = slsqp(nfi.f, x0, fprime=nfi.fprime, acc=1e-16, iter=15000, iprint=1, full_output=True) out = nfi.flat_param #%%