Graph Neural Networks and Time Series as Directed Graphs for Quality
  Recognition
Graph Neural Networks (GNNs) are becoming central in the study of time
series, coupled with existing algorithms as Temporal Convolutional Networks and
Recurrent Neural Networks. In this paper, we see time series themselves as
directed graphs, so that their topology encodes time dependencies and we start
to explore the effectiveness of GNNs architectures on them. We develop two
distinct Geometric Deep Learning models, a supervised classifier and an
autoencoder-like model for signal reconstruction. We apply these models on a
quality recognition problem.
Angelica Simonetti, Ferdinando Zanchetta
# Graph Neural Networks and Time Series as Directed Graphs for Quality Recognition

###### Abstract

Graph Neural Networks (GNNs) are becoming central in the study of time series, coupled with existing algorithms as Temporal Convolutional Networks and Recurrent Neural Networks. In this paper, we see time series themselves as directed graphs, so that their topology encodes time dependencies and we start to explore the effectiveness of GNNs architectures on them. We develop two distinct Geometric Deep Learning models, a supervised classifier and an autoencoder-like model for signal reconstruction. We apply these models on a quality recognition problem.

## 1 Temporal convolutional Networks

Convolutional neural networks (CNNs, see [14], [15], [16], [12], [13]) are deep learning algorithms employing so called _convolutional layers_: these are layers that are meant to be applied on grid-like data, e.g. images. For data organized in sequences, ld CNNs were developed ([10], [11]) and, more recently, TCNs have become popular in the study of time series (see [l] and the references therein). Throughout the paper, \(\mathrm{TS}(r,m)\) will denote the set of multivariate time series with \(m\) channels and length \(r\) in the temporal dimension. Given \(\mathbf{x}\in\mathrm{TS}(r,m)\) we will denote as \(\mathbf{x}(i)_{j}\) (or simply as \(\mathbf{x}_{ij}\) when no confusion is possible), for \(i=1,...,r\) and \(j=1,...,m\), the \(j\) the coordinate of the vector \(\mathbf{x}(i)\in\mathbb{R}^{m}\). For a given natural number \(n\), we shall denote as \([n]\) the ordered set \((1,...,n)\). Now, recall that given a filter \(K\in\mathbb{R}^{f}\), we can define a one-channel, one-dimensional (ID) convolution as an operator

\[\mathrm{conv}\mathrm{1D}:\mathrm{TS}(r,1)\rightarrow\mathrm{TS}(l,1)\] \[\mathrm{conv}\mathrm{1D}(\mathbf{x})_{j}=\sum_{i=1}^{f}K_{i} \mathbf{x}_{\alpha(j,i)}+b_{j}\]

where \(\alpha(j,-):[f]\rightarrow\mathbb{Z}\) are injective index functions, \(\mathbf{x}_{i}:=0\) if \(i\notin[r]\) and \(b\in\mathbb{R}^{l}\) is a bias vector. The numbers \(K_{i}\) are called the _parameters_ or _weights_ of the convolution. The most commonly used index functions are of the form \(\alpha(j,i)=(n+d\cdot i)+j\) for some integers \(n,d\). As a consequence, from now on we shall assume that the one dimensional convolutions we consider have this form. If, \(\alpha(j,i)\leq j\) for all \(i,j\) then the convolution is said to be _causal_ as it will look only 'backward'. These are the building blocks of TCNs, that are CNNs where only causal convolutions appear. If \(|d|>1\), the convolution is called _dilated_. One could define multi-channel (i.e. handling multivariate time series), ld convolutions in two steps. First, we define convolutions taking a multivariate time series to an univariate time series as operators \(\mathrm{conv}:\mathrm{TS}(r,n)\rightarrow\mathrm{TS}(l,1)\) as \(\mathrm{conv}(\mathbf{x})_{i}=\sum_{j=1}^{n}\mathrm{conv}\mathrm{1D}_{j}( \mathbf{x}(-)_{(j)})\) where \(\mathrm{conv}\mathrm{1D}_{j}\) are one-channel, one-dimensional convolutions. Then we can define ld convolutions transforming multivariate time series into multivariate time series as operators \(\mathrm{conv}:\mathrm{TS}(r,n)\rightarrow\mathrm{TS}(l,m)\) that are multi-channel ld convolutions when co-restricted at each
non temporal dimension of the output. The usefulness of TCNs in the context of time series arises from the fact that causal convolutions by design are able to exploit temporal dependencies, while not suffering from some of the algorithmic problems of RNNs such as LSTMs: for example they appear to be faster to train and more scalable (see [1] for a discussion).

## 2 Time series as Directed Graphs

### Generalities.

