Package: cherry-rl
Cherry is a reinforcement learning framework for researchers built on top of PyTorch.Unlike other reinforcement learning implementations, cherry doesn't implement a single monolithic  interface to existing algorithms.
Instead, it provides you with low-level, common tools to write your own algorithms.
Drawing from the UNIX philosophy, each tool strives to be as independent from the rest of the framework as possible.
So if you don't like a specific tool, you don’t need to use it.FeaturesCherry extends PyTorch with only a handful of new core concepts.PyTorch modules for reinforcement learning:cherry.nn.Policy: base class for $\pi(a \mid s)$ policies.cherry.nn.ActionValue: base class for $Q(s, a)$ action-value functions.Data structures for reinforcement learning compatible with PyTorch:cherry.Transition: namedtuple to store $(s_t, a_t, r_t, s_{t+1})$ transitions (and more).cherry.ExperienceReplay: a list-like buffer to store and sample transitions.Low-level interfaceà laPyTorch to write and debug your algorithms.cherry.td.*andcherry.pg.*: temporal difference and policy gradient utilities.cherry.algorithms.*: helper functions for popular algorithms (PPO,TD3,DrQ, andmore).cherry.debug.*andcherry.plot.*: logging, visualization, and debugging tools.To learn more about the tools and philosophy behind cherry, check out ourGetting Started tutorial.Overview and ExamplesThe following snippet showcases a few of the tools offered by cherry.
Many more high-quality examples are available in theexamples/folder.Defining acherry.nn.PolicyclassVisionPolicy(cherry.nn.Policy):# inherits from torch.nn.Moduledef__init__(self,feature_extractor,actor):super(VisionGaussianPolicy,self).__init__()self.feature_extractor=feature_extractorself.actor=actordefforward(self,obs):mean=self.actor(self.feature_extractor(obs))std=0.1*torch.ones_like(mean)returncherry.distributions.TanhNormal(mean,std)# policies always return a distributionpolicy=VisionPolicy(MyResnetExtractor(),MyMLPActor())action=policy.act(obs)# sampled from policy's distributiondeterministic_action=policy.act(obs,deterministic=True)# distribution's modeaction_distribution=policy(obs)# work with the policy's distributionBuilding acherry.ExperienceReplayofcherry.Transition# building the replayreplay=cherry.ExperienceReplay()state=env.reset()fortinrange(1000):action=policy.act(state)next_state,reward,done,info=env.step(action)replay.append(state,action,reward,next_state,done)next_state=state# manipulating the replayreplay=replay[-256:]# indexes like a listbatch=replay.sample(32,contiguous=True)# sample transitions into a replaybatch=batch.to('cuda')# move replay to devicefortransitioninreversed(batch):# iterate over a replaytransition.reward*=0.99# get all states, actions, and rewards as PyTorch tensors.reinforce_loss=-torch.sum(policy(batch.state()).log_prob(batch.action())*batch.reward())Designing algorithms withcherry.td,cherry.pg, andcherry.algorithms# defining a new algorithm@dataclasses.dataclassclassMyA2C:discount:float=0.99defupdate(self,replay,policy,state_value,optimizer):# discount rewardsvalues=state_value(replay.action())discounted_rewards=cherry.td.discount(self.discount,replay.reward(),replay.done(),bootstrap=values[-1].detach())# Compute lossespolicy_loss=cherry.algorithms.A2C.policy_loss(log_probs=policy(replay.state()).log_prob(replay.action()),advantages=discounted_rewards-values.detach(),)value_loss=cherry.algorithms.A2C.state_value_loss(values,discounted_rewards)# Optimization stepoptimizer.zero_grad()(policy_loss+value_loss).backward()optimizer.step()return{'a2c/policy_loss':policy_loss,'a2c/value_loss':value_loss}# using MyA2Cmy_a2c=MyA2C(discount=0.95)my_policy=MyPolicy()linear_value=cherry.models.LinearValue(128)adam=torch.optim.Adam(policy.parameters())forstepinrange(1000):replay=collect_experience(policy)my_a2c.update(replay,my_policy,linear_value,adam)Installpip install cherry-rlChangelogA human-readable changelog is available in theCHANGELOG.mdfile.DocumentationDocumentation and tutorials are available on cherry’s website:http://cherry-rl.net.ContributingHere are a couple of guidelines we strive to follow.It's always a good idea to open an issue first, where we can discuss how to best proceed.If you want to contribute a new example using cherry, it would preferably stand in a single file.If you would like to contribute a new feature to the core library, we suggest to first implement an example showcasing your new functionality. Doing so is quite useful:it allows for automatic testing,it ensures that the functionality is correctly implemented,it shows users how to use your functionality, andit gives a concrete example when discussing the best way to merge your implementation.AcknowledgementsCherry draws inspiration from many reinforcement learning implementations, includingOpenAI Baselines,John Schulman'simplementationsIlya Kostrikov'simplementations,Shangtong Zhang'simplementations,Dave Abel'simplementations,Vitchyr Pong'simplementations,Kai Arulkumaran'simplementations,RLLab/Garage.Why 'cherry' ?Because it's the sweetest part ofthe cake.