Hands-On Interactive Neuro-Symbolic NLP with DR ai L (2022)

Abstract: We recently introduced DRaiL, a declarative neural-symbolic modeling framework designed to support a wide variety of NLP scenarios. In this paper, we enhance DRaiL with an easy to use Python interface, equipped with methods to define, modify and augment DRaiL models interactively, as well as with methods to debug and visualize the predictions made. We demonstrate this interface with a challenging NLP task: predicting sentence and entity level moral sentiment in political tweets.

## introduction language in real world settings is complex and ambiguous, and relies on a shared understanding of the world for its interpretation. most current nlp methods represent language in a latent highdimensional space by learning word co-occurrence patterns from massive amounts of textual data @xcite . this representation is very powerful, but it can be insufficient to capture non-linguistic context such as the physical, social and cultural environments @xcite . manually annotating these diverse sources of context is a major challenge, and as a result, interactive and humans-in-the-loop approaches are gaining popularity to enhance and correct nlp models @xcite . however, the complexity of the representation learned by deep learning models create challenges for the communication between humans and machines. to circumvent these challenges, most existing humansin-the-loop techniques solicit people to provide feedback on individual predictions instead, or allow people to augment the dataset by providing additional examples @xcite . while straightforward, working in the space of the raw 1 work done while the first author was at purdue university. neuro-symbolic representations present us with an opportunity us to enrich expressive language representations. on the one hand, symbols can be used to represent higher-level concepts and abstractions to characterize the information expressed in the text without resorting to individual annotations. on the other hand, distributed representations can help us ground these concepts and generalize to linguistic variations. moreover, symbolic rules allow us to explicitly model the dependencies between aspects of the language and higher-level abstractions and behaviors. most importantly, neuro-symbolic representations are inherently explainable, making them particularly useful for interactive and humansin-the-loop approaches. recently, we introduced drail @xcite , a neurosymbolic modeling framework for nlp. in this work, we enhance drail with a python interface to facilitate the interactive exploration of neurosymbolic nlp models. we demonstrate how to model two challenging language scenarios using drail, and propose a set of diagnostic and visualization operations to probe and debug drail predictions. then, we demonstrate how we can interactively enhance and modify drail programs to correct mistakes and introduce additional knowledge. this work represents a first step towards an interactive neuro-symbolic framework. an executable version of this demo is publicly available, and it includes the full code flow to run an example 2 . the source code for drail, as well as its documentation and additional examples have been released to the community foot_1 2 case studies morality framing in political discourse we previously introduced morality framing, a knowledge representation framework for capturing sentence and entity level moral sentiment @xcite . it is built on top of the moral foundation theory (mft) @xcite that proposes six moral foundations (mfs). morality frames extend mft by introducing entity sentiment dimensions. the mfs are considered as frame predicates, and positive and negative entity roles are associated with each predicate. for example, the mf 'care/harm' is motivated by entities that are either 'providing care' or 'doing harm' to a specific target entity. ## problem specification in this section, we demonstrate how to model the scenarios described in section 2. to model a problem in drail, we need to decompose the domain into a set of entities, labels, predicates and probabilistic rules that express the different decisions and their inter-dependencies. to predict morality frames, we break down the problem into two main decisions: 1) the most prominent moral foundation expressed in the tweet, and 2) for each entity mentioned in the tweet, the role they playing. in addition to this, we include some contextualizing information. for political tweets, we model the topic being discussed, the author of the tweet, and their party affiliation. in the case of covid-19 debate, we model the stance (i.e. pro or anti vax), as well as the main theme highlighted in the argument (e.g. government distrust). in this demo, we present a python api that allows us to instantiate and learn drail programs. the api is centered around a learner class. we currently support two types of learners, a local-learner in which rule weights are learned independently of each other, and a globallearner in which all rule weights are learned jointly. the learner receives a set of parameters, including the inference algorithm and loss function to be used, as well as the learning rate. entities and predicates entities are the base elements in a drail program. entities are named, and can correspond to either symbolic elements (e.g. a topic) or attributed elements (e.g. a tweet associated with its textual content). then, we can specify relations between one or more entities in drail. relations can correspond to observed or predicted information. when observations are available for a particular relation, either as input information or as training data, it can be passed to drail using column separated files. each column in the file will correspond to each of the entities involved in the relation. for example: rules and constraints in drail, decisions and dependencies between different decisions can be modeled using probabilistic rules. we can express these rules using templates of the form: p 0 ∧ p 1 ... ∧ p n-1 ⇒ p n , where the body of the rule template can contain observed or predicted predicates, and the head corresponds to the output to be predicted (given the body). rules can be grounded in data, and each rule grounding is associated with a weight representing the likelihood of the rule grounding holding true. in drail, these weights are learned using neural nets. for this reason, each rule template is associated to a feature function and a neural scoring function. we support different types of rules. we can define simple rules that map observed inputs to predicted outputs, and estimate the likelihood of each possible assignment. for example, mapping tweets to mfs: learner.define_rule( "istweet(t) => hasmf(t,m)^?", @xmath0.0, @xmath1, @xmath2 or we can write rules that capture the dependencies between different aspects, and estimate the likelihood them co-occurring. for example, mfs and vaccination stances: here, lmd is a hyper-parameter that can be used to manually tune the importance of each rule. given a learned weight w for a given rule, its final weight will be calculated by multiplying lmd*w. we use ? after a predicate to signal that this is a predicate that is not observed, and should be predicted. finally, we can also write hard dependencies or constraints that enforce behaviors. for example, enforcing entities to maintain the same polarity when mentioned in tweets with the same stance: feature extractors and scoring functions drail gives us the flexibility to define any feature function and neural architecture to represent rules and learn their weights. to define feature functions, we need to extend drail's featureextractor class. this programmatic interface gives us a lot of flexibility with passing and importing resources, as well as manipulating features: from drail.features.feature_extractor import featureextractor from transformers import autotokenizer class mf_ft(featureextractor): def __init__(self, id2data): super(mf_ft, self).__init__() self.@xmath3.@xmath4.from_pretrained( 'bert-base-uncased') def entity_bert(self, rule_gr): @xmath5.get_body_predicate("hasentity") (tweet, entity) = pred['arguments'] @xmath6.id2data[tweet][entity]['text'] @xmath7.tokenizer.encode(text) return bert_input the constructor allows us to pass any data structure. in the example above, we pass a dictionary that maps entity ids to their attributes (e.g. the text of the tweet). then, we import the transformers library to obtain the inputs for bert. alternatively, this could be pre-computed and passed to the constructor directly. drail allows us to obtain the predicates and arguments of each rule grounding with the function rulegrounding.get_body_predicate(name, @xmath8, which returns the predicate as a dictionary of the form {"name": name, "arguments": [arg0, arg1, ...]}. custom featureextractors can be instantiated in the learner by doing: learner.@xmath9.json") drail provides a similar programmatic interface to define neural scoring functions, which is built on top of pytorch: from drail.neuro.nn_model import neuralnetworks from transformers import autoconfig, automodel, bertmodel import torch class bertclassifier(neuralnetworks): def __init__(self, config): super(bertclassifier, self).__init__(config) def build_architecture(self): self.@xmath10.from_pretrained( 'bert-base-uncased', @xmath11.@xmath12.nn.dropout(config["dropout_prob"]) self.@xmath13.nn.linear(config["h_dim"], config["o_dim"]) ↩→ def forward(self, x): # will return feats that were defined in feat function @xmath14.get_inputs(x) @xmath15.bert_model(bert_inputs['input_ids'], bert_inputs['attention_mask'], bert_inputs['token_type_ids']) ↩→ ↩→ @xmath16.dropout(pooled_output) @xmath17.hidden2label(pooled_output) given that the neural architectures are defined programmatically, there is a lot of flexibility as to what each architecture can look like. for each rule, we can define a configuration dictionary config which can be passed to the constructor of the neural classifier. this allows us to specify variable parameters (e.g. number of hidden/output units), and to reuse classifiers for different rules. to see the full set of rules that were tested for each use case, we refer the user to our previous work @xcite , and to the live demo and repository linked to this paper. grounding, inference and learning to instantiate our database and ground our rules, we need to call the create_dataset function and pass the directory that contains the files that were defined for each predicate. we are able to specify train, dev and test splits by creating filters in the database. this operation is useful when we want to perform k-fold cross-validation, as it allows us to dynamically change the splits in an execution loop. @xmath18.create_dataset("data_dir") db.add_filter( @xmath19, @xmath20, @xmath21, @xmath22 ## interactive evaluation and debugging in this section, we present an evaluation module equipped with functions to interactively debug and visualize drail models. these functions are especially valuable when evaluating model predictions over unlabeled data, where we cannot directly measure performance. to showcase this capability, we use the sets of unlabeled tweets about abortion and the covid-19 vaccine described in sec. 2. ## drail vs. other systems in previous work, we have positioned the modeling approach of drail with respect to related work, including declarative languages to express probabilistic graphical models @xcite , relational and graph embeddings @xcite , and a comprehensive set of neuro-symbolic systems @xcite @xcite . while performing an exhaustive comparison between systems is beyond the scope of this demo, we refer the reader to the drail modeling paper for this analysis @xcite , as well as to the many successful applications of our modeling strategy @xcite @xcite @xcite @xcite . ## summary in this paper, we present an interactive api for drail, a recently introduced neuro-symbolic modeling framework. we demonstrate how to use this api to model a challenging nlp problem, and interactively debug predictions on unlabeled datasets, where traditional evaluation techniques cannot be applied. we motivate the advantage of neuro-symbolic representations to communicate knowledge from humans to machines, and show that we can effectively enhance the performance of the model by interactively adding new knowledge.