我正在使用Pygad通过遗传算法来训练Keras NN,据我所知,代码可以工作,但是,我不知道如何防止它在终端中打印纪元:
ga_instance = pygad.GA(num_generations=15, num_parents_mating=5, fitness_func=fitness_func,
initial_population=keras_ga.population_weights, on_generation=on_generation, suppress_warnings=True)
正如您在这里看到的,我已经设置了suppress_warnings=True
,但是,当我运行ga_instance.run()
时,终端将继续打印这些文本:
1/1 [==============================] - 0s 34ms/step
1/1 [==============================] - 0s 36ms/step
1/1 [==============================] - 0s 34ms/step
1/1 [==============================] - 0s 36ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 37ms/step
1/1 [==============================] - 0s 38ms/step
1/1 [==============================] - 0s 33ms/step
1/1 [==============================] - 0s 34ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 29ms/step
我不确定这个问题是由PyGad还是Tensorflow引起的,但我想不出解决这个问题的方法。
感谢您的宝贵时间。
为其他遇到这种情况的人补充:正如已经实现的,这个输出是由keras的model.predict()
函数生成的,可能在您的适应度函数中使用。设置verbose=0
禁用它,但不是最优的,因为predict()
是针对PyGad当前不支持的输入的批处理进行优化的。对于小批量数据的重复预测,请使用模型的__call__()
,例如model(solution)
而不是model.predict(solution)
。
见:https://www.tensorflow.org/api_docs/python/tf/keras/Model预测
检查fitness
和on_generation
函数,如果您使用任何打印语句并删除它们。
否则,除非有警告或错误,否则PyGad
不会与自己一起输出。
并且您已经阻止了suppress警告
suppress_warnings
: bool参数,用于控制是否打印警告消息。默认为False。有关参数的更多信息,请查看文档:https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html