ยินดีต้อนรับสู่โครงการ Evotorch! Evotorch เป็นไลบรารีการคำนวณแบบโอเพนซอร์สที่พัฒนาขึ้นที่ Nnaisense สร้างขึ้นบน Pytorch ดูเอกสารสำหรับคำแนะนำเชิงลึกเกี่ยวกับการใช้ Evotorch และเข้าร่วมกับเราในการอภิปราย
เริ่มต้นด้วยการติดตั้ง Evotorch:
pip install evotorch
ด้วย Evotorch เราสามารถแก้ปัญหาการเพิ่มประสิทธิภาพที่หลากหลายไม่ว่าพวกเขาจะแตกต่างกันหรือไม่ (เช่นอนุญาตให้มีการไล่ระดับสี) ในประเภทปัญหาที่สามารถแก้ไขได้ด้วย Evotorch คือ:
อัลกอริทึมการคำนวณวิวัฒนาการต่างๆมีอยู่ใน Evotorch:
เนื่องจากอัลกอริทึมเหล่านี้ทั้งหมดถูกนำไปใช้ใน pytorch พวกเขาได้รับประโยชน์จากการใช้ vectorization และการทำให้เป็นคู่ขนานบน GPU ทำให้เร่งการเพิ่มประสิทธิภาพอย่างมากเมื่อมี GPU การใช้เรย์ Evotorch จะปรับสเกลอัลกอริทึมเหล่านี้ให้ดียิ่งขึ้นโดยการแยกเวิร์กโหลดข้าม:
ด้านล่างนี้เป็นตัวอย่างโค้ดที่แสดงให้เห็นถึง API ของ Evotorch
ฟังก์ชั่นวัตถุประสงค์ใด ๆ ที่กำหนดให้ทำงานกับ pytorch สามารถใช้โดยตรงกับ Evotorch ฟังก์ชั่นวัตถุประสงค์ที่ไม่ใช่ vectorized ได้รับการแก้ปัญหาเป็นเทนเซอร์ไฟฉาย 1 มิติและส่งคืนความฟิตเป็นสเกลาร์ ฟังก์ชั่นวัตถุประสงค์แบบเวกเตอร์ได้รับชุดของการแก้ปัญหาเป็นเทนเซอร์ไฟฉาย 2 มิติและส่งคืนเทนเซอร์ 1 มิติของฟิตเนส ตัวอย่างต่อไปนี้แสดงให้เห็นถึงวิธีการกำหนดและแก้ปัญหา Rastrigin คลาสสิก
from evotorch import Problem
from evotorch . algorithms import SNES
from evotorch . logging import StdOutLogger , PandasLogger
import math
import matplotlib . pyplot as plt
import torch
# Declare the objective function
def rastrigin ( x : torch . Tensor ) -> torch . Tensor :
A = 10
( _ , n ) = x . shape
return A * n + torch . sum (( x ** 2 ) - A * torch . cos ( 2 * math . pi * x ), 1 )
# Declare the problem
problem = Problem (
"min" ,
rastrigin ,
initial_bounds = ( - 5.12 , 5.12 ),
solution_length = 100 ,
vectorized = True ,
# device="cuda:0" # enable this line if you wish to use GPU
)
# Initialize the SNES algorithm to solve the problem
searcher = SNES ( problem , popsize = 1000 , stdev_init = 10.0 )
# Initialize a standard output logger, and a pandas logger
_ = StdOutLogger ( searcher , interval = 10 )
pandas_logger = PandasLogger ( searcher )
# Run SNES for the specified amount of generations
searcher . run ( 2000 )
# Get the progress of the evolution into a DataFrame with the
# help of the PandasLogger, and then plot the progress.
pandas_frame = pandas_logger . to_dataframe ()
pandas_frame [ "best_eval" ]. plot ()
plt . show ()ตัวอย่างต่อไปนี้แสดงให้เห็นถึงวิธีการแก้ปัญหาการเรียนรู้การเสริมแรงที่มีให้ผ่านห้องสมุดยิม
from evotorch . algorithms import PGPE
from evotorch . logging import StdOutLogger , PicklingLogger
from evotorch . neuroevolution import GymNE
# Declare the problem to solve
problem = GymNE (
env = "Humanoid-v4" , # Solve the Humanoid-v4 task
network = "Linear(obs_length, act_length)" , # Linear policy
observation_normalization = True , # Normalize the policy inputs
decrease_rewards_by = 5.0 , # Decrease each reward by 5.0
num_actors = "max" , # Use all available CPUs
# num_actors=4, # Explicit setting. Use 4 actors.
)
# Instantiate a PGPE algorithm to solve the problem
searcher = PGPE (
problem ,
# Base population size
popsize = 200 ,
# For each generation, sample more solutions until the
# number of simulator interactions reaches this threshold
num_interactions = int ( 200 * 1000 * 0.75 ),
# Stop re-sampling solutions if the current population size
# reaches or exceeds this number.
popsize_max = 3200 ,
# Learning rates
center_learning_rate = 0.0075 ,
stdev_learning_rate = 0.1 ,
# Radius of the initial search distribution
radius_init = 0.27 ,
# Use the ClipUp optimizer with the specified maximum speed
optimizer = "clipup" ,
optimizer_config = { "max_speed" : 0.15 },
)
# Instantiate a standard output logger
_ = StdOutLogger ( searcher )
# Optional: Instantiate a logger to pickle and save the results periodically.
# In this example, among the saved results will be the center of the search
# distribution, since we are using PGPE which is distribution-based.
_ = PicklingLogger ( searcher , interval = 10 )
# Run the algorithm for the specified amount of generations
searcher . run ( 500 )
# Get the center point of the search distribution,
# obtain a policy out of that point, and visualize the
# agent using that policy.
center_solution = searcher . status [ "center" ]
trained_policy = problem . make_net ( center_solution )
problem . visualize ( trained_policy )ตัวอย่างเพิ่มเติมสามารถพบได้ที่นี่
หากคุณใช้ Evotorch ในการวิจัยของคุณโปรดพิจารณาอ้างถึงบทความของเรา
@article { evotorch2023arxiv ,
title = { {EvoTorch}: Scalable Evolutionary Computation in {Python} } ,
author = { Toklu, Nihat Engin and Atkinson, Timothy and Micka, Vojtv{e}ch and Liskowski, Pawel{} and Srivastava, Rupesh Kumar } ,
journal = { arXiv preprint } ,
year = { 2023 } ,
note = { https://arxiv.org/abs/2302.12600 }
}โปรดดูแนวทางการบริจาคของเรา