**Definition 2.1**.: A _directed graph_ (_digraph_) \(G\) is the datum \(G=(V_{G},E_{G},h_{G},t_{G})\) of two sets \(V_{G}\) (the _set of vertices_), \(E_{G}\) (the _set of edges_) and two functions \(h_{G},t_{G}:E_{G}\to V_{G}\) associating to each edge \(e\) its _head_\(h_{G}(e)\) and its _tail_\(t_{G}(e)\) respectively. A morphism \(\varphi:G\to H\) between two directed digraphs \(G\) and \(H\) is the datum of two functions \(\varphi_{V}:V_{G}\to V_{H}\), \(\varphi_{E}:E_{G}\to E_{H}\) such that \(h_{H}\circ\varphi_{E}=\varphi_{V}\circ h_{G}\) and \(t_{H}\circ\varphi_{E}=\varphi_{V}\circ t_{G}\)

From now on, for simplicity we will assume that our digraphs have at most one edge connecting two different nodes (for each direction) and at most one self loop for each node. In this case, given a digraph \(G=(V_{G},E_{G},h_{G},t_{G})\) and an ordering of the vertices \((v_{i})_{i\in[|V_{G}|]}\), we can define the _adjacency matrix of \(G\)_ as the matrix defined by \(A_{ij}=1\) if there is there exists an edge having as tail \(v_{i}\) and head \(v_{j}\) or \(A_{ij}=0\) otherwise. If the adjacency matrix of a graph is symmetric, we say that our graph is _undirected_. We can assign _weights_ to the edges of a graph by letting the entries of the adjacency matrix to be arbitrary real numbers. When speaking about weighted digraphs, we shall always assume that there is an edge having as tail \(v_{i}\) and head \(v_{j}\) if and only if \(A_{ij}\neq 0\). We speak in this case of _weighted digraphs_.

**Definition 2.2**.: A _digraph with features of dimension \(n\)_ is the datum \((G,F_{G}=(h_{v})_{v\in V_{G}})\) of a (weighted or unweighted) digraph \(G\) and vectors \(h_{v}\in\mathbb{R}^{n}\) of _node features_ for each vertex \(v\in V_{G}\). For a given digraph \(G\), we shall denote as \(\operatorname{Feat}(G,n)\) the set of all digraphs with features of dimension \(n\) having \(G\) as underlying digraph.

Real-world graph-structured datasets usually come in form of one or more digraphs with features. Given a digraph with features \((G,F_{G}=(h_{v})_{v\in V_{G}})\) and a digraph morphism \(\varphi:H\to G\), we can pullback the features of \(G\) to obtain a graph with features \((H,\varphi^{*}F_{G}=(h_{\varphi_{V}(v)})_{v\in V_{H}})\). This defines a function \(\varphi^{*}:\operatorname{Feat}(G,n)\to\operatorname{Feat}(H,n)\). Graph Neural Networks (GNNs, [23]) are models that are used on graph-structured data using as building blocks the so called _graph convolutions_ ([4], [6]): given a graph, they update each node feature vector combining the information contained in the feature vectors of adjacent nodes. In general, a graph convolution is a function \(\operatorname{gconv}:\operatorname{Feat}(G,n)\to\operatorname{Feat}(G,m)\) that is permutation invariant in a sense that we shall make precise below. graph convolutions update node features of a digraph using a _message passing mechanism_ that can be written in the following general form

