2.1. Problem Statement
Given the wheat flour transportation hazard factor dataset as training input, the goal of unsupervised anomaly detection is to identify whether previously unseen observations are anomalous. For anomaly detection in wheat flour transportation, historical values help characterize the current time point. Therefore, we first defined a time window of length at time point t, transforming the wheat flour transportation hazard factor dataset into as the training input for the model. Secondly, we calculated the anomaly scores for time window . Finally, the anomaly scores were compared against a threshold to determine the binary label y, where 1 denotes an anomaly, and 0 denotes a normal observation.
2.2. Data Sources and Construction
The experimental data used in this paper were sourced from routine sampling inspections conducted by the Chinese Administration for Market Regulation of China [
16], comprising 4865 wheat flour transportation detection records collected from April 2010 to July 2023, with a sampling interval of one day. Since each detection record includes a detection date, we sorted the records chronologically and constructed a hazard factor time series for wheat flour transportation to characterize the dynamic evolution of the transportation process. Meanwhile, to consider both the potential increase in hazard levels caused by improper temperature and humidity control or inadequate packaging integrity and the risks of toxicity and carcinogenicity associated with bioaccumulation and biological activity, we selected heavy metals, mycotoxins, and other contaminants as indicators for evaluating wheat flour transportation risks. These hazard indicators primarily include mercury (Hg), arsenic (As), lead (Pb), cadmium (Cd), chromium (Cr), zearalenone (ZEN), deoxynivalenol (DON), ochratoxin A (OTA), aflatoxin B1 (AFB1), and benzo[a]pyrene (BaP). Furthermore, this paper established anomaly labels according to the maximum allowable limits specified in the food safety standard for maximum contaminant levels (GB 2762-2025) [
17] and the food safety standard for maximum mycotoxin levels (GB 2761-2017) [
18], which were used for model validation and performance evaluation. The corresponding national food safety standards for each hazard factor are summarized in
Table 1.
2.4. Graph Structure Learning
During transportation, coupled interactions among multiple hazard factors exert significant influence on the overall safety status of wheat flour. But these coupled interactions exhibit nonlinear characteristics, which make them difficult to characterize effectively using traditional statistical methods. Therefore, to present these coupled interactions, this paper transforms the wheat flour hazard factor detection data within each time window into a graph
consisting of nodes and edges, which is then used as input for a multi-scale spatial–temporal graph model. Here, node
represents hazard factors, and each hazard factor is connected by edges to its
k most relevant hazard factors. Specifically, we first use the detection values of each hazard factor within the current time window as features, denoted as
. Secondly, to ensure balanced coverage of all coupling relationships, we quantify the coupling interactions (including synergistic and antagonistic effects) among the
m hazard factors based on maximal information [
19] and compute the coupling coefficient matrix
:
where
a and
b denote the number of grid points in the
x and
y directions after two-dimensional grid partitioning, respectively;
B represents a constant, set to 0.6 in this paper;
is the joint probability distribution function of
and
; and
and
are the marginal probability distribution functions of
and
, respectively.
Finally, high coupling coefficients indicate the presence of coupling effects among hazard factors within the current time window. Thus, this paper employs the k-nearest neighbors (k-NN) approach to identify, for each hazard factor, the
k hazard factors with the highest coupling coefficients, which together constitute its set of coupling effects. An adjacency matrix is subsequently constructed to implicitly represent the coupling effects among hazard factors:
2.5. Multi-Scale Spatial–Temporal Graph Model
Graph structure learning only captures positional information within the current time window
wt, failing to integrate long-term and short-term dynamic trends across multiple windows. To address this limitation, we design a multi-scale spatial–temporal graph model (MSTGM). Specifically, a multi-scale convolutional attention (MSCA) [
20] is employed to capture hazard factor features in wheat flour across multi-scale time windows. It consists of three components: deep convolutions that aggregate local information, multi-branch deep strip convolutions that capture multi-scale contextual information, and 1 × 1 convolutions that model relationships across channels, whose mathematical equation is given by
where
xt denotes the
tth feature within the time window;
represents element-wise matrix multiplication;
DWConv indicates depthwise convolution; and
signifies the
ith branch, whose structure is illustrated in
Figure 2.
Meanwhile, to comprehensively consider the spatial–temporal characteristics of hazard factors during wheat flour transportation, this paper designs a spatial–temporal feature extraction module (STFEM) that integrates spatial graph convolution with sequence modeling. Specifically, graph convolution is applied to each graph
Gt to aggregate hazard factor information from neighboring regions while maintaining weight-sharing properties, capturing potential coupling relationships among hazard factors:
where
Wl denotes the trainable parameter matrix of layer
l,
denotes the sum of the adjacency matrix and the identity matrix,
denotes the degree matrix corresponding to
, and
denotes the output of layer
l for time window
.
However, to obtain a larger receptive field, it is often necessary to stack multiple GCN layers, which may result in indistinguishable node features after convergence and vanishing gradients during backpropagation [
21]. In practice, most state-of-the-art GCN models do not exceed four layers. Therefore, this paper proposes a residual graph convolutional network (RGCN), which employs skip connections with activation functions across three cascaded GCN layers, described as follows:
where
GCN(.) denotes graph convolution,
ReLU(.) denotes the activation function, and
denotes element-wise summation.
In addition, previous studies have demonstrated that LSTM can capture the temporal dependencies of hazard factors in wheat flour [
22]. Therefore, to jointly capture spatial–temporal dependencies, the proposed RGCN is incorporated into the LSTM through matrix multiplication, enabling the extraction of broader spatial features. Specifically, each LSTM unit contains two internal states,
ct and
ht. The cell state
ct is regulated by three gates: the forget gate ft, the input gate it, and the output gate
ot. Since deep neural networks are prone to vanishing or exploding gradients during training, an exponential gating function is introduced as the activation function in both the input and forget gates. In addition, a normalized state is incorporated to stabilize gradient propagation, thereby reducing the risk of gradient vanishing or explosion and improving the stability of model training:
where
denotes a series operation;
ht−1 represents the previous hidden state;
denotes the current input;
denotes the sigmoid activation function;
exp denotes the exponential gate activation function; and
and
denote functional composition and element-wise multiplication, respectively.
Wf,
Wi,
Wo,
Wc and
bf,
bi,
bo,
bc are learnable parameters;
nt denotes the normalized state at the current time step;
nt-1 denotes the normalized state at the previous time step; and
denotes the normalized hidden state at the current time step.
During the computation of the input and forget gates, the exponential gating activation function may exhibit numerical overflow. To this end, a steady-state parameter,
mt, is designed to regulate the numerical range of the exponential gating activation function, enhancing model stability and improving the accuracy of spatial–temporal feature extraction for hazard factors:
where
mt denotes the stable state at the current time step,
mt−1 denotes the stable state at the previous time step, and
it and
ft denote the values of the output gate and forget gate, respectively. The overall architecture of the STFEM is illustrated in
Figure 3. This approach not only handles extreme values more effectively, thereby reducing the impact of anomalous data on model performance, but also substantially enhances model stability. Moreover, it achieves performance comparable to that of more complex models while maintaining low computational complexity.
2.6. Reconstruction Representation
Although a multi-scale spatial–temporal graph model can capture dependencies across temporal and spatial dimensions, accurately distinguishing between normal and abnormal wheat flour remains crucial for unsupervised anomaly detection. Variational Autoencoder (VAE) is capable of extracting latent patterns from high-dimensional data and has been widely applied to anomaly detection tasks [
23]. Specifically, VAE employs dimensionality reduction to compress the spatial–temporal feature representation
ht of the wheat flour hazard factor into a low-dimensional latent variable
zt and subsequently reconstructs
ht from
zt. Assuming that
zt follows a prior distribution
pθ(
zt), the reconstructed feature
ĥt can be sampled from the posterior distribution
pθ(
ht|
zt). However, conventional VAEs typically employ a multi-layer perceptron (MLP) as both inference and generative networks, which can lead to poor interpretability and high parameter complexity in wheat flour anomaly detection tasks. Kolmogorov–Arnold networks (KANs) [
24] employ learnable activation functions to parameterize weights in spline form, offering high flexibility and enabling complex functions to be simulated with fewer parameters, thereby improving model interpretability. Building on this idea, the traditional VAE is enhanced by employing KAN as both the inference network
qφ(
zt|
ht) and the generative network
pθ(
ht|
zt), where
φ and
θ denote the parameters of the inference and generative networks, respectively. The internal architecture of the proposed model is illustrated in
Figure 4.
As can be seen from
Figure 4, learnable activation functions and spline functions are first applied to
ht to reduce the embedding dimensionality of the spatial–temporal features of the wheat flour hazard factor, ensuring that they approximately follow a normal distribution. The mean and variance are then computed to obtain the latent representation vector
zt:
where
represents the spline function of the encoder,
Sk(.) denotes the learnable activation function of the encoder,
μ denotes the mean, and
σ denotes the variance. Secondly, the features are reconstructed using the spline function and the learnable activation function within the decoder:
where
represents the spline function of the decoder, and
denotes the learnable activation function of the decoder. Finally, model parameters are optimized by adjusting the spline function and the learnable activation function through the reconstruction loss.