poplar.nn.networks — poplar 0.0.0 documentation

# `poplar.nn.networks`

## Module Contents

### Classes

`LinearModel` | LinearModel class implementing a standard multi-layer perceptron with some convenience features for function approximation use.

### Functions

`load_model`(→ LinearModel) | Load an existing LinearModel from file.

class poplar.nn.networks.LinearModel(in_features: int, out_features: int, neurons: list, activation: Any, name='model', device='cpu', rescaler=None, out_activation=None, initialisation=None, dropout=0.0, batch_norm=False)

  Bases: `torch.nn.Module`

  LinearModel class implementing a standard multi-layer perceptron with some convenience features for function approximation use.

  This is a subclass of torch.nn.Module.

  Parameters:
    in_featuresint

      Number of features for the input layer of the model.

    out_featuresint

      Number of features for the output layer of the model.

    neuronslist

      A list containing the number of neurons in each layer of the model (excluding input/output).

    activationAny

      The activation function to be used for each hidden layer.

    namestr, optional

      A name for the model, used for file naming. Defaults to “model”.

    devicestr, optional

      pytorch device to initialise the model to, by default “cpu”

    rescaler_type_, optional

      An object for rescaling inputs/outputs. by default IdentityRescaler (see mlsel.nn.rescaling.py for examples)

    out_activation_type_, optional

      Activation function for the output layer, by default None

    initialisation_type_, optional

      Function for setting the initial weights of all neurons, by default uses xavier_uniform rescaling

    dropoutfloat, optional

      Sets the dropout probability for all layers, by default 0 (no dropout).

    batch_normbool, optional

      If True, enables batch normalisation between layers, by default False

  forward(x: torch.Tensor)

    Computes the output for a set of inputs, and removes extra dimensions in the output.

    Parameters:
      xtorch.Tensor

        The input tensor to the model.

    Returns:
      torch.Tensor

        The resulting output tensor, with no dimensions of size 1.

  save(outdir: str)

    Saves the model to a pickle file for reloading later.

    Parameters:
      outdirstr

        Output file directory in which to place the model directory.

  run_on_dataset(inputs: torch.Tensor, n_batches=1, luminosity_distances=None, runtime=False)

    Run this model on a set of inputs, applying all necessary rescalings and transformations.

    If the output is distance-normalised, luminosity distances can also be provided to convert these into unnormalised values.

    Parameters:
      inputstorch.Tensor

        Input tensor to run through the model.

      n_batchesint, optional

        Number of batches to process the input data in, by default 1 (the entire dataset)

      luminosity_distancestorch.Tensor, optional

        Set of luminosity distance values to multiply the output data by, by default None

      runtimebool, optional

        If True, returns timing statistics. By default False

    Returns:
      output: torch.Tensor

        The output of the model after reversing the input scalings.

      timings: list (only returned if runtime is True)

        The time taken for the network [in total, per_datapoint].

  test_threshold_accuracy(xdata: torch.Tensor, ydata: torch.Tensor, threshold: float, confusion_matrix=False, **run_kwargs)

    _summary_

    Parameters:
      xdatatorch.Tensor

        Set of input (target) data to be processed by the network.

      ydatatorch.Tensor

        Set of true values to compare the network output with.

      thresholdfloat

        A threshold value with which to compare the accuracy of each network when operating as a classifier (i.e. 0: below threshold, 1: above threshold)

      confusion_matrixbool, optional

        If True, outputs the result in confusion matrix format. By default False

      **kwargs

        Keyword arguments passed to run_on_dataset.

    Returns:
      accuracy: double

        Accuracy of the network, normalised to [0,1].

      confmat: torch.Tensor (only returned if confusion_matrix is True)

        Confusion matrix of the network output over the two classes (below threshold, above threshold).

poplar.nn.networks.load_model(path: str, device='cpu') → LinearModel

  Load an existing LinearModel from file.

  Parameters:
    pathstr

      Path to .pkl file to be loaded.

    devicestr, optional

      The PyTorch device to load the model to, by default “cpu”

  Returns:
    LinearModel

      Loaded LinearModel.