\[h^{\prime}_{v_{i}}=\sigma(\psi(h_{v_{i}},\oplus_{v_{j}\in N^{\alpha}(v_{i})} \varphi(h_{v_{i}},h_{v_{j}},A_{ij},A_{ji})) \tag{2.3}\]

where \(\sigma\) is an activation function, \(\alpha\in\{h,t,u\}\), \(N^{h}(v_{i})=\{v_{j}\in|V_{G}|\mid A_{ji}\neq 0\}\), \(N^{t}(v_{i})=\{v_{j}\in|V_{G}|\,|A_{ij}\neq 0\}\),\(N^{u}(v_{i})=N^{h}(v_{i})\cup N^{t}(v_{i})\), \(\oplus\) denotes a permutation invariant function and \(\psi\), \(\varphi\) denote differentiable functions (weaker regularity assumptions can be made). Many popular message passing mechanisms are a particular case of the following one:

\[h^{\prime}_{v_{i}}=\sigma(\sum_{v_{j}\in N^{\alpha}(v_{i})}c_{f^{\alpha}(i,j) }A_{f^{\alpha}(i,j)}Wh_{v_{j}}+l_{i}A_{ii}Bh_{v_{i}}) \tag{2.4}\]

here \(\sigma\) is an activation function, \(f^{\alpha}(i,j)=(i,j)\) if \(v_{j}\in N^{t}(v_{i})\) and \((j,i)\) if \(v_{j}\in N^{h}(v_{i})\), \(c_{ij}\), \(l_{i}\) are optional normalization coefficients, \(W,B\) are matrices of weights. For digraphs, the choice of \(\alpha\) should be thought of as whether a node embedding should be updated by looking at the information of the nodes that are sending to it a signal, by looking at the information of the nodes that are receiving from it a signal or both. These are three different semantics, all justifiable depending on the problem at hand. Two important graph convolutions arise as particular cases of the previous formula: Kipf and Welling's graph convolution for undirected graphs (see [9]) and GraphSage convolution ([7]) as it appears in the popular package PyTorch Geometric ([17], [5]). Notice that message passing mechanisms as in (2.3) are _permutation invariant_ in the sense that they do not depend on the ordering given to the set of vertices and only depend on the topology of the underlying graph structure. We remark that in [9] and [7] the above convolutions are described only for undirected graphs, but the formulas also make sense for digraphs. In fact the standard
implementations used by practitioners are already capable to handle digraphs and are being used also in that context. For example, some papers introducing attention mechanisms (e.g. GAT, see [22]) explicitly introduce this possibility. However, in the digraph case the underlying mathematical theory is somewhat less mature (see [20] and the reference therein, for the roots of the mathematics behind the directed graph Laplacian the reader is referred to [2]).

### Graph Neural Networks for time series as directed graphs.

There are many ways to turn time series into digraphs with features. To start with, we introduce two basic examples.

**Example 1:** A multivariate time series \(\mathbf{x}\in\mathrm{TS}(n,m)\cong\mathbb{R}^{n\times m}\) can be seen as an unweighted digraph with features as follows. To start with, we consider a set of \(n\times m\) nodes \(v_{ij}\), with \(i=1,...,n\) and \(j=1,...,m\). Then, we create edges \(v_{ij}\to v_{lk}\) only if \(l\geq i\) and the edge is not a self loop (i.e. edges receive information only from the present and the past). We assign the scalar \(\mathbf{x}(i)_{j}\) as feature for each node \(v_{ij}\). This construction results in an unweighted digraph with features \((G_{\mathbf{x}},F_{\mathbf{x}}=(\mathbf{x}_{ij}\in\mathbb{R}))\). One can modify the topology of the graph just constructed. For example, one could create edges \(v_{ij}\to v_{lk}\) only if the edge is not a self-loop, \(l\geq i\) and \(l-i=0,1\) or if the edge is not a self-loop, \(l\geq i\), and \(l-i\) is both divisible by a given positive integer \(d\) and is smaller than \(k\cdot d\) for a given positive integer \(k\). This construction results in the directed graph structure pioneered in [24]): see figure 1.

**Example 2**: A multivariate time series \(\mathbf{x}\in\mathrm{TS}(n,m)\cong\mathbb{R}^{n\times m}\) can be seen as a one dimensional time series with \(m\) channels. In this case, the time series can be turned into a digraph with features \((G_{\mathbf{x}},F_{\mathbf{x}}=(\mathbf{x}(i)\in\mathbb{R}^{m}))\) by considering a directed graph of \(n\) nodes \(v_{i}\), \(i=1,...,n\) and edges \(v_{i}\to v_{l}\) are added if the edge is not a self-loop and \(l\geq i\), \(l-i=1\) or and \(l-i\) is both divisible by a given positive integer \(d\) and is smaller than \(k\cdot d\) for a given positive integer \(k\). We assign the vector \(\mathbf{x}(i)\in\mathbb{R}^{m}\) as a vector of features for each node \(v_{i}\). This completes the construction of the desired directed digraph with features.

These examples are of course just a starting point and one can take advantage of further domain knowledge to model the topology of the graph in a more specific way. For instance, one could use auto-correlation functions, usually employed to determine the order of ARMA models (see [21]), to choose the right value for parameters like \(k\) or \(d\). As proved in Lemma 2.5 under certain hypotheses, ordinary convolutions on time series can be seen as transformations between graphs, that is graph convolutions, however the latter evidently carry a very different meaning than ordinary TCNs and can be more flexible. Thus thinking of a time series as a digraph opens to a whole new set of possibilities to be explored. For example, they can be effective as temporal pooling layers when combined with other algorithms or they can leverage the message passing mechanisms that is thought to be more effective to solve the task at hand.

