{'fraction_non_alphanumeric': 0.027350302676682956, 'fraction_numerical': 0.008679162716067392, 'mean_word_length': 4.627086183310533, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Leveraging class abstraction for commonsense reinforcement learning via residual policy gradient methods

Enabling reinforcement learning (RL) agents to leverage a knowledge base while learning from experience promises to advance RL in knowledge intensive domains. However, it has proven difficult to leverage knowledge that is not manually tailored to the environment. We propose to use the subclass relationships present in open-source knowledge graphs to abstract away from specific objects. We develop a residual policy gradient method that is able to integrate knowledge across different abstraction levels in the class hierarchy. Our method results in improved sample efficiency and generalisation to unseen objects in commonsense games, but we also investigate failure modes, such as excessive noise in the extracted class knowledge or environments with little class structure.


Introduction
Deep reinforcement learning (DRL) has enabled us to optimise control policies in MDPs with high-dimensional state and action spaces such as in game-play [Silver et al., 2016] and robotics [Lillicrap et al., 2016]. Two main hindrances in bringing deep reinforcement learning to the real world are the sample inefficiency and poor generalisation performance of current methods [Kirk et al., 2021]. Amongst other approaches, including prior knowledge in the learning process of the agent promises to alleviate these obstacles and move reinforcement learning (RL) from a tabula rasa method to more human-like learning. Depending on the area of research, prior knowledge representations can vary from pretrained embeddings or weights [Devlin et al., 2019] to symbolic knowledge representations such as logics [Vaezipoor et al., 2021] and knowledge graphs (KGs) [Zhang et al., 2020b]. While the former are easier to integrate into deep neural network-based algorithms, they lack specificity, abstractness, robustness and interpretability [van Harmelen and ten Teije, 2019].
One type of prior knowledge that is hard to obtain for purely data-driven methods is commonsense knowledge. Equipping reinforcement learning agents with commonsense or world knowledge is an important step towards improved human-machine interaction [Akata et al., 2020], as interesting interactions demand machines to access prior knowledge not learnable from experience. Commonsense games [Jiang et al., 2020;Murugesan et al., 2021] have emerged as a testbed for methods that aim at integrating commonsense knowledge into a RL agent. Prior work has focused on augmenting the state by extracting subparts of ConceptNet [Murugesan et al., 2021]. Performance only improved when the extracted knowledge was tailored to the environment. Here, we focus on knowledge that is automatically extracted and should be useful across a range of commonsense games.
Humans abstract away from specific objects using classes, which allows them to learn behaviour at class-level and generalise to unseen objects [Yee, 2019]. Since commonsense games deal with real-world entities, we look at the problem of leveraging subclass knowledge from open-source KGs to improve sample efficiency and generalisation of an agent in commonsense games. We use subclass knowledge to formulate a state abstraction, that aggregates states depending on which classes are present in a given state. This state abstraction might not preserve all information necessary to act optimally in a state. Therefore, a method is needed that learns to integrate useful knowledge over a sequence of more and more fine-grained state representations. We show how a naive ensemble approach can fail to correctly integrate information from imperfectly abstracted states, and design a residual learning approach that is forced to learn the difference between policies over adjacent abstraction levels. The properties of both approaches are first studied in a toy setting where the effectiveness of class-based abstractions can be controlled. We then show that if a commonsense game is governed by class structure, the agent is more sample efficient and generalises better to unseen objects, outperforming embedding approaches and methods augmenting the state with subparts of ConceptNet. However, learning might be hampered, if the extracted class knowledge aggregates objects incorrectly. To summarise, our key contributions are: • we use the subclass relationship from open-source KGs to formulate a state abstraction for commonsense games; • we propose a residual learning approach that can be integrated with policy gradient algorithms to leverage imperfect state abstractions; • we show that in environments with class structure our method leads to more sample efficient learning and better generalisation to unseen objects.

Related Work
We introduce the resources available to include commonsense knowledge and the attempts that have been made by prior work to leverage these resources. Since the class knowledge is extracted from knowledge graphs, work on including KGs in deep neural network based architectures is discussed. The setting considered here also offers a new perspective on state abstraction in reinforcement learning. Commonsense KGs. KGs store facts in form of entityrelation-entity triplets. Often a KG is constructed to capture either a general area of knowledge such as commonsense [Ilievski et al., 2021], or more domain specific knowledge like the medical domain [Huang et al., 2017]. While ConceptNet [Speer et al., 2017] tries to represent all commonsense knowledge, others focus on specific parts such as cause and effect relations [Sap et al., 2019]. Manually designed KGs [Miller, 1995] are less error prone, but provide less coverage and are more costly to design, making hybrid approaches popular [Speer et al., 2017]. Here, we focus on WordNet [Miller, 1995] Figure 1: (Left) Visualisation of a state and its abstractions in Wordcraft based on classes extracted from WordNet. Colours indicate that an object is mapped to a class, where the same colour refers to the same class within an abstraction. (Right) Subgraph of the class tree that is used to determine the state abstractions.
al., 2020b]. Most methods are based on an attention mechanism over parts of the knowledge base [Moon et al., 2019;Gou et al., 2021]. Two key differences are that the knowledge graphs used are curated for the task, and therefore contain little noise. Additionally, most tasks are framed as supervised learning problems where annotation about correct reasoning patterns are given. Here, we extract knowledge from opensource knowledge graphs and have to deal with errors in the class structure due to problems with entity reconciliation and incompleteness of knowledge. State abstraction in RL. State abstraction aims to partition the state space of a base Markov Decision Process (MPD) into abstract states to reduce the complexity of the state space on which a policy is learnt [Li et al., 2006]. Different criteria for aggregating states have been proposed [Givan et al., 2003]. They guarantee that an optimal policy learnt for the abstract MDP remains optimal for the base MDP. To leverage state abstraction an aggregation function has to be learned [Zhang et al., 2020a], which either needs additional samples or is performed on-policy leading to a potential collapse of the aggregation function [Kemertas and Aumentado-Armstrong, 2021]. The case in which an approximate state abstraction is given as prior knowledge has not been looked at yet. The abstraction given here must not satisfy any consistency criteria and can consist of multiple abstraction levels. A method that is capable of integrating useful knowledge from each abstraction level is needed.

