gpt4 book ai didi

python - pybrain黑盒优化自定义优化器模拟退火

转载 作者:行者123 更新时间:2023-11-28 18:39:07 24 4
gpt4 key购买 nike

我在解析有关黑盒优化的 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,使用GA(ContinuousOptimizer, Evolution)这两个不透明类。

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]

最佳答案

您可以使用 StochasticHillClimber() :

class pybrain.optimization.StochasticHillClimber(evaluator=None, initEvaluable=None, **kwargs)
Stochastic hill-climbing always moves to a better point, but may also go to a worse point with a probability that decreases with increasing drop in fitness (and depends on a temperature parameter).

temperature
The larger the temperature, the more explorative (less greedy) it behaves.

关于python - pybrain黑盒优化自定义优化器模拟退火,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com