**Lemma 2.5**.: _Consider a convolution \(\mathrm{conv1d}:\mathbb{R}^{d}\cong\mathrm{TS}(d,1)\to\mathrm{TS}(d-r,1) \cong\mathbb{R}^{d-r}\), \((\mathrm{conv1d}(\boldsymbol{x}))_{i}=\sum_{j=0}^{r-1}K_{j}\boldsymbol{x}_{i+j}\). Then there exists a weighed digraph \(G\), a graph convolution \(\mathrm{gconv}:\mathrm{Feat}(G,1)\to\mathrm{Feat}(G,1)\) and a subgraph \(\iota:H\subseteq G\) such that \(\mathrm{Feat}(G,1)\cong\mathrm{TS}(d,1)\), \(\mathrm{Feat}(H,1)\cong\mathrm{TS}(d-r,1)\) and, under these bijections, \(\mathrm{conv1d}\) arise as the map \(\iota^{*}\circ\mathrm{gconv}:\mathrm{Feat}(G,1)\to\mathrm{Feat}(H,1)\)._

Proof.: Define \(G\) to be the digraph having \(d\) vertices \(v_{1},...,v_{d}\) and whose weighted adjacency matrix is given by \(A_{ij}=K_{j-i+1}\) if \(1\leq j-i+1\leq r\) and zero otherwise. Let \(\iota:H\subseteq G\) be its weighed subgraph consisting of the vertices \(v_{r},...,v_{d}\). We consider the graph convolution \(\mathrm{gconv}:\mathrm{Feat}(G,1)\to\mathrm{Feat}(G,1)\) arising from the message passing mechanism given by

Figure 1: One possible structure of a time-digraph, as described in Example 1. Here only adjacent connections and all the connections for node \(v\) are shown, and \(d=4\)
formula (2.4) with \(\alpha=h\), \(W=1\), \(l_{i}=0\), \(\sigma=1\) and \(c_{ij}=1\). We define the bijection \(\mathrm{TS}(d,1)\cong\mathrm{Feat}(G,1)\) as follows: for each \(\mathbf{x}\in\mathrm{TS}(d,1)\), \(\mathbf{x}(i)\) becomes the feature of the node \(v_{i}\) in \(G\) (and analogously for \(\mathrm{TS}(d-r,1)\cong\mathrm{Feat}(H,1)\)).

The previous lemma can be extended mutatis mutandis also to the case of multivariate time series and contains as a particular case the one of dilated convolutions. The process of learning the weights of a dilated convolution can be thought as the process of learning the weights of the adjacency matrix of a graph.

**Remark 2.6**.: Simple Laplacian-based graph convolutions on undirected graphs can be seen as 1-step Euler discretizations of a Heat equation (see fore example [6, 4]). In general, a GNN consisting of a concatenation graph convolutions can be thought as a diffusive process of the information contained in the nodes along the edges: in our context, directed graph convolutions applied to time series digraphs "diffuse" the information through time by updating at each step node features using the information coming from a 'temporal neighbourhood'.

## 3 Our Models

We propose two different types of models, both taking advantage of the time-digraph structure described in the previous section: a supervised classifier/regressor and an unsupervised method made of two separate steps, an autoencoder to reconstruct the time series, followed by a clustering algorithm applied to the reconstruction errors. The core of the algorithm is the same for the two approaches, so we will first focus on this core building block and then proceed to discuss the two models separately.

### Main Building Block

The main building block for all the models presented in this paper is a collection of layers that combines TCNs with GNNs. Inspired by what has already been done in the literature, we propose this main building block in two versions: an encoder and a decoder (they are sketched in Figure 2(a)). They can be used on their own or put together to construct an autoencoder model.

In the encoder version, the input is first given to a block of either \(n\) TCN layers or \(n\) GNN layers (we tested primarily Sage convolution-based layers as they appeared more effective after some preliminary tries) with skip connections, following the approach proposed in [19]. The effectiveness of skip-connections in the model developed in _op.cit_ is due to the fact that stacking together the outputs of dilated convolutions with different dilations allows to consider short and long term time dependencies at the same time. Skip connections have been used also in the context of GNN: for example, Sage convolutions ([7]) introduce them already in the message passing mechanism. This motivates the introduction of skip connections in GNNs handling digraphs with features coming from time series: in this context they do not only allow to bundle together information coming from "temporal neighbourhoods" of different radii as in the architecture developed in [19] but they also help to reduce oversmoothing that traditionally curses GNNs architectures (see [3] for a discussion and the references therein). The skip-connections block is described in figure (2(b)) and works as follows. The input goes through the first TCN/GNN layer, followed by a 1-dimensional convolution and an activation function. We call the dimension of the output of this convolution skip dimension. Then the output is both stored and

Figure 2: The structure of the main building block, both as an encoder and a decoder
passed to the next TCN/GNN layer as input and so on. In the architectures we have tested, the TCN/GNN layers are simply dilated 1d convolutions or a single graph convolutions (followed by an activation function), but more involved designs are possible. At the end all the stored tensors are stacked together and passed to a series of \(m\) graph convolutions, each one followed by an activation function. We tested the convolutions: GCN (cfr. [9]), Sage (cfr. [7]), GAT (cfr. [22]).