Problem Setting
Reinforcement learning enables us to learn optimal behaviour in an MDP M = (S, A, R, T, γ) with state space S, action space A, reward function R : S × A → R, discount factor γ and transition function T : S × A → ∆S , where ∆S represents the set of probability distributions over the space S. The goal is to learn from experience a policy π : S → ∆A that optimises the objective: where G is the discounted return. A state abstraction function φ : S → S aggregates states into abstract states with the goal to reduce the complexity of the state space. Given an arbitrary weighting function w : S → [0, 1] s.t. ∀s ∈ S , s∈φ −1 (s ) w(s) = 1, one can define an abstract reward function R and transition function T on the abstract state space S : (2) to obtain an abstract MDP M = (S , A, R , T , γ). If the abstraction φ satisfies consistency criteria (see section 2, State abstraction in RL) a policy learned over M allows for optimal behaviour in M , which we reference from now on as base MDP [Li et al., 2006]. Here, we assume that we are given abstraction functions φ 1 , ..., φ n with φ i : S i−1 → S i , where S 0 corresponds to the state space of the base MDP. Since φ i must not necessarily satisfy any consistency criteria, learning a policy over one of the corresponding abstract MDPs M i can result in a non-optimal policy. The goal is to learn a policy π or action value function Q, that takes as input a hierarchy of state abstractions s = (s 1 , ..., s n ). Here, we want to make use of the more abstract states s 2 , ...s n for more sample efficient learning and better generalisation.

