Example #1
0
 def fprime(y):
     logLik, gw = eval(y)
     #print '==================='
     #print '=>', joint, np.min(model.logpxz(_w, x, _z)), np.min(model.mclik(_w, x))
     #print '==================='
     gw = ndict.flatten(gw)
     log['loglik'].append(logLik)
     return - gw
Example #2
0
def lbfgs(posterior, w, hook=None, hook_wavelength=2, m=100, maxiter=15000):
    print 'L-BFGS', m, maxiter
    
    cache = [1234,0,0]
    
    def eval(y):
        # TODO: solution for random seed in ipcluster
        # maybe just execute a remote np.random.seed(0) there?
        np.random.seed(0)
        _w = ndict.unflatten(y, w)
        logpw, gw = cache[1], cache[2]
        if np.linalg.norm(y) != cache[0]:
            logpw, gw = posterior.grad(_w)
            #print logpw, np.linalg.norm(y)
            cache[0], cache[1], cache[2] = [np.linalg.norm(y), logpw, gw]
        return logpw, gw
    
    def f(y):
        logLik, gw = eval(y)
        return - logLik.mean()
    
    def fprime(y):
        logLik, gw = eval(y)
        #print '==================='
        #print '=>', joint, np.min(model.logpxz(_w, x, _z)), np.min(model.mclik(_w, x))
        #print '==================='
        gw = ndict.flatten(gw)
        log['loglik'].append(logLik)
        return - gw
    
    t = [0, 0, time.time()]
    def callback(wz):
        if hook is None: return
        t[1] += 1
        if time.time() - t[2] > hook_wavelength:
            _w = ndict.unflatten(wz, w)
            hook(t[1], _w, cache[1]) # num_its, w, logpw
            t[2] = time.time()
    
    x0 = ndict.flatten(w)
    xn, f, d = scipy.optimize.fmin_l_bfgs_b(func=f, x0=x0, fprime=fprime, m=m, iprint=0, callback=callback, maxiter=maxiter)
    
    #scipy.optimize.fmin_cg(f=f, x0=x0, fprime=fprime, full_output=True, callback=hook)
    #scipy.optimize.fmin_ncg(f=f, x0=x0, fprime=fprime, full_output=True, callback=hook)
    w = ndict.unflatten(xn, w)
    #print 'd: ', d
    if d['warnflag'] is 2:
        print 'warnflag:', d['warnflag']
        print d['task']
    info = d
    return w, info