<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Detecting out-of-distribution text using topological features of transformer-based language models</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Andres Pollano</string-name>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Anupam Chaudhuri</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Anj Simmons</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Deakin University</institution>
          ,
          <addr-line>Geelong</addr-line>
          ,
          <country country="AU">Australia</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Hashtag AI</institution>
          ,
          <addr-line>Melbourne</addr-line>
          ,
          <country country="AU">Australia</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>University of Melbourne</institution>
          ,
          <addr-line>Melbourne</addr-line>
          ,
          <country country="AU">Australia</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>To safeguard machine learning systems that operate on textual data against out-of-distribution (OOD) inputs that could cause unpredictable behaviour, we explore the use of topological features of self-attention maps from transformer-based language models to detect when input text is out of distribution. Self-attention forms the core of transformer-based language models, dynamically assigning vectors to words based on context, thus in theory our methodology is applicable to any transformer-based language model with multihead self-attention. We evaluate our approach on BERT and compare it to a traditional OOD approach using CLS embeddings. Our results show that our approach outperforms CLS embeddings in distinguishing in-distribution samples from far-out-of-domain samples, but struggles with near or same-domain datasets.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;Large language model</kwd>
        <kwd>Topological data analysis</kwd>
        <kwd>Out of distribution detection</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>
        Machine learning (ML) models perform well on the datasets
they have been trained on, but can behave unreliably when
tested on data that is out-of-distribution (OOD). For example,
when a ML model has been trained to recognise diferent
breeds of cats is fed an image of a dog, the results are
unpredictable. OOD detection is the task of identifying that an
input does not seem to be drawn from the same distribution
as the training data, and thus the prediction given by the
ML model should not be trusted. OOD detectors can be used
to defend ML models deployed in high stakes applications
from OOD data by providing a warning/error message for
OOD inputs rather than processing the input and producing
untrustworthy results [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ].
      </p>
      <p>
        In this paper, we focus on OOD detection for textual
inputs to safeguard ML models that perform natural language
processing (NLP) tasks. For example, a sentiment
classiifcation model trained on formal restaurant reviews may
not produce valid results when applied to informal posts
from social media. Determining that an input is OOD
requires a way to measure the distance between an input and
the in-distribution data. This in turn requires a method to
convert textual data into an embedding space in which we
can measure distance. One approach to this is to input the
text to a transformer-based language model, such as BERT
[
        <xref ref-type="bibr" rid="ref2">2</xref>
        ], to extract an embedding vector for the input text (e.g.,
the hidden representation of the special [] token). We
can then measure the distance of the embedding vector for
an input text to the nearest (or k-nearest) embedding
vector of a text from an in-distribution validation set. When
this distance is beyond some threshold (which needs to be
calibrated for the application), the input text is flagged as
out of distribution. The internal state of transformer-based
language models contains important information, which
may be able to ofer richer representations than only using
the embedding obtained from the last or penultimate layer.
For example, Azaria and Mitchell [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] demonstrated that it
is possible to train a classifier on the activation values of
the hidden layers of large language models to predict when
they are generating false information rather than true
information. However, training a classifier for OOD detection in
this manner is not a suitable approach, as the distribution
of the OOD data that will be encountered is not knowable
in advance. That is, due to the nature of OOD detection,
we need to extract an embedding vector and associated
distance metric (calibrated solely on the training/validation
data) without training a further classifier over this space.
      </p>
      <p>
        Recently, Kushnareva et al. [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] proposed an approach
to analyze the topology of attention maps of
transformerbased language models to determine when text had been
artificially generated, and Perez and Reinauer [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] propose
using the topology of attention maps of transformer-based
language models to detect adversarial textual attacks.
Specifically, topological data analysis (TDA) provides a way to
extract high-level features (related to the topology of the
attention maps for each attention head in each layer) that can
serve as an embedding vector of lower dimension than the
full internal model state. In this paper, we investigate the
suitability of these topological embeddings for the task of
OOD detection, and contrast them to traditional approaches.
Some of the work related to out-of-distribution detection in
the context of transformer-based language models and using
Mahalanobis distance can be referred to here [
        <xref ref-type="bibr" rid="ref6 ref7 ref8 ref9">6, 7, 8, 9</xref>
        ].
      </p>
      <p>We have made the code used to generate our results public
under the MIT licence, with the intention of aiding the
application of TDA methods to transformer-based models.1</p>
    </sec>
    <sec id="sec-2">
      <title>2. Background</title>
      <sec id="sec-2-1">
        <title>2.1. Topological Data Analysis</title>
        <p>
          Topology studies properties of geometric objects invariant
under continuous deformation. For instance, a donut and
a cofee cup are topologically equivalent. Algebraic
topology, as in Hatcher’s work [
          <xref ref-type="bibr" rid="ref10">10</xref>
          ], attaches algebraic objects
such as groups to topological spaces. Certain features of
these algebraic object can help to quantify those topological
spaces.
        </p>
        <sec id="sec-2-1-1">
          <title>1https://github.com/andrespollano/neural_nets-tda</title>
          <p>
            Persistence extends topology to finite data sets, tracing
back to Frosini [
            <xref ref-type="bibr" rid="ref11">11</xref>
            ], Robins [
            <xref ref-type="bibr" rid="ref12">12</xref>
            ]. Persistence homology
