An earlier version of this story was published at AIhub.

Are you excited about self-driving cars? Do you trust autonomous driving technology if you are now invited to step into a self-driving vehicle? Most importantly, how do you know whether you would be safe during the journey?

For AI researchers, safety is difficult to measure, unlike accuracy, which we are more familiar with. It often feels awkward to describe safety using accuracy-related terms, such as “99 out of 100 journeys are safe” or “99.5% of pedestrians on the roads are correctly identified.“ Hearing this immediately makes us think – what happened on that one trip? Did somebody get injured? What happened to the 0.5% of pedestrians that did not get identified? How confident is the vehicle when it identifies a pedestrian? Does the behavior of the vehicle change when it is only 50% sure that there is a pedestrian compared to when it is 90% sure? The vehicle should brake harder for a 90% certainty than for a 10% certainty, right?

Also, how does the model find a trade-off between safety and other objectives? Driving at 5 km/hr is safe most of the time, but the passenger most likely wants to reach their destination as soon as possible.

These questions are at the core of safety in AI. Understanding how a machine learning model learns and reasons is crucial to making technologies more trustworthy.

Contributions

In a recent paper, we have presented a framework that represents, quantifies, and evaluates safety. We define safety using probabilistic if-else statements (or probabilistic logic formulae). For example, P(safe) ← 0.99, P(speed_low) expresses that “if the the agent has a low speed, then it will stay safe with a probability of 0.99”. Hence, the higher P(speed_low) is, the safer the agent would be.

The Reinforcement Learning (RL) objective is to learn an optimal policy. A policy is a full description of the agent’s behavior in all possible scenarios. We say a policy is optimal if by following it, the agent behaves optimally in all scenarios, yielding a maximum expected reward.

Our framework is based on shielding, a popular Safe Reinforcement Learning (Safe RL) technique that aims at achieving the traditional RL objective while ensuring the agent stays safe during the learning process. To do so, it relies on a shield, a logical component that monitors the agent’s actions and rejects those that violate the given safety constraint. Before performing an action, the agent consults the shield to determine whether the intended action is safe. One key benefit of using a shield is that safety is formally defined as part of the policy, ensuring that safety constraints are enforced whenever the agent takes an action.

Our approach, the Probabilistic Logic Shields (PLS), is a variant of a shield but has several advantages compared to a traditional one, including:

  • PLS uses a probabilistic logical formula to model safety (recall P(safe) ← 0.99, P(speed_low)) . Hence, it better integrates with the modern continuous Deep Reinforcement Learning methods.
  • PLS enables gradient computation from the safety constraints via probabilistic logic programming, thus it can be seamlessly applied to any policy gradient algorithm.
  • PLS is a generalization of traditional shielding techniques as it not only operates on symbolic state spaces but also on unstructured input, e.g. images. In fact, PLS considers the inherent noise and uncertainty in the input images to improve learning, while traditional shielding methods assume that the observations are accurate.
  • Since PLS takes into account noise in the observation, it finds a better trade-off between the traditional RL objective and the safety objective compared to traditional shielding.

We will now introduce a running example, illustrate our approach, and then compare it to the more traditional shielding methods.

Running example

Consider a self-driving agent in the figure above that encounters a red light and another vehicle to its right. To be able to handle this image as input, the agent is equipped with a standard neural policy (i.e. a policy represented by a neural network). The agent’s policy takes the image as input and produces a probability distribution over five predefined actions: do-nothing, accelerate, brake, turn-left, turn-right . For example, for the image above, a policy may be the following.

  • π(do-nothing)← 0.1
  • π(accelerate)← 0.5
  • π(brake)← 0.1
  • π(turn-left)← 0.1
  • π(turn-right)← 0.2

The symbol ← assigns a probability to the symbol on the left hand side. Here, the probabilities are produced by the agent’s neural policy. We use π to denote the probability of selecting an action in a given state (i.e. image). The sum of all π(.) must be one. Notice that π is not aware of safety.

To take into account safety, the shield has a set of sensors to detect from the image whether there is an obstacle (i.e., the red light and the other car) in front, on the left or on the right of the agent.

  • P(obs_front)← 0.8
  • P(obs_left)← 0.2
  • P(obs_right)← 0.5

The shield has safety-related knowledge represented by a set of probabilistic if-else statements. The first rule below states “if there is an obstacle in the front, and the agent accelerates, then a crash will occur with a probability of 0.9.”

  • P(crash)← 0.9, P(obs_front), π(accelerate).
  • P(crash)← 0.4, P(obs_left), π(turn-left).
  • P(crash)← 0.4, P(obs_right), π(turn-right).

The shield defines safety as the probability of not having a crash, i.e. P(safe)= 1- P(crash) .

Now, our goal is to modify the original policy π so that it becomes safer.

Probabilistic Logic Shields

Given the above information, our approach consists of three steps.

  1. Computing action safety P(safe|a) : how safe will the agent be if it executes an action a ?
  2. Computing policy safety $P_π$(safe) : how safe will the agent be if it randomly selects an action using a policy π
  3. Combining the two components above to make the policy π safer. The improved, safer policy is denoted by π+ and π+ is guaranteed to be safer than π . This is key to improving the safety of the entire learning process.

The following table illustrates the three steps. The last column shows the improved policy that the agent will use to sample an action to execute in the environment. We will now explain how these values are calculated.