The graph convolutions are defined to encode in the embedding of a given node, at each pass, the information provided by all the nodes in its neighbourhood. Now, looking at how a time-digraph is defined, one sees that in our set up this translates to building the embedding of a given node, that is a data point of the time series, using the information given by the data points that are close in time (short-term memory behaviour) or at a certain distance \(d\) away in time (long-term memory behaviour), where \(d\) is set in the construction of the graph.

Finally the intermediate output produced by the graph convolutions is given to an optional 1d convolution with kernel size 1 to adjust the number of channels and then to either an average pooling layer or a max pooling layer that shrinks the temporal dimension of the graph. In other words, if we think about the time-graph as a time window of length \(T\), the pooling layers outputs a time window, and therefore a time-graph, of length \(T/s\), thus realizing the characteristic _bottleneck_ of an autoencoder as described for instance in [18, 19, 8]. We will refer to \(s\) as the shrinking factor.

The decoder version changes the order of the blocks we just described, in a symmetric way. It starts with an upsample of the input time series which is then passed to the the graph convolutions followed by the skip-connections block. It terminates with a 1d final convolution with kernel size 1 that reduces the number of channels, or hidden dimensions, thus giving as output a time series of the same dimensions as the initial input. In the case where the skip-connections block is built with GNN layers, this final convolution can be replaced by a final TCN layer.

### Regression/Classification

The classifier/regressor model uses the main building block described above as an encoder. The input graph is given to the encoder which predicts a suitable embedding for each of its nodes. At this point the embeddings of the single nodes are combined together into a vector that gives a representation of the whole graph. Recalling that each graph represent a time window, one can think of this first part as a way to extract a small number of features from each time window. These features are then fed to a multi-layer perceptron that outputs a vector of probabilities in the case we use it as a classifier or a value in the case it is used as a regressor instead. As for the way the node embeddings are combined together we explored a few possibilities in the context of classification, the two main options being a flattening layer and a mean pooling layer. For easiness of notation, from now on we will refer to these models as TCNGraph-Classifier/Regressor if the skip-connections block uses TCN layers, TGraphClassifier/Regressor if it is built with graph convolutions.

Figure 4: The structure of the classifier (or regressor)

Figure 3: The structure of the block with skip connections
### Autoencoders for unsupervised anomaly detection

The second architecture we propose is a an autoencoder model, thus it employs two main building blocks, first used as an encoder and then as a decoder and the output is the reconstruction of the given time series represented by the time graph. In our experiments we use the signal reconstruction obtained with this architecture for anomaly detection purposes. Let us briefly describe our method. The main idea is that the autoencoder model provides a good reconstruction of the input time series when the signal is normal and worst reconstructions on time windows where an anomaly appears (again we refer to [19] among others for a similar approach to anomaly detection), as it is constructed to remove noise from a signal. Thus, once we have the reconstructed times series, we compute both the Root Mean Square Error and the Mahalanobis score (see [19] for more details in a similar context), for each given time window, with respect to the original time series. In the case one has to deal with more than one time series bundled together in the time-digraph, there are simple methods to get a single score for each time window. Now we can treat these two measures of the reconstruction error as features of the time windows and use an unsupervised clustering algorithm (we tested both Kmeans and DBscan), to assign a binary label to each window, based on the cluster they fall into (see Section 4.2 for more details).

This approach gives a completely unsupervised method to handle anomaly detection of one or more time series. Again, from now on we will refer to these models as: TCNGraphAE if the skip-connections block uses TCN layers, TGraphAE if it is built with graph convolutions. and TGraphMixedAE if the encoder uses graph convolutions and the Decoder uses TCN layers. If the skip connections blocks consist only of dilated convolutions and we do not have a final graph convolution to filter the signal, we obtain an TCN autoencoder/Classifier with a structure similar to the one described in [19]. We call this latter models TCNAE and TCNClassifier. We regard these models as state of the art models in this context and we use them as a benchmark.

## 4 Experiments

For our experiments we used a database made of ECG signals. These signals have been recorded with a Butterflive medical device at a sampling rate of 512 Hz. A lowband Butterworth filter at 48 Hz was applied to each signal. Then every 5 second long piece of signal was manually labeled according to readability of the signal: label \(3\) was given to good quality signals, label \(2\) was given to medium quality signals and label \(1\) was given to low quality/unreadable signals. In total we had a database made of 10590 \(5\)-second-long sequences.

