# Galaxy Spectra Networks (GaSNet) – III.
## Reconstructive pre-trained network for spectrum reconstruction, redshift estimate, and anomaly detection

Fucheng Zhong, Nicola Napolitano, Caroline Heneka, Jens-Kristian Krogager, Ricardo Demarco, Nicolas Bouché, Jonathan Loveday, Alexander Fritz, Aurélien Verdier, Boudewijn Roukema, et al.

► To cite this version:

Fucheng Zhong, Nicola Napolitano, Caroline Heneka, Jens-Kristian Krogager, Ricardo Demarco, et al.. Galaxy Spectra Networks (GaSNet) – III. Reconstructive pre-trained network for spectrum reconstruction, redshift estimate, and anomaly detection. Monthly Notices of the Royal Astronomical Society, 2025, 543 (1), pp.691-708. 10.1093/mnras/staf1482. hal-05371863

HAL Id: hal-05371863

https://hal.science/hal-05371863v1

Submitted on 19 Nov 2025

**HAL** is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
ROYAL ASTRONOMICAL SOCIETY

MNRAS 543, 691–708 (2025)

https://doi.org/10.1093/mnras/staf1482

Advance Access publication 2025 September 15

# Galaxy Spectra Networks (GaSNet) – III. Reconstructive pre-trained network for spectrum reconstruction, redshift estimate, and anomaly detection

Fucheng Zhong,<sup>1,2★</sup> Nicola R. Napolitano<sup>1,2,3★</sup> Caroline Heneka<sup>1,4</sup> Jens-Kristian Krogager<sup>1,5</sup>

Ricardo Demarco,<sup>6</sup> Nicolas F. Bouché,<sup>5</sup> Jonathan Loveday,<sup>7</sup> Alexander Fritz,<sup>8</sup> Aurélien Verdier<sup>1,9</sup>

Boudewijn F. Roukema<sup>1,5,10</sup> Cristóbal Sifón<sup>1,11</sup> Franz E. Bauer,<sup>12</sup> Letizia P. Cassarà<sup>1,13</sup>

Roberto J. Assef<sup>14</sup> and Steve Ardern<sup>15</sup>

Accepted 2025 August 26. Received 2025 July 22; in original form 2024 December 30

## ABSTRACT

Classification of spectra (1) and anomaly detection (2) are fundamental steps to guarantee the highest accuracy in redshift measurements (3) in modern all-sky spectroscopic surveys. We introduce a new Galaxy Spectra Neural Network (GaSNet-III) model that utilizes neural networks to perform these three tasks simultaneously with high efficiency. Two different reconstruction networks – an autoencoder-like network and a U-Net – are used to reconstruct the rest-frame spectrum, which is then compared with the observed spectrum via a $\chi^2$ metric across the entire type and redshift spaces to find the best-fitting solution. SDSS DR16 spectra are used as a reference data set to provide a fully self-consistent science test to show that our model achieves accuracy comparable to that of classical principal component analysis-based methods, and even better in some specific metrics, while maintaining significantly higher efficiency. In particular, the model achieves an average of > 98 per cent classification accuracy across all classes, and redshift accuracies of over 99 per cent for stars, over 98 per cent for galaxies with errors of the order of $\mathcal{O}(10^{-4})$, and over 93 per cent for quasars with errors of the order of $\mathcal{O}(10^{-3})$. Tests on DESI spectra demonstrate that the model can generalize well to other surveys without retraining, with only a small degradation in performance. Furthermore, by comparing different peaks of $\chi^2$ curves, we define a robustness measure that enables the identification of anomalous spectra. The GaSNet-III provides accurate and high-efficiency spectrum modelling to perform accurate redshift estimates and anomaly detection in vast data volumes from future spectroscopic sky surveys.

**Key words:** techniques: spectroscopic – surveys – software: machine learning – galaxies: distances and redshifts.

## 1 INTRODUCTION

Spectroscopic surveys, complemented with their accurate redshift measurements, map the 3D matter field of the local and far uni-

verse. Among the primary scientific purposes of these surveys is to constrain the cosmological structure based on the statistics of galaxies (e.g. Gil-Marín et al. 2020; Neveux et al. 2020; de Mattia et al. 2021). Meanwhile, it also provides comprehensive spectral data products of extragalactic and galactic targets for specific research purposes. One excellent example is the SDSS survey (Abazajian et al. 2003), which has observed more than a million galaxies, stars, and quasars (QSOs). The multi-fiber spectrograph enables the efficient

<footnote n="1"><sup>1</sup>School of Physics and Astronomy, Sun Yat-sen University, Zhuhai Campus, 519802 Zhuhai, PR China</footnote>

<footnote n="2"><sup>2</sup>Department of Physics E. Pancini, University Federico II, Via Cinthia 6, I-80126 Naples, Italy</footnote>

<footnote n="3"><sup>3</sup>INAF – Osservatorio Astronomico di Capodimonte, Salita Moiariello 16, I-80131 Napoli, Italy</footnote>

<footnote n="4"><sup>4</sup>Institut für Theoretische Physik, Universität Heidelberg, Philosophenweg 16, D-69120 Heidelberg, Germany</footnote>

<footnote n="5"><sup>5</sup>Univ. Lyon1, Ens de Lyon, CNRS, Centre de Recherche Astrophysique de Lyon (CRAL) UMR5574, F-69230 Saint-Genis-Laval, France</footnote>

<footnote n="6"><sup>6</sup>Department of Physics and Astronomy and Institute of Astrophysics, Facultad de Ciencias Exactas, Universidad Andrés Bello, Sede Concepción, 4260000 Talcahuao, Chile</footnote>

<footnote n="7"><sup>7</sup>Astronomy Centre, University of Sussex, Falmer, Brighton BN1 9QH, UK</footnote>

<footnote n="8"><sup>8</sup>Kuffner Observatory, Johann-Staud-Strasse 10, A-1160 Vienna, Austria</footnote>

<footnote n="9"><sup>9</sup>Institute of Physics, Laboratory of Astrophysics, École Polytechnique Fédérale de Lausanne (EPFL), Observatoire de Sauverny, CH-1290 Versoix, Switzerland</footnote>

<footnote n="10"><sup>10</sup>Institute of Astronomy, Faculty of Physics, Astronomy and Informatics, Nicolaus Copernicus University, Grudziadzka 5, PL-87-100 Toruń, Poland</footnote>

<footnote n="11"><sup>11</sup>Instituto de Física, Pontificia Universidad Católica de Valparaíso, Casilla 4059, Valparaíso, Chile</footnote>

<footnote n="12"><sup>12</sup>Instituto de Alta Investigación, Universidad de Tarapacá, Casilla 7D, Arica 1030000, Chile</footnote>

<footnote n="13"><sup>13</sup>INAF–IASF Milano, via Alfonso Corti 12, I-20133 Milano, Italy</footnote>

<footnote n="14"><sup>14</sup>Instituto de Estudios Astrofísicos, Facultad de Ingeniería y Ciencias, Universidad Diego Portales, Av. Ejército Libertador 441, 8370191 Santiago, Chile</footnote>

<footnote n="15"><sup>15</sup>Department of Physics, University of Bath, Bath BA2 7AY, UK</footnote>

<footnote>* E-mail: nicolarosario.napolitano@unina.it (NN); zhongfch@mail2.sysu.edu.cn (FZ)</footnote>
observation of a large number of different targets within a specified field of the sky, with 1000 fibers used in the SDSS-III facilities. The upcoming/ongoing stage-IV spectroscopy surveys, 4MOST (de Jong et al. 2019) and DESI (DESI Collaboration 2022) will be even more data-intensive, with tens of millions of spectra expected to be produced in future observations. For instance, 4MOST telescope facilities have more than 1500 fibers, and their resolution will be larger than 5000 for the low-resolution spectrum, covering over 15 000 deg² and producing over 20 million spectra in a five-year survey (de Jong et al. 2012).

The vast volume of data products practically makes sky surveys a playground for machine learning in data analysis and mining. In fact, this is already happening. Machine learning has become increasingly common in astronomy in recent years (Baron 2019). In the field of optical spectra, various deep learning (DL)-based methods are already being used for parameter estimation and classification. For instance, Malek et al. (2013) and Marchetti et al. (2013; VIPERS) applied a support vector machine for the classification of galaxies, stars, and AGNs between 0.4 < z < 1.2, and used automatic principal component analysis (PCA) for classifying spectra and repairing parts of the spectra affected by gaps, noise, and sky residuals. Fabbro et al. (2018; StarNet) and Guiglion et al. (2024) have applied DL to estimate stellar properties, and Boucaud et al. (2020) to estimate galaxy photometry. Additionally, Leung & Bovy (2019; astroNN) and Wang et al. (2020; SPCANet) used DL to predict multi-element abundances. Furthermore, Li et al. (2019) employed DL for detecting strong gravitational lenses. Other studies, such as Ball et al. (2006), Kim & Brunner (2017), Wang, Guo & Luo (2017), Zou et al. (2020), and Wu et al. (2024), utilized DL in classification. Busca & Balland (2018) and Rastegarnia et al. (2022; QuasarNET) applied DL for QSO classification and redshift estimation to the eBOSS DR16Q catalogue (Lyke et al. 2020), which includes the visually inspected QSOs. In this context, we have developed a series of tools for detecting emission lines from strong gravitationally lensed background galaxies embedded in the spectra of foreground lens systems (Zhong, Li & Napolitano 2022; GasNet-I), and for detailed classification and redshift prediction with the corresponding errors (Zhong et al. 2024; GaSNet-II).

All these methods typically utilize supervised learning, relying on labelled training data to infer the relationship between input and labels. On the other hand, traditional methods typically look for the best-fitting values by modelling the spectrum, e.g. through template fitting or PCA (e.g. Yip et al. 2004a, b; Bolton et al. 2012; Marchetti et al. 2013). Modelling the spectrum provides greater interpretability for predictions, which is scientifically expected. Reconstruction in DL is similar to modelling in traditional methods and may achieve the same level of predictive accuracy. To assess this, we used two different neural networks to reconstruct the rest-frame spectrum: an autoencoder-like network (Hinton & Salakhutdinov 2006) and U-Net (Ronneberger, Fischer & Brox 2015). By comparing the input observed spectrum with the reconstructed spectrum, we can determine the best-fitting type and redshift by minimizing the chi-squared value. The idea is simple and straightforward: we determine the spectral components needed to reproduce the observed spectrum under given conditions and identify their positions in the rest-frame reconstructed spectrum. All of the pixels in the observed spectrum will be compared with a rest-frame spectrum constructed by the network, from the largest to the minimum wavelength of the rest-frame spectrum, and the best-fitting redshift will be decided. This technique is similar to self-supervised learning using partial masks (see Liu et al. 2023), where the masks or ignored regions correspond to wavelengths outside the observed range in the rest frame.

