Evotorch 프로젝트에 오신 것을 환영합니다! Evotorch는 Pytorch 위에 지어진 Nnaisense에서 개발 된 오픈 소스 진화 계산 라이브러리입니다. Evotorch 사용에 대한 심층적 인 지침은 문서를 참조하고 토론을 위해 Slack에 참여하십시오.
Evotorch를 설치하여 시작하십시오 :
pip install evotorch
Evotorch를 사용하면 차별화 가능한지 여부에 관계없이 다양한 최적화 문제를 해결할 수 있습니다 (즉, 그라디언트 하강을 허용합니다). Evotorch와 함께 해결할 수있는 문제 유형은 다음과 같습니다.
Evotorch에서 다양한 진화 계산 알고리즘을 사용할 수 있습니다.
이러한 모든 알고리즘은 Pytorch에서 구현되므로 GPU에서 벡터화 및 GPU의 병렬화를 통해 이점을 얻어 GPU를 사용할 수있을 때 최적화 속도를 크게 높입니다. Ray를 사용하여 Evotorch는 워크로드를 분할하여 이러한 알고리즘을 더욱 확장합니다.
다음은 Evotorch의 API를 보여주는 몇 가지 코드 예제입니다.
Pytorch와 함께 작동하도록 정의 된 모든 목적 함수는 Evotorch와 직접 사용할 수 있습니다. 비 벡터화 된 객관적인 기능은 단순히 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 }
}기여 가이드 라인을 참조하십시오.