We turned the problem into a binary classification: label \(0\) was assigned to signals having label=1 and label \(1\) was assigned to signals having label=2,3. A Train/Valid/Test split was performed on the database with weights \(0.3/0.35/0.35\). Train, Valid and Test sets are made of signals coming from different recordings and the signals having label \(0\) are approximately the I8% of each set. For the final evaluation of our models, we run the models for \(10\) times, then, for each score, the best and the worst results were removed and the mean and standard deviation of the remaining \(8\) runs were computed and are reported in Tables I, \(2\).

### Supervised Classification

To test how graph convolutions perform for our classification problem using a supervised method, we applied a convolution smoother of window \(20\) to our dataset, and then we performed a downsample of ratio 4 (i.e. we kept one point every 4). We subdivided each Train/Valid/Test set in non overlapping slices of 5 seconds and we applied a min-max scaler to each sequence independently.

Each 5 second long time series \(\mathbf{x}\in\mathrm{TS}(640,1)\) was given a simple directed graph structure as in Example 2, consisting of one node per signal's point (resulting in 640 nodes). We call \(k\cdot d\) the _lookback window_. We used l28 as our lookback window (l second) and we set \(d\) to be equal to \(4\). We used the Adam optimizer to train all our models. Results are displayed in Table I. Further details about the models used are contained

Figure 5: The structure of the autoencoder
in the Appendix. For the graph convolutions involved in model TCNGraphClassifier the underlying message passings used \(\alpha=t\) as in Formula 2.4: this results in the time dependencies to be read in the reversed direction by these layers.

### Unsupervised Anomaly Detection with Autoencoders

For our unsupervised experiments, the data was pre-processed and prepared as in the supervised case with the only difference that the signals were divided into pieces of one second and that the lookback window was set to 25. Then, we trained the autoencoder to reconstruct these 1 second long signals. \(d\) was set to be either \(4\) or \(8\) depending on the model.

We used the following training procedure: first each model has been trained on the train set for \(50\)-\(100\) epochs. Then the worst reconstructed \(20\)% of signals was discarded. This decreased the percentage of unreadable data in the training set from approximately \(18\)% to \(6\)% for each model. Each model was then retrained from scratch for \(150\)-\(325\) epochs on the refined training set. The rationale of this choice is that, for autoencoders to be used effectively as anomaly detectors, the 'anomalies' should be as few as possible to prevent overfitting the anomalies (see [19], where signals with too many anomalies were discarded). We found that using an autoencoder trained for less epochs can be employed effectively to reduce the proportion of anomalies in the training set. The trained models were then used to compute, for each signal in the Valid set, the reconstruction loss and the Mahalanobis scores as in [19]. Both the resulting scores were then averaged and normalized to provide one mean reconstruction error and one mean Mahalanobis score for each labeled \(5\) second slice of signal. We thus obtained a set of pairs of scores, one for each 5 second long signal in the Valid set: we will refer to it as the errors Valid set.

We used the error Valid set as a feature space to train two unsupervised cluster algorithms: Kmeans and DBscan. For dbscan we set the minimum number of points in a cluster to be equal to 4, as customary for 2 dimensional data, and we used as epsilon the mean of the distances of the points plus 2 times their standard deviation. For both, to get the final labels on the Test set, we used two different techniques. One option we considered is to obtain the final labels for the Test set repeating exactly the procedure

\begin{table}
\begin{tabular}{c|c c c|c c c} \hline \hline \multirow{2}{*}{**Model**} & \multicolumn{3}{c|}{Positive class = label 1} & \multicolumn{3}{c}{Positive class = label 0} \\  & **Precision** & **Recall** & **Accuracy** & **Precision** & **Recall** & **Accuracy** \\ \hline TGraphClassifier & \(0.965\pm 0.002\) & \(0.991\pm 0.002\) & \(0.962\pm 0.003\) & \(0.941\pm 0.012\) & \(0.806\pm 0.011\) & \(0.962\pm 0.003\) \\ TCNGraphClassifier & \(0.939\pm 0.013\) & \(0.988\pm 0.006\) & \(0.936\pm 0.010\) & \(0.912\pm 0.044\) & \(0.653\pm 0.083\) & \(0.936\pm 0.010\) \\ TCNClassifier & \(0.975\pm 0.003\) & \(0.994\pm 0.002\) & \(0.973\pm 0.003\) & \(0.962\pm 0.011\) & \(0.863\pm 0.017\) & \(0.973\pm 0.003\) \\ \hline \hline \end{tabular}
\end{table}
Table 1: Results of the classifiers. Best scores are colored in purple.