Methodology
The method can be separated into two components: (i) constructing the abstraction functions φ 1 , ..., φ n from the subclass relationships in open source knowledge graphs; (ii) learning a policy over the hierarchy of abstract states s = (s 1 , ..., s n ) given the abstraction functions.
Constructing the abstraction functions φ i . A state in a commonsense game features real world entities and their relations, which can be modelled as a set, sequence or graph of entities. The idea is to replace each entity with its superclass, so that states that contain objects with the same superclass are aggregated into the same abstract state. Let E be the vocabulary of symbols that can appear in any of the abstract states s i , i.e. s i = {e 1 , ..., e k |e l ∈ E}. The symbols that refer to real-world objects are denoted by O ⊆ E and C tree represents their class tree. A class tree is a rooted tree in which the leaves are objects and the parent of each node is its superclass. The root is a generic entity class of which every object/class is a subclass (see Appendix A for an example).
To help define φ i , we introduce an entity based abstraction φ E i : E → E. Let C k represent objects/classes with depth k in C tree and L be the depth of C tree , then we can define φ E i and φ i : where Pa(e) are the parents of entity e in the class tree C tree This abstraction process is visualised in Figure 1. In practice, we need to be able to extract the set of relevant objects from the game state and construct a class tree from open-source KGs. If the game state is not a set of entities but rather text, we use spaCy 2 to extract all nouns as the set of objects. The class tree is extracted from either DBpedia, ConceptNet or WordNet. For the detailed algorithms of each KG extraction, we refer to Appendix A. Here, we discuss some of the caveats that arise when extracting class trees from open-source KGs, and how to tackle them. Class tree can become imbalanced, i.e. the depths of the leaves, representing the objects, differs ( Figure 2). As each additional layer with a small number of classes adds computational overhead but provides little abstraction, we collapse layers depending on their contribution towards abstraction ( Figure 2). While in DBpedia or Word-Net the found entities are mapped to a unique superclass, entities in ConceptNet are associated with multiple superclasses.
To handle the case of multiple superclasses, each entity is mapped to the set of all i-step superclasses for i = 1, ..., n.
To obtain representations for these class sets, the embeddings of each element of the set are averaged.
Learning policies over a hierachy of abstract states.
Since prior methods in commonsense games are policy gradient-based, we will focus on this class of algorithms, while providing a similar analysis for value-based methods in Appendix B. First, we look at a naive method to learn a policy in our setting, discuss its potential weaknesses and then propose a novel gradient update to overcome these weaknesses. A simple approach to learning a policy π over s is to take an ensemble approach by having a network with separate parameters for each abstraction level to predict logits, that are then summed up and converted via the softmax operator into a final policy π. Let s i,t denote the abstract state on the i-th level at timestep t, then π is computed via: where NN θi is a neural network processing the abstract state s i parameterised by θ i . This policy can then be trained via any policy gradient algorithm [Schulman et al., 2015;Espeholt et al., 2018]. From here on, we will refer to this approach as sum-method.
There is no mechanism that forces the sum approach to learn on the most abstract level possible, potentially leading to worse generalisation to unseen objects. At train time making all predictions solely based on the lowest level (ignoring all higher levels) can be a solution that maximises discounted return, though it will not generalise well to unseen objects. To circumvent this problem, we adapt the policy gradient so that the parameters θ i at each abstraction level are optimised to approximate an optimal policy for the i-th abstraction level given the computed logits on abstraction level i − 1. Let s n i,t = (s i,t , ..., s n,t ) denote the hierarchy of abstract states at timestep t down to the i-th level. Define the policy on the i-th level as and notice that π = π 1 . To obtain a policy gradient expression that contains the abstract policies π i , we write π as a product of abstract policies: π n (a|s n,t ).
and plug it into the policy gradient expression for an episodic task with discounted return G: where π n+1 ≡ 1. Notice that in Equation 9, the gradient of the parameters θ i depend on the values of all policies of the level equal or lower than i. The idea is to take the gradient for each abstraction level i only with respect to θ i and not the full set of parameters θ. This optimises the parameters θ i not with respect to their effect on the overall policy, but their effect on the abstract policy on level i. The residual policy gradient is given by: ∇ θi log(π i (a|s n i,t ))G . (10) Each element of the first sum resembles the policy gradient loss of a policy over the abstract state s i . However, the sampled trajectories are from the overall policy π and not the policy π i and the policy π i inherits a bias from previous layers in form of logit-levels. We refer to the method based on the update in Equation 10 as residual approach. An advantage of the residual and sum approach is that the computation of the logits from each layer can be done in parallel. Any sequential processing of levels would have a prohibitively large computational overhead.