<table><caption>**Table 1.** An overview of the data structure. Subclass nan' labels the spectra with low SNR line features that cannot be subcategorized.</caption><thead><tr><th>Class</th><th>Subclass</th><th>z&#773;</th><th>SNR&#773;</th><th>&chi;<sup>2</sup></th><th>Num</th></tr></thead><tbody><tr><td>STAR</td><td>A0</td><td>0.0</td><td>26.2</td><td>1.81</td><td>20 000</td></tr><tr><td>STAR</td><td>F5</td><td>0.0</td><td>30.5</td><td>1.52</td><td>20 000</td></tr><tr><td>STAR</td><td>F9</td><td>0.0</td><td>34.9</td><td>1.53</td><td>20 000</td></tr><tr><td>STAR</td><td>G2</td><td>0.0</td><td>33.7</td><td>1.26</td><td>20 000</td></tr><tr><td>STAR</td><td>K1</td><td>0.0</td><td>32.8</td><td>1.84</td><td>20 000</td></tr><tr><td>STAR</td><td>K3</td><td>0.0</td><td>31.1</td><td>1.63</td><td>20 000</td></tr><tr><td>STAR</td><td>K5</td><td>0.0</td><td>31.0</td><td>1.79</td><td>20 000</td></tr><tr><td>GALAXY</td><td>nan</td><td>0.46</td><td>5.82</td><td>0.96</td><td>19 997</td></tr><tr><td>GALAXY</td><td>AGN</td><td>0.21</td><td>14.3</td><td>1.68</td><td>20 000</td></tr><tr><td>GALAXY</td><td>STARBURST</td><td>0.15</td><td>9.78</td><td>1.96</td><td>20 000</td></tr><tr><td>GALAXY</td><td>STARFORMING</td><td>0.11</td><td>12.4</td><td>1.13</td><td>20 000</td></tr><tr><td>QSO</td><td>nan</td><td>1.62</td><td>2.64</td><td>1.04</td><td>19 735</td></tr><tr><td>QSO</td><td>BROADLINE</td><td>1.78</td><td>6.54</td><td>1.58</td><td>19 996</td></tr></tbody></table>

The overwhelming volume of data from Stage-IV sky surveys will provide one of the best opportunities for data-driven science, but it also poses a challenge to data-analysis efficiency, especially in time-domain astronomy. Owing to the high efficiency of DL, we can scan the entire parameter space of types and redshifts within milliseconds for each spectrum, generating chi-squared curves for millions of spectra in one hour. By comparing the different local minima of these chi-squared curves, we can classify spectra by their nature (e.g. stars, galaxies, or active galactic nuclei or QSOs), identify anomalies, and determine the redshift of the observed objects. Thus, all these tasks can be reduced to a problem of finding and comparing the peaks of the chi-squared curves. Therefore, this work combines DL-based methods with traditional spectral analysis to perform a textbook case of data-driven science. The anomaly detection part is particularly interesting to spot signatures of new phenomena or peculiar or rare events (see e.g. emission/absorption lines from fore/background systems – for instance, strong gravitational lenses), strongly enhancing the potential of new discoveries.

The paper is organized as follows: in Section 2, we introduce the data distribution and pre-processing; in Section 3, we introduce the method of the spectral modelling with spectral reconstruction models; in Section 4, we show the training and testing results; in Section 5, we discuss the performance with respect to different learned spectral components and potential anomalies. In Section 6, we present our conclusions.

# 2 DATA

## 2.1 SDSS: train and test data

The data used in this paper are taken from SDSS-DR16 (Ahumada et al. 2020), where 1D, stacked, and calibrated spectra are pre-classified into three main classes: GALAXY, STAR, and QSO. Within each class, the spectra are further categorized into different subclasses, but this further classification has not been used in this work (see Zhong et al. 2024). However, to equally represent each subclass, we select an equal number of spectra from the most representative subclasses of each of the three main classes. This is shown in Table 1, where we show the subclasses from which we randomly select ~20 000 spectra with the quality flag ZWARNING = 0. There are seven subclasses in the STAR, four subclasses in GALAXY, and two subclasses in QSO. The subclasses in GALAXY and QSO are mainly distinguished by emission line features, such as the width and intensity of the lines. We note the presence of a 'nan' subclass, which
<figure><img src="image_2.png" /><figcaption>**Figure 1.** SDSS-DR16 spectra properties. From top to bottom, the density distributions of redshift, SNR, and SDSS best-fitting $\chi^2$. The average values are shown in the legenda. Sparse samples are found in regions with high redshift and high SNR.</figcaption></figure>

represents spectra that could not be sub-categorized due to the lack of emission/absorption line features or low signal-to-noise ratio (SNR). Due to the sparse sampling at high redshifts, we apply a redshift cut of $z < 1.5$ ($z_{max} = 1.5$) for galaxies and $z < 5$ ($z_{max} = 5$) for QSOs in our test samples. The mean redshift ($\bar{z}$), mean SNR ($\overline{SNR}$), number of spectra after applying the redshift cut (Num), and mean best fit reduced chi-squared values are summarized in Table 1. The distribution densities of redshift, SNR, and SDSS best-fitting $\chi^2$ are shown in Fig. 1. The data distributions are generally unbalanced across the main parameter range. Looking at the redshift distributions, GALAXY-classified sources drop rapidly in numbers $z > 1$, while QSO drops in number density at $z > 4$. On the other hand, the SNR for GALAXY and QSO is typically below 40 and 20, respectively, and generally lower than that of stars, which can be as high as 80. Finally, the best-fitting $\chi^2$ shows peaks around 1.5 for the three classes, with a tail towards 8–10 almost similar in all classes. Note that the under-sampled redshift ranges, as well as low SNRs, may lead to reduced performance due to either poor training or low data quality.

<figure><img src="image_3.png" /><figcaption>**Figure 2.** Examples of STAR, GALAXY, and QSO spectrum pre-processing. The ID consists of PLATE-MJD-FIBER, while class, subclass, redshift, SNR, and best-fitting $\chi^2$ are shown in the panel titles.</figcaption></figure>

The optical spectra wavelength range in SDSS spans approximately 3,600–10,000 Å with a resolution of $R \sim 2,000$.<fnref n="1" /> We trimmed the spectra to a uniform wavelength grid from 3,600 to 9,200 Å due to heavy noise at the near-infrared end, and resampled it with equal spacing in logarithmic wavelength, with a spacing of $\delta_{\lambda} = 10^{-4}$. As a result, each trimmed spectrum contains 4076 pixels in the logarithmic wavelength range of $log_{10}(3,600) - log_{10}(9,200)$.

The spectra are pre-processed before being input into the network. First, the spectral flux is fitted with a fifth-order polynomial to model the continuum, considering the extinction or reddening (Eisenstein et al. 2006). The fitted continuum is then subtracted from the flux, leaving the absorption and emission line features in the residuals. Next, the residual spectrum is normalized and made dimensionless according to the following:

$$f_i' = f_i - \bar{f}_i, \qquad (1)$$

$$F_i = f_i' / \left( \sum_{i=1}^{n} f_i'^2 / n \right)^{1/2}, \qquad (2)$$

where $f_i$ and $\bar{f}_i$ represent the original flux and the continuum estimated using a fifth-order polynomial fit at the $i$th pixel, respectively, and $n = 4, 076$ is the number of pixels. The normalized, continuum-subtracted flux is denoted as $F$, which will serve as the input for the network. Fig. 2 shows some examples of spectra before and after pre-processing. Normalization removes the scale dependence of magnitude, while the polynomial fitting subtraction preserves the absorption and emission line features.

<footnote n="1">¹https://www.sdss4.org/surveys/eboss/</footnote>
<figure><img src="image_4.png" /><figcaption>**Figure 3.** DESI spectra properties. The density distributions of redshift (top) and SNR (bottom), with average values shown in the legend.</figcaption></figure>

## 2.2 DESI: External test data

To test the generalizability of our model, we introduce an external test set randomly selected from DESI. This external test data will serve solely as a test set and will not be used in model training. The external test data are randomly selected from the region within RA of [120°, 200°] and Dec. of [0°, 20°], with redshifts ranging from 0 to 1.5. The selection is performed using the services and data provided by the SPectra Analysis and Retrievable Catalog Lab (SPARCL; Juneau et al. 2024) and the Astro Data Lab (Fitzpatrick et al. 2014; Nikutta et al. 2020).

For each STAR/GALAXY/QSO class, we selected 10k spectra. Their redshift and the SNR distribution are shown in Fig. 3. One notable aspect is that the SNR of the DESI data set is significantly lower than that of the SDSS data, in particular for galaxy spectra, which will affect the final accuracy. The DESI spectra are processed in the same way as SDSS spectra. However, due to the DESI spectra being spaced on a linear scale rather than a logarithmic scale, interpolating them to match the SDSS wavelength grid may introduce numerical inaccuracies, which may reduce the final prediction accuracy. In Section 4.5, we report the redshift accuracy of this data set by using the model trained on SDSS spectra.

# 3 METHOD

## 3.1 Reconstruction model as a spectrum modelling tool

U-Net and autoencoder have usually been used in astronomy to reconstruct spectra or images. For instance, they are used in spectroscopic data reduction and denoising (Portillo et al. 2020; Vojtekova et al. 2021; Scourfield et al. 2023), outliers detection (Sharma et al. 2019), features mapping (Teimoorinia et al. 2022),

and in reconstructing the rest-frame spectrum (Melchior et al. 2023). The reduced representation of a spectrum is often a latent vector of an autoencoder, which can be clustered for redshift or anomaly detection (Liang et al. 2023a, b), or even to try to derive star formation histories (Iglesias-Navarro et al. 2024). However, our approach does not directly utilize the latent vector, as it contains some stochasticity. Although latent vector combined with physical quantities can improve performance (Ginolfi et al. 2025; M-TOPnet), we instead focus on reconstructing the rest-frame spectrum for different classes, using a specialized encoder for each class mentioned above. We then use the $\chi^2$ as a diagnostic to define the similarity between the original and reconstructed spectra and use this for classification, redshift estimation, and anomaly detection.

In more detail, to reconstruct the rest-frame spectrum and calculate the $\chi^2$ at different redshifts, the reconstructed spectrum is modelled over a wavelength range extending into the ultraviolet and infrared compared to the input spectrum (which we recall $\lambda$ is assumed to span the 3600–9200 Å optical range). By comparing the input spectrum with the reconstructed spectrum and identifying the global minima on the $\chi^2$ curves, we can identify the best-fitting class, redshift, and potential anomalies. In particular, focusing on the redshift, having determined the correct extragalactic class, the presence of a significant global minimum can provide a robust estimate, while the presence of multiple local minima might cause the redshift to be degenerate. Typically, this is expected due to the matching pattern based on emission and absorption lines (for more detail, see Section 4.1).

Given that the relation between the emitted wavelength $\lambda_e$ at redshift $z$ and the observed wavelength is $\lambda_o = (1+z)\lambda_e$, which can be expressed in logarithmic wavelength space as $\log_{10}(1+z) = \log_{10}(\lambda_o/\lambda_e)$, the spectral reconstruction model can reconstruct the rest-frame spectrum based on two key principles:

(i) translation symmetry with respect to $\log_{10}(1+z)$,

(ii) similarity of spectra.

We assume the observed spectra correspond to a finite set of rest-frame spectra that contain similar features. This implies that most observed spectra can be represented by a set of rest-frame spectral components. By including sufficient spectra at different redshifts in the training data set, each region of the rest-frame spectral components should be captured and well-modelled. The spectrum is then reconstructed in the rest frame by superimposing these spectral components.

