Conservative Safety Critics for Exploration

1 INTRODUCTION . Reinforcement learning ( RL ) is a powerful framework for learning-based control because it can enable agents to learn to make decisions automatically through trial and error . However , in the real world , the cost of those trials – and those errors – can be quite high : a quadruped learning to run as fast as possible , might fall down and crash , and then be unable to attempt further trials due to extensive physical damage . However , learning complex skills without any failures at all is likely impossible . Even humans and animals regularly experience failure , but quickly learn from their mistakes and behave cautiously in risky situations . In this paper , our goal is to develop safe exploration methods for RL that similarly exhibit conservative behavior , erring on the side of caution in particularly dangerous settings , and limiting the number of catastrophic failures . A number of previous approaches have tackled this problem of safe exploration , often by formulating the problem as a constrained Markov decision process ( CMDP ) ( Garcıa & Fernández , 2015 ; Altman , 1999 ) . However , most of these approaches require additional assumptions , like assuming access to a function that can be queried to check if a state is safe ( Thananjeyan et al. , 2020 ) , assuming access to a default safe controller ( Koller et al. , 2018 ; Berkenkamp et al. , 2017 ) , assuming knowledge of all the unsafe states ( Fisac et al. , 2019 ) , and only obtaining safe policies after training converges , while being unsafe during the training process ( Tessler et al. , 2018 ; Dalal et al. , 2018 ) . In this paper , we propose a general safe RL algorithm , with bounds on the probability of failures during training . Our method only assumes access to a sparse ( e.g. , binary ) indicator for catastrophic failure , in the standard RL setting . We train a conservative safety critic that overestimates the probability of catastrophic failure , building on tools in the recently proposed conservative Q-learning framework ( Kumar et al. , 2020 ) for offline RL . In order to bound the likelihood of catastrophic failures at every iteration , we impose a KL-divergence constraint on successive policy updates so that the stationary distribution of states induced by the old and the new policies are not arbitrarily ∗Work done during HB ’ s ( virtual ) visit to Sergey Levine ’ s lab at UC Berkeley different . Based on the safety critic ’ s value , we consider a chance constraint denoting probability of failure , and optimize the policy through primal-dual gradient descent . Our key contributions in this paper are designing an algorithm that we refer to as Conservative Safety Critics ( CSC ) , that learns a conservative estimate of how safe a state is , using this conservative estimate for safe-exploration and policy updates , and theoretically providing upper bounds on the probability of failures throughout training . Through empirical evaluation in five separate simulated robotic control domains spanning manipulation , navigation , and locomotion , we show that CSC is able to learn effective policies while reducing the rate of catastrophic failures by up to 50 % over prior safe exploration methods . 2 PRELIMINARIES . We describe the problem setting of a constrained MDP ( Altman , 1999 ) specific to our approach and the conservative Q learning ( Kumar et al. , 2020 ) framework that we build on in our algorithm . Constrained MDPs . We take a constrained RL view of safety ( Garcıa & Fernández , 2015 ; Achiam et al. , 2017 ) , and define safe exploration as the process of ensuring the constraints of the constrained MDP ( CMDP ) are satisfied while exploring the environment to collect data samples . A CMDP is a tuple ( S , A , P , R , γ , µ , C ) , where S is the state space , A is the action space , P : S ×A×S → [ 0 , 1 ] is a transition kernel , R : S × A → R is a task reward function , γ ∈ ( 0 , 1 ) is a discount factor , µ is a starting state distribution , and C = { ( ci : S → { 0 , 1 } , χi ∈ R ) |i ∈ Z } is a set of ( safety ) constraints that the agent must satisfy , with constraint functions ci taking values either 0 ( alive ) or 1 ( failure ) and limits χi defining the maximal allowable amount of non-satisfaction , in terms of expected probability of failure . A stochastic policy π : S → P ( A ) is a mapping from states to action distributions , and the set of all stationary policies is denoted by Π . Without loss of generality , we can consider a single constraint , where C denotes the constraint satisfaction function C : S → { 0 , 1 } , ( C ≡ 1 { failure } ) similar to the task reward function , and an upper limit χ . Note that since we assume only a sparse binary indicator of failure from the environmentC ( s ) , in purely online training , the agent must fail a few times during training , and hence 0 failures is impossible . However , we will discuss how we can minimize the number of failures to a small rate , for constraint satisfaction . We define discounted state distribution of a policy π as dπ ( s ) = ( 1 − γ ) ∑∞ t=0 γ tP ( st = s|π ) , the state value function as V πR ( s ) = Eτ∼π [ R ( τ ) |s0 = s ] , the state-action value function as QπR ( s , a ) = Eτ∼π [ R ( τ ) |s0 = s , a0 = a ] , and the advantage function as AπR ( s , a ) = QπR ( s , a ) − V πR ( s ) . We define similar quantities for the constraint function , as VC , QC , and AC . So , we have V πR ( µ ) = Eτ∼π [ ∑∞ t=0R ( st , at ) ] and V π C ( µ ) denoting the average episodic failures , which can also be interpreted as expected probability of failure since V πC ( µ ) = Eτ∼π [ ∑∞ t=0 C ( st ) ] = Eτ∼π [ 1 { failure } ] = P ( failure|µ ) . For policy parameterized as πφ , we denote dπ ( s ) as ρφ ( s ) . Note that although C : S → { 0 , 1 } takes on binary values in our setting , the function V πC ( µ ) is a continuous function of the policy π . Conservative Q Learning . CQL ( Kumar et al. , 2020 ) is a method for offline/batch RL ( Lange et al. , 2012 ; Levine et al. , 2020 ) that aims to learn a Q-function such that the expected value of a policy under the learned Q function lower bounds its true value , preventing over-estimation due to out-ofdistribution actions as a result . In addition to training Q-functions via standard Bellman error , CQL minimizes the expected Q-values under a particular distribution of actions , µ ( a|s ) , and maximizes the expected Q-value under the on-policy distribution , π ( a|s ) . CQL in and of itself might lead to unsafe exploration , whereas we will show in Section 3 , how the theoretical tool introduced in CQL can be used to devise a safe RL algorithm . 3 THE CONSERVATIVE SAFE-EXPLORATION FRAMEWORK . In this section we describe our safe exploration framework . The safety constraint C ( s ) defined in Section 2 is an indicator of catastrophic failure : C ( s ) = 1 when a state s is unsafe and C ( s ) = 0 when it is not , and we ideally desire C ( s ) = 0 ∀s ∈ S that the agent visits . Since we do not make any assumptions in the problem structure for RL ( for example a known dynamics model ) , we can not guarantee this , but can at best reduce the probability of failure in every episode . So , we formulate the constraint as V πC ( µ ) = Eτ∼π [ ∑∞ t=0 C ( st ) ] ≤ χ , where χ ∈ [ 0 , 1 ) denotes probability of failure . Our approach is motivated by the insight that by being “ conservative ” with respect to how safe a state is , and hence by over-estimating this probability of failure , we can effectively ensure constrained exploration . Figure 1 provides an overview of the approach . The key idea of our algorithm is to train a conservative safety critic denoted as QC ( s , a ) , that overestimates how unsafe a particular state is and modifies the exploration strategy to appropriately account for this safety under-estimate ( by overestimating the probability of failure ) . During policy evaluation in the environment , we use the safety critic QC ( s , a ) to reduce the chance of catastrophic failures by checking whether taking action a in state s has QC ( s , a ) less than a threshold . If not , we re-sample a from the current policy π ( a|s ) . We now discuss our algorithm more formally . We start by discussing the procedure for learning the safety critic QC , then discuss how we incorporate this in the policy gradient updates , and finally discuss how we perform safe exploration ( Garcıa & Fernández , 2015 ) during policy execution in the environment . Overall objective . Our objective is to learn an optimal policy π∗ that maximizes task rewards , while respecting the constraint on expected probability of failures . π∗ = arg max π∈ΠC V πR ( µ ) where ΠC = { π ∈ Π : V πC ( µ ) ≤ χ } ( 1 ) Learning the safety critic . The safety critic QC is used to obtain an estimate of how unsafe a particular state is , by providing an estimate of probability of failure , that will be used to guide exploration . We desire the estimates to be “ conservative ” , in the sense that the probability of failure should be an over-estimate of the actual probability so that the agent can err on the side of caution while exploring . To train such a critic QC , we incorporate tools from CQL to estimate QC through updates similar to those obtained by reversing the sign of α in Equation 2 of CQL ( H ) ( Kumar et al. , 2020 ) . This gives us an upper bound on QC instead of a lower bound , as ensured by CQL . We denote the over-estimated advantage corresponding to this safety critic as ÂC . Formally the safety critic is trained via the following objective , where the objective inside arg min is called CQL ( ζ ) , ζ parameterizes QC , and k denotes the kth update iteration . Q̂k+1C ← arg min QC α · ( −Es∼Denv , a∼πφ ( a|s ) [ QC ( s , a ) ] + E ( s , a ) ∼Denv [ QC ( s , a ) ] ) + 1 2 E ( s , a , s′ , c ) ∼Denv [ ( QC ( s , a ) − B̂πφQ̂kC ( s , a ) ) 2 ] ( 2 ) Here , B̂πφ is the empirical Bellman operator discussed in section 3.1 and equation 2 of Kumar et al . ( 2020 ) . α is a weight that varies the importance of the first term in equation 2 , and controls the magnitude of value over-estimation , as we now highlight in red above . For states sampled from the replay buffer Denv , the first term seeks to maximize the expectation of QC over actions sampled from the current policy , while the second term seeks to minimize the expectation of QC over actions sampled from the replay buffer . Denv can include off-policy data , and also offline-data ( if available ) . We interleave the gradient descent updates for training ofQC , with gradient ascent updates for policy πφ and gradient descent updates for Lagrange multiplier λ , which we describe next . Policy learning . Since we want to learn policies that obey the constraint we set in terms of the safety critic , we can solve the objective in equation 1 via : max πφ Es∼ρφ , a∼πφ [ A πφ R ( s , a ) ] s.t . Es∼ρφ , a∼πφQC ( s , a ) ≤ χ ( 3 ) We can construct a Lagrangian and solve the policy optimization problem through primal dual gradient descent max πφ min λ≥0 Es∼ρφ , a∼πφ [ A πφ R ( s , a ) − λ ( QC ( s , a ) − χ ) ] We can apply vanilla policy gradients or some actor-critic style Q-function approximator for optimization . Here , QC is the safety critic trained through CQL as described in equation 2 . We defer specific implementation details for policy learning to the final paragraph of this section . Algorithm 1 CSC : safe exploration with conservative safety critics 1 : Initialize V rθ ( task value fn ) , Q s ζ ( safety critic ) , policy πφ , λ , Denv , thresholds , δ , χ . 2 : Set V̂ πφold C ( µ ) ← χ. . V̂ πφold C ( µ ) denotes avg . failures in the previous epoch . 3 : for epochs until convergence do . Execute actions in the environment . Collect on-policy samples . 4 : for episode e in { 1 , . . . , M } do 5 : Set ← ( 1− γ ) ( χ− V̂ πφold C ( µ ) ) 6 : Sample a ∼ πφold ( s ) . Execute a iff QC ( s , a ) ≤ . Else , resample a . 7 : Obtain next state s′ , r = R ( s , a ) , c = C ( s′ ) . 8 : Denv ← Denv ∪ { ( s , a , s′ , r , c ) } . If available , Denv can be seeded with off-policy/offline data 9 : end for 10 : Store the average episodic failures V̂ πφold C ( µ ) ← ∑M e=1 V̂ e C 11 : for step t in { 1 , . . . , N } do . Policy and Q function updates using Denv 12 : Gradient ascent on φ and ( Optionally ) add Entropy regularization ( Appendix A.2 ) 13 : Gradient updates for the Q-function ζ : = ζ − ηQ∇ζCQL ( ζ ) 14 : Gradient descent step on Lagrange multiplier λ ( Appendix A.2 ) 15 : end for 16 : φold ← φ 17 : end for Executing rollouts ( i.e. , safe exploration ) . Since we are interested in minimizing the number of constraint violations while exploring the environment , we do not simply execute the learned policy iterate in the environment for active data collection . Rather , we query the safety critic QC to obtain an estimate of how unsafe an action is and choose an action that is safe via rejection sampling . Formally , we sample an action a ∼ πφold ( s ) , and check if QC ( s , a ) ≤ . We keep re-sampling actions πφold ( s ) until this condition is met , and once met , we execute that action in the environment . In practice , we execute this loop for 100 iterations , and choose the action a among all actions in state s for which QC ( s , a ) ≤ and the value of QC ( s , a ) is minimum . If no such action a is found that maintains QC ( s , a ) ≤ , we just choose a for which QC ( s , a ) is minimum ( although above the threshold ) . Here , is a threshold that varies across iterations and is defined as = ( 1 − γ ) ( χ − V̂ πφoldC ( µ ) ) where , V̂ πφold C ( µ ) is the average episodic failures in the previous epoch , denoting a sample estimate of the true V πφold C ( µ ) . This value of is theoretically obtained such that Lemma 1 holds . In the replay buffer Denv , we store tuples of the form ( s , a , s′ , r , c ) , where s is the previous state , a is the action executed , s′ is the next state , r is the task reward from the environment , and c = C ( s′ ) , the constraint value . In our setting , c is binary , with 0 denoting a live agent and 1 denoting failure . Overall algorithm . Our overall algorithm , shown in Algorithm 1 , executes policy rollouts in the environment by respecting the constraint QC ( s , a ) ≤ , stores the observed data tuples in the replay buffer Denv , and uses the collected tuples to train a safety value function QC using equation 2 , update the policy and the dual variable λ following the optimization objective in equation 6 . Implementation details . Here , we discuss the specifics of the implementation for policy optimization . We consider the surrogate policy improvement problem Sutton ( 2020 ) : max πφ Es∼ρφold , a∼πφ [ A πφold R ( s , a ) ] s.t . Es∼ρφold [ DKL ( πφold ( ·|s ) ||πφ ( ·|s ) ) ] ≤ δ and V πφ C ( µ ) ≤ χ ( 4 ) Here , we have introduced a DKL constraint to ensure successive policies are close in order to help obtain bounds on the expected failures of the new policy in terms of the expected failures of the old policy in Section 4 . We replace the DKL ( πφold ( ·|s ) ||πφ ( ·|s ) ) term by its second order Taylor expansion ( expressed in terms of the Fisher Information Matrix F ) and enforce the resulting constraint exactly ( Schulman et al. , 2015a ) . Following equation 22 ( Appendix A.2 ) we have , max πφ Es∼ρφold , a∼πφ [ A πφold R ( s , a ) ] s.t . V πφold C ( µ ) + 1 1− γ Es∼ρφold , a∼πφ [ AC ( s , a ) ] ≤ χ s.t . Es∼ρφold [ DKL ( πφold ( ·|s ) ||πφ ( ·|s ) ) ] ≤ δ ( 5 ) We replace the true AC by the learned over-estimated ÂC , and consider the Lagrangian dual of this constrained problem , which we can solve by alternating gradient descent as shown below . max πφ min λ≥0 Es∼ρφold , a∼πφ [ A πφold R ( s , a ) ] − λ ( V πφold C ( µ ) + 1 1− γ Es∼ρφold , a∼πφ [ ÂC ( s , a ) ] − χ ) s.t . 1 2 ( φ− φold ) TF ( φ− φold ) ≤ δ ( 6 ) Note that although we use FIM for the updates , we can also apply vanilla policy gradients or some actor-critic style Q-function approximator to optimize equation 6 . Detailed derivations of the gradient updates are in Appendix A.2 .

Summary: This paper introduces a method for performing safe exploration in RL. It addresses the problem of ensuring that partially-trained policies do not visit unsafe regions of the state space, while still being exploratory enough to collect useful training experiences. The proposed technique is based on learning conservative estimates of the probability of a catastrophic failure occurring at different states. Based on these, the authors show that it is possible to upper bound the likelihood of reaching an unsafe state at every training step, thereby guaranteeing that all safety constraints are satisfied with high probability. Importantly, the authors also show that (at least asymptotically), the method is no worse than standard unsafe reinforcement learning algorithms.