groups, derived from homology groups, serve as invariants
for discrete objects.
          </p>
          <p>For any finite set of points, we can construct a distance
matrix where both the rows and columns are labeled by
these points, and each entry in the matrix represents the
distance between a pair of points. We can apply tools from
Topological Data Analysis (TDA) to this set of points,
allowing us to assign certain invariant characteristics to the
collection.</p>
          <p>In the context of language or text, we can think of each
word as a point in some vector space, with a distance defined
between words. For example, the distance might be related
to semantic similarity or other linguistic relationships. By
considering a text as a collection of such points, we can
assign various numerical characteristics to it. These
characteristics can distinguish the text from others and provide
insights into its structure and content.
2.1.1. Simplicial Complex and Chain
A simplicial complex is a fundamental construct in
algebraic topology, used to approximate and study more
complex topological spaces. It is formed by combining simpler
building blocks called simplices.</p>
          <p>Simplices: A -dimensional simplex, denoted as  , is
the convex hull of  + 1 afinely independent points. For
example, a 0-simplex is a point, a 1-simplex is a line segment,
a 2-simplex is a triangle, and a 3-simplex is a tetrahedron.</p>
          <p>Forming a Simplicial Complex: A simplicial complex
 in R is a collection of simplices that satisfies two
conditions:</p>
        </sec>
        <sec id="sec-2-1-2">
          <title>1. Any face of a simplex in  is also in .</title>
          <p>2. The intersection of any two simplices in  is either
empty or a common face of both.</p>
          <p>Simplicial Chains: To study the algebraic properties of
simplicial complexes, we introduce the concept of simplicial
chains. A simplicial chain in a complex is a formal sum of
simplices. For a given dimension , the group of -chains,
denoted , is the free abelian group generated by the
dimensional simplices of the complex.</p>
          <p>Boundary Operators: The boundary of a simplex is the
sum of its faces. The boundary operator  :  → − 1
maps each -simplex to its ( − 1)-dimensional boundary.
This operator is crucial for defining the homology of the
complex.</p>
          <p>For example, the boundary of a 2-simplex (triangle)  =
[0, 1, 2] is the sum of its 1-dimensional faces (edges):
2( ) = [1, 2] + [2, 0] + [0, 1].</p>
          <p>Chain Complex: A chain complex is a sequence of chain
groups connected by boundary operators:</p>
          <p>− 1
0 →  →−−  − →1−− − · · · →
1 →−1 0 → 0.</p>
          <p>Cycle and Boundary Groups:
 = ker ,
 = im +1,
 ⊂ .</p>
          <p>Simplicial Homology: The th simplicial homology
group of a complex  is () = ()/(), with
the Betti number  () = dim ().
2.1.2. Vietoris-Rips Complex
The Vietoris-Rips complex is a key construct in topological
data analysis, used for forming a simplicial complex from a
set of data points based on their pairwise distances.</p>
          <p>Definition: Given a set of points  and a distance
threshold , the Vietoris-Rips complex ℛ() is defined as
follows: for any subset  ⊆ ,  is a simplex in ℛ() if
and only if the distance between every pair of points in  is
less than or equal to .</p>
          <p>Formal Construction:
• Vertices: Each point in  is a 0-simplex (vertex).
• Edges: An edge (1-simplex) connects vertices  and
 if (,  ) ≤ .
• Higher Simplices: A -simplex is formed by a set of
 + 1 vertices if every pair of vertices in the set is
connected by an edge.</p>
        </sec>
      </sec>
      <sec id="sec-2-2">
        <title>2.2. BERT Model</title>
        <p>
          BERT [
          <xref ref-type="bibr" rid="ref2">2</xref>
          ] is a transformer-based language model that has
been pre-trained on a large corpus of text from BooksCorpus
and English Wikipedia. Input text first needs to be tokenized,
in which each word is converted to one or more tokens.
The first token is the special [] token, followed by
the tokenization of each word, using the special [ ]
token to separate “sentences” (e.g., question and answer,
these don’t necessarily correspond to linguistic sentences).
BERT is trained to achieve two objectives: Masked Language
Modelling (MLM) in which tokens are masked at random
(replaced with the special [ ] token) and the language
model needs to learn to fill these in; and Next Sentence
Prediction (NSP) in which the final hidden vector of the
special [] token is used to predict if two sentences
follow each other in the corpus.
        </p>
        <p>
          As a transformer-based model, BERT consists of multiple
layers, each with multiple attention heads. While multiple
variants of BERT are available, for the purpose of this paper
we use  , which consists of 12 layers, each with
12 attention heads (i.e., 144 attention heads in total) that
operate on an input matrix, , of  tokens and 768 hidden
dimensions, .
2.2.1. Sentence Embeddings
The final hidden vector of the special [] token can be
used to embed the input sequence (which varies in length) in
 hidden dimensions (178 in the case of  ). The
authors of the BERT paper [
          <xref ref-type="bibr" rid="ref2">2</xref>
          ] note that the []
embedding is not a meaningful sentence representation without
ifne-tuning. Nevertheless, Uppaal et al. [
          <xref ref-type="bibr" rid="ref13">13</xref>
          ] claim that the