Based on this idea, we modify the autoencoder-like network architecture to learn both the spectral components from the training set and the corresponding coefficients for each input spectrum, having assumed this latter to be a superposition of $m$ different rest-frame learned spectral components. In this way, we can fully model the input spectrum using a DL-based method. A consequence of this architecture design is that we need to define individual networks for different classes, as each class will be characterized by a different set of spectral components.<fnref n="2" /> We will return to this in the next section.

Additionally, the U-Net has been proven to capture noise-complex data, e.g. in lensed galaxy image reconstruction (Zhong et al. 2025);

<footnote n="2">²Technically, we can put all components learned from different encoders trained by different classes into one encoder, and this encoder only learns the coefficients. All the different encoders can be reduced into one encoder to improve efficiency and tackle the class blending case.</footnote>
denoising of galaxy images (Akhaury et al. 2022; Park et al. 2024); weak lensing mass map denoising (Shirasaki, Yoshida & Ikeda 2019); and gravitational wave signal denoising (Murali & Lumley 2023). However, despite being very efficient for reconstruction, the U-Net does not provide the coefficients or components needed for a more interpretative analysis. For this reason, we need to use an alternative architecture to obtain this more important information.

## 3.2 Network models

As anticipated, two models are used for reconstructing the spectrum: the autoencoder-like model and the U-Net. The former model (denoted as Encoder, for short) has been constructed using the encoding part of an autoencoder to map the input spectrum to $m = 10$ coefficients in the final layer (see Section 5.1 for a discussion). To obtain the learned spectral components, we build a matrix with $m$ rows containing learnable parameters extracted from the same final layer above.

The reconstructed flux ($\hat{F}$) and the number of pixels ($N$) by the Encoder model are computed as follows:

$$ (A, \omega'_m) = \text{Encoder}(\mathbf{F}), \qquad (3) $$

$$ \omega_m = \omega'_m^2 / \sum_m (\omega'_m^2), \qquad (4) $$

$$ \hat{\mathbf{F}} = A \omega_m \cdot \mathbf{M}_{m \times N}, \qquad (5) $$

$$ N = n + \text{Log}_{10} \left( \frac{1 + z_{\text{max}}}{1 + z_{\text{min}}} \right) / \delta_{\lambda}, \qquad (6) $$

where $\mathbf{F}$ is the pre-processed input spectrum from equation (2), $A$ and $\omega_m$ represent the overall amplitude and the normalized coefficients, respectively, $n$ and $N$ denote the number of pixels in the input and reconstructed spectra, the parameters $z_{\text{min}}$ and $z_{\text{max}}$ are the minimum and maximum redshifts considered in the model, and $\delta_{\lambda} \equiv \delta(\log_{10}(\lambda)) = 10^{-4}$ is the spacing of the logarithmic wavelength for the SDSS spectrum.

The coefficients are normalized to ensure their sum equals 1. The values in the matrix, $M_{m \times N}$, are fixed once the training process is completed and become part of the model parameters, while the coefficients vary with changes in the input spectrum. The matrix $M_{m \times N}$ serves as spectral components, similar to the basis templates used in SDSS-III BOSS (see Bolton et al. 2012 and Tsalmantza & Hogg 2012). This is shown in the upper panel of Fig. 4, where the overall scheme of the Encoder architecture used in this paper is illustrated. The extraction of these learned spectral components and the choice of the number of components needed for the optimal spectrum reconstruction will be discussed in detail in Sections 4.2 and 5.1. Compared to a classical autoencoder, our network requires less parameters, and the components matrix can be extracted for interpretation after training. This architecture is designed to identify the most common rest-frame components in the input spectra and determine their relative contributions.

If we are not interested in learned spectral components and related coefficients, the rest-frame spectrum can be alternatively reconstructed using a U-Net (UNet for short) so that we can obtain the reconstructed spectra by $\hat{\mathbf{F}} = \text{UNet}(\mathbf{F})$, where $\mathbf{F}$ represents the input spectrum, as before. The UNet is composed of down-sampling and up-sampling blocks. The down-sampling branch consists of convolutional layers, while the up-sampling branch employs trans-pose convolutional layers. Skip connections between corresponding down- and up-sampling blocks allow the UNet to capture and

<figure><img src="image_5.png" /></figure>

<figure><img src="image_6.png" /><figcaption>**Figure 4.** The network architecture. Four layers in down-sampling blocks or up-sampling blocks are used. The Encoder is illustrated in the upper panel, and the UNet is in the bottom panel. $\mathbf{F}$ represents the input spectrum with dimension $n$, while $\hat{\mathbf{F}}$ represents the output spectrum with dimension $N$, ($N > n$). The symbol $\otimes$ represents the multiplication of the learned spectral components by the coefficients. The spectral components can be extracted from the Encoder, while the UNet reconstructs the spectrum in an end-to-end way.</figcaption></figure>

interpret the noise in the spectrum. The UNet architecture is shown in the bottom panel of Fig. 4.

To conclude this section, we need to remind that, for both networks, the input spectra come from different classes of objects (STAR, GALAXY, and QSO). Hence, as anticipated in the previous section, we need to specialize each network to reconstruct spectra for each one of these classes using a training set selected in that specific class. A discussion about the strategy of defining this training set, i.e. whether this is to be based on theoretical models or observed data, is beyond the purpose of this paper. In our case, we use pre-classified spectra from SDSS as explained in Section 2, which we acknowledge is prone to selection bias. However, depending on the class $x = (s, g, q)$ training set, then we can define a specialized Encoder$_x$ or UNet$_x$. As we will see, these will be a fundamental part of our classification step (see Section 3.5).

## 3.3 Loss functions and chi-squared curves

We employed two different loss functions during the training of the Encoder and the UNet. The Encoder loss function is based on the $\chi^2$ between the reconstructed spectrum and the input spectrum, while the
UNet loss function is a Smooth L1 Loss<fnref n="3" /> between the input spectrum and the reconstructed spectrum, which combines the advantages of both the Mean Absolute Error (MAE) and Mean Squared Error (MSE). The Smooth L1 is used based on the assumption that it provides a noise-free representation of the input spectrum. Since the size of the reconstructed spectrum is larger than that of the input spectrum, a proper region of the reconstructed spectrum must be selected to calculate the loss. A shift as a function of redshift, s(z), is applied to align the index of the reconstructed rest-frame spectrum with that of the input observed spectrum.

The loss functions of Encoder, $L_E$, and UNet, $L_U$, are calculated as follows:

$$L_E = \chi^2 = \frac{1}{n} \sum_{i=1}^{n} (\hat{F}_{i+s} - F_i)^2 / \sigma_i^2, \qquad (7)$$

$$L_U = \text{SmoothL1} = \frac{1}{n} \sum_{i=1}^{n} \begin{cases} |\hat{F}_{i+s} - F_i| - 0.5, & \text{if } |\hat{F}_{i+s} - F_i| \ge 1, \\ 0.5 (\hat{F}_{i+s} - F_i)^2, & \text{else} \end{cases}, \qquad (8)$$

where $\sigma_i$ represents the error in the flux, while $i + s(z) = i - \log_{10}(z + 1)/\delta_\lambda$ is the rest-frame wavelength index, having resampled the wavelength range with a logarithmic step $\delta_\lambda$. The reason for adopting this formalism can be summarized as follows: Due to the fixed wavelength range in the observed spectra, the redshift equivalently introduces a mask/window on the rest-frame spectra. From the perspective of DL, we aim to reconstruct the entire rest-frame spectra under the condition of knowing a specific unmask/window of the observed spectra. This is a common task within the DL community. The shifting $s(z)$ represents the translation property of the spectrum as determined by the redshift. Using the loss functions defined above, we basically extrapolate the rest-frame spectrum based on information about the spectrum used in the training data.

Once we obtain the reconstructed spectrum, the $\chi^2$ values corresponding to different redshifts can be calculated by using the input spectrum and flux error to convolve with the reconstructed spectrum. The location of the minimum $\chi^2$ determines the best-predicted redshift. The chi-squared curve expressed in the convolution is calculated by the following:

$$\chi_s^2 = \frac{1}{n-d} \sum_{i=1}^{n} (\hat{F}_{i+s} - F_i)^2 / \sigma_i^2, \qquad (9)$$

$$\chi^2 = \frac{1}{n-d} (\sigma^{-2} * \hat{F}^2 - 2(\mathbf{F} \cdot \sigma^{-2}) * \hat{F} + C), \qquad (10)$$

where C is a constant for each spectrum, and the operator * represents convolution.<fnref n="4" /> The d represents the number of modelling parameters (in our case, with $m=10$, $d=16$). The $\chi_s^2$ represents the chi-squared value at the sth pixel of the chi-squared curve (an s-pixel shifting in the rest frame gives one chi-squared value), while the $\chi^2$ here represents the whole chi-squared curve. The quantities ($\sigma$, $\hat{F}$, $\mathbf{F}$) without the sub-index can be considered as a vector. At the same time, one can deem the $\sigma^{-2}$ and $\mathbf{F}\sigma^{-2}$ as the kernels for the convolution of the vectors $\hat{F}^2$ and $\hat{F}$. The chi-squared curve is reduced as one constant term and two convolutions: one is the convolution between the inverse variance and the square of the reconstructed spectrum, while the second is the convolution between the input spectrum weighted by the inverse variance and the reconstructed spectrum. The chi-squared curve has the same dimension as the reconstructed spectrum, N, indicating that we scan the redshift from $z_{min}$ to $z_{max}$.

The problem of predicting the best redshift is thus reduced to finding the minimum peak of the chi-squared curves.

After identifying the minimum chi-square and its corresponding wavelength, denoted as $\lambda_1$, the best-predicted redshift is given by

$$z_p = \lambda_o / \lambda_1 - 1, \qquad (11)$$

where the $\lambda_o$ is the central wavelength of the input spectrum. By making the derivative of the index shift $\delta s(z) = \pm 1$ (indicating one-pixel shifting), the theoretical uncertainty of the predicted redshift is:

$$\frac{\delta z_p}{1 + z_p} = \mp \delta_\lambda = \mp 10^{-4}. \qquad (12)$$

## 3.4 Statistics and indicator

To evaluate the classification and redshift prediction results, we used the following quantities:

(i) Classification accuracy:

$$\text{Acc} = \frac{\text{TP}}{\text{Num}}. \qquad (13)$$

The overall accuracy is calculated as the number of correct class predictions (true positive samples, TP) divided by the total number of test samples (Num), while the class-wise accuracy (see diagonal element in Fig. 10) is calculated as the number of correct predictions for a specified class divided by the total number of samples in that class.

(ii) The relative redshift error:

$$\Delta z = |z_p - z_t| / (1 + z_t), \qquad (14)$$

where $z_p$ is the best-fitting redshift from our model, and $z_t$ is the ground truth redshift (in our case, assumed to be the SDSS pipeline redshift).

(iii) The MAE of redshift:

$$\text{MAE} = \frac{\text{Num}}{\sum_{i=1}^{\text{Num}} (z_p - z_t)} / \text{Num}, \qquad (15)$$

where Num represents the number of spectra.

(iv) The MAD:

$$\text{MAD} = \text{Median}[|\Delta z - \text{Median}(\Delta z)|], \qquad (16)$$