Experimental Evaluation
Our methodology is based on the assumption that abstraction via class knowledge extracted from open-source KGs is useful in commonsense game environments. This must not necessarily be true. It is first sensible to study the workings of our methodology in an idealised setting, where we control whether and how abstraction is useful for generalisation and sample efficiency. Then, we evaluate the method on two commonsense games, namely a variant of Textworld Commonsense [Murugesan et al., 2021] and Wordcraft.
Toy environment. We start with a rooted tree, where each node is represented via a random embedding. The leaves of the tree represent the states of a base MDP. Each depth level of the tree represents one abstraction level. Every inner node of the tree represents an abstract state that aggregates the states of its children. The optimal action for each leaf (base state) is determined by first fixing an abstraction level l and randomly sampling one of five possible actions for each abstract state on that level. Then, the optimal action for a leaf is given by the sampled action for its corresponding abstract state on level l. The time horizon is one step, i.e. for each episode a leaf state is sampled, and if the agent chooses the correct action, it receives a reward of one. We test on new leaves with unseen random embeddings, but keep the same abstraction hierarchy and the same pattern of optimal actions. The sum and residual approach are compared to a policy trained only given the base states and a policy given only the optimal abstract states (oracle). To study the effect of noise in the abstraction, we replace the optimal action as determined by the optimal state with noise probability σ (noise setting) or ensure that the abstraction step only aggregates a single state (ambiguity setting). All policies are trained via REINFORCE [Williams, 1992] with a value function as baseline and entropy regularisation. More details on the chosen trees and the policy network architecture can be found in Appendix C.
Textworld Commonsense. In text-based games, the state and action space are given as text. In Textworld Commonsense (TWC), an agent is located in a household and has to put objects in their correct commonsense location, i.e. the location one would expect these objects to be in a normal household. The agent receives a reward of one for each object that is put into the correct location. The agent is evaluated by the achieved normalised reward and the number of steps needed to solve the environment, where 50 steps is the maximum number of steps allowed. To make abstraction necessary, we use a large number of objects per class and increase the number of games the agent sees during training from 5 to 90. This raises the exposure to different objects at training time. The agent is evaluated on a validation set where it encounters previously seen objects and a test set where it does not. The difficulty level of a game is determined by the number of rooms, the number of objects to move and the number of distractors (objects already in the correct location), which are here two, two and three respectively. To study the effect of inaccuracies in the extratcted class trees from Word-Net, ConceptNet and DBpedia, we compare them to a manual aggregation of objects based on their reward and transition behaviour. Murugesan et al.
[2021] benchmark different architectures that have been proposed to solve text-based games [He et al., 2016]. Here, we focus on their proposed method that makes use of a recurrent neural network architecture and numberbatch embeddings [Speer and Lowry-Duda, 2017], which performed best in terms of normalised reward and number of steps needed. For more details on the architecture and the learning algorithm, we refer to the initial paper. As baselines, we choose: (i) adding class information via hyperbolic embeddings trained on WordNet [Nickel and The abstract state perfectly determines the action to take in the base state (top). Noise is added to this relation, so that in 50% of the time the optimal action is determined randomly beforehand (middle). In the ambiguous setting, not every abstract state has multiple substates (bottom). Experiments are run over five seeds. Wordcraft. An agent is given a goal entity and ingredient entities, and has to combine the ingredient entities to obtain the goal entity. A set of recipes determines which combination of input objects leads to which output entity. One can create different settings depending on the number of distractors (irrelevant entities in the set of recipe entities) and the set of recipes available at training time. Generalisation to unseen recipes guarantees that during training not all recipes are available, and generalisation to unseen goals ensures that at test time the goal entity has not been encountered before as a goal entity. Jiang et al.
[2020] trained a policy using the IMPALA algorithm [Espeholt et al., 2018]. We retain the training algorithm and the multi-head attention architecture [Vaswani et al., 2017] used for the policy network to train our policy components NN θ1 , ..., NN θn .

Results
After checking whether the results in the ideal environment are as expected, we discuss results in TWC and Wordcraft.

Toy Environment
From Figure 3 top, one can see that the sum and residual approach are both more sample efficient than the baseline without abstraction; on par with the sample efficiency of the oracle. The same holds for the generalisation performance. As the base method faces unseen random embeddings at test time, there is no possibility for generalisation. In the noise setting (Figure 3 middle), the policy trained on the abstract state reaches its performance limit at the percentage of states whose action is determined via their abstract state; the base, residual and sum method reach instead optimal performance. The achieved reward at generalisation time decreases for both the sum and residual method, but the sum approach suffers more from overfitting to the noise at training time when compared to the residual approach. In the case of ambiguous abstraction, we see that the residual approach outperforms the sum approach. This can be explained by the residual gradient update, which forces the residual method to learn everything on the most abstract level, while the sum approach distributes the contribution to the final policy over the abstraction layers. At test time, the sum approach puts too much emphasis on the uninformative base state causing the policy to take wrong actions.

Textworld Commonsense
The results suggest that both the sum and residual approach are able to leverage the abstract states. Sample efficiency. From Figure 4, one can see that the residual and sum approach learn with less samples compared to the base approach. The peak mean difference is 20 episodes from a total of 100 training episodes. While the sum approach learns towards the beginning of training as efficiently, it collapses for some random seeds towards the end of training, resulting in higher volatility of the achieved normalised reward. This is similar to what was previously observed in experiments in the noisy setting of the ideal environment. That the performance of the sum approach collapses for certain seeds suggests, that the learned behaviour for more abstract states depends on random initialisation of weights. This stability issue is not present for the residual approach. Figure 5 (right) shows that adding hyperbolic embeddings does not improve sample efficiency and even decreases . Base refers to the baseline with no abstraction, Base-H refers to baseline with hyperbolic embeddings and Base-L for baseline added LocatedAt relations. In bold, the best performing method without manually specified knowledge. We highlight in red the conditions in which the manual class graph outperforms the other methods. Experiments are run over ten seeds.  Figure 5: Training performance in the difficult environment using the residual approach when the class knowledge comes from different knowledge graphs trained (left) and when the baseline is augmented with hyperbolic embeddings or the LocatedAt relation from ConceptNet (right). Experiments are run over ten seeds.
performance towards the end of training. Adding the Locate-dAt relations improves sample efficiency to a similar degree as adding class abstractions.
Generalisation. Table 1 shows that, without any additional knowledge the performance of the baseline, measured via mean normalised reward and mean number of steps, drops when faced with unseen objects at test time. Adding hyperbolic embeddings does not alleviate that problem but rather hampers generalisation performance on the validation and test set. When the LocatedAt relation from ConceptNet is added to the state representation the drop in performance is reduced marginally. Given a manually defined class abstraction, generalisation to unseen objects is possible without any drop in performance for the residual and the sum approach. This remains true for the residual approach when the manually defined class knowledge is exchanged with the Word-Net class knowledge. The sum approach based on WordNet performs worse on the validation and training set due to the collapse of performance at training time.
KG ablation. From Figure 5 and Table 1 it is evident that for the residual approach, the noise and additional abstraction layers introduced by moving from the manually defined classes to the classes given by WordNet, only cause a small drop in generalisation performance and no additional sam-  Figure 6: Generalisation results for the Wordcraft environment with respect to unseen goal entities. Experiments are run over ten seeds.
ple inefficiency. The abstractions derived from DBpedia and ConceptNet hamper learning particularly for the residual approach ( Figure 5). By investigating the DBpedia abstraction, we notice that many entities are not resolved correctly. Therefore objects with completely different semantics get aggregated. The poor performance from the ConceptNet abstraction hints at a problem with averaging class embeddings over multiple superclasses. Although two objects may have an overlap in their set of superclasses, the resulting embeddings could still differ heavily due to the non-overlapping classes. Figure 6 shows the generalisation performance in the Wordcraft environment. For the case of random embeddings, abstraction can help to improve generalisation results. Replacing random embeddings with GloVe embeddings improves generalisation beyond the class abstraction, and combining class abstraction with GloVe embeddings does not result in any additional benefit. Looking at the recipe structure in Wordcraft, objects are combined based on their semantic similarity, which can be better captured via word embeddings rather than through classes. Although no generalisation gains can be identified, adding abstraction in the presence of GloVe embeddings leads to improved sample efficiency. A more detailed analysis discussing the difference in generalisation between class abstraction and pretrained embeddings in the Wordcraft environment can be found in Appendix D.

Conclusion
We show how class knowledge, extracted from open-source KGs, can be leveraged to learn behaviour for classes instead of individual objects in commonsense games. To force the RL agent to make use of class knowledge even in the presence of noise, we propose a novel residual policy gradient update based on an ensemble learning approach. If the extracted class structure approximates relevant classes in the environment, the sample efficiency and generalisation performance to unseen objects are improved. Future work could look at other settings where imperfect prior knowledge is given and whether this can be leveraged for learning. Finally, KGs contain more semantic information than classes, which future work could try to leverage for an RL agent in commonsense games.

A Extracting class trees from commonsense KGs
To construct the abstraction function φ i presented in Section 4 from open-source knowledge graphs, we need a list of objects that can appear in the game. We collect trajectories via random play and extract the objects out of each state appearing in these trajectories. The extraction method depends on the representation of the state. In Wordcraft [Jiang et al., 2020] (see Section 5), the state is a set of objects, such that no extraction is needed. In Textworld [Côté et al., 2018] the state consists of text. We use spaCy 3 for POS-tagging and take all nouns to be relevant objects in the game. Next, we look at how to extract class knowledge from WordNet [Miller, 1995], DBpedia [Lehmann et al., 2015] and ConceptNet [Speer et al., 2017] given a set of relevant objects. An overview of the characteristics of the extracted class trees for each knowledge graph and each environment is presented in

A.1 WordNet
We use WordNet's hypernym relation between two nouns as the superclass relation. To query WordNet's synsets and the hypernym relation between them, we use NLTK 4 . For each object, we query recursively the superclass relation until the entity synset is reached. Having obtained this chain of superclasses for each object, we merge it into a class tree by aggregating superclasses appearing in multiple superclass chains of different objects into one node. An example of the resulting class tree can be found in Figure 7. One problem that arises is entity resolution, i.e. picking the semantically correct synset for a given word in case of multiple available semantics. Here, we simply take the first synset/hypernym returned. More complex resolution methods are possible. Objects that have no corresponding synsets in WordNet have the noun "entity" as a parent in the class tree.

A.2 DBpedia
We use the SPARQL 5 language to first query all the URI's that have the objects name as a label (using the rdfs:label property). If a URI that is part of DBpedia's ontology can be found, we query the rdfs:subClassOf relation recursively until the URI owl:Thing is reached. If the URI corresponding to the object name is not part of the ontology, we query the rdf:type relation recursively until the URI owl:Thing is reached. This gives us again a chain of classes connected via the superclass relation for each object, which can be merged into a class tree as described in Section A.1. Any entity that can not be mapped to a URI in DBpedia is directly mapped to the owl:Thing node of the class tree.

A.3 ConceptNet
We use ConceptNet's API 6 to query the IsA relation for a list of objects. In ConceptNet, it can happen that there are multiple nodes with different descriptions for the same semantic concept connected to an object via the IsA relation. For example, the object airplane is connected among other things via the IsA relation to heavier-than-air craft, aircraft and fixedwing aircraft. This causes two problems: (i) sampling only one superclass could lead to a class tree where objects of the same superclass are not aggregated due to minor differences in the class description; (ii) recursively querying the superclass relation until a general entity class is reached leads to a prohibitively large class tree, and is not guaranteed to terminate due to possible cycles in the IsA relation. To tackle the first problem, we maintain a set of superclasses for each object per abstraction level, instead of a single superclass per level. The representative embedding of the superclass of an object is then calculated by averaging the embeddings of the elements in the set of superclasses. To tackle the second problem, we restrict the superclasses to the two step neighbourhood of the IsA relation, i.e. we have two abstraction levels.
Further simplifications are that only IsA relations are considered with a weight above or equal to one 7 and superclasses with a description length of less than or equal to three words.

B Residual Q-learning
First, the adaption of Q-learning to the setting described in section 3 is explained. Then results of the algorithm in the toy environment from section 5 are presented.

B.1 Algorithm
In Section 4 we focused on how to adapt policy gradient methods to learn a policy over a hierarchy of abstract states s = (s 1 , ..., s n ). Here we provide a similar adaption of Qlearning [Watkins and Dayan, 1992] and present results from the toy environment. The sum approach naturally extends to Q-learning but instead of summing over logits from each layer, we sum over action values. Let s t = (s 1,t , ..., s n,t ) be the hierarchy of abstract states at time t, then the action value is given by: where N N θi is a neural network parameterized by θ i . The idea of the residual Q-learning method is similar to the residual policy-gradient, i.e. we want to learn an action value function on the most abstract level and then adapt this on lower  levels as more information on the state becomes available. Let s t = (s 1,t , ..., s n,t ) be a hierarchy of abstract states at time t and R i the reward function of the abstract MDP on the i-th level as defined in Section 3. Further define R n+1 ≡ 0. Then one can write the action value function for an episodic task with discount factor γ as: where Q res i is defined as: The dependency of the reward function R i,t on the state s i,t and action a is omitted for brevity. At each layer of the abstraction the residual action value function Q res i learns how to adapt the action values of the previous layer to approximate the discounted return for the now more detailed state-action pair. This information difference is characterised by the difference in the reward functions of both layers R i − R i+1 .
To learn Q res i we use an adapted version of the Q-learning update rule. Given a transition (s t , a, r t , s t+1 ), where s represents the full hierarchy over abstract states, the update is given by: This can be seen as performing Q-learning over the following residual MDP M res t , a) and T res i = T i (s i,t+1 |s i,t , a) · T i+1 (s i+1,t+1 |s i+1,t , a).
We cannot apply the update rule directly, since each transition only contains a single sample for both reward functions, The abstract state perfectly determines the action to take in the base state (top). Noise is added to this relation, such that in 40% of the time the optimal action is determined randomly beforehand (middle). In the ambiguous setting not every abstract state has multiple substates (bottom). Experiments are run over five seeds.
i.e. r t is a sample of R i and R i+1 . As a result for every transition the difference would be 0. To be able to apply the learning rule, one solution would be to maintain averages of the reward functions for each state. This is infeasible in highdimensional state spaces. Instead, we approximate R i+1 via the difference between the current and next state action value of the previous abstraction level, i.e.
The adaptions made by Mnih et al. [2015] to leverage deep neural networks for Q-learning can be used to scale residual Q-learning to high-dimensional state spaces.