practice of using this to obtain sentence embeddings “is
standard for most BERT-like models”, and find that in the case
of RoBERTa (a BERT-like model without the NSP training
objective) this embedding serves as a “near perfect” OOD
detector even without fine-tuning.
2.2.2. Attention Maps
Each attention head computes an attention map,  , of
shape  ×  as an intermediate step of the calculation. We
use the same definition of attention maps as Kushnareva
et al. [
          <xref ref-type="bibr" rid="ref4">4</xref>
          ] presented below:
 =  (  )
  = softmax
︂( ( )(  ) )︂
√
        </p>
        <p>Where  ,   ,   are learned projection matrices
of shape  ×  and  is the output of the attention head
applied to the  ×  matrix  from the previous layer. In
this paper, we analyse the attention maps for each of the
144 attention heads in  using TDA.</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>3. Experiment design</title>
      <p>
        In this section, we outline the design of our methodology
for our OOD detection using Topological Data Analysis.
For a supervised classification task, given a test sample ,
OOD detection aims to determine whether it belongs to
the in-distribution (ID) dataset  ∈  or not. Some of
the background and literature review related to confidence
score for OOD detection can be found in [
        <xref ref-type="bibr" rid="ref14 ref15 ref9">9, 14, 15</xref>
        ]. We
consider a -dimensional representation of an input text
 as ℎ() in R. To analyse the benefits of TDA in OOD
detection, we consider two encoding functions ℎ1() and
ℎ2():
1. Topological feature vector ℎ1(): given , we
generate a vector of 1 topological features using the
graph representations of the 144 attention maps
generated by  . In 3.3 and subsection 3.4,
we explain in detail how the topological features are
generated from an input sentence.
2. Sentence embedding ℎ2(): we take the
2dimensional text embedding of the [] token
output by  , which captures the
contextual and semantic information of the input text
.
      </p>
      <p>
        Similar to Uppaal et al. [
        <xref ref-type="bibr" rid="ref13">13</xref>
        ], we define the OOD detection