where $\Delta z$ represents the relative redshift error.

(v) The redshift 'Robustness':

$$R = \chi_2^2 / \chi_1^2 - 1, \qquad (17)$$

where $\chi_1^2$ is the global minimum peak of chi-squared curves and $\chi_2^2$ is the second-ranked minimum peak $\chi^2$. Indeed, when more than one peak with a value close to the global minimal value is found, this is the main reason for a catastrophic redshift prediction (see Section 5.2). We call it the 'degeneracy' of redshift. Catastrophic redshifts for SDSS QSOs are identified with a deviation $\Delta z > 0.01$ (Dawson et al. 2016). Therefore, we find each local minimum peak within each $\Delta z = 0.01$ interval in the $\chi^2$ curves and then used the ratio of the second minimum peak, $\chi_2^2$, to the global minimum value as an indicator of the best-fitting redshift confidence. For instance, $R=0$ means that there are at least two different $z_p$ values with the same global minimum $\chi^2$ value. In this case, models cannot give a reliable prediction, and the robustness of the predicted redshift is low. On the other hand, large R values indicate more robust solutions as the second peak (if any) has a $\chi_2^2$ value larger than the first one ($\chi_1^2$).

<footnote n="3">³https://pytorch.org/docs/stable/generated/torch.nn.SMOOTH_L1_Loss.html</footnote>

<footnote n="4">⁴This convolution operator in deep learning differs from the usual convolution in that the kernel does not need to be flipped.</footnote>
(vi) The Good fraction:

$$GF_x = \text{Num}(\Delta z < t_x)/\text{Num}. \qquad (18)$$

It is defined as the ratio of the number of spectra with $\Delta z < t_x$ to the total number of test spectra. For the scientific requirement of cosmology measurements, $\Delta z$ should be smaller than a threshold value, $t_x$. Every measurement under that threshold is considered a good prediction. For SDSS, the requirement was $\Delta z < 10^{-3}$ for all redshifts, and $c\Delta z < 300 + 400(z - 1.5)$ for $z > 1.5$ QSOs (Dawson et al. 2016). For comparison, the 4MOST requirements are $\Delta z < 10^{-4}$ for galaxies and $\Delta z < 3 \times 10^{-3}$ for QSOs. Due to the bin size in wavelength of the SDSS spectra and the theoretical uncertainty of our best-fitting redshift (both are $\delta_{\lambda} = 10^{-4}$, as equation (12) shows), we take thresholds of $t_G = 3 \times 10^{-4}$ for galaxies (which means an error within 3 pixels), $t_Q = 3 \times 10^{-3}$ for QSOs, and $t_S = 3 \times 10^{-4}$ for stars.

(vii) The Logarithmic Good fraction:

$$\Delta \ln(z) = |\ln(1 + z_t) - \ln(1 + z_p)| \qquad (19)$$

$$GF'_x = \text{Num}(\Delta \ln(z) < t_x)/\text{Num}, \qquad (20)$$

where $t_x = 0.0015$ for galaxies and $t_x = 0.015$ for QSOs. This is introduced only to assess the performance of GaSNet-III against other standard tools in Section 4.4.<fnref n="5" />

## 3.5 Classification and pipeline

As seen in Section 3.2, for each class (STAR, GALAXY, QSO) we employ a separate Encoder/UNet model to reconstruct the spectrum, assuming it comes from one of the classes in turn. We then generate the chi-squared curve corresponding to the spectrum being a STAR, GALAXY, or QSO object, respectively.

This process produces three 'chi-squared curves' for a single input spectrum, effectively scanning the three class-type options. By identifying the minimum value among these chi-squared curves, we can pinpoint the type and redshift of the input spectrum.

For every model trained on a specific class $x = (s, g, q)$, for STAR, GALAXY, QSO, we map the input spectrum and flux error to three output quantities: ($\chi_x^2$, $z_x$, $R_x$), where $\chi_x^2$ represents the minimum value of the chi-squared curve. The whole can be synthesized as follows:

$$\text{Encoder}_x(\mathbf{F}, \sigma) = (\chi_x^2, z_x, R_x), \quad x \in (s, g, q), \qquad (21)$$

where Encoder$_x$ represents a model trained on the spectra of type $x$. By comparing the minimum $\chi^2$ values from different Encoder$_x$, one can obtain the classification results. The UNet is also trained in the same way to return the corresponding values ($\chi_x^2$, $z_x$, $R_x$), $x \in (s, g, q)$. The entire pipeline is summarized in Fig. 5.

We finally remark that the classification step provides a way to identify anomalies based on the values of $\chi_x^2$ and the redshift robustness parameter. Because a simple definition of anomaly is difficult to give, here, we start by looking into sources that do not show any reasonable similarity with any of the classes described above. Hence, $\chi^2$-wise, this means that an anomalous candidate might be a spectrum with a large $\chi_x^2$ for every class $x = s, g, q$. One can expect this not to be a compelling definition, as, for instance, blending or defect spectra might fall in this simple definition. Instead, one can consider as a potential anomaly a spectrum that shows a large

<figure><img src="image_7.png" /><figcaption>**Figure 5.** The pipeline for classification and anomaly detection. A threshold value $\chi_T^2$ is assumed to identify outliers. Pre-trained models **Encoder$_s$**, **Encoder$_g$**, and **Encoder$_q$**, which are trained on STAR, GALAXY, and QSO spectra, respectively, map the input spectrum and error, $(\mathbf{F}, \sigma)$, to three output quantities $(\chi_x^2, z_x, R_x)$.</figcaption></figure>

$\chi^2$ in one of the classes and still shows some local minima, meaning that there might be some identifiable features (absorption/emission lines) but in a very irregular pattern, such that there might be multiple solutions for a redshift (e.g. a broken continuum, artificial emission or absorption lines from bad data reduction or defects, presence of substructure in the features, etc.). More details are given in Section 5.2.

As a concluding remark, we note that we can further use clustering within the coefficients or latent space to perform a subclass classification or anomaly identification. This is beyond the purpose of this paper. We will take clustering into account in future studies.

## 3.6 About the efficiency of the DL approach

To conclude this method section, we want to address the question of whether one could use a classical approach for the spectrum reconstruction based on a given set of spectral components (see e.g. SDSS templates, Bolton et al. 2012, or DESI templates, Guy et al. 2023). In our DL approach, the Encoder will predict the corresponding coefficients once the input spectrum is given. However, based on the selected set of learned spectral components (in our case, the ones extracted from the Encoder after training), the coefficients can also be calculated analytically. Assuming the flux $\mathbf{F}$ is a superposition of unknown coefficients $\omega$ in equation (5), the $\chi^2$ curve is then calculated as shown in equation (10):

$$\chi_s^2 = \frac{1}{n} \sum_n \left[ \sigma_n^{-2} \left( \sum_m \omega_m \mathcal{M}_{m,n+s} \right)^2 - 2F_n \sigma_n^{-2} \left( \sum_m \omega_m \mathcal{M}_{m,n+s} \right) \right] + C, \qquad (22)$$

where $\mathcal{M}$ is the spectral components matrix. Taking the derivative of $\chi^2$ with respect to $\omega$ and setting it to zero, $\partial\chi_s^2/\partial\omega_j = 0$, we get a linear algebraic equation:

$$\sum_m \omega_m \sum_n \sigma_n^{-2} \mathcal{M}_{j,n+s} \mathcal{M}_{m,n+s} = \sum_n F_n \sigma_n^{-2} \mathcal{M}_{j,n+s}. \qquad (23)$$

For each pixel point $s$ during the scan, the coefficients $\omega_m$ differ. By solving this equation, one can find the optimal combination of coefficients at each pixel point $s$ as:

$$\omega_{m,s} = \sum_j (A_{m,j})_s^{-1} (B_j)_s \qquad (24)$$

$$A_{m,j,s} = [\sigma^{-2} * (e_m^\top e_j^\top)]_s \qquad (25)$$

$$B_{j,s} = [(F\sigma^{-2}) * e_j^\top]_s, \qquad (26)$$

where $e_j^\top = \mathcal{M}_j$, represents one row (a spectral component) in the spectral components matrix. However, calculating $\omega_{m,s}$ analytically requires at least $m^2$ times more computational resources compared

<footnote n="5">⁵This metric is internally used in the 4MOST collaboration for the preliminary comparison of the different pipelines.</footnote>
to DL predictions, in case of solely considering the computation of $A_{m,j,s}$. This demonstrates the efficiency of using a DL model for predicting these coefficients.

# 4 RESULTS

## 4.1 Model training and validation

The SDSS spectra selected for this analysis (see again Section 2) are divided into three subsets for each class: 20 000 for model training, 5000 for validation, and the remaining for testing (we randomly separated the STAR, GALAXY, and QSO into those three sets individually, the number of testing set is shown in the Num legend of Fig. 9). The fixed number of training spectra serves several purposes:

(i) To investigate how performance is affected by varying the amount of training data.

(ii) To demonstrate that no significant improvement occurs when the training samples reach a sufficient amount (approximately 10 000).

(iii) To test the impact of insufficient data.

Those results will be summarized in Appendix A.

For classification purposes, we have found that UNet is prone to overfitting on STAR objects, while it does not show signs of overfitting on GALAXY and QSO objects. The reason for that is that the redshift of STAR objects is close to zero, so using $F_i$ directly as training samples for UNet, causes the input and output spectra to be almost identical. In this case, learning meaningful translation symmetry and feature representations becomes difficult due to the minimum redshift. This can cause UNet, to act more like a 'copier' than a modelling tool for the spectrum (unlike the Encoder, which can reconstruct spectra using learned spectral components). This would produce an overfitting effect that would negatively impact the classification results. Indeed, since the classification relies on the global minimum chi-square, a 'copier' will always yield the minimum chi-square, regardless of the input spectra. To prevent this, the STAR spectra are flipped by UNet, before being passed to the next downsample layer. This artificially introduced 'parity' symmetry<fnref n="6" /> can replace the translation effects. In this way, we achieve accuracy that is very close to that of the Encoder. By introducing parity symmetry within UNet, both the Encoder and UNet can serve as chi-square-generating tools in classification pipelines.

The three Encoder, and the three UNet models ($x = s, g, q$) are trained using STAR, GALAXY, and QSO spectra, respectively, for 100 training epochs. A stepwise learning rate decay is applied, starting from $10^{-3}$ and gradually decreasing to $10^{-4}$. The model checkpoints corresponding to the minimum loss will be used as the final models for evaluation. Fig. 6 shows the loss of 100 training epochs. The loss drops rapidly and then decreases slowly, converging to a stable value.

An example of STAR/GALAXY/QSO spectrum reconstruction and chi-squared curves is shown in Fig. 7. Here, the input spectrum is reconstructed by three Encoders, and the corresponding chi-squared curves and parameters ($\chi_x^2, R_x, z_x$) are shown in the legend. The reconstructed spectrum from the Encoder with the $\chi^2$ global minimum (in black) and the original input spectrum (shown in light shading) are presented and matched at the correct location (given by the best-fitting redshift). The figure demonstrates that we can find the correct type and redshift by reconstructing the rest-frame spectrum