B.2 Results in the Toy Environment
The results resemble the results already discussed for the policy gradient case (cfr. Section 6.1). Without any noise or ambiguity in the state abstraction the residual and sum approach learn as efficiently with respect to the number of samples and generalise as well as if the perfect abstraction is given (Figure 8 top). The sum approach struggles to learn the action values on the correct abstraction level if the abstraction is ambiguous, hampering its generalisation performance (Figure 8  bottom). Both, the sum and residual approach, generalise worse in the presence of noise (Figure 8 middle). However, the achieved reward at test time is less influenced by noise for the residual approach. We did not evaluate the residual Q-learning approach on the more complex environments as policy gradient methods have shown better performance in previous work [Murugesan et al., 2021] C Toy environment Here, a visualisation of the toy environment, details on the tree representing the environment and the used network architecture for the policy are given.

C.1 Environment Details
As explained in Section 5 the environment is determined by a tree where each node represents a state of the base MDP (leaves) or an abstract state (inner nodes). In a noise free setting the optimal action for a base state is determined by a corresponding abstract state, while with noise the optimal action is sampled at random with probability σ or else determined by the corresponding abstract state. A visualisation of the environment and what changes for each condition can be seen in Figure 9 and is summarised in Table 3.

C.2 Network architecture and Hyperparameter
The policy is trained via REINFORCE [Williams, 1992] with a value function as baseline and entropy regularisation. The value function and policy are parameterised by two separate networks and the entropy regularisation parameter is β. For the baseline and the optimal abstraction method the value network is a one layer MLP of size 128 and the policy is a threelayer MLP of size (256,128,64). In case abstraction is available the value network is kept the same but the policy network is duplicated (with a separate set of weights) for each abstraction level to parameterise the N N θi (Section 4).

