Evolution Strategy (ES) เป็นเทคนิคการปรับให้เหมาะสมตามแนวคิดเรื่องการปรับตัวและวิวัฒนาการ คุณสามารถเรียนรู้เพิ่มเติมเกี่ยวกับเรื่องนี้ได้ที่ https://blog.openai.com/evolution-strategies/
มันเข้ากันได้กับทั้ง python2 และ python3
ติดตั้งจากแหล่งที่มา:
$ python setup.py install
ติดตั้งเวอร์ชันล่าสุดจากที่เก็บ git โดยใช้ pip:
$ pip install git+https://github.com/alirezamika/evostra.git
ติดตั้งจาก PyPI:
$ pip install evostra
(คุณอาจต้องใช้ python3 หรือ pip3 สำหรับ python3)
เจ้าหน้าที่ AI กำลังเรียนรู้การเล่น Flappy Bird โดยใช้ Evostra
เจ้าหน้าที่ AI กำลังเรียนรู้ที่จะเดินโดยใช้ evostra
น้ำหนักอินพุตของโมดูล EvolutionStrategy คือรายการอาร์เรย์ (หนึ่งอาร์เรย์ที่มีรูปร่างใดๆ สำหรับแต่ละเลเยอร์ของโครงข่ายประสาทเทียม) ดังนั้นเราจึงสามารถใช้เฟรมเวิร์กใดก็ได้เพื่อสร้างโมเดลและเพียงแค่ส่งน้ำหนักไปยัง ES
ตัวอย่างเช่น เราสามารถใช้ Keras เพื่อสร้างโมเดลและส่งน้ำหนักไปยัง ES ได้ แต่ที่นี่ เราใช้ FeedForwardNetwork โมเดลในตัวของ Evostra ซึ่งเร็วกว่ามากสำหรับกรณีการใช้งานของเรา:
import numpy as np
from evostra import EvolutionStrategy
from evostra . models import FeedForwardNetwork
# A feed forward neural network with input size of 5, two hidden layers of size 4 and output of size 3
model = FeedForwardNetwork ( layer_sizes = [ 5 , 4 , 4 , 3 ])
ตอนนี้เรากำหนดฟังก์ชัน get_reward ของเรา:
solution = np . array ([ 0.1 , - 0.4 , 0.5 ])
inp = np . asarray ([ 1 , 2 , 3 , 4 , 5 ])
def get_reward ( weights ):
global solution , model , inp
model . set_weights ( weights )
prediction = model . predict ( inp )
# here our best reward is zero
reward = - np . sum ( np . square ( solution - prediction ))
return reward
ตอนนี้เราสามารถสร้างอ็อบเจ็กต์ EvolutionStrategy และรันมันเพื่อทำซ้ำบางอย่างได้:
# if your task is computationally expensive, you can use num_threads > 1 to use multiple processes;
# if you set num_threads=-1, it will use number of cores available on the machine; Here we use 1 process as the
# task is not computationally expensive and using more processes would decrease the performance due to the IPC overhead.
es = EvolutionStrategy ( model . get_weights (), get_reward , population_size = 20 , sigma = 0.1 , learning_rate = 0.03 , decay = 0.995 , num_threads = 1 )
es . run ( 1000 , print_step = 100 )
นี่คือผลลัพธ์:
ซ้ำ 100 รางวัล: -68.819312 ซ้ำ 200 รางวัล: -0.218466 ซ้ำ 300 รางวัล: -0.110204 ซ้ำ 400 รางวัล: -0.001901 ซ้ำ 500 รางวัล: -0.000459 ซ้ำ 600 รางวัล: -0.000287 ซ้ำ 700 รางวัล: -0.000939 ซ้ำ 800 รางวัล: -0.000504 ซ้ำ 900 รางวัล: -0.000522 ซ้ำ 1,000 รางวัล: -0.000178
ตอนนี้เรามีน้ำหนักที่ปรับให้เหมาะสมแล้ว และเราสามารถอัปเดตโมเดลของเราได้:
optimized_weights = es . get_weights ()
model . set_weights ( optimized_weights )