Function and module reference — UOpt Python 2024 documentation

# Function and module reference

## The context manager

cadjn.init()

  Returns a CADJN context manager object.

  Returns:

    CADJN context manager object.

  Return type:

    CADJN

class cadjn.CADJN

  CADJN context manager.

  This class loads the CADJN C library and creates handles for the necessary functions.

  The most important methods are `init`, which creates CAdjNet neural net objects, `load`, which creates CAdjNet neural net objects from files, and `save`, which saves CAdjNet neural net objects.

  init(init)

    Returns a CADJN context manager object.

    Parameters:

      init (str) – Neural net definition in JSON format.

    Returns:

      CAdjNet neural net object.

    Return type:

      CAdjNet

  load(fname)

    Returns a CADJN context manager object.

    Parameters:

      fname (str) – File to load.

    Returns:

      CAdjNet neural net object.

    Return type:

      CAdjNet

  save(net, fname)

    Saves CAdjNet neural net to file.

    Parameters:

      - net (CAdjNet) – Neural net to save

      - fname (str) – File to save to.

    Returns:

      CAdjNet neural net object.

    Return type:

      CAdjNet

## The neural network

class cadjn.cadjn.CAdjNet(cadjn, net, data)

  The CADJN neural net object.

  Hv(d_w, w, data, specs)

    Compute a product of the Hessian of the scalar objective function `objw` given weights, data, and specs, with d_w. Also returns the gradient as with `g` and the scalar objective value.

    Parameters:

      - d_w (numpy.ndarray) – The derivative direction(s). A 2D array of shape [N, NDD], where N is the number of weights (`numel`) and NDD is some integer > 0. A single vector with shape [N] is also accepted.

      - w (numpy.ndarray) – The network weights. The gradient returned is the derivative of the scalar objective function w.r.t. this parameter.

      - data (numpy.ndarray) – Data and specs to process, as for `objw`.

      - specs (numpy.ndarray) – Data and specs to process, as for `objw`.

    Returns:

      Product of the Hessian of the scalar objective function with d_w, a 2D array of shape [N, NDD] (N is `numel`), gradient of the scalar objective function, a vector of shape [N], and the scalar objective value.

    Return type:

      (numpy.ndarray, numpy.ndarray, float)

  Jv(d_w, w, data, specs)

    Compute a product of the Jacobian of the vector objective function `objwv` given weights, data, and specs, with d_w. Also returns the vector objective value.

    Parameters:

      - d_w (numpy.ndarray) – The derivative direction(s). A 2D array of shape [N, NDD], where N is the number of weights (`numel`) and NDD is some integer > 0. A single vector with shape [N] is also accepted.

      - w (numpy.ndarray) – The network weights. The gradient returned is the derivative of the scalar objective function w.r.t. this parameter.

      - data (numpy.ndarray) – Data and specs to process, as for `objw`.

      - specs (numpy.ndarray) – Data and specs to process, as for `objw`.

    Returns:

      The Jacobian product with d_w, a 2D array of shape [M*J, NDD], where M is the number of samples (the leading dimension of data) and J is `numout`, and the vector objective value.

    Return type:

      (numpy.ndarray, numpy.ndarray)

  compute(data)

    Compute the neural network response from data, which is a 2D array of input data, one per row.

    Parameters:

      data (numpy.ndarray) – Data to process, must be a 2D array of shape [M, I], where M is the number of samples (batch size) and I is the number of inputs (`numin`).

    Returns:

      Network response, a 2D array of shape [M, J], where J is the number of outputs (`numout`).

    Return type:

      numpy.ndarray

  g(w, data, specs)

    Compute the gradient of the scalar objective function `objw` given weights, data, and specs. Also returns the scalar objective value.

    Parameters:

      - w (numpy.ndarray) – The network weights. The gradient returned is the derivative of the scalar objective function w.r.t. this parameter.

      - data (numpy.ndarray) – Data and specs to process, as for `objw`.

      - specs (numpy.ndarray) – Data and specs to process, as for `objw`.

    Returns:

      Gradient of the scalar objective function, a vector of shape [N] (N is `numel`), and the scalar objective value.

    Return type:

      (numpy.ndarray, float)

  getweights()

    Set the weights of the neural network. The vector is a sequence of bias vectors and transfer matrixes according to the network configuration. Use `setweights` to set the weights.

    Returns:

      Vector of network weights, an 1D array of shape [N], where N is the number of weights (`numel`).

    Return type:

      numpy.ndarray

  numel()
    Returns:

      The number of weights of the network.

    Return type:

      int

  numin()
    Returns:

      The number of inputs of the network.

    Return type:

      int

  numout()
    Returns:

      The number of outputs of the network.

    Return type:

      int

  obj(data, specs)

    Compute the scalar objective (error) function of the neural network response from data, which is a 2D array of input data, one per row, given the specs, which is a 2D array of the intended response, one per row. The result is the result of `residual` evaluated by the objective function configured in the network.

    Parameters:

      - data (numpy.ndarray) – Data to process, must be a 2D array of shape [M, I], where I is the number of inputs (`numin`).

      - specs (numpy.ndarray) – Data to process, must be a 2D array of shape [M, J], where J is the number of outputs (`numout`).

    Returns:

      Value of the network scalar objective function.

    Return type:

      float

  objv(data, specs)

    Compute the vector objective (error) function of the neural network response from data, which is a 2D array of input data, one per row, given the specs, which is a 2D array of the intended response, one per row. The result is the result of `residual` evaluated by the vector objective function configured in the network.

    Parameters:

      - data (numpy.ndarray) – Data to process, must be a 2D array of shape [M, I], where I is the number of inputs (`numin`).

      - specs (numpy.ndarray) – Data to process, must be a 2D array of shape [M, J], where J is the number of outputs (`numout`).

    Returns:

      Value of the network vector objective function.

    Return type:

      numpy.ndarray

  objw(w, data, specs)

    Like `obj`, but apply `setweights` before computation.

    Parameters:

      - w (numpy.ndarray) – Vector of network weights, must be an 1D array of shape [N], where N is the number of weights (`numel`).

      - data (numpy.ndarray) – These parameters are passed on to `obj`.

      - specs (numpy.ndarray) – These parameters are passed on to `obj`.

    Returns:

      Value of the network scalar objective function.

    Return type:

      float

  objwv(w, data, specs)

    Like `objv`, but apply `setweights` before computation.

    Parameters:

      - w (numpy.ndarray) – Vector of network weights, must be an 1D array of shape [N], where N is the number of weights (`numel`).

      - data (numpy.ndarray) – These parameters are passed on to `objv`.

      - specs (numpy.ndarray) – These parameters are passed on to `objv`.

    Returns:

      Value of the network vector objective function.

    Return type:

      numpy.ndarray

  residual(data, specs)

    Compute the residual (error) of the neural network response from data, which is a 2D array of input data, one per row, given the specs, which is a 2D array of the intended response, one per row. The result is the result of `compute` minus the specs.

    Parameters:

      - data (numpy.ndarray) – Data to process, must be a 2D array of shape [M, I], where M is the number of samples (batch size) and I is the number of inputs (`numin`).

      - specs (numpy.ndarray) – Data to process, must be a 2D array of shape [M, J], where J is the number of outputs (`numout`).

    Returns:

      Residual of network response, a 2D array of shape [M, J], where J is the number of outputs (`numout`).

    Return type:

      numpy.ndarray

  setweights(w)

    Set the weights of the neural network. The vector is a sequence of bias vectors and transfer matrixes according to the network configuration. Use `getweights` to get the current weights vector.

    Parameters:

      w (numpy.ndarray) – Vector of network weights, must be an 1D array of shape [N], where N is the number of weights (`numel`).

    Returns:

      Status, 0 means no error

    Return type:

      int

  vJ(d_w, w, data, specs)

    Compute a product of d_w with the Jacobian of the vector objective function `objwv` given weights, data, and specs. Also returns the vector objective value.

    Parameters:

      - d_w (numpy.ndarray) – The derivative direction(s). A 2D array of shape [NDD, M*J], where M is the number of samples (the leading dimension of data), J is `numout` and NDD is some integer > 0. A single vector with shape [M*J] is also accepted.

      - w (numpy.ndarray) – The network weights. The gradient returned is the derivative of the scalar objective function w.r.t. this parameter.

      - data (numpy.ndarray) – Data and specs to process, as for `objw`.

      - specs (numpy.ndarray) – Data and specs to process, as for `objw`.

    Returns:

      The product of d_w with the Jacobian, a 2D array of shape [NDD, N], where N is `numel` and NDD is some integer > 0, and the vector objective value.

    Return type:

      (numpy.ndarray, numpy.ndarray)