Skip to content

2. Training the Autoencoder

EthanTreg edited this page Sep 18, 2024 · 3 revisions

If your data does match the conditions that the network was trained for, or you want to adapt the network, then the network can be retrained.

The autoencoder is trained in a semi-supervised manner to enforce the latent space to be physical.
This is done by first training the decoder using supervised learning on synthetic data.
Once the decoder is trained, it can be used to train the encoder using unsupervised learning by training the network as an autoencoder.

Generating Synthetic Data

Synthetic spectra is generated using Xspec with the model that you want to fit.
This can be done independently; however, the data needs to meet the requirements as mentioned in Data Requirements.
Alternatively, the script synthesize_spectra.py can be used to generate the spectra for you.

  1. Configure settings in synthesize-spectra:
    • synthesize options:
      • synthetic-number: recommended 100,000, the more, the better, but the slower the training
    • data options:
      • spectra-directory: real spectra fits files path
    • model:
      • parameters-number: number of free parameters
      • model-name: Xspec name of the model
      • custom-model-name: Xspec name of custom model to be imported
      • model-directory: custom model directory path
      • log-parameters: list of indices of logged parameters, index starts from 0
      • parameter-limits: list of dictionaries of free parameter limits, dictionary must contain the upper and lower limit to sample from
      • fixed-parameters: dictionary of index and value for fixed parameters, index starts from 1
    • output:
      • synthetic-path: path to save synthetic spectra file
  2. Run synthesize_spectra.npy, can be provided with optional configuration file path argument, default is ../config.yaml

synthesize_spectra.py will generate a .pickle file containing the preprocessed spectral data, parameters and uncertainties for use in training.

Training the Autoencoder

There are two methods to train the autoencoder:

  • Run spectrum_fit.py: Provides several analysis results; however, this makes it slower and more configuration settings are required, this is not recommended
  • Import from fspnet.spectrum_fit import init and net.training: More control, can be implemented into existing code, and will be faster, this is recommended

Both approaches requires setting several parameters in the config.yaml file:

  • training options:
    • decoder-save: 2
    • encoder-save: 2
    • decoder-load: 0
    • encoder-load: 0
    • epochs: recommended 100, more is better, but takes longer and has diminishing returns
    • network-configs-directory: '../network_configs/', make sure this directory exists/is correct and contains the files Encoder V8.json & Decoder V8.json
  • data options:
    • decoder-data-path: synthetic spectra file path
    • encoder-data-path: spectra path
  • model options:
    • log-parameters: list of free parameter with indices starting from 0, used for any parameter that spans a large range, doesn't have to be the same as used for generating synthetic spectra
  • output options:
    • network-states-directory: path to save network training progress

For the first approach, simply run spectrum_fit.py and the trained autoencoder will be saved under network-states-directory as a PyTorch file with the name encoder-name and suffix encoder-save, the decoder will also be saved under a similar naming convention.
Plots from the training and results will be saved under spectrum-fitoutputplots-directory and the predicted data will be save under spectrum-fitoutputparameter-predictions-path as a dictionary of arrays.

For the second approach, init returns the autoencoder and decoder data loaders, and the decoder and autoencoder.
Then call decoder.training(num_epochs, d_loaders) to train the decoder, then we need to fix the decoder while we train the encoder, so we call net.net.net[1].requires_grad_(False) as net.net.net is a list containing the encoder and decoder.
Finally call net.training(num_epochs, e_loaders) to train the encoder.
While the network trains, its state will be saved every epoch under network-states-directory as a PyTorch file with the name encoder-name and suffix encoder-save, the decoder will also be saved under a similar naming convention.

Example code:

from fspnet.spectrum_fit import init

# Initialise data loaders and networks
e_loaders, d_loaders, decoder, net = init()

# Train the decoder
decoder.training(num_epochs, d_loaders)

# Fix the decoder's weights so they do not change while training the encoder
net.net.net[1].requires_grad_(False)

# Train the encoder as an autoencoder
net.training(num_epochs, e_loaders)

# Generate predictions
net.predict(e_loaders[-1], path='optional/path/to/predictions')

Running Scripts via Command Line

Every script directly under fspnet can be run via the command line using the command:
python3 -m fspnet.[script name] [--option name] [option value]
All have the optional argument --config_path, which is the path to the configuration file,
default is ../config.yaml.