function as (), which maps an instance  to {, }
as follows:
 (; ℎ) =
{︃
      </p>
      <p>
        if (; ℎ) ≥ 
 if (; ℎ) &lt; 
where (; ℎ) is an OOD scoring function using a
distance-based method (Mahalanobis distance to the ID class
centroids or Euclidean distance to k-nearest ID neighbour),
described in subsection 3.5, and  is the threshold chosen
so that a high proportion of ID samples’ scores are above  .
3.1. Data
As the in-distribution dataset, we choose the headlines and
abstract text of ‘Politics’ and ‘Entertainment’ news articles
from HufPost from the news-category dataset [
        <xref ref-type="bibr" rid="ref16">16</xref>
        ]. To test
the robustness of the OOD method, we conduct experiments
on three kinds of dataset distribution shifts [
        <xref ref-type="bibr" rid="ref17">17</xref>
        ]:
• Near Out-of-Domain shift. In this paradigm, ID
and OOD samples come from diferent distributions
(datasets) exhibiting semantic similarities. In our
experiments, we evaluate the abstract of news articles
from the cnn-dailymail dataset [
        <xref ref-type="bibr" rid="ref18">18</xref>
        ].
• Far Out-of-Domain shift. In this type of shift,
the OOD samples come from a diferent domain and
exhibit significant semantic diferences. In particular,
we evaluate the IMDB movie review dataset [
        <xref ref-type="bibr" rid="ref19">19</xref>
        ] as
OOD samples.
• Same-Domain shift. We also test a more
challenging setting, where ID and OOD samples are
drawn from the same domain, but with diferent
labels. Specifically, we extract the ‘Business’ news
articles from the news-category dataset.
      </p>
      <p>In our experiments we used a sample of 30,000 points
from the in-distribution dataset for the fine-tuned version
of the model, and use a validation and test size of 1,000
datapoints.
3.2. Model
We focus on the attention heads of a pre-trained
 (L=12, H=12) generated from an input text
 to produce topological features and compare this
encoding to the embeddings of the [] token as the sentence
representation. We replicate our experiments on a
finetuned  on the ID news categorisation task
 → {’Politics’, ’Entertainment’}. We fine-tune the model
for 3 epochs, using Adam with batch size of 32 and learning
rate 10− 5.
3.3. Attention Maps and Attention Graphs
(a) Attention maps
(12 × 12) derived from
pre-trained BERT for
the input text "President
issues vows as tensions
with China rise"
(b) BERT Attention Map (c) Undirected attention
(Layer 7; Head 10) graph (Layer 7; Head 10)
where edges are
proportional to the maximal
attention between the
two vertices. The edge
width represents shorter
distances (attention
strength)</p>
      <p>Attention maps play a crucial role in our methodology
as they form the basis for extracting topological features
used in our OOD detection. An attention map   is a
 × -dimensional matrix where each entry represents the
attention weight between two tokens. Each element 
can be interpreted as the level of ‘attention’ token  pays to
token  in the input sequence during the encoding process.
The higher the weight the stronger the relation between two
tokens. They are non-negative and the attention weights
of a token sum up to one (i.e. ∑︀
=1  = 1 for all
 = 1, ..., .).</p>
      <p>
        To generate topological features from an attention map,
we first convert it into an attention graph following the
approach of Perez and Reinauer [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ]. Given an attention
matrix  , we create an undirected weighted graph where
the vertices represent the tokens of the input text , and
the weights are determined by the attention weights in the
corresponding attention map. To emphasise the important
relationships and reduce noise, we calculate the distance
between vertices as 1 − max( , ). The distance
calculation reflects the inverse of the maximum attention weight
between two tokens, ensuring the relationship is
symmetric and the strong relationships result in smaller distances.
To prevent the formation of self-loops, all diagonals in the
adjacency matrix are set to 0. Figure 1 shows an example of
constructing the attention graph for an attention map.
      </p>
      <sec id="sec-3-1">
        <title>3.4. Persistent Homology</title>
        <p>The constructed attention graphs from the attention
heads contain the structure and relationships we need to
extract topological features. To encode the topological
information provided by the attention graph, we use a filtration
process to generate a persistence diagram. Filtration in
TDA is a systematic process where a topological space is
progressively constructed across varying scales to analyse
the emergence, persistence and disappearance of simplicial
complexes, such as connected components, holes, or voids.</p>
        <p>
          We apply one of the most widely used types of filtration
process to the attention graphs, the Vietoris-Rips filtration.
This process starts with only the vertices of the graph,
considering them as zero-dimensional simplices. Then it adds
edges one by one, depending on their weights (i.e. distances).
Edges with shorter distances below a threshold are added
ifrst, gradually connecting the vertices by increasing the
threshold until a complete graph is formed. As edges are
added, the filtration process captures the graph’s properties
and the relationships between its vertices [
          <xref ref-type="bibr" rid="ref20">20</xref>
          ]. This process
is visualised in Figure 2.
        </p>
        <p>To construct a persistence diagram, we keep track of
the lifetime of persistence features as the threshold is
increased. One can think of 0-dimensional persistent
features as connected components, 1-dimensional features as
holes and 2-dimensional features as voids (2-dimensional
holes) and so on. The birth and death time of a
persistence feature is the threshold value at which the feature
(a) Persistence diagram gen- (b) Topological features
exerated from the filtra- tracted from the
persistion process for atten- tence diagram,
calculattion map in Layer 7, ing persistence entropy,
Head 10. The set of and amplitude with
‘Bot0 (red points) repre- tleneck’ and
‘Wassertsents the birth and death stein’ distances for
hoof ‘connected compo- mology dimensions 0, 1,
nents’ and the set of 1 2 and 3. (In the case of
(teal points) represents NaN values, e.g. due to
the birth and death of no higher dimensional
‘holes’. simplices, we set the
persistence entropy feature
to -1, as per the default
behaviour of Giotto-tda)
appeared and disappeared. For example, when the
threshold is 0 all 0-dimensional features are born (vertices), and
when two vertices  and  are connected at threshold  ,
one 0-dimensional feature will disappear. Similarly, a
1dimensional feature (hole) will appear at the threshold
where 3 vertices connect to each other, and disappear when
a fourth vertex forms a 2-dimensional simplex (void). The
birth and death of all -dimensional simplices are recorded
in a persistence diagram. An example persistence diagram
is shown in Figure 3a.</p>
        <p>From the persistence diagrams, we extract various
topological features to represent the underlying graph’s
structure. In our experiments, we focus on the following
topological features:
1. Persistence Entropy: This feature quantifies the
complexity of the persistence diagram as calculated
by the Shannon entropy of the persistence values
(birth and death), with higher entropy indicating a
more complex topology.
2. Amplitude: We compute amplitude using two
different distance measures: ‘bottleneck’ and
‘Wasserstein’. The amplitude measures the maximum
persistence value within the diagram, providing insights
into the significance of the topological features.</p>
        <p>
          We focus on diferent homology dimensions to capture
topological features of varying complexities. In our experiments,
we consider homology dimensions [
          <xref ref-type="bibr" rid="ref1 ref2 ref3">0, 1, 2, 3</xref>
          ] to account for
diferent aspects of the attention graph’s topology. We use
the Giotto-tda library to generate the persistence diagrams
and extract the topological features, as per Figure 3b. Both
persistence entropy and amplitude features are used in the
experiment through concatenating all features into a single
feature vector.
        </p>
      </sec>
      <sec id="sec-3-2">
        <title>3.5. OOD Scoring Function</title>
        <p>
          Similar to Perez and Reinauer [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ], given ℎ(), a
dimensional representation of an input text , we employ
two distance-based methods as the OOD scoring functions:
1. Mahalanobis distance to the ID class centroids:
the Mahalanobis distance is used to measure the
distance between the feature vector ℎ() and the class
centroids. This distance is based on the covariance
matrix of the class features, which is based on the
assumption that the data in that class follows a
multivariate Gaussian distribution. The OOD score is
calculated as follows:
Maha(; ℎ; Σ;  ) =
∈( −  ) Σ − 1( −  )
where  is the standardised feature vector for the
input ℎ(), Σ is the covariance matrix of the
standardised ID feature vectors and  is the set of class
mean standardised embeddings. Both Σ and   are
extracted from the ID validation set embeddings to
account for the inherent distribution of the ID data.
The covariance matrix Σ captures how the features
vary with respect to one another, and   represents
the centroid or average representation of data
belonging to class .
2. Euclidean distance to k-nearest ID neighbour:
We measure the distance between ℎ() and the
knearest ID neighbour’s feature vector from the
validation set. Given ℎ() and a set of  ID feature
vectors {ℎ(1), ℎ(2), ..., ℎ()}, the Euclidean
distance to the k-nearest ID neighbour is calculated as
follows:
        </p>
        <p>KNN(; ℎ) = || −  ||2
where  and  are the standardised feature vector
for the input ℎ() and its k-nearest ID sample ℎ().</p>
        <p>In our experiments, we set  = 5.</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>4. Results</title>
      <p>We conduct our experiments using Topological Data
Analysis to generate topological feature vectors ℎ1() from
attention maps, which are then compared to standard
sentence embeddings ℎ2() generated from the [] token
of BERT. Table 1 shows the OOD detection performance
of both approaches for three out-of-distribution datasets,
using both pre-trained and fine-tuned BERT models.</p>
      <p>For visualisation purposes, we use UMAP projections
of the in-distribution (validation and test sets) and
out-ofdistribution data points in the corresponding feature space.
Figure 4, Figure 5, and Figure 6 show the data
representations from the TDA and CLS approaches for the far
out-ofdomain dataset (IMDB), near out-of-domain dataset
(CNN/Dailymail) and the same-domain dataset (business
newscategory), respectively.</p>
      <p>The results demonstrate that the TDA-based approach
consistently outperforms the CLS embeddings in detecting
OOD samples in the IMDB dataset from both the pre-trained
and fine-tuned models. OOD detection using TDA can detect
IMDB review samples with 8-9% FPR95, in stark contrast to
the 87-91% FPR95 exhibited by CLS embeddings. As seen in</p>
    </sec>
    <sec id="sec-5">
      <title>5. Discussion</title>
      <p>From our experiments, we showed that the TDA approach
outperforms the CLS approach at detecting far
out-ofdomain OOD samples like those in the IMDB dataset. Yet,
its efectiveness deteriorates with near out-of-domain
(CNN/Dailymail) or same-domain (business news-category)
datasets. To understand why, we looked at the samples that
IMDB
CNN/Dailymail
News-Category (Business)
Pre-trained model
Fine-tuned model
AUROC</p>
      <p>FPR95 ↓</p>
      <p>MAHA
AUROC FPR95 ↓
AUROC</p>
      <p>FPR95 ↓
each approach thrived and struggled with, and we highlight
three observations:</p>
      <p>
        (1) The TDA approach accentuates features
associated with textual flow or grammatical structures
rather than lexical semantics, consistent the findings
of Deng and Duzhin [
        <xref ref-type="bibr" rid="ref21">21</xref>
        ] and Kushnareva et al. [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ]. For
example, TDA was adept at identifying OOD samples that
are structurally unique in the IMDB dataset, as the most
confident OOD samples detected were:
• ‘OK...i have seen just about everything....and some are
considered classics that shouldn’t be ( like all those
Halloween movies that suck crap or even Steven king
junk).......and some are considered just OK that are
really great.....( like carnival of souls )........and then
some are just plain ignored............like ( evil ed ) [. . . ]’
• ‘Time line of the film: * Laugh * Laugh * Laugh *
Smirk * Smirk * Yawn * Look at watch * walk out *
remember funny parts at the beginning * smirk &lt; br /
&gt; &lt;br /&gt; [. . . ]’
      </p>
      <p>In contrast, TDA struggled with detecting
CNN/Dailymail OOD samples as they have similar sentence structures
and length to the ID samples, even if they are semantically
unrelated. Table 2 shows the samples with the least
confident OOD score from the CNN/Dailymail dataset, and their
nearest ID neighbour.</p>
      <p>Footage showed an unusual
’apocalyptic’ dust storm
hitting Belarus. China has
sufered four massive
sandstorms since the start of the
year. Half of dust in
atmosphere today is due to
human activity, said Nasa.</p>
      <p>Video posted by YouTube
user Richard Stewart
showing a Porsche Cayman
flying out of control. Police
cited unidentified driver for
the crash. Car reportedly
wrecked and needed to be
towed from the scene.</p>
      <p>Trump’s Proposed Cuts To
Foreign Food Aid Are
Proving Unpopular. The
president might see zeroed-out
funding for foreign food aid
as "putting America first,"
but members of Congress
clearly disagree.</p>
      <p>Trump Signs Larry
NassarInspired Sexual Assault Bill
Behind Closed Doors. The
president quietly signed the
bill the week after two White
House stafers resigned amid
allegations of domestic
violence.</p>
      <p>(2) CLS embeddings are sensitive to the semantic
and contextual meaning of the samples, regardless
of sentence structure. This explains why this approach
struggled with OOD detection from IMDB reviews, as it
often classified IMDB movie reviews as in-distribution due to
their semantic similarities with the entertainment news
articles from the ID dataset, especially those related to movies.
A closer look at the IMDB samples with smallest OOD score
from the CLS embeddings in Table 3 exemplifies this insight,
identifying ID samples of similar topic as nearest neighbours
even though they are clearly from diferent domains.</p>
      <p>
        (3) Fine-tuning has improved performance of CLS
embeddings for near or same-domain shifts, but shows
no significant benefit for TDA . Fine-tuning induces a
model to divide a single domain cluster into class clusters,
as highlighted by Uppaal et al. [
        <xref ref-type="bibr" rid="ref13">13</xref>
        ]. For CNN/Dailymail
and Business news OOD datasets, this is beneficial for the
CLS approach as it learns to better distinguish topics.
However, fine-tuning made the CLS embeddings of IMDB movie
reviews appear even more similar to entertainment news,
deteriorating OOD performance.
      </p>
      <p>For the TDA approach, fine-tuning did not present any
considerable benefits. This can be partly attributed to
observation (1) that TDA primarily captures structural diferences,
and fine-tuning, which is driven by semantics, does not
significantly alter the topological representation.</p>
      <p>IMDB review sample
’[...] I would spend good,
hard-earned cash money to
see it again on DVD. And
as long as we’re requesting
Smart Series That Never Got
a Chance...How about DVD
releases of Maximum Bob
(another well written, odd
duck show with a delightful
cast of characters.) [...]’
’[...] I am generally not a fan
of Zeta-Jones but even I must
admit that Kate is
STUNNING in this movie. [...]’</p>
      <p>DVDs: Great Blimp,
Badlands, Buster Keaton &amp; More.</p>
      <p>Let’s catch up with some
reissues of classic – and not so
classic – movies, with a few
documentaries tossed in at
the end for good measure.</p>
      <p>How ‘Erin Brockovich’
Became One Of The Most
Rewatchable Movies Ever
Made. Julia Roberts gives the
best performance of her
career, aided by a sassy
Susannah Grant script full of
oneliners.</p>
    </sec>
    <sec id="sec-6">
      <title>6. Conclusion</title>
      <p>In this paper, we explore the capabilities of Topological
Data Analysis for identifying Out-of-Distribution samples
by leveraging the attention maps derived from BERT, a
transformer-based Large Language Model. Our results
demonstrate the potential of TDA as an efective tool to
capture the structural information of textual data.</p>
      <p>Nevertheless, our experiments also highlighted the
intrinsic limitations of TDA-based methods. Predominantly, our
TDA method captured the inter-word relations derived from
the attention maps, but failed to account for the actual
lexical meaning of the text. This distinction suggests that while
TDA ofers valuable insights into textual structure, a lexical
and more holistic understanding of textual data is needed
for OOD detection, especially with near or same-domain
shifts.</p>
      <p>For future work, it might be worth combining the
topological features that capture the structural information of
textual data, with those that encode the semantics of text
in an ensemble model that might boost our ability to detect
OOD samples. In addition, there is an opportunity to
investigate the efectiveness of TDA in other NLP tasks where
the textual structure might be important.</p>
    </sec>
    <sec id="sec-7">
      <title>Acknowledgments</title>
      <p>The research was supported by a National Intelligence
Postdoctoral Grant (NIPG-2021-006).</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>S.</given-names>
            <surname>Wong</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Barnett</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Rivera-Villicana</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Simmons</surname>
          </string-name>
          ,
          <string-name>
            <given-names>H.</given-names>
            <surname>Abdelkader</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.-G.</given-names>
            <surname>Schneider</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Vasa</surname>
          </string-name>
          ,
          <article-title>MLGuard: Defend your machine learning model!</article-title>
          ,
          <source>in: Proceedings of the 1st International Workshop on Dependability and Trustworthiness of Safety-Critical Systems with Machine Learned Components, SE4SafeML</source>
          <year>2023</year>
          ,
          <year>2023</year>
          , p.
          <fpage>10</fpage>
          -
          <lpage>13</lpage>
          . doi:
          <volume>10</volume>
          .1145/3617574.3617859.
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>J.</given-names>
            <surname>Devlin</surname>
          </string-name>
          , M.-
          <string-name>
            <given-names>W.</given-names>
            <surname>Chang</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Lee</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Toutanova</surname>
          </string-name>
          , BERT:
          <article-title>Pre-training of deep bidirectional transformers for language understanding</article-title>
          ,
          <source>in: Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies</source>
          , Volume
          <volume>1</volume>
          (Long and Short Papers),
          <year>2019</year>
          , pp.
          <fpage>4171</fpage>
          -
          <lpage>4186</lpage>
          . URL: https://aclanthology. org/N19-1423. doi:
          <volume>10</volume>
          .18653/v1/
          <fpage>N19</fpage>
          -1423.
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>A.</given-names>
            <surname>Azaria</surname>
          </string-name>
          , T. Mitchell,
          <article-title>The internal state of an llm knows when its lying (</article-title>
          <year>2023</year>
          ). URL: https://arxiv.org/ abs/2304.13734.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>L.</given-names>
            <surname>Kushnareva</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Cherniavskii</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V.</given-names>
            <surname>Mikhailov</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            <surname>Artemova</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Barannikov</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Bernstein</surname>
          </string-name>
          ,
          <string-name>
            <given-names>I.</given-names>
            <surname>Piontkovskaya</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Piontkovski</surname>
          </string-name>
          , E. Burnaev,
          <article-title>Artificial text detection via examining the topology of attention maps</article-title>
          ,
          <source>in: Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing</source>
          , Association for Computational Linguistics, Online and
          <string-name>
            <given-names>Punta</given-names>
            <surname>Cana</surname>
          </string-name>
          , Dominican Republic,
          <year>2021</year>
          , pp.
          <fpage>635</fpage>
          -
          <lpage>649</lpage>
          . URL: https://aclanthology.org/
          <year>2021</year>
          .emnlp-main.
          <volume>50</volume>
          . doi:
          <volume>10</volume>
          . 18653/v1/
          <year>2021</year>
          .emnlp-main.
          <volume>50</volume>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <surname>I. Perez</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R.</given-names>
            <surname>Reinauer</surname>
          </string-name>
          ,
          <article-title>The topological bert: Transforming attention into topology for natural language processing</article-title>
          ,
          <year>2022</year>
          . URL: https://arxiv.org/abs/2206.15195. arXiv:
          <volume>2206</volume>
          .
          <fpage>15195</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>A.</given-names>
            <surname>Podolskiy</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Lipin</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Bout</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            <surname>Artemova</surname>
          </string-name>
          ,
          <string-name>
            <surname>I. Piontkovskaya</surname>
          </string-name>
          ,
          <article-title>Revisiting mahalanobis distance for transformer-based out-of-domain detection</article-title>
          ,
          <source>in: Proceedings of the AAAI Conference on Artificial Intelligence</source>
          , volume
          <volume>35</volume>
          ,
          <year>2021</year>
          , pp.
          <fpage>13675</fpage>
          -
          <lpage>13682</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>P.</given-names>
            <surname>Colombo</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E. D.</given-names>
            <surname>Gomes</surname>
          </string-name>
          , G. Staerman,
          <string-name>
            <given-names>N.</given-names>
            <surname>Noiry</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P.</given-names>
            <surname>Piantanida</surname>
          </string-name>
          ,
          <article-title>Beyond mahalanobis-based scores for textual ood detection</article-title>
          ,
          <source>arXiv preprint arXiv:2211.13527</source>
          (
          <year>2022</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>X.</given-names>
            <surname>Li</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Li</surname>
          </string-name>
          ,
          <string-name>
            <given-names>X.</given-names>
            <surname>Sun</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            <surname>Fan</surname>
          </string-name>
          ,
          <string-name>
            <given-names>T.</given-names>
            <surname>Zhang</surname>
          </string-name>
          ,
          <string-name>
            <given-names>F.</given-names>
            <surname>Wu</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Y.</given-names>
            <surname>Meng</surname>
          </string-name>
          , J. Zhang,  folden:
          <article-title>-fold ensemble for out-ofdistribution detection</article-title>
          ,
          <source>arXiv preprint arXiv:2108.12731</source>
          (
          <year>2021</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>K.</given-names>
            <surname>Lee</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Lee</surname>
          </string-name>
          ,
          <string-name>
            <given-names>H.</given-names>
            <surname>Lee</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Shin</surname>
          </string-name>
          ,
          <article-title>A simple unified framework for detecting out-of-distribution samples and adversarial attacks</article-title>
          ,
          <source>Advances in neural information processing systems</source>
          <volume>31</volume>
          (
          <year>2018</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          [10]
          <string-name>
            <given-names>A.</given-names>
            <surname>Hatcher</surname>
          </string-name>
          , Algebraic Topology, Cambridge University Press,
          <year>2002</year>
          . URL: https://pi.math.cornell.edu/~hatcher/ AT/ATpage.html.
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          [11]
          <string-name>
            <given-names>P.</given-names>
            <surname>Frosini</surname>
          </string-name>
          ,
          <article-title>Measuring shapes by size functions, in: Intelligent Robots and Computer Vision X: Algorithms</article-title>
          and Techniques, volume
          <volume>1607</volume>
          ,
          <string-name>
            <surname>SPIE</surname>
          </string-name>
          ,
          <year>1992</year>
          , pp.
          <fpage>122</fpage>
          -
          <lpage>133</lpage>
          . URL: https://doi.org/10.1117/12.57059. doi:
          <volume>10</volume>
          .1117/ 12.57059.
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          [12]
          <string-name>
            <given-names>V.</given-names>
            <surname>Robins</surname>
          </string-name>
          ,
          <article-title>Towards computing homology from finite approximations, in: Topology proceedings</article-title>
          , volume
          <volume>24</volume>
          ,
          <year>1999</year>
          , pp.
          <fpage>503</fpage>
          -
          <lpage>532</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          [13]
          <string-name>
            <given-names>R.</given-names>
            <surname>Uppaal</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Hu</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Y.</given-names>
            <surname>Li</surname>
          </string-name>
          ,
          <article-title>Is fine-tuning needed? pretrained language models are near perfect for out-ofdomain detection</article-title>
          ,
          <source>in: Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume</source>
          <volume>1</volume>
          :
          <string-name>
            <surname>Long</surname>
            <given-names>Papers)</given-names>
          </string-name>
          ,
          <year>2023</year>
          , pp.
          <fpage>12813</fpage>
          -
          <lpage>12832</lpage>
          . URL: https://aclanthology.org/
          <year>2023</year>
          .
          <article-title>acl-long</article-title>
          .
          <volume>717</volume>
          . doi:
          <volume>10</volume>
          .18653/v1/
          <year>2023</year>
          .
          <article-title>acl-long</article-title>
          .
          <volume>717</volume>
          .
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          [14]
          <string-name>
            <given-names>Y.</given-names>
            <surname>Sun</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Y.</given-names>
            <surname>Ming</surname>
          </string-name>
          ,
          <string-name>
            <given-names>X.</given-names>
            <surname>Zhu</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Y.</given-names>
            <surname>Li</surname>
          </string-name>
          ,
          <article-title>Out-of-distribution detection with deep nearest neighbors</article-title>
          ,
          <source>in: International Conference on Machine Learning, PMLR</source>
          ,
          <year>2022</year>
          , pp.
          <fpage>20827</fpage>
          -
          <lpage>20840</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          [15]
          <string-name>
            <given-names>J.</given-names>
            <surname>Yang</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Zhou</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Y.</given-names>
            <surname>Li</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Z.</given-names>
            <surname>Liu</surname>
          </string-name>
          ,
          <article-title>Generalized outof-distribution detection: A survey</article-title>
          ,
          <source>arXiv preprint arXiv:2110.11334</source>
          (
          <year>2021</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>
          [16]
          <string-name>
            <given-names>R.</given-names>
            <surname>Misra</surname>
          </string-name>
          , News category dataset (
          <year>2022</year>
          ). URL: https: //arxiv.org/abs/2209.11429.
        </mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          [17]
          <string-name>
            <given-names>U.</given-names>
            <surname>Arora</surname>
          </string-name>
          ,
          <string-name>
            <given-names>W.</given-names>
            <surname>Huang</surname>
          </string-name>
          ,
          <string-name>
            <given-names>H.</given-names>
            <surname>He</surname>
          </string-name>
          ,
          <article-title>Types of out-ofdistribution texts and how to detect them</article-title>
          ,
          <source>in: Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing</source>
          ,
          <year>2021</year>
          , pp.
          <fpage>10687</fpage>
          -
          <lpage>10701</lpage>
          . URL: https://aclanthology.org/
          <year>2021</year>
          .emnlp-main.
          <volume>835</volume>
          . doi:
          <volume>10</volume>
          .18653/v1/
          <year>2021</year>
          .emnlp-main.
          <volume>835</volume>
          .
        </mixed-citation>
      </ref>
      <ref id="ref18">
        <mixed-citation>
          [18]
          <string-name>
            <given-names>A.</given-names>
            <surname>See</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P. J.</given-names>
            <surname>Liu</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C. D.</given-names>
            <surname>Manning</surname>
          </string-name>
          ,
          <article-title>Get to the point: Summarization with pointer-generator networks</article-title>
          ,
          <source>in: Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume</source>
          <volume>1</volume>
          :
          <string-name>
            <surname>Long</surname>
            <given-names>Papers)</given-names>
          </string-name>
          ,
          <year>2017</year>
          , pp.
          <fpage>1073</fpage>
          -
          <lpage>1083</lpage>
          . URL: https://www. aclweb.org/anthology/P17-1099. doi:
          <volume>10</volume>
          .18653/v1/
          <fpage>P17</fpage>
          -1099.
        </mixed-citation>
      </ref>
      <ref id="ref19">
        <mixed-citation>
          [19]
          <string-name>
            <given-names>A. L.</given-names>
            <surname>Maas</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R. E.</given-names>
            <surname>Daly</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P. T.</given-names>
            <surname>Pham</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Huang</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A. Y.</given-names>
            <surname>Ng</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            <surname>Potts</surname>
          </string-name>
          ,
          <article-title>Learning word vectors for sentiment analysis</article-title>
          ,
          <source>in: Proceedings of the 49th Annual Meeting of the Association for Computational Linguistics: Human Language Technologies</source>
          ,
          <year>2011</year>
          , pp.
          <fpage>142</fpage>
          -
          <lpage>150</lpage>
          . URL: http: //www.aclweb.org/anthology/P11-1015.
        </mixed-citation>
      </ref>
      <ref id="ref20">
        <mixed-citation>
          [20]
          <string-name>
            <given-names>U.</given-names>
            <surname>Bauer</surname>
          </string-name>
          ,
          <article-title>Ripser: eficient computation of vietoris-rips persistence barcodes</article-title>
          ,
          <source>Journal of Applied and Computational Topology</source>
          <volume>5</volume>
          (
          <year>2021</year>
          )
          <fpage>391</fpage>
          -
          <lpage>423</lpage>
          . URL: https: //doi.org/10.1007/s41468-021-00071-5. doi:
          <volume>10</volume>
          .1007/ s41468-021-00071-5.
        </mixed-citation>
      </ref>
      <ref id="ref21">
        <mixed-citation>
          [21]
          <string-name>
            <given-names>R.</given-names>
            <surname>Deng</surname>
          </string-name>
          ,
          <string-name>
            <given-names>F.</given-names>
            <surname>Duzhin</surname>
          </string-name>
          ,
          <article-title>Topological data analysis helps to improve accuracy of deep learning models for fake news detection trained on very small training sets</article-title>
          ,
          <source>Big Data Cogn. Comput</source>
          .
          <volume>6</volume>
          (
          <year>2022</year>
          )
          <article-title>74</article-title>
          . URL: https://doi.org/ 10.3390/bdcc6030074. doi:
          <volume>10</volume>
          .3390/bdcc6030074.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>