<figure><img src="image_8.png" /><figcaption>**Figure 6.** Loss on the training and validation in 100 epochs. The solid lines represent the training loss, while the dashed lines represent the validation loss. The minimum validation loss points are marked with vertical dashed lines, and their values are shown in the legends. The upper panel shows the loss for the three Encoders, where loss $L_E$ from equation (7) is used, and the lower panel shows the loss for the three UNets, where loss $L_U$ from equation (8) is used. The different Encoder/UNet models are trained with STAR (Encoder, UNet), GALAXY (Encoder, UNet), and QSO (Encoder, UNet) spectra, respectively.</figcaption></figure>

and finding the global minimum of the chi-squared curves. More examples can be found in Appendix B.

## 4.2 Learned spectral components

As explained in Section 3, the spectral components $\mathcal{M}$ are extracted from the Encoder model. In Fig. 8, we display the typical STAR, GALAXY, and QSO spectral components, with obvious emission line features, extracted from the Encoder. In this figure, we have marked some emission lines utilized in SDSS<fnref n="7" /> to demonstrate that the Encoder model effectively captures the features of emission and absorption lines, which will ultimately be used to perform the redshift predictions discussed in the next section.

We remark that the noisy ultraviolet end of the spectral components shown in Fig. 8 is due to the insufficient training samples at high redshifts. This highlights the challenge of accurately modelling spectra at higher redshifts where data sparsity affects the precision of the learned spectral components.

## 4.3 Performance on test data

The best-fitting redshifts from the Encoder and UNet models applied to the test samples selected from the data set detailed in Table 1 are shown in Fig. 9. It shows the relative redshift error, $\Delta z$, versus the SDSS redshift as compared to the cosmological survey requirement marked as a red dashed line. From the figure, we can see that, except

<footnote n="6">⁶https://pytorch.org/docs/stable/generated/torch flip.html</footnote>

<footnote n="7">⁷https://classic.sdss.org/dr7/algorithms/speclinefits.php</footnote>
<figure><img src="image_9.png" /><figcaption>**Figure 7.** Example of input and reconstructed STAR/GALAXY/QSO spectra (we only show the one that has a global minimum chi-squared value in three reconstructed spectra), the chi-squared curves from three Encoders, and the corresponding output parameters in Fig. 5. *z* is the SDSS pipeline redshift. The chi-squared curves have been normalized and shifted for clarity. The vertical dash line shows the global minimum χ² for different chi-squared curves. Identifying the global minimum χ² can estimate the type and redshift of the spectrum. The gaps at the low and high frequency ends of the reconstructed spectra for the STAR in the first row represent noise that is not constrained due to the lack of high Doppler-shifted stellar training spectra (lack of high redshift or blue-shift with *z* ~ ±0.005). For safety, the network for STAR is set to scan the range from *z* = -0.005 to *z* = 0.005.</figcaption></figure>

for a minor fraction of large Δz outliers, the SDSS pipeline redshifts (which we assume as ground truth) can be accurately reproduced for most spectra. This is quantified by the GF indicator for redshift predictions within the specified threshold, which is over 99 per cent for STAR and around 98 per cent for GALAXY spectra in both the Encoder and UNet models. However, for QSOs, the GF decreases to approximately 93 per cent, which is still an excellent performance, although not as good as for the other classes. A primary reason for significant redshift deviation is that spectra with low SNR do not provide a reliable global minimum on the chi-squared curves, as indicated by the red points in the figure, where SNR ≤ 1. By comparing the two networks, we see that the Encoder achieves a higher GF for STAR, which has a higher average SNR, while the UNet performs better in GALAXY and QSO spectra, which tend to have lower SNRs. Although the UNet is an end-to-end reconstruction model and does not provide the complete coefficients for modelling the spectra and corresponding spectral components, it demonstrates a clear advantage in capturing and reducing noise in the spectra. One can confirm this point by the chi-square comparison in Table 2. In this table, we present the mean values of reduced χ² and R² on the test data, as well as the chi-squared values for the SDSS pipeline model. The results show that the Encoder achieves a lower minimum

chi-squared value for the STAR and GALAXY spectra compared to the SDSS pipeline model, although not for the QSO spectra. UNet, on the other hand, achieves a lower average minimum chi-square in all types of spectra. This indicates that our model performs at least on par with the SDSS pipeline in modelling the STAR and GALAXY spectra.

For the spectrum classification purpose, we randomly selected 10 000 spectra from the test set of each class to use as the test data for pipeline spectrum classification. We only considered the minimum values of (χ²_s, χ²_g, χ²_q) to determine the best-fitting type for the input spectrum. To estimate the error of classification accuracy, a 5-fold multiple test was implemented for model training (the 25 000 spectra from each class were divided into five equal parts, with four parts used for training and one for validation. This process was repeated five times, so that each part served once as the validation set while the other four were used for training). We trained five structurally identical network models for each class, and finally assembled the predictions of them to get the average and standard deviation. Fig. 10 presents the confusion matrix for the classification of the total 30 000 test spectra. From the metrics, we can see that the overall classification accuracy combining the three classes exceeds 98 per cent for both the Encoder and UNet, with the best accuracy
<figure><img src="image_10.png" /><figcaption>**Figure 8.** One of the spectral components learned by the Encoder. Each spectrum is just one row of the components matrix, *M*. STAR, GALAXY, and QSO spectral components are displayed from top to bottom. Labels in the figure indicate some relevant emission/absorption lines.</figcaption></figure>

reached for STAR types (~100 per cent) and slightly worse for GALAXY types (~99 per cent), while QSO types are the worst but with a still striking ~96 – 97 per cent.

In Fig. 11, we present the minimum $\chi^2$ distribution on the test set of Encoder models. This figure shows the distribution peaks for three encoders, each approximating a value of 1, which indicates that our model can effectively reconstruct the spectrum.

Due to the GF indicator for STAR being more than 99.9 per cent, we focus only on presenting the GF of GALAXY and QSO as a function of SNR in the middle panel of Fig. 12. The figure suggests that the main driver for the predicted redshift accuracy is SNR. Indeed, GF increases significantly when the SNR rises from 0 to 2, after which it stabilizes at a high value. We remark that the main impact of the SNR is on the difficulty of defining an unambiguous redshift, which is higher for lower SNRs. From this point of view, one can expect the *R* parameter, i.e. the Robustness in redshift prediction defined in Section 3.4, to be also correlated to the SNR (see the top panel of Fig. 12). This *R* parameter, being connected to the ratio of the $\chi^2$ of the second-ranked minimum, provides a measure of the redshift solution quality as the larger the *R* value, the more robust the redshift is. In fact, if there is a second local $\chi^2$ minimum, *R* value shows how much larger the $\chi^2$ value is than the first one. In the worst-case scenario, for very low SNR (e.g. < 2), due to noise overwhelming the spectrum, no significant emission or absorption

features were identified in the spectrum, and the $\chi^2$ scanning shows smooth variation with no clear peak producing an $R \sim 0$, i.e. low Robustness for the redshift solution. In general, one can define a threshold, $R_T$, to judge if a solution is robust enough, i.e. if the $\chi^2$ is significantly larger than $\chi^2_1$. In Fig. 13 (middle), we report the variation of GF for different $R_T$ thresholds for the subset with $R > R_T$, where we can see that the larger the $R_T$, the larger the overall GF, with a steep increase for $R_T < 1$, mirroring the SNR trend discussed above. In particular, for $R_T \sim 0.1$, the GALAXY type already has a GF~1. The completeness of this subset is defined as the ratio of the number of spectra with *R* greater than a given threshold to the total number of samples, $\text{Num}(R > R_T)/\text{Num}$. In this case, in the same Fig. 13 (top), we show the completeness as a function of $R_T$, showing that GF increases significantly as the *R* threshold rises from 0 to 0.5, while the corresponding completeness decreases as the fraction of spectra with higher Robustness decreases. However, the fact that the corresponding GF is ~1 means that for a large $R_T$, all GF indicator solutions are also robust.

In the bottom panel of Fig. 12 and 13, we report the variation of average accuracy for different SNR or $R_T$ subset with $R > R_T$. As expected, the accuracy shows a positive correlation with both SNR and $R_T$. In Fig. 14, we finally show the Robustness distribution of the Encoders, illustrating that the primary regions for the three different types of spectra fall within distinct ranges. This Robustness
<figure><img src="image_11.png" /><figcaption>**Figure 9.** The redshift relative error in test data. The pattern observed in the first column for stars arises from the wavelength binning. The upper panel shows the results from Encoder models, and the bottom panel shows the results from UNet models. From left to right, the columns are STAR, GALAXY, and QSO spectra. The GF threshold, $t_x$, is drawn with a horizontal dashed line. The key statistical values are reported at the bottom right of each panel.</figcaption></figure>

<table><caption>**Table 2.** The average values of $\chi_s^2$ and $R_x$, and the comparison with the SDSS model.</caption><thead><tr><th>Model</th><th>χ<sup>2</sup><sub>s</sub></th><th>χ<sup>2</sup><sub>g</sub></th><th>χ<sup>2</sup><sub>q</sub></th><th>R<sub>s</sub></th><th>R<sub>g</sub></th><th>R<sub>q</sub></th></tr></thead><tbody><tr><td>Encoder</td><td>1.089</td><td>1.203</td><td>1.378</td><td>16.199</td><td>2.402</td><td>0.511</td></tr><tr><td>UNet</td><td>1.004</td><td>1.163</td><td>1.244</td><td>13.766</td><td>3.720</td><td>0.607</td></tr><tr><td>SDSS model</td><td>1.624</td><td>1.432</td><td>1.304</td><td>—</td><td>—</td><td>—</td></tr></tbody></table>

<figure><img src="image_12.png" /><figcaption>**Figure 10.** The confusion matrix for the classification of 30 000 test spectra. A 5-fold multiple test was implemented for model training, and the average and standard deviation are computed in each box. Due to the box size limit, we reduced the percentage error to only two decimal places. We give per class both the number and its absolute error, and below the percentage error. The left panel shows the results from the Encoder, while the right panel corresponds to UNet. Each matrix element contains both the number of spectra classified and the corresponding normalized value. The overall accuracy (ACC, shown in the title) and the class-wise accuracy (diagonal elements) are computed.</figcaption></figure>

<figure><img src="image_13.png" /><figcaption>**Figure 11.** The minimum chi-square distribution of Encoder models.</figcaption></figure>

distribution is mainly driven by the SNR differences among these spectra.

## 4.4 Comparisons

In this section, we compare the performances of the GaSNet-III models against three different standard software for redshift inferences: QXP++ (Krogager et al. in preparation), Redmonster (Hutchinson et al. 2016), and pPXF (Cappellari 2023). We used the same catalogue of test samples, randomly selected from the SDSS, which included 7886 galaxies and 7886 QSOs. The comparison results are summarized in Table 3.

Once again, we focus on extragalactic sources (GALAXY and QSO types) as these are the most relevant sources on which to test performances. The first obvious thing to note is that the GaSNet-III
<figure><img src="image_14.png" /><figcaption>**Figure 12.** The variation of metrics (Robustness, GF, and Acc) with SNR. We focus on the main SNR range (0–100) for GALAXY and QSO spectra. In the range from 1 to 100, the SNR is divided into 19 logarithmic bins, with an additional bin in the range 0–1. The fluctuations for QSOs at high SNR are attributed to the sparse number of samples in those bins.</figcaption></figure>

