pybrain黑盒优化定制优化器模拟退火

时间:2015-03-07 17:10:02

标签: python optimization pybrain

我无法在黑盒优化上解析pybrains文档。我希望输入我自己的nn权重优化器功能。 Pybrain已经实施了GA和爬山。我无法复制它们的格式,例如实现模拟退火以优化nn的权重 任何人都知道如何使这个退火函数可以被pybrain使用?如下所示是使用内置GA功能的简单示例。如何改变我的退火功能?

def annealingoptimize(domain,costf,T=10000.0,cool=0.95,step=1):
  # Initialize the values randomly
  vec=[float(random.randint(domain[i][0],domain[i][1]))
       for i in range(len(domain))]

  while T>0.1:
    # Choose one of the indices
    i=random.randint(0,len(domain)-1)

    # Choose a direction to change it
    dir=random.randint(-step,step)

    # Create a new list with one of the values changed
    vecb=vec[:]
    vecb[i]+=dir
    if vecb[i]<domain[i][0]: vecb[i]=domain[i][0]
    elif vecb[i]>domain[i][1]: vecb[i]=domain[i][1]

    # Calculate the current cost and the new cost
    ea=costf(vec)
    eb=costf(vecb)
    p=pow(math.e,(-eb-ea)/T)

    print vec,ea


    # Is it better, or does it make the probability
    # cutoff?
    if (eb<ea or random.random()<p):
      vec=vecb

    # Decrease the temperature
    T=T*cool
  return vec

##GA EXAMPLE, uses GA(ContinuousOptimizer, Evolution) these two opaque classes.

from pybrain.datasets.classification import ClassificationDataSet
# below line can be replaced with the algorithm of choice e.g.
# from pybrain.optimization.hillclimber import HillClimber
from pybrain.optimization.populationbased.ga import GA
from pybrain.tools.shortcuts import buildNetwork

# create XOR dataset
d = ClassificationDataSet(2)
d.addSample([0., 0.], [0.])
d.addSample([0., 1.], [1.])
d.addSample([1., 0.], [1.])
d.addSample([1., 1.], [0.])
d.setField('class', [ [0.],[1.],[1.],[0.]])

nn = buildNetwork(2, 3, 1)
# d.evaluateModuleMSE takes nn as its first and only argument
ga = GA(d.evaluateModuleMSE, nn, minimize=True)
for i in range(100):
    nn = ga.learn(0)[0]

1 个答案:

答案 0 :(得分:1)

您可以使用StochasticHillClimber()

  

class pybrain.optimization.StochasticHillClimber(evaluator=None, initEvaluable=None, **kwargs)
  随机爬山总是移动到更好的点,但也可能会出现更糟糕的点,其概率会随着适应度的下降而增加(并且取决于温度参数)。

     

<强> temperature
  温度越高,它的行为就越有探索性(不那么贪婪)。