\begin{table}
\begin{tabular}{c|c c c|c c c} \hline \hline \multirow{2}{*}{**Model**} & \multicolumn{3}{c|}{Positive class = label 1} & \multicolumn{3}{c}{Positive class = label 0} \\  & **Precision** & **Recall** & **Accuracy** & **Precision** & **Recall** & **Accuracy** \\ \hline _Kmeans_, _approach A_ & & & & & & \\ \hline TGraphMixedAE & \(0.973\pm 0.006\) & \(0.974\pm 0.011\) & \(0.955\pm 0.006\) & \(0.854\pm 0.051\) & \(0.847\pm 0.035\) & \(0.955\pm 0.006\) \\ TGraphAE & \(0.960\pm 0.007\) & \(0.993\pm 0.006\) & \(0.959\pm 0.003\) & \(0.952\pm 0.040\) & \(0.765\pm 0.044\) & \(0.959\pm 0.003\) \\ TCNGraphAE1 & \(0.967\pm 0.003\) & \(0.998\pm 0.002\) & \(0.968\pm 0.002\) & \(0.985\pm 0.014\) & \(0.806\pm 0.016\) & \(0.969\pm 0.002\) \\ TCNGraphAE2 & \(0.965\pm 0.002\) & \(0.997\pm 0.001\) & \(0.966\pm 0.002\) & \(0.976\pm 0.007\) & \(0.796\pm 0.012\) & \(0.966\pm 0.002\) \\ TCNAE1 & \(0.966\pm 0.012\) & \(0.995\pm 0.005\) & \(0.964\pm 0.009\) & \(0.966\pm 0.032\) & \(0.798\pm 0.074\) & \(0.964\pm 0.009\) \\ TCNAE2 & \(0.949\pm 0.005\) & \(0.999\pm 0.001\) & \(0.951\pm 0.004\) & \(0.995\pm 0.006\) & \(0.692\pm 0.031\) & \(0.954\pm 0.004\) \\ \hline _Dbsscan_, _approach B_ & & & & & & \\ \hline TGraphMixedAE & \(0.984\pm 0.005\) & \(0.944\pm 0.012\) & \(0.939\pm 0.007\) & \(0.745\pm 0.037\) & \(0.909\pm 0.028\) & \(0.939\pm 0.007\) \\ TGraphAE & \(0.968\pm 0.006\) & \(0.989\pm 0.006\) & \(0.962\pm 0.002\) & \(0.933\pm 0.034\) & \(0.813\pm 0.038\) & \(0.962\pm 0.002\) \\ TCNGraphAE1 & \(0.971\pm 0.004\) & \(0.991\pm 0.005\) & \(0.966\pm 0.003\) & \(0.940\pm 0.028\) & \(0.829\pm 0.022\) & \(0.966\pm 0.003\) \\ TCNGraphAE2 & \(0.979\pm 0.007\) & \(0.985\pm 0.006\) & \(0.967\pm 0.001\) & \(0.913\pm 0.031\) & \(0.877\pm 0.043\) & \(0.967\pm 0.001\) \\ TCNAE1 & \(0.971\pm 0.007\) & \(0.985\pm 0.011\) & \(0.962\pm 0.007\) & \(0.913\pm 0.057\) & \(0.833\pm 0.043\) & \(0.962\pm 0.007\) \\ TCNAE2 & \(0.973\pm 0.006\) & \(0.988\pm 0.005\) & \(0.966\pm 0.002\) & \(0.925\pm 0.027\) & \(0.846\pm 0.039\) & \(0.966\pm 0.002\) \\ \hline \hline \end{tabular}
\end{table}
Table 2: Results of the autoencoder algorithms. Best scores are colored in purple and the second best in blue.
used for the Valid set (approach A). The second technique we used goes as follows: first, we trained an SVM classifier on the errors Valid set, labeled using the clustering provided by the unsupervised method. Then, we obtained an errors Test set applying the procedure described above for the Valid set, but using the normalizers fitted on the errors Valid set. Finally, we used the trained SVC to predict the labels of the Test signals (approach B). We report the results in table 2, while the clusters obtained for models TGraphAE and TCNAEI are displayed in Figure 7.

The signals reconstructed by these models are displayed in Figure 6. Both models reconstruct good signals in a comparable way and fail to properly reconstruct the bad signals, as expected, despite their small number of parameters. Notice that these methods are fully unsupervised and do not require the use of even a few labeled samples. As in the supervised setting, the graph convolutions involved in models TGraphMixedAE, TCGraphAEI and TCNGraphAEI use \(\alpha=t\) in their underlying message passings as in Formula 2.4. As a consequence, in these models, time dependencies were read in the right direction by the TCNs and in the reversed one by the graph convolutions, resulting in parameter-efficient "bidirectional" structures.

### Discussion