III models, either Encoder or UNet, show very low MAD, both for galaxies and QSOs, which are much lower than those from some of the classical methods (e.g. pPXF for galaxies), but generally comparable to the values from Redmonster for galaxies and even better than Redmonster's values for QSOs. In particular, this latter result is significant as high accuracy for QSOs is usually challenging to reach due to the high degeneracies resulting from the broad width and variety of emission lines in their spectra (see, e.g. Hewett & Wild 2010). In terms of GF' (equation (20)), the GaSNet-III performance is statistically consistent with the best performance reached by Redmonster, except for the $GF'_q$ of the UNet, which is around 2 per cent lower than Redmonster's. If we combine these excellent accuracies with the computational time, which is about three orders of magnitude faster, we conclude that the DL approach is a very competitive alternative to the best-performing classical methods for redshift measurement. In Section 5.2, we will show that the significant advantage of this approach is the possibility of combining accurate and fast performance in redshift estimates with the ability to detect anomalies, both for quality checks and for new discoveries.

## 4.5 Redshift prediction of external test data

In this section, we input the DESI spectra into the Encoder to predict their redshifts and compare the results with the values obtained from the DESI pipeline. In machine learning, to generalize a model to a new domain (e.g. a different spectroscopic survey like the DESI or 4MOST), the training data should be representative of the target domain. The standard approach is to retrain or adapt the model using

<figure><img src="image_15.png" /><figcaption>**Figure 13.** The variation of metrics and completeness with different $R$ threshold values ($R \ge R_T$). $R_T$ ranges from 0 to 100 and is divided into 20 bins. Taking $R_T \sim 0.1$ and $R_T \sim 1.0$, we can significantly improve the subset's GF of GALAXY and QSO.</figcaption></figure>

<figure><img src="image_16.png" /><figcaption>**Figure 14.** Robustness distribution for test set samples from the Encoders.</figcaption></figure>

new, representative data. We therefore cannot expect a model trained on SDSS data to accurately classify or estimate redshifts for DESI spectra without retraining or domain adaptation. Although directly applying the model to a new domain may degrade performance, we aim to verify that our reconstructive approach can generalize and produce reliable predictions on unseen data.

We utilize DESI spectra as mentioned in Section 2.2. This external data set is entirely unseen during model training; moreover, it has different noise characteristics, instrumental responses, observing conditions, and calibration procedures. These data lie entirely outside the training domain of our model. We demonstrate that, thanks to the Robustness of our method, the model can still accurately predict
redshifts without any transfer learning or additional calibration. This highlights that the fundamental strength of the model lies in its ability to identify the intrinsic physical properties of astronomical objects via their spectra, as well as exhibiting the powerful generalization capability of our approach.

Fig. 15 shows the redshift predictions for DESI spectra. By applying a threshold on the Robustness ($R > 0.02$), we can exclude unreliable predictions and achieve a high GF as for SDSS spectra. Most redshifts of stars and QSOs are accurately recovered. However, due to the impact of low SNR – especially for galaxy spectra – the GF for galaxies is only 90.6 per cent, and approximately 42 per cent of galaxies are excluded after applying the criterion $R > 0.02$. Among these excluded galaxies, 77.3 per cent have an SNR lower than 1. For the SDSS spectra, more than 98.5 per cent of the samples satisfy $R > 0.02$, indicating that the model performs significantly better on the SDSS data set. Further transfer learning and calibration may be required to improve performance for low-SNR DESI galaxies.

The classification based on the global minimal chi-square is significantly affected by the SNR signature of the survey (especially the low SNR of the DESI data set), so it needs further calibration, which we will discuss in the Appendix C.

# 5 DISCUSSION

In the previous section, we presented the results from the GaSNet-III models for redshift determination. Since the performances are dependent on some choices we have made for the spectrum reconstruction, in this section, we discuss the tests done in terms of the number of spectral components and the minimum reconstruction level to optimize the models. This will naturally bring us to the question of whether the reconstructed spectra can allow us to detect anomalies in the true spectra. ‘Anomalies’ are important for two reasons: (1) they can point to a lack of information in the reconstructed spectra, or (2) more interestingly, they highlight that the features deviate from regular patterns in a given spectral class, hence opening up avenues to new discoveries.

## 5.1 The minimum representation of a spectrum

In Section 2, we have described the pre-processing step consisting of applying a fifth-order polynomial fitting, while in Section 3.2 we have adopted ten rest-frame spectral components from an Encoder to reconstruct the input spectrum, meaning only 16 parameters are required for its reconstruction. As both the pre-processing step and the number of spectral components are expected to affect the quality of the reconstructed spectrum, we want to determine the minimum representation needed for such a reconstruction in this section. To assess the impact of both the pre-processing and the number of spectral components, we employ a quadratic polynomial fitting in the spectrum pre-processing and then, with this new pre-processed spectrum, use 3, 5, and 10 spectral components, respectively. Table 4 shows the model’s performance after each model is trained on 20 000 spectra.

As the number of spectral components increases, the average minimum chi-squared values, $\bar{\chi}^2$, decrease, as expected. The case with the fewest spectral components, shown in Table 4, where a quadratic polynomial fitting and three rest-frame spectral components are used, requires only six modelling parameters for each input spectrum. This configuration yields a GF of 99.93, 97.91, and 91.15 per cent and an average minimum chi-square of 1.416, 1.610, and 1.507 for STAR, GALAXY, and QSO, respectively.

Reducing the number of modelling parameters is intended to find an optimal balance between fitting ability, generalization, and interpretability of the model. Using fewer parameters enhances the generalization and interpretability of the model. Our test demonstrates that the spectrum can be effectively modelled with only six parameters, achieving a GF above 90 per cent and an average minimum chi-square below ~ 1.6. However, to achieve optimal performance, the number of spectral components should be ≥ 5. The chi-square suggests that using more spectral components leads to better spectrum fitting. However, for redshift estimation, a higher GF is obtained with the spectra of galaxy $m = 5$ than with $m = 10$.

Finally, we can evaluate the impact of the pre-processing with different degrees of polynomial fitting by directly comparing the quadratic polynomial case with $m = 10$ spectral components (see Table 4) with the fifth-polynomial fitting (Encoder in Table 2), where we have assumed $m = 10$. From this comparison, we see that the fifth-order polynomial preprocessing gives a lower average $\bar{\chi}^2$, with respect to the quadratic one, although the improvement is small.

## 5.2 Anomalies

Spectrum anomalies include a wide variety of deviations from some regular pattern expected for a given class of astronomical sources. Defects in the data also fall into this category, and their identification is part of the quality check of survey products. More interestingly, though, are anomalies having physical meaning, as these might contain information on missing knowledge. For instance, to be identified as anomalies, these deviations have to be defined with respect to a ‘model’. Thus, if the model is incomplete or failing, anomalies allow us to identify the missing physics behind the model. However, there might be anomalies exceeding the features expected in a given class, for example, if these deviations are peculiar to a new class or unaccounted phenomena. In all these cases, the identification of anomalies can bring important information for new discoveries.

To schematize, we can tentatively group the spectra anomalies into three kinds: (1) data defects, (2) anomalies from incomplete models, and (3) extreme anomalies exceeding well-understood models. To quantitatively identify the anomalies of these three kinds, we can treat them as outliers in diagnostics that measure the ‘distance’ of a given spectrum from standard features. A straightforward way is to use the $\chi^2$ and $R$, e.g. spectra with a large $\chi^2$ for all classes would imply a very uncertain classification, while a poor Robustness (i.e. $R \sim 0$) might indicate the presence of multiple equivalent minima for the redshift solution. Some examples of both large $\chi^2$ and minimum $R$ obtained from the test sample are reported in Fig. 16, which eventually represent the catastrophic redshift predictions and misclassifications seen in Fig. 9.

Among these cases, we can recognize:

(i) Low SNR causing redshift degeneracy. When there are multiple close local minima or even no significant peaks in the chi-squared curve, the $R$ is small, and the best-fitting redshift becomes uncertain. This is demonstrated in rows 4 and 6 of Fig. 16, where $R = 0.00$. In these cases, the low SNR blurs the distinct spectral line features for confident redshift determination. This case cannot be considered an anomaly of any of the three kinds above.

(ii) Large $\chi^2$ caused by a very intense emission line or a defective spectrum. Since chi-square fitting takes into account the entire input
<table><caption>**Table 3.** The comparison of different methods. The time is measured in seconds per spectrum per core (sec/spec/core). The indices in MAD<sub>g</sub>/GF'<sub>g</sub> (equation 20) and MAD<sub>q</sub>/GF'<sub>q</sub> represent GALAXY and QSO spectra, respectively.</caption><thead><tr><th>Method</th><th>MAD<sub>g</sub> /10<sup>-4</sup></th><th>GF'<sub>g</sub></th><th>MAD<sub>q</sub> /10<sup>-4</sup></th><th>GF'<sub>q</sub></th><th>Time</th></tr></thead><tbody><tr><td>QXP++</td><td>3.7</td><td>0.980</td><td>—</td><td>—</td><td>0.4</td></tr><tr><td>Redmonster</td><td>0.27</td><td>0.999</td><td>4.52</td><td>0.977</td><td>&lt;0.4</td></tr><tr><td>pPXF</td><td>772.3</td><td>0.854</td><td>—</td><td>—</td><td>0.03</td></tr><tr><td>Encoder</td><td>0.33</td><td>0.994</td><td>2.04</td><td>0.982</td><td>&lt;0.001</td></tr><tr><td>UNet</td><td>0.31</td><td>0.994</td><td>2.65</td><td>0.954</td><td>&lt; 0.001</td></tr></tbody></table>

<table><caption>**Table 4.** The performance of using: (1) a quadratic polynomial fitting in spectrum pre-processing, and (2) a number of *m* = 2, 5, 10 spectral components for the Encoder. GF, Δz, MAE, and MAD are defined in Section 3.4, while χ² represents the mean reduced chi-square.</caption><thead><tr><th>Model</th><th>m</th><th>GF</th><th>Δz /10<sup>-3</sup></th><th>MAE /10<sup>-3</sup></th><th>MAD /10<sup>-3</sup></th><th>χ<sup>2</sup></th></tr></thead><tbody><tr><td>Encoder<sub>g</sub></td><td>3</td><td>0.9993</td><td>0.077</td><td>0.077</td><td>0.043</td><td>1.416</td></tr><tr><td>Encoder<sub>q</sub></td><td>5</td><td>0.9995</td><td>0.077</td><td>0.077</td><td>0.042</td><td>1.311</td></tr><tr><td>Encoder<sub>g</sub></td><td>10</td><td>0.9995</td><td>0.077</td><td>0.077</td><td>0.043</td><td>1.096</td></tr><tr><td>Encoder<sub>q</sub></td><td>3</td><td>0.9791</td><td>1.510</td><td>2.240</td><td>0.035</td><td>1.610</td></tr><tr><td>Encoder<sub>g</sub></td><td>5</td><td>0.9791</td><td>1.110</td><td>1.662</td><td>0.035</td><td>1.298</td></tr><tr><td>Encoder<sub>q</sub></td><td>10</td><td>0.9757</td><td>1.311</td><td>2.005</td><td>0.035</td><td>1.211</td></tr><tr><td>Encoder<sub>g</sub></td><td>3</td><td>0.9115</td><td>17.50</td><td>46.38</td><td>0.230</td><td>1.507</td></tr><tr><td>Encoder<sub>q</sub></td><td>5</td><td>0.9107</td><td>16.91</td><td>43.51</td><td>0.236</td><td>1.465</td></tr><tr><td>Encoder<sub>g</sub></td><td>10</td><td>0.9246</td><td>12.68</td><td>34.46</td><td>0.231</td><td>1.402</td></tr></tbody></table>

