def internalUpdate(self, population): """Insert all new points into the partition. """ if population[0][1] is None: # skip unscored updates inside convolution return if not isinstance(population[0][0], self.config.space.type): # again, skip intermediate return pts = [Point(self.segment, x, None, s) for x, s in population] self.config.stats.start("save") Point.bulkSave(self.segment, pts, self.stats) self.config.stats.stop("save")
def internalUpdate(self, population): """Insert all new points into the partition. """ if population[0][1] is None: # skip unscored updates inside convolution return if not isinstance(population[0][0], self.config.space.type): # again, skip intermediate return pts = [Point(self.segment, x, None, s) for x,s in population] self.config.stats.start("save") Point.bulkSave(self.segment, pts, self.stats) self.config.stats.stop("save")
def train(self): """ Run the optimizer for `optimizer.config.generations` generations, each with population size `optimizer.config.populationSize`. :returns: A tuple with two elements containing the best solution found and the maximal fitness (objective value). """ trainStart = time() stats = RunStats() stats.recording = self.config.recording maxScore = -1e100 maxOrg = None gens = self.config.generations if gens < 1: return maxScore, maxOrg successfulMutations = 0 successImprovement = 0 lastTime = time() for idx in xrange(gens): startTime = time() stats.start("generation") i = idx population = [] start = time() stats.start("sample") self.config.selectedScores = [] total = 0. count = 0. for w, x in enumerate( self.algorithm.batch(self.config.populationSize)): stats.stop("sample") stats.start("score") if not hasattr(self.config, 'convert') or self.config.convert: z = self.algorithm.convert(x) score = float(self.fitness(self.algorithm.convert(x))) else: z = x score = float(self.fitness(x)) if self.config.bounded and not self.config.in_bounds(z): score = -1e300 if hasattr(self.fitness, 'statistics'): fitnessStats = self.fitness.statistics else: fitnessStats = None if score != score: score = -1e300 total += score count += 1 self.since += 1 stats.stop("score") population.append((x, score, fitnessStats)) stats.start("sample") if len(self.config.selectedScores) > w: baseScore = self.config.selectedScores[w] if score > baseScore: successfulMutations += 1.0 successImprovement += score - baseScore genavg = total / count attempts = (idx + 1) * self.config.populationSize success = ((successfulMutations + 0.) / attempts) avgImprove = (successImprovement / (successfulMutations + 1e-10)) if self.sort: population = sorted(population, key=lambda x: x[1], reverse=True) genmax = population[0][1] genorg = population[0][0] genstat = population[0][2] else: genmax = max([s for x, s, f in population]) for x, s, f in population: if s == genmax: genorg = x genstat = f break if genmax > maxScore: del maxOrg del maxScore maxScore = genmax maxOrg = genorg #print str(self.config.encode(genorg)) #print genstat if hasattr(maxOrg, 'computeEdgeStatistics'): maxOrg.computeEdgeStatistics() print maxOrg.edges else: del genorg while self.since >= self.groupby: self.since -= self.groupby self.groupCount += 1 self.data.append((self.groupCount, genavg, maxScore, genmax)) cnt = 0 pop2 = [] gps = [] for point, score, fitnessStats in population: stats.start("point") pop2.append((point, score)) if self.save: try: pt = None bn = None bit = None other = None if isinstance(point, ndarray): pt = maximum(1e-30 * ones(len(point)), abs(point)) pt *= sign(point) elif isinstance(point, TernaryString): bit = point elif hasattr(point, 'computeEdgeStatistics'): bn = point # bn.computeEdgeStatistics() # print bn.edges else: other = point gp = Point(point=pt, bayes=bn, binary=bit, other=other, statistics=fitnessStats, score=score, count=1, segment=self.segment) gps.append(gp) except: raise stats.stop("point") if self.save: stats.start("save") Point.objects.bulkSave(gps, stats) stats.stop("save") population = pop2 stats.start("update") self.algorithm.update(i + 2, population) stats.stop("update") stats.stop("generation") del population if self.save: gc.collect() if True: # (time() - lastTime) > 1.0: lastTime = time() if self.config.printOut: if self.config.recording: print stats if hasattr(self.algorithm, 'var'): print i, ": ", time( ) - startTime, self.algorithm.var, '%.16f' % genmax, '%.16f' % maxScore else: print i, ": ", time() - startTime, genmax, maxScore if maxScore >= self.config.stopAt: break if self.config.printOut: print "total time: ", time() - trainStart print "best score: ", maxScore self.maxScore = maxScore self.maxOrg = maxOrg return maxScore, maxOrg
def train(self, n): from pyec.util.partitions import ScoreTree, Partition, Point from pyec.trainer import RunStats stats = RunStats() stats.recording = False numBatches = len(self.data) / self.batchSize lr = 0.001 / ((n / numBatches + 1.0) ** 2) current = self.epoch % numBatches start = current * self.batchSize end = start + self.batchSize data = self.data[start:end] completed = self.complete(data) energy = sum([self.energy(d) for d in completed]) / len(completed) print "Energy of data: ", energy # , " v ", energy2, "\n\n" sampled = self.sampler.batch(self.batchSize) energys = sum([self.energy(d) for d in sampled]) / len(sampled) print "Energy of sample: ", energys for point in completed: gp = Point( point=None, bayes=None, binary=point, score=-self.energy(point), count=1, segment=self.sampler.selectors[-1].segment, ) gp.save() try: Partition.objects.separate(gp, self.sampler.config, stats) ScoreTree.objects.insert(gp, self.sampler.config, stats) except: gp.alive = False gp.save() wcb = abs(self.w).sum() dw, dv, dh = self.correlate(completed) mw, mv, mh = self.correlate(sampled) diffw = dw - mw diffv = dv - mv diffh = dh - mh self.wc += (1 - self.momentum) * lr * diffw self.bvc += (1 - self.momentum) * lr * diffv self.bhc += (1 - self.momentum) * lr * diffh self.w += self.wc self.bv += self.bvc self.bh += self.bhc self.wc *= self.momentum self.bhc *= self.momentum self.bvc *= self.momentum print "scale of deriv: ", average(diffw), average(diffv), average(diffh) """ gw, gv, gh = self.correlate(g) dw, dv, dh = self.correlate(c2) self.wcg += (1 - self.momentum) * self.rate * (dw - gw) self.bvcg += (1 - self.momentum) * self.rate * (dv - gv) self.bhcg += (1 - self.momentum) * self.rate * (dh - gh) self.wg += self.wc self.bvg += self.bvc self.bhg += self.bhc self.wcg *= self.momentum self.bhcg *= self.momentum self.bvcg *= self.momentum """ self.sampler.completeTraining(self, n)
def sample(self): return Point.sampleTournament(self.history.segment, self.history.temperature(), self.config)
def sample(self): return Point.sampleProportional(self.history.segment, self.history.temperature(), self.config)
def train(self, n): from pyec.util.partitions import ScoreTree, Partition, Point from pyec.trainer import RunStats stats = RunStats() stats.recording = False numBatches = len(self.data) / self.batchSize lr = 0.001 / ((n / numBatches + 1.)**2) current = self.epoch % numBatches start = current * self.batchSize end = start + self.batchSize data = self.data[start:end] completed = self.complete(data) energy = sum([self.energy(d) for d in completed]) / len(completed) print "Energy of data: ", energy #, " v ", energy2, "\n\n" sampled = self.sampler.batch(self.batchSize) energys = sum([self.energy(d) for d in sampled]) / len(sampled) print "Energy of sample: ", energys for point in completed: gp = Point(point=None, bayes=None, binary=point, score=-self.energy(point), count=1, segment=self.sampler.selectors[-1].segment) gp.save() try: Partition.objects.separate(gp, self.sampler.config, stats) ScoreTree.objects.insert(gp, self.sampler.config, stats) except: gp.alive = False gp.save() wcb = abs(self.w).sum() dw, dv, dh = self.correlate(completed) mw, mv, mh = self.correlate(sampled) diffw = dw - mw diffv = dv - mv diffh = dh - mh self.wc += (1 - self.momentum) * lr * diffw self.bvc += (1 - self.momentum) * lr * diffv self.bhc += (1 - self.momentum) * lr * diffh self.w += self.wc self.bv += self.bvc self.bh += self.bhc self.wc *= self.momentum self.bhc *= self.momentum self.bvc *= self.momentum print "scale of deriv: ", average(diffw), average(diffv), average( diffh) """ gw, gv, gh = self.correlate(g) dw, dv, dh = self.correlate(c2) self.wcg += (1 - self.momentum) * self.rate * (dw - gw) self.bvcg += (1 - self.momentum) * self.rate * (dv - gv) self.bhcg += (1 - self.momentum) * self.rate * (dh - gh) self.wg += self.wc self.bvg += self.bvc self.bhg += self.bhc self.wcg *= self.momentum self.bhcg *= self.momentum self.bvcg *= self.momentum """ self.sampler.completeTraining(self, n)