예제 #1
1
    def infer_x(self, y, sigma = 1.0, tolerance = 1e-2):
        """Infer probable x from input y
        @param y  the desired output for infered x.
        @return   a list of probable x
        """
        OptimizedInverseModel.infer_x(self, y)
        if self.fmodel.size() == 0:
            return self._random_x()

        x_guesses = [self._guess_x_simple(y)[0]]
        #x_guesses = self._guess_x_kmeans(y)
        
        if CMA_IMPLEM == "C++":

        # C++ IMPLEM
        
            result = []
            for xg in x_guesses:
                sigma = 0.05
                #print self.lower, self.upper
                p = lci.to_params(list(xg),
                                  sigma, 
                                lbounds=self.lower, 
                                ubounds=self.upper, 
                                max_fevals=20
                                  )
                objfunc = lci.to_fitfunc(self._error)
                cmasols = lci.pcmaes(objfunc, p)
                # collect and inspect results
                bcand = cmasols.best_candidate()
                error = bcand.get_fvalue()
                x = lcmaes.get_candidate_x(bcand)
                result.append((error, x))
            #print "x", [xi for fi, xi in sorted(result)]
            return [xi for fi, xi in sorted(result)]
    
        elif CMA_IMPLEM == "python":
                
            # PYTHON IMPLEM
            result = []
            for xg in x_guesses:
                res = cma.fmin(self._error, xg, 0.05, 
                               options={'bounds':[self.lower, self.upper],
                               'verb_log':0,
                               'verb_disp':False,
                               #'ftarget':tolerance/2,
                               'maxfevals':20})
                result.append((res[1], res[0]))
     
    #        return [xi for fi, i, xi in sorted(result)[:min(N_RESULT, len(result))]]
            return [xi for fi, xi in sorted(result)]
예제 #2
0
    def fit(self, X, y, opt_fit=False, libcma=False, **params):
        if opt_fit:

            def err_function(params):
                mdl = NrmModel(bias=params[0],
                               conf_threshold=params[1],
                               mu0=params[2],
                               kappa0=params[3],
                               alpha0=params[4],
                               beta0=params[5])
                return -mdl.score(X, y)

            start = np.array([self.bias, self.conf_threshold] +
                             list(self.prior))
            if not libcma:
                import cma
                x = cma.fmin(err_function,
                             start,
                             1.5,
                             restarts=5,
                             options={'verbose': -9})[0]
            else:
                import lcmaes
                import lcmaes_interface as lci
                print(('Start:', err_function(start)))
                ffunc = lci.to_fitfunc(err_function)
                lbounds = [-np.inf, 0, -np.inf] + 3 * [0]

                fopt = lci.to_params(
                    list(start),
                    1.5,
                    str_algo=b'aipop',
                    lbounds=lbounds,
                    restarts=5,
                )

                res = lci.pcmaes(ffunc, fopt)
                bcand = res.best_candidate()
                print(('End:', bcand.get_fvalue()))
                x = lcmaes.get_candidate_x(bcand)
            return self.set_params(bias=x[0],
                                   conf_threshold=x[1],
                                   mu0=x[2],
                                   kappa0=x[3],
                                   alpha0=x[4],
                                   beta0=x[5])
        return self
예제 #3
0
import lcmaes_interface as lci


# setup input parameters
def myfun(x):
    return sum([xi**2 for xi in x])  # myfun accepts a list of numbers as input


dimension = 10
x0 = [2.1] * dimension
sigma0 = 0.1

# run optimization via lci
res = lci.pcmaes(
    lci.to_fitfunc(myfun),
    lci.to_params(
        x0,
        sigma0,  # all remaining parameters are optional
        str_algo=b'aipop',  # b=bytes, because unicode fails
        lbounds=[-5] * dimension,
        ubounds=[5] * dimension,
        restarts=2,  # means 2 runs, i.e. one restart
    ))
lci.plot()  # plot from file set in lci.to_params
예제 #4
0
import lcmaes_interface as lci

# setup input parameters
def myfun(x): return sum([xi**2 for xi in x])  # myfun accepts a list of numbers as input
dimension = 10 
x0 = [2.1] * dimension
sigma0 = 0.1

# run optimization via lci
res = lci.pcmaes(lci.to_fitfunc(myfun),
                 lci.to_params(x0, sigma0,  # all remaining parameters are optional
                               str_algo=b'aipop',  # b=bytes, because unicode fails
                            lbounds=[-5] * dimension, ubounds=[5] * dimension,
                            restarts=2,  # means 2 runs, i.e. one restart
                           )
             ) 
lci.plot()  # plot from file set in lci.to_params