wavelength range, mismatches may occur when dealing with very strong and narrow emission lines, resulting in a large chi-squared value. These cases are shown in rows 1 and 3 of Fig. 16. Similarly, spectra that are partially defective can lead to poor fits and higher chi-squared values. This case is shown in row 2 of Fig. 16. These cases are consistent with anomalies of the first and second kinds illustrated above. In particular, for the first kind, we can check if, by adding spectra with strong emission lines, we could correctly recover these spectra.

(iii) Large chi-square caused by Broad Absorption Line (BAL) QSO and Lyman α forest. The high-redshift QSO spectrum is usually imprinted by Lyman α absorption, which makes them irregular and challenging to fit with spectral components. Additionally, the BAL region is also not easily perfectly fitted by spectral components, as the

discrepancy between the SDSS model and Encoder reconstruction is evident in row 5 of Fig. 16. This is again the case of the second kind, as we acknowledge that there is an insufficient number of BAL QSOs in training samples, preventing the model from capturing its features.

As anticipated, some anomalous cases can become normal if the reconstruction model is also able to capture peculiar features of known objects. For instance, objects with intense emission lines in stars or galaxies can produce large χ² that can be corrected by fitting individual lines separately or using a specialized training sample. Additionally, spectrum pre-processing can include some procedures to cut catalogued strong emission lines. Baron & Poznanski (2017) demonstrated various anomalies by comparing synthetic spectra with observed spectra using Random Forests. Their more sophisticated spectral analysis revealed that the primary cause of these anomalies is unusual line features, such as BPT (Baldwin, Phillips, and Terlevich diagrams; Baldwin, Phillips & Terlevich 1981; Veilleux & Osterbrock 1987) outliers or strong, weak, and broad emission lines. So, analyzing the residuals is crucial to detecting anomalies in scientific applications. This would be a reason for improving the reconstruction of peculiar cases or for identifying some missing physics in the emission line process. Additionally, the asymmetry of broad emission lines may pose challenges for precise redshift estimation. However, we believe it will not affect the coarse classification results. In our method, such asymmetries can be defined as anomalies for further analysis.

More difficult is to predict situations where anomalies can bring new discoveries. Some textbook examples are cases where the residual spectrum with inconsistent redshifted emission or absorption lines may indicate background emission from strong gravitational lensing systems (e.g. Bolton et al. 2006) or intergalactic chemical absorption from foreground structures (e.g. Welsh et al. 2010).

To conclude, in this section, we have tried to give a few examples, although far from complete, illustrating the potential offered by the usage of even simple diagnostics to identify outliers that might contain important information to consider for both quality checks and science.

## 6 CONCLUSION

In this work, we have presented two spectral reconstruction model networks designed to reconstruct the rest-frame spectrum from processed observed spectra from SDSS (taken as a reference data

<figure><img src="image_17.png" /><figcaption>**Figure 15.** The redshift relative error in external test data (DESI). The labels are identical to those in Fig. 9, except we take the Robustness, *R* > 0.02, to exclude those unreliable prediction points.</figcaption></figure>
<figure><img src="image_18.png" /><figcaption>**Figure 16.** Examples of maximum chi-square and minimum Robustness cases in the test spectra. From top to bottom, the cases include STAR, GALAXY, and QSO spectra. An additional (blue) dashed line represents the SDSS model, while the other labels are identical to Fig. 7.</figcaption></figure>

set): an Encoder and a UNet model. The Encoder model generates complete spectrum modelling parameters, including rest-frame spectral components derived from the training spectra and corresponding coefficients. In our implementation, we used 16 parameters for each spectrum: 10 spectral components and a fifth-order polynomial for continuous fitting of the input spectrum. The rest-frame spectral components produced by the Encoder reveal absorption and emission line features effectively captured by the network. This approach enhances the interpretability of the Encoder in spectrum modelling compared to the end-to-end UNet.

In particular, we demonstrated that our model can be transferred to other surveys without retraining or calibration, achieving similarly high accuracy as before with very little degradation. In this sense, the required training sample size can be further reduced when

applying our model to new surveys. Although observing conditions, calibration procedures, and instrument responses may vary across surveys, our model can still effectively capture the intrinsic physical properties of the spectra, demonstrating its powerful generalization capability.

Both models are built-in stand-alone algorithms, named Galaxy Spectra Network – III (GaSNet-III, after previous developments in GasNet-I and GasNet-II), performing (1) spectrum classification, (2) redshift estimation, and (3) anomaly detection. Indeed, the reconstructed rest-frame spectra have been used for a coarse spectral classification (in STAR, GALAXY, and QSO-types), while the redshift prediction is obtained by ‘moving’ the rest-frame reconstructed spectrum to match the input spectrum. For this latter task, for the first time, we can demonstrate that a DL method can reach the same level of accuracy as classical methods based on cross-correlation or template fitting but with a computational speed that is about three orders of magnitude faster. Besides this clear advantage with respect to standard tools, GaSNet-III makes use of reconstructed ‘model’ spectra to recognize strongly deviating spectra, possibly identifying anomalies.

This has been made possible by scanning the entire parameter space of spectral type (S/G/Q) and redshift with the computational efficiency of DL. We have generated $\chi^2$ curves in the whole parameter space. This has enabled us to identify the global $\chi^2$ minimum and assess the Robustness, *R*, within these curves.

The average classification accuracy is 98 per cent for STAR-GALAXY-QSO classification, and 99.9, 97.4, and 98.6 per cent for stars, galaxies, and QSOs, respectively. The fraction of estimated redshifts with a relative error below the required thresholds of ~$10^{-4}$ for stars and galaxies, and ~$10^{-3}$ for QSOs (defined as a good fraction, GF) is approximately 99.9, 97.8, 92.8 per cent for STAR, GALAXY, and QSO spectra, respectively, using the SDSS pipeline classification and redshifts as ground truth. The high accuracy of STAR and anomalies defined by ML may mitigate the misclassification of quasars and stars, and also discover potential new science cases via anomaly detection. These results are compared with standard methods like QXP++ (Krogager et al., in preparation), Redmonster (Hutchinson et al. 2016), and pPXF (Cappellari 2023), and only Redmonster has shown performances slightly better than GaSNet-III in galaxies.

The enhanced performance of GaSNet-III resides in the well-designed Encoder network architecture, which is specifically sensitive to the redshift translation symmetry in logarithmic wavelength space and the statistical similarities across observed spectra. By subtracting the continuum using a polynomial fit, we directed the network to model absorption and emission features in the residuals while capturing statistical similarities via a set of rest-frame spectral components learned from the training set.

The performances of the two networks, though, are comparable but with some differences: the Encoder model achieves a lower average $\chi^2$ in STAR and GALAXY spectrum modelling and a comparable chi-square in QSO modelling when compared to the SDSS pipeline, while our UNet achieves a lower average $\chi^2$ on all types of spectra.

As anticipated, the Robustness and minimum $\chi^2$ values serve as indicators of confidence in best-fitting estimates, but they have been used as a metric to identify potential outliers or anomalies. The reconstructed rest-frame spectra from pre-trained models exhibit reduced noise levels. By calculating $\chi^2$ curves between the reconstructed and input spectra across different pre-trained models, we determine not only the best-fitting class and redshift but also the identity of the potential anomalies. Anomalies primarily include galaxies with extremely strong narrow emission lines, high-redshift BAL QSO
spectra, and defective spectra. Setting a lower threshold of $R \ge 0.1$ can effectively remove most cases of catastrophic redshift prediction (see Fig. 13).

Our new tool achieves the same level of modelling accuracy as traditional methods, while providing a new, DL-based solution for accurate and high-efficiency spectrum modelling, which is particularly valuable for handling massive data volumes from future spectroscopic sky surveys. In future work, we plan to incorporate velocity dispersion as an additional parameter in the Encoder model to increase its interpretability.

## ACKNOWLEDGEMENTS

We thank Dr DDM for her valuable comments. FZ acknowledges the support of the China Scholarship Council (grant no. 202306380249). NRN acknowledges support from the Guangdong Science Foundation grant (ID: 2022A1515012251). CH's work is funded by the Volkswagen Foundation. CH also acknowledges support from the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) under Germany's Excellence Strategy EXC 2181/1 – 390900948 (the Heidelberg STRUCTURES Excellence Cluster). We gratefully acknowledge funding from ANID grants: CATA-BASAL FB210003 (FEB, RD, CS, RJA); FONDECYT Regular 1231718 (RJA) and 1241005 (FEB); and Millennium Science Initiative AIM23-0001 (FEB). BFR acknowledges the Polish LSST/Rubin grant: MNiSW grant DIR/WK/2018/12. AV acknowledges the support from the SNF 200020-175751 and 200020-207379 'Cosmology with 3D Maps of the Universe' research grant. This research uses services or data provided by the SPectra Analysis and Retrievable Catalog Lab (SPARCL) and the Astro Data Lab, which are both part of the Community Science and Data Center (CSDC) Program of NSF NOIRLab. NOIRLab is operated by the Association of Universities for Research in Astronomy (AURA), Inc. under a cooperative agreement with the U.S. National Science Foundation.

## DATA AVAILABILITY

The code and example data are available at the following GitHub link: https://github.com/Fucheng-Zhong/GaSNet-III.

## REFERENCES

* Abazajian K. et al., 2003, AJ, 126, 2081

* Ahumada R. et al., 2020, ApJS, 249, 3

* Akhaury U., Starck J.-L., Jablonka P., Courbin F., Michalewicz K., 2022, Front. Astron. Space Sci., 9, 357

* Baldwin J. A., Phillips M. M., Terlevich R., 1981, PASP, 93, 5

* Ball N. M., Brunner R. J., Myers A. D., Tcheng D., 2006, ApJ, 650, 497

* Baron D., 2019, preprint (arXiv:1904.07248)

* Baron D., Poznanski D., 2017, MNRAS, 465, 4530

* Bolton A. S., Burles S., Koopmans L. V. E., Treu T., Moustakas L. A., 2006, ApJ, 638, 703

* Bolton A. S. et al., 2012, AJ, 144, 144

* Boucaud A. et al., 2020, MNRAS, 491, 2481

* Busca N., Balland C., 2018, preprint (arXiv:1808.09955)

* Cappellari M., 2023, MNRAS, 526, 3273

* Dawson K. S. et al., 2016, AJ, 151, 44

* de Jong R. S. et al., 2012, in McLean I. S., Ramsay S. K., Takami H. eds, Proc. SPIE Conf. Ser. Vol. 8446, Ground-based and Airborne Instrumentation for Astronomy IV. SPIE, Bellingham, p. 84460T