Action a Old policy π(a) Action safety P(safe|a) Safer policy π+(a)
do-nothing 0.1 1 0.17
accelerate 0.5 0.28 0.24
brake 0.1 1 0.17
turn-left 0.1 0.92 0.15
turn-right 0.2 0.8 0.27
$P_π(safe)$ =0.592 $P_{π+}(safe)$ =0.761

Action safety

The first step is to quantify how safe it is to take an action. This can be easily computed using the if-else statement. For example, P(crash)← 0.9, P(obs_front), P(accelerate) and P(obs_front)← 0.8 together infer that the probability of having a crash if the agent decides to accelerate is P(crash|accelerate)=P(obs_front)* P(crash) = 0.72 . Then, the probability of staying safe if the agent accelerates is simply P(safe|accelerate) = 1- P(crash|accelerate)= 0.28 .

Policy safety

The second step is to compute how safe it is to follow a given policy, i.e. $P_π$(safe) .This is defined as the average of each action safety weighted by its probability, In our running example, $P_{π}$(safe) = 1*0.1+0.28*0.5+1*0.1+0.92*0.1+0.8*0.2 = 0.592 .

Making the policy safer

Finally, we now would like to increase the probability of selecting safe actions and decrease the probability of selecting unsafe ones. To do this, we compute a coefficient P(safe|a)/$P_π$(safe) indicating how safe an action a is compared to the other actions. If the action a is relatively safe, the coefficient should be larger than one. Otherwise, it is smaller than one. For example, P(safe|accelerate)/$P_π$(safe)=0.28/0.592=0.47, indicating that accelerate is relatively unsafe.

Then, we can multiply the coefficient with the original π(a) to obtain a safer policy, i.e. π+(a). In our running example, π+(accelerate)=0.24 , which is lower than the original value 0.5. Actions that are safer than average will get a greater probability in the safer policy, while actions that are less safe than average will get a lower probability. The new policy π+ is guaranteed to be safer than the original policy π . For a proof, we refer the reader to the paper.

Comparison with other approaches

We evaluate our approach using Atari games where the agent has five discrete actions (e.g. accelerate, turn-left, etc). The image on the left illustrates the whole lap and the one on the right is an agent’s input image during the learning process. The RL objective of the agent is to finish the lap as fast as possible and is modeled by a standard reward function. That is, the agent gets a small negative reward (i.e. a punishment) when it moves forward on the road. The faster it completes the lap, the less punishment it will get. The safety objective is not to drive onto the grass and is expressed by the probabilistic if-else statements above.

We compare our approach with two other baselines.

  • Standard deep RL (PPO in the figure/paper) is the baseline that does not consider the safety objective. This agent generally takes much risk, e.g. making hard turns to reach the destination sooner.
  • Traditional Shields (VSRL and ϵ-VSRL in the figure/paper) are similar to our approach but have two assumptions: (1) the input image is assumed to be noise-less and (2) an action is either safe or unsafe, i.e. P(safe|a)=0 or 1 . This agent is safer than the standard RL agent but may not be more rewarding, as shown in our experiments.
  • PLS (PLPG in the figure/paper) finds the best trade-off between the reward and the safety objectives. This agent is the safest and most rewarding among all agents.

The following figures visualize the trade-off between safety constraint violation (x-axis) and return (y-axis) in three different domains (i.e. the three columns). In this blog post, we focus on the Car Racing (CR) domain with noisy sensors (namely the lower-right figure) and invite the reader to the paper for details about the other domains.

Each small data point is an agent’s policy trained for 600k steps and the large data points are the average of five seeds. An ideal policy should lie in the upper-left corner (i.e. fewest violation and highest return). We evaluate the agents with two different laps, namely CR1 (circles in the figure) and CR2 (triangles in the figure), and the data points of them should be interpreted independently.

The results show that the PLPG (our approach) agents achieve the lowest violation while maintaining a comparable return to the other agents. When augmented with noisy sensors, the violation of PLPG is lower than the safety-agnostic baseline (PPO) and the traditional shields (VSRL and ε-VSRL).

A Broader Perspective

In this work, we have integrated probabilistic logic and machine learning in one framework, which falls under the umbrella term of neuro-symbolic AI. Neuro-symbolic AI combines the logic-based and learning-based methods, giving us two main benefits.

The first benefit is transparency and explainability. Reasoning processes in machine learning are often implicit and non-trivial, and this blog has illustrated how we explicitly create a safer policy by integrating logic into machine learning. This approach enhances our understanding of why complex AI systems do what they do and how they make decisions. It is particularly important in applications where decisions made by AI systems have significant consequences such as healthcare and finance.

The second benefit is enforcing structures and constraints, which is crucial in safety-critical domains like transportation and energy sections. Explicit constraint representations have been applied to enable stronger guarantees and performance in machine learning tasks such as image classification, cyber-physical system control, etc. This blog has illustrated such an example in RL. In fact, this type of integration can be potentially applied to any gradient-based approaches for safe, constrained, or logic-guided learning. The main idea is to augment a lightweight, autonomous logic layer on top of neural learners to provide instant feedback and supervision, which is especially promising in domains requiring rapid action and learning.

From a logical perspective, we have demonstrated the viability of including reward notions into probabilistic logic programming to insist learning (so that the task is not only probability maximization). This is similar to concepts such as the fitness functions in evolutionary algorithms, the (semantic) loss functions in deep learning, the reward-based properties in probabilistic planning and model checking. It is also closely related to multi-objective learning and optimization.

It is important to note that a theoretical foundation of the integration of logic-based and reward-based gradients is still an open problem. More research is required to understand the impact of combining different objectives and enable better coordination and design of objectives and stronger guarantees.