Exemplo n.º 1
0
    def findMin(self, x, y, numIters=200):
        meanfunc = self.model.meanfunc
        covfunc = self.model.covfunc
        likfunc = self.model.likfunc
        inffunc = self.model.inffunc
        hypInArray = self._convert_to_array()

        try:
            # opt = minimize.run(self._nlzAnddnlz, hypInArray, length=-numIters)
            opt = minimize.run(self._nlzAnddnlz, hypInArray, length=numIters)
            optimalHyp = deepcopy(opt[0])
            funcValue = opt[1][-1]
            self.logger.warning("Number of line searches %g", opt[2])
        except:
            self.errorCounter += 1
            if not self.searchConfig:
                raise Exception("Can not learn hyperparamters using minimize.")
        self.trailsCounter += 1

        if self.searchConfig:
            searchRange = self.searchConfig.meanRange + self.searchConfig.covRange + self.searchConfig.likRange
            if not (self.searchConfig.num_restarts
                    or self.searchConfig.min_threshold):
                raise Exception('Specify at least one of the stop conditions')
            while True:
                self.trailsCounter += 1  # increase counter
                for i in range(hypInArray.shape[0]):  # random init of hyp
                    hypInArray[i] = np.random.uniform(low=searchRange[i][0],
                                                      high=searchRange[i][1])
                # value this time is better than optiaml min value
                try:
                    # thisopt = minimize.run(self._nlzAnddnlz, hypInArray, length=-numIters)
                    thisopt = minimize.run(self._nlzAnddnlz,
                                           hypInArray,
                                           length=numIters)
                    self.logger.warning("Number of line searches %g",
                                        thisopt[2])
                    if thisopt[1][-1] < funcValue:
                        funcValue = thisopt[1][-1]
                        optimalHyp = thisopt[0]
                except:
                    self.errorCounter += 1
                if self.searchConfig.num_restarts and self.errorCounter > self.searchConfig.num_restarts / 2:
                    self.logger.warning(
                        "[Minimize] %d out of %d trails failed during optimization",
                        self.errorCounter, self.trailsCounter)
                    raise Exception(
                        "Over half of the trails failed for minimize")
                if self.searchConfig.num_restarts and self.trailsCounter > self.searchConfig.num_restarts - 1:  # if exceed num_restarts
                    self.logger.warning(
                        "[Minimize] %d out of %d trails failed during optimization",
                        self.errorCounter, self.trailsCounter)
                    return optimalHyp, funcValue
                if self.searchConfig.min_threshold and funcValue <= self.searchConfig.min_threshold:  # reach provided mininal
                    self.logger.warning(
                        "[Minimize] %d out of %d trails failed during optimization",
                        self.errorCounter, self.trailsCounter)
                    return optimalHyp, funcValue
        return optimalHyp, funcValue