* de Jong R. S. et al., 2019, The Messenger, 175, 3

* de Mattia A. et al., 2021, MNRAS, 501, 5616

* DESI Collaboration, 2022, AJ, 164, 207

* Eisenstein D. J. et al., 2006, ApJS, 167, 40

* Fabbro S., Venn K. A., O'Briain T., Bialek S., Kiely C. L., Jahandar F., Monty S., 2018, MNRAS, 475, 2978

* Fitzpatrick M. J. et al., 2014, in Peck A. B., Benn C. R., Seaman R. L. eds, Proc. SPIE Conf. Ser. Vol. 9149, Observatory Operations: Strategies, Processes, and Systems V. SPIE, Bellingham, p. 91491T

* Gil-Marín H. et al., 2020, MNRAS, 498, 2492

* Ginolfi M. et al., 2025, A&A, 693, A73

* Guiglion G. et al., 2024, A&A 682 A9

* Guy J. et al., 2023, AJ, 165, 144

* Hewett P. C., Wild V., 2010, MNRAS, 405, 2302

* Hinton G. E., Salakhutdinov R. R., 2006, Science, 313, 504

* Hutchinson T. A. et al., 2016, AJ, 152, 205

* Iglesias-Navarro P., Huertas-Company M., Martín-Navarro I., Knapen J. H., Pernet E., 2024, A&A, 689, A58

* Juneau S. et al., 2024, preprint (arXiv:2401.05576)

* Kim E. J., Brunner R. J., 2017, MNRAS, 464, 4463

* Leung H. W., Bovy J., 2019, MNRAS, 483, 3255

* Li R., Shu Y., Su J., Feng H., Zhang G., Wang J., Liu H., 2019, MNRAS, 482, 313

* Liang Y., Melchior P., Lu S., Goulding A., Ward C., 2023a, AJ, 166, 75

* Liang Y., Melchior P., Hahn C., Shen J., Goulding A., Ward C., 2023b, ApJ, 956, L6

* Liu X., Zhang F., Hou Z., Wang Z., Mian L., Zhang J., Tang J., 2023, IEEE transactions on knowledge and data engineering 35 1 857

* Lyke B. W. et al., 2020, ApJS, 250, 8

* Malek K. et al., 2013, A&A, 557, A16

* Marchetti A. et al., 2013, MNRAS, 428, 1424

* Melchior P., Liang Y., Hahn C., Goulding A., 2023, AJ, 166, 74

* Murali C., Lumley D., 2023, Phys. Rev. D, 108, 043024

* Neveux R. et al., 2020, MNRAS, 499, 210

* Nikutta R., Fitzpatrick M., Scott A., Weaver B. A., 2020, Astron. Comput., 33, 100411

* Park H., Jo Y., Kang S., Kim T., Jee M. J., 2024, ApJ, 972, 45

* Portillo S. K. N., Parejko J. K., Vergara J. R., Connolly A. J., 2020, AJ, 160, 45

* Rastegarnia F., Mirtorabi M. T., Moradi R., Vafaei Sadr A., Wang Y., 2022, MNRAS, 511, 4490

* Ronneberger O., Fischer P., Brox T., 2015, Springer International Publishing, 234

* Scourfield M., Saintonge A., de Mijolla D., Viti S., 2023, MNRAS, 526, 3037

* Sharma K., Kembhavi A., Kembhavi A., Sivarani T., Abraham S., 2019, Bull. Soc. R. Sci. Liege, 88, 174

* Shirasaki M., Yoshida N., Ikeda S., 2019, Phys. Rev. D, 100, 043527

* Teimoorinia H., Archinuk F., Woo J., Shishehchi S., Bluck A. F. L., 2022, AJ, 163, 71

* Tsalmantza P., Hogg D. W., 2012, ApJ, 753, 122

* Veilleux S., Osterbrock D. E., 1987, ApJS, 63, 295

* Vojtekova A., Lieu M., Valtchanov I., Altieri B., Old L., Chen Q., Hroch F., 2021, MNRAS, 503, 3204

* Wang K., Guo P., Luo A. L., 2017, MNRAS, 465, 4311

* Wang R. et al., 2020, ApJ, 891, 23

* Welsh B. Y., Lallemand R., Vergely J. L., Raimond S., 2010, A&A, 510, A54

* Wu Y., Tao Y., Fan D., Cui C., Zhang Y., 2024, MNRAS, 527, 1163

* Yip C. W. et al., 2004a, AJ, 128, 585

* Yip C. W. et al., 2004b, AJ, 128, 2603

* Zhong F., Li R., Napolitano N. R., 2022, Res. Astron. Astrophys., 22, 065014

* Zhong F. et al., 2024, MNRAS, 532, 643

* Zhong F. et al., 2025, ApJS, 277, 12

* Zou Z., Zhu T., Xu L., Luo A. L., 2020, PASP, 132, 044503

## APPENDIX A: IMPACT OF THE TRAINING SET SIZES

The number of well-modelled or well-calibrated observed spectra with accurate redshifts is often limited and not always as abundant as desired for model training. In this section, we investigate the performance of the Encoder model with varying training set sizes,
<table><caption>**Table A1.** The performance of different training set sizes. Num represents the size of the training set.</caption><thead><tr><th>Model</th><th>Num 10<sup>3</sup></th><th>GF per cent</th><th>Δz /10<sup>-3</sup></th><th>MAE /10<sup>-3</sup></th><th>MAD /10<sup>-3</sup></th><th>x̄<sup>2</sup></th></tr></thead><tbody><tr><td>Encoder<sub>s</sub></td><td>5</td><td>99.93</td><td>0.079</td><td>0.079</td><td>0.043</td><td>1.306</td></tr><tr><td>Encoder<sub>s</sub></td><td>10</td><td>99.94</td><td>0.079</td><td>0.079</td><td>0.045</td><td>1.144</td></tr><tr><td>Encoder<sub>s</sub></td><td>20</td><td>99.95</td><td>0.077</td><td>0.077</td><td>0.043</td><td>1.119</td></tr><tr><td>Encoder<sub>g</sub></td><td>5</td><td>94.59</td><td>2.814</td><td>4.391</td><td>0.037</td><td>1.592</td></tr><tr><td>Encoder<sub>g</sub></td><td>10</td><td>97.26</td><td>1.148</td><td>1.786</td><td>0.036</td><td>1.296</td></tr><tr><td>Encoder<sub>g</sub></td><td>20</td><td>97.49</td><td>1.397</td><td>2.132</td><td>0.035</td><td>1.219</td></tr><tr><td>Encoder<sub>q</sub></td><td>5</td><td>90.74</td><td>17.67</td><td>51.15</td><td>0.208</td><td>1.552</td></tr><tr><td>Encoder<sub>q</sub></td><td>10</td><td>91.85</td><td>14.90</td><td>43.08</td><td>0.234</td><td>1.435</td></tr><tr><td>Encoder<sub>q</sub></td><td>20</td><td>92.02</td><td>15.47</td><td>40.21</td><td>0.245</td><td>1.432</td></tr></tbody></table>

selecting sets of 5000, 10 000, and 20 000 spectra for each class. For each set, we apply a quadratic polynomial fitting in spectrum pre-processing and utilize ten spectral components in the model.

Table A1 presents the performance of models across different training set sizes. The results indicate that the model reaches near-optimal performance with training sets of 10 000 spectra or more. Increasing the amount of training data beyond this point offers only tiny gains, likely due to the limited diversity within the training samples, as the network has already captured most of the essential spectral features.

To further enhance performance, selecting training spectra that are representative and diverse could be more effective than randomly selecting samples from the survey. By pre-selecting spectra that cover a variety of observed spectra, we can probably improve the model performance from a broader and more representative data set.

## APPENDIX B: EXAMPLES OF ENCODER MODELLING

We present six randomly selected normal spectra reconstructed by the Encoder in Fig. B1.

<figure><img src="image_19.png" /><figcaption>**Figure B1.** Examples of input and reconstructed STAR, GALAXY, and QSO spectra. The labels are identical to Fig. 7.</figcaption></figure>
APPENDIX C: MINIMAL REDUCED
CHI-SQUARE VERSUS REDSHIFT

In the classification part, we compare the minimal reduced chi-square values from three different types of spectra: STAR, Galaxy, and QSO. Therefore, we may need to calibrate or rescale the chi-square values to make them comparable. The calculated chi-square values can be influenced by differences in the average SNR across these spectral types. In addition, variations in redshift also affect the chi-square values, as the shift of absorption and emission features makes some of them, e.g. contributing to the classification/redshift inference at a lower redshift, to fall outside the observed wavelength ranges at higher redshift, thus reducing the overall accuracy, if no other features enter the observation window at lower wavelengths. In Fig. C1, we show the average minimal chi-square values varying with the type and redshift. We found that the chi-square values are mainly influenced by the SNR and the size of the training sample in a given redshift bin. To quantify the latter we have defined the ‘under-sampling factor’ $f_u = N_{train}/N_{train,z}$, where $N_{train}$ and $N_{train,z}$ are the total training set and the training set in a given redshift bin, respectively. The larger $f_u$ is, the more under-sampled a given class is in a given redshift. Thus, the minimal chi-square positively correlates with the SNR and the $f_u$. As expected, as the SNR improved, the one sigma flux error will be small, from the definition of chi-square as shown in equation (9), the chi-square will be smaller; on the other hand, as the $f_u$ increases in this specific bin, the model lacks sufficient training, which results in a larger chi-square value. Those are attributed to the unbalanced training data across redshift. This is accompanied by the fact that at different redshifts, the different emission/absorption lines are included. Additionally, at higher redshifts, observational limitations and decreasing SNR make it more challenging to obtain balanced training data.

One might propose a factor as a function of redshift to rescale the chi-square curves for chi-square comparison across different classes. The rescaled chi-square values are expected to be equal to one when they correspond to the correct type and redshift, so the calibrated factor depends on the training set and its averages of chi-square curves as follows:

$$ \chi'(z) = \frac{\chi(z)}{\bar{\chi}(z)}, \qquad (C1) $$

where the calibration factors $1/\bar{\chi}$ will weight the chi-square curve, obtaining the calibrated chi-square $\chi'$. However, due to the complex dependencies described in Fig. 9, particularly those arising from

<figure><img src="image_20.png" /><figcaption>**Figure C1.** Mean $\chi^2$, SNR, and the first panel shows the average minimal chi-square values across different redshift bins. The second panel presents the median SNR as a function of redshift. The third panel displays the inverse of the training data frequency versus redshift. A redshift bin width of 0.1 is used. The dashed horizontal line indicates the threshold where the number of training samples in a bin equals 20.</figcaption></figure>

unbalanced training data across redshift, this calibration might reduce classification performance. In some redshift bins, the model may have sufficient training, while in others it does not. As a result, the calibration will overestimate the performance of those insufficient training regions. Furthermore, using the redshift from the SDSS pipeline might introduce an additional or potential systematic bias within their method. A better option is training the model with balanced and uniformly simulated spectra and calibrating it using simulated or visually inspected spectral information, which will be implemented in future work.

This paper has been typeset from a TeX/LA TeX file prepared by the author.