D Wordcraft environment D.1 Network architecture and Hyperparameter
We build upon the network architecture and training algorithm used by Jiang et al. [2020]. Here we only present the  changes necessary to compute the abstract policies π i defined in Section 4 and the chosen hyperparameters.
To process a hierarchy of states s t = (s t,1 , ..., s t,n ) one can simply add a dimension on the state tensor representing the abstraction level. Classical network layers such as multilayer perceptron (MLP) can be adapted to process such a state tensor by adding a weight dimension representing the weights for each abstraction level and using batched matrix multiplication. We tuned the learning rate and entropy regularisation parameter (β) by performing a grid search over possible parameter values. We decided to collapse layers based on the additional abstraction each layer contributed (Section 4). For the sum and residual approach a learning rate of 0.0001 and β = 0.1 were used across experiments and layer 9-15 of the class hierarchy were collapsed. For the baseline a learning rate of 0.001 and the β = 0.1 were used. Only in the unseen goal condition the baseline with Glove embedding had to be trained with a learning rate of 0.0001.

D.2 Additional Results
In the Wordcraft environment the results with respect to generalisation to unseen recipes ( Figure 10) show similar patterns to the generalisation results presented in Section 6.3. With random embeddings adding class abstraction improves generalisation, but not as much if random embeddings are replaced with Glove embeddings [Pennington et al., 2014]. The combination of class abstraction with Glove embeddings does not lead to additional improvements. The objects that need to be combined in Wordcraft rarely follow any class rules, i.e. objects are not combined based on their class. Word embeddings capture more fine-grained semantics rather than just class knowledge [Pennington et al., 2014], therefore it is possible that class prior knowledge is less prone to overfitting. In a low data setting it is possible that word embeddings contain spurious correlations which a policy can leverage to perform well on during training but do not generalize well.
To test this hypothesis we created a low data setting in which training was performed when only 10% of all possible goal entities were part of the training set instead of the usual 80%. In Figure 11 one can see that all methods have a drop in performance (cfr. Section 6.3), but the drop is substanially smaller for the methods with abstraction and random embeddings compared to the baseline with Glove embeddings. The generalisation performance reverses, i.e. the baseline with Glove embeddings now performs the worst.
To understand whether and how the learned policy makes use of the class hierarchy we visualise a policy trained with the residual approach, random embeddings and access to the the full class hierarchy extracted from WordNet. For the visualisation we choose recipes that follow a class rule. In Wordcraft one can create an airplane by combining a bird and something that is close to a machine. From Figure 13 one can see that the policy learns to pick the correct action on level nine by recognising that a vertebrate and machine need to be combined.

E Runtime Analysis
Often an improved sample efficiency is achieved via an extension of an existing baseline, which introduces a computa-  tional overhead. If the method is tested in an environment where sampling is not the computational bottleneck it remains an open question whether the improvement in sample efficiency also translates into higher reward after a fixed time budget. To test this we measured the averaged time needed for one timestep over multiple seeds and fixed hardware and plotted the reward after time passed for the baseline and for the residual method with abstract states based on WordNet. All experiments were run on an a workstation with an Intel Xeon W2255 processor and a GeForce RTX 3080 graphics card. From Figure 12 we can see that the baseline as well as the extension via abstraction achieve the same reward after a fixed time. Even if computation time is an issue the residual approach takes no more time than the baseline and its generalisation performance in terms of normalised reward is better. For each abstraction level the state representation is shown, which entity has been selected (-1 means no entity has been selected yet) and the abstracted goal entity. The probability with which each entity is picked is plotted and the red bars indicate the optimal actions.