quasarnp.layers

A module defining numpy implementations of the four layers used in QuasarNet.

In addition to the four layers defined here (dense, batch_normalization, conv1d and flatten), this module defines three activation functions necessary to replicate the behaviour of QuasarNet. These three activation functions are the relu, linear and sigmoid activation functions.

quasarnp.layers.batch_normalization(x, mean, var, beta, gamma, epsilon)[source]

Computes the batch normalized version of the input.

This function implements a batch normalization layer. Batch normalization renormalizes the input to the layer to a more parsable data range.

Parameters:
  • x (numpy.ndarray) – Input data.

  • mean (numpy.ndarray) – Moving mean of the dataset, computed during training.

  • var (numpy.ndarray) – Moving variance of the dataset, computer during training.

  • beta (array_like) – Offset value added to the normalized output.

  • gamma (array_like) – Scale value to rescale the normalized output.

  • epsilon (float) – Small constant for numerical stability.

Returns:

Output of batch normalization.

Return type:

numpy.ndarray

Notes

The operation implemented in this function is:

\[\begin{split}\\frac{\gamma (x - \mu)}{\sigma} + \\beta\end{split}\]

where \(\mu\) is the moving mean of the dataset and \(\sigma\) is the moving variance of the dataset, both of which are computed during training.

More details and documentation on the TensorFlow batch_normalization function that this function mimics can be found at https://www.tensorflow.org/api_docs/python/tf/nn/batch_normalization.

quasarnp.layers.conv1d(x, w, stride=1, b=None, padding='valid')[source]

Computes a 1-d convolution given 3-d input and weight arrays.

Parameters:
  • x (numpy.ndarray) – Input data of shape (batch_size, in_width, in_channels)

  • w (numpy.ndarray) – Weights array of shape (filter_width, in_channels, out_channels)

  • stride (int, optional) – The number of entries by which the filter is moved at each step. Defaults to 1.

  • b (numpy.ndarray, optional) – Bias array to be added to the output data. Defaults to None.

  • padding (string, optional) – What padding strategy to use for this conv layer. Defaults to “valid”, which means no padding is done. An alternative option is “same”, which pads the layer such that the output has the same width as the input when stride = 1.

Returns:

Array output of the convolution step with shape (batch_size, out_width, out_channels).

Return type:

numpy.ndarray

Notes

More details and documentation on the TensorFlow conv1d function that this function mimics can be found at https://www.tensorflow.org/api_docs/python/tf/nn/conv1d.

quasarnp.layers.dense(x, w, b, phi)[source]

Computes a dense layer operation on the input data.

This function implements a single dense layer for a neural network. The input data is dotted with the weights vector. The bias is added to the output. Finally, the activation function is run over the data and the result is returned.

Parameters:
Returns:

Output of the dense layer.

Return type:

numpy.ndarray

quasarnp.layers.flatten(x)[source]

Flatten an array of data.

This function flattens an array in “C” like order, which is identical to the process that TensorFlow’s flatten function performs. Batch size is not affected

Parameters:

x (numpy.ndarray) – Input data.

Returns:

Flattened array.

Return type:

numpy.ndarray