In the case of the supervised classification, a TCN classifier without the use of graph convolutions proved to be the best performing one. This is probably due to the effect of the final flattening layer that may provide the best mechanism in this context to link the encoder to the final MLP. The graph based model had a worse performance but achieved its best using a mean pooling mechanism, as it can be expected. However the graph based classifier obtained good results and had less than half of the parameters than the TCN classifier (see Table 3), thus exhibiting the more expressive power of the graph convolutions.

In the case of the unsupervised classification, the best

Figure 6: The reconstructed signals with TGraphAE (a) and TCNAEI (b). For each subfigure, the top image represents the reconstruction of a good signal (label 1) and the bottom one is the reconstruction of a bad signal (label 0).
performing models were on average the TCN based ones where graph convolutions were added right before and after the bottleneck. This gives a good indication of the fact that graph convolutions applied to digraphs with features can serve as good layers to filter signal coming from different layers. Also the second best performing model overall consists of a Graph based encoder and a TCN decoder, strengthening the hypothesis that graph convolution can be used to improve the effectiveness of ordinary models or to reduce considerably the number of parameters of state of the art architecture without decreasing too much their performance. It has to be noted that in the best performing models, the message passing mechanisms of the graph convolutions were particular cases of formula 2.4 with \(\alpha=t\): as a consequence, these layers learn time dependencies as if the features learnt by the first part of the encoder where reversed in time. Therefore two types of time dependencies were combined in the same algorithm in a parameter-efficient way, mimicking the behaviour of other 'bidirectional' models (such as bidirectional LSTMs).

Summing up, GNN applied to digraphs with features coming from time series showed their effectiveness in improving established algorithms and also their potential to replace them. Moreover, effective fully unsupervised pipelines can be devised to solve anomaly detection and quality recognition problems using the models described in this paper. We plan to continue the study of GNNs applied to time digraphs with features in the context of multivariate time series, constructing more complex time digraph structures and using more capable message passing mechanisms.

### Reproducibility Statement

The experiments described in this paper are reproducible with additional details provided in Appendix A. This section includes a list of all the hyperparameters used to train our models (see Table 3) and more details on the algorithms' implementation.

### Acknowledgements

The authors wish to thank Prof. Rita Fioresi for many stimulating discussions on the content of this paper and A. Arigliano, G. Faglioni and A. Malagoli of VST for providing them with the labeled database used for this work.

A. Simonetti wishes to thank professor J. Evans for his continuous support. The research of F. Zanchetta was supported by Gnsaga-Indam, by COST Action CaLISTA CA2109, HORIZON-MSCA-2022-SE-01-01 CaLIGOLA, MNESYS PE, GNSAGA and has been carried out under a research contract cofounded by the European Union and PON Ricerca e Innovazione 2014-2020 as in the art. 24, comma 3, lett. a), of the Legge 30 dicembre 2010, n. 240 e s.m.i. and of D.M. 10 agosto 2021 n. 1062.

Figure 7: The clusters obtained with TGraphAE and TCNAEI, as follows: (a) TGraphAE-dbscan, (b) TGraphAE-kmeans, (c) TCNAEI-dbscan, (d) TCNAEI-kmeans. For each subfigure, in the top image the points are colored based on their true label and in the one on the bottom they are colored based on the predicted cluster (label 1 in orange and label 0 in blue).
## Appendix A Models' hyperparameters and details

The specific hyperparameters of the models described in the previous sections are listed in table 3. Here is a description of this table, to understand all the names and abbreviations there appearing.

_Num Channels_ gives the number of layers used in the skip connections block, together with the output dimension of each layer in the form of a list; the number after the comma indicates the channel dimension of the signal in the bottleneck resulting after the application of the (ID convolution) graph convolution at the end of the encoder.

_Skip dims_ gives the list of the skip dimensions as described in Section 5.1.

_GConv type_ gives the information on the graph convolution that follows the skip connections block, if present: it gives the type of convolution used and the list of its hidden dimensions - in case the convolution is a GAT layer, it also specifies the number of heads chosen (GAT2H, for instance, means that 2 heads were selected).

As for _Pool type_, _Downsample_, _Upsample_, we indicate the type of layer used referring to their standard notation (see for example pytorch geometric libraries).

Finally for dilated convolutions, we used a kernel size of \(7\) for autoencoders models and \(8\) for the supervised models. When skip connections blocks consist of a sequence of dilated convolutions, we used increasing dilations \(2^{0},2^{1},2^{2},...,2^{n}\) where \(n\) is the number of convolutions appearing in the considered block. We used SiLU activation functions and we employed both batch normalization and dropout between layers. In model TGraphClassifier, a final ID dilated convolution with dilation \(1\) was applied after the decoder.