Exemplo n.º 2
0
    def findMin(self, x, y):
        meanfunc = self.model.meanfunc
        covfunc = self.model.covfunc
        likfunc = self.model.likfunc
        inffunc = self.model.inffunc
        hypInArray = self._convert_to_array()

        opt = minimize.run(self._nlzAnddnlz, hypInArray, length=-40)
        try:
            opt = minimize.run(self._nlzAnddnlz, hypInArray, length=-40)
            optimalHyp = deepcopy(opt[0])
            funcValue = opt[1][-1]
        except:
            self.errorCounter += 1
            if not self.searchConfig:
                raise Exception("Can not use minimize. Try other hyparameters")
        self.trailsCounter += 1

        if self.searchConfig:
            searchRange = self.searchConfig.meanRange + self.searchConfig.covRange + self.searchConfig.likRange
            if not (self.searchConfig.num_restarts
                    or self.searchConfig.min_threshold):
                raise Exception('Specify at least one of the stop conditions')
            while True:
                self.trailsCounter += 1  # increase counter
                for i in xrange(hypInArray.shape[0]):  # random init of hyp
                    hypInArray[i] = np.random.uniform(low=searchRange[i][0],
                                                      high=searchRange[i][1])
                # value this time is better than optiaml min value
                try:
                    thisopt = minimize.run(self._nlzAnddnlz,
                                           hypInArray,
                                           length=-40)
                    if thisopt[1][-1] < funcValue:
                        funcValue = thisopt[1][-1]
                        optimalHyp = thisopt[0]
                except:
                    self.errorCounter += 1
                if self.searchConfig.num_restarts and self.errorCounter > self.searchConfig.num_restarts / 2:
                    print "[Minimize] %d out of %d trails failed during optimization" % (
                        self.errorCounter, self.trailsCounter)
                    raise Exception(
                        "Over half of the trails failed for minimize")
                if self.searchConfig.num_restarts and self.trailsCounter > self.searchConfig.num_restarts - 1:  # if exceed num_restarts
                    print "[Minimize] %d out of %d trails failed during optimization" % (
                        self.errorCounter, self.trailsCounter)
                    return optimalHyp, funcValue
                if self.searchConfig.min_threshold and funcValue <= self.searchConfig.min_threshold:  # reach provided mininal
                    print "[Minimize] %d out of %d trails failed during optimization" % (
                        self.errorCounter, self.trailsCounter)
                    return optimalHyp, funcValue
        return optimalHyp, funcValue
Exemplo n.º 3
0
    def findMin(self, x, y, numIters = 200):
        meanfunc = self.model.meanfunc
        covfunc = self.model.covfunc
        likfunc = self.model.likfunc
        inffunc = self.model.inffunc
        hypInArray = self._convert_to_array()

        try:
            # opt = minimize.run(self._nlzAnddnlz, hypInArray, length=-numIters)
            opt = minimize.run(self._nlzAnddnlz, hypInArray, length=numIters)
            optimalHyp = deepcopy(opt[0])
            funcValue  = opt[1][-1]
            print("Number of line searches %g" % opt[2])
        except:
            self.errorCounter += 1
            if not self.searchConfig:
                raise Exception("Can not learn hyperparamters using minimize.")
        self.trailsCounter += 1

        if self.searchConfig:
            searchRange = self.searchConfig.meanRange + self.searchConfig.covRange + self.searchConfig.likRange
            if not (self.searchConfig.num_restarts or self.searchConfig.min_threshold):
                raise Exception('Specify at least one of the stop conditions')
            while True:
                self.trailsCounter += 1                 # increase counter
                for i in xrange(hypInArray.shape[0]):   # random init of hyp
                    hypInArray[i]= np.random.uniform(low=searchRange[i][0], high=searchRange[i][1])
                # value this time is better than optiaml min value
                try:
                    # thisopt = minimize.run(self._nlzAnddnlz, hypInArray, length=-numIters)
                    thisopt = minimize.run(self._nlzAnddnlz, hypInArray, length=numIters)
                    print("Number of line searches %g" % thisopt[2])
                    if thisopt[1][-1] < funcValue:
                        funcValue  = thisopt[1][-1]
                        optimalHyp = thisopt[0]
                except:
                    self.errorCounter += 1
                if self.searchConfig.num_restarts and self.errorCounter > self.searchConfig.num_restarts/2:
                    print("[Minimize] %d out of %d trails failed during optimization" % (self.errorCounter, self.trailsCounter))
                    raise Exception("Over half of the trails failed for minimize")
                if self.searchConfig.num_restarts and self.trailsCounter > self.searchConfig.num_restarts-1:         # if exceed num_restarts
                    print("[Minimize] %d out of %d trails failed during optimization" % (self.errorCounter, self.trailsCounter))
                    return optimalHyp, funcValue
                if self.searchConfig.min_threshold and funcValue <= self.searchConfig.min_threshold:           # reach provided mininal
                    print("[Minimize] %d out of %d trails failed during optimization" % (self.errorCounter, self.trailsCounter))
                    return optimalHyp, funcValue
        return optimalHyp, funcValue