Python SciPy差异进化不';t计算给定的迭代次数
我试图学习我的神经网络来玩一个非常简单的游戏,但没有成功。问题是来自scipy的differential_evolution()的工作时间不够长:我设置了Python SciPy差异进化不';t计算给定的迭代次数,python,tensorflow,keras,scipy,evolutionary-algorithm,Python,Tensorflow,Keras,Scipy,Evolutionary Algorithm,我试图学习我的神经网络来玩一个非常简单的游戏,但没有成功。问题是来自scipy的differential_evolution()的工作时间不够长:我设置了maxiter=1000,但该函数只对41次迭代有效。 代码如下: def fitness_func(x, *args): #print('fitness func started') arch, width, height = args net = genome_to_nn(x, arch) my_game =
maxiter=1000
,但该函数只对41次迭代有效。
代码如下:
def fitness_func(x, *args):
#print('fitness func started')
arch, width, height = args
net = genome_to_nn(x, arch)
my_game = Game_2(height, width)
count = 0
move = -1
while count < 100:
count += 1
field = my_game.get_np_field()
decision_tensor = net(field)
move = int(tf.math.argmax( decision_tensor , axis =1))
if move != 2:
my_game.make_a_move(move)
if count % 2:
my_game.make_random()
my_game.next_iter()
result = 1/(150 + my_game.score)
return result
if __name__ == '__main__':
field_width = 5
field_height = 10
inp_size = field_width*(field_height-1) + 1
model = keras.Sequential(
[
layers.Dense(10, input_dim = inp_size),
layers.Dense(10, input_dim = 10 ,activation='sigmoid'),
layers.Dense(3, input_dim =10, activation='softmax')
]
)
args = (model, field_width, field_height)
bounds = np.asarray([(-10,10) for i in range(len(nn_to_genome(model)))])
print('start evolution')
res = differential_evolution(fitness_func, bounds= bounds, args=args, maxiter=50, workers=70, disp=True)
print('DE finished')
fitted_model = genome_to_nn(res.x, model)
print(res)
另外,如果maxiter是5000中的50(大于41),则函数仍将进行41次迭代
如果我设置了maxiter=30
,我会得到以下结果:
message: 'Maximum number of iterations has been exceeded.'
nfev: 298425
nit: 30
success: False
UPD:我将适应度函数的返回值改为
result = - my_game.score
所以,现在返回值在[-100100]范围内(以前是在[1/250;1/50]),它可以工作了!但是,我仍然不知道为什么它不能使用旧版本的函数。官方文档没有说明对其返回值的任何限制(除了它应该是一个数字)您好,您也可以在Tensorflow Probability中使用反优化器
result = - my_game.score