RNN

scalation.modeling.forecasting.neuralforecasting.RNN
See theRNN companion object
class RNN(val x: TensorD, val y: TensorD, val y_orig: MatrixD, fname: Array[String] = ..., val n_mem: Int = ...) extends RNNCell, FitM

The RNN class implements Recurrent Neural Network (RNN) via Back Propagation Through Time (BPTT). At each time point x_t, there is a vector representing several variables or the encoding of a word. Intended to work for guessing the next work in a sentence or for multi-horizon forecasting.

Value parameters

fname

the feature/variable names

n_mem

the size for hidden state (h) (dimensionality of memory)

x

the input sequence/time series

y

the output sequence/time series

y_orig

the original target matrix before any preprocessing

Attributes

Companion
object
Graph
Supertypes
trait FitM
trait RNNCell
class Object
trait Matchable
class Any
Show all

Members list

Type members

Inherited classlikes

protected case class HiddenState(n_mem: Int, n_seq: Int, seq_length: Int, var param: TensorD = ..., var grad: TensorD = ..., var pre_act_grad: MatrixD = ...)

Case class representing the hidden state of the RNN.

Case class representing the hidden state of the RNN.

Value parameters

grad

tensor for hidden state gradients

n_mem

number of memory units

n_seq

number of sequences

param

tensor for hidden state parameters

pre_act_grad

matrix for pre-activation gradients

seq_length

length of each sequence

Attributes

Inherited from:
RNNCell
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
protected case class ParamGroup[T](var param: T, var velocity: T, var grad: T)

Case class representing a group of parameters, including the parameter itself, its velocity, and its gradient.

Case class representing a group of parameters, including the parameter itself, its velocity, and its gradient.

Value parameters

grad

the gradient of the parameter, used for updating the parameter during training

param

the parameter (e.g., weights or biases)

velocity

the velocity associated with the parameter, used for momentum in optimization

Attributes

Inherited from:
RecurrentBase
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Value members

Concrete methods

def backward(x: TensorD, y: TensorD, batch_start: Int, batch_end: Int, H: HiddenState = ...): Unit

Performs the backward pass of the RNN, computing gradients for the network parameters. This function implements backpropagation through time (BPTT) for a batch of sequences. It calculates the gradients of the loss with respect to the network parameters, which are then used to update the weights in the training process.

Performs the backward pass of the RNN, computing gradients for the network parameters. This function implements backpropagation through time (BPTT) for a batch of sequences. It calculates the gradients of the loss with respect to the network parameters, which are then used to update the weights in the training process.

Value parameters

H

the hidden state tensor, default is the class member H

batch_end

the ending index (inclusive) of the current batch in the full dataset

batch_start

the starting index of the current batch in the full dataset

x

the input tensor of shape [seq_length, n_var, batch_size]

y

the target tensor of shape [seq_length, output_dim, batch_size]

Attributes

Returns

Unit (no return value, updates are made to class members)

def forward(x: TensorD = ..., y: TensorD = ..., batch_start: Int, batch_end: Int, L: VectorD = ..., H: HiddenState = ...): Unit

Performs the forward pass of the RNN, computing the predictions and loss for the network. This function implements the forward propagation for a batch of sequences. It calculates the predictions for each time step and computes the loss based on the difference between the predictions and the target values.

Performs the forward pass of the RNN, computing the predictions and loss for the network. This function implements the forward propagation for a batch of sequences. It calculates the predictions for each time step and computes the loss based on the difference between the predictions and the target values.

Value parameters

H

the hidden state tensor, default is the class member H

L

the loss vector to store the loss values for each time step

batch_end

the ending index (inclusive) of the current batch in the full dataset

batch_start

the starting index of the current batch in the full dataset

x

the input tensor of shape [seq_length, n_var, batch_size]

y

the target tensor of shape [seq_length, output_dim, batch_size]

Attributes

Returns

Unit (no return value, updates are made to class members)

def test(original_extremes: (Double, Double) = ...): Unit

Tests the RNN model by reconstructing predictions and comparing them with the original data. This function reconstructs the full-length predictions from overlapping sequences, prints the dimensions of the reconstructed predictions and original data, and plots the predictions against the original data. It also calculates and prints the SMAPE and MAE values for each variable, and plots the loss function over epochs.

Tests the RNN model by reconstructing predictions and comparing them with the original data. This function reconstructs the full-length predictions from overlapping sequences, prints the dimensions of the reconstructed predictions and original data, and plots the predictions against the original data. It also calculates and prints the SMAPE and MAE values for each variable, and plots the loss function over epochs.

Value parameters

original_extremes

a tuple representing the original extremes of the data for unscaling. default is (1.0, 1.0).

Attributes

override def train(x: TensorD = ..., y: TensorD = ...): Unit

Trains the RNN model using the provided input and target tensors. This function implements the training loop for the RNN model. It iterates over the specified number of epochs, divides the data into batches, performs forward and backward propagation, clips gradients to avoid exploding gradients, and updates the model parameters.

Trains the RNN model using the provided input and target tensors. This function implements the training loop for the RNN model. It iterates over the specified number of epochs, divides the data into batches, performs forward and backward propagation, clips gradients to avoid exploding gradients, and updates the model parameters.

Value parameters

x

the input tensor of shape [seq_length, n_var, n_seq]

y

the target tensor of shape [seq_length, output_dim, n_seq]

Attributes

Returns

Unit (no return value, updates are made to class members)

Definition Classes

Inherited methods

override protected def clip_gradients(threshold: Double): Unit

Clip the gradients of the model parameters to a specified threshold. This method ensures that the gradients do not exceed the given threshold to prevent exploding gradients.

Clip the gradients of the model parameters to a specified threshold. This method ensures that the gradients do not exceed the given threshold to prevent exploding gradients.

Value parameters

threshold

the threshold value for gradient clipping

Attributes

Definition Classes
Inherited from:
RNNCell
def diagnose(y: VectorD, yp: VectorD, w: VectorD = ...): VectorD

Diagnose and return the health of the model by computing the Quality of Fit (QoF) metrics/measures, from the error/residual vector and the predicted & actual responses. For some models the instances may be weighted.

Diagnose and return the health of the model by computing the Quality of Fit (QoF) metrics/measures, from the error/residual vector and the predicted & actual responses. For some models the instances may be weighted.

Value parameters

w

the weights on the instances (defaults to null)

y

the actual response/output vector to use (test/full)

yp

the predicted response/output vector (test/full)

Attributes

See also

Regression_WLS Override to add more metrics.

Inherited from:
FitM
def fit: VectorD

Return the Quality of Fit (QoF) measures corresponding to the labels given. Note, if sse > sst, the model introduces errors and the rSq may be negative, otherwise, R^2 (rSq) ranges from 0 (weak) to 1 (strong). Override to add more quality of fit measures.

Return the Quality of Fit (QoF) measures corresponding to the labels given. Note, if sse > sst, the model introduces errors and the rSq may be negative, otherwise, R^2 (rSq) ranges from 0 (weak) to 1 (strong). Override to add more quality of fit measures.

Attributes

Inherited from:
FitM
protected def get_previous_hidden_state(H: HiddenState, t: Int): MatrixD

Get the previous hidden state at time step t.

Get the previous hidden state at time step t.

Value parameters

H

The hidden state

t

The time step

Attributes

Returns

The previous hidden state matrix

Inherited from:
RNNCell
def help: String

Return the help string that describes the Quality of Fit (QoF) metrics/measures.

Return the help string that describes the Quality of Fit (QoF) metrics/measures.

Attributes

See also

Fit for an implementation. Override to correspond to fitLabel.

Inherited from:
FitM
protected def initializeBiasGroup(size: Int): ParamGroup[VectorD]

Initialize a bias group with a specified size.

Initialize a bias group with a specified size.

Value parameters

size

the size of the bias vector

Attributes

Returns

A ParamGroup containing the initialized bias vector, velocity vector, and gradient vector

Inherited from:
RecurrentBase
protected def initializeParamGroup(rows: Int, cols: Int, stdDev: Double): ParamGroup[MatrixD]

Initialize a parameter group with a specified number of rows, columns, and standard deviation.

Initialize a parameter group with a specified number of rows, columns, and standard deviation.

Value parameters

cols

the number of columns in the parameter matrix

rows

the number of rows in the parameter matrix

stdDev

the standard deviation for initializing the parameter matrix

Attributes

Returns

A ParamGroup containing the initialized parameter matrix, velocity matrix, and gradient matrix

Inherited from:
RecurrentBase
def log_(x: MatrixD): MatrixD

Attributes

Inherited from:
RecurrentBase
def log_(x: VectorD): VectorD

Attributes

Inherited from:
RecurrentBase
inline def rSq0_: Double

Attributes

Inherited from:
FitM
inline def rSq_: Double

Return the coefficient of determination (R^2). Must call diagnose first.

Return the coefficient of determination (R^2). Must call diagnose first.

Attributes

Inherited from:
FitM

Attributes

Inherited from:
RecurrentBase

Attributes

Inherited from:
RecurrentBase
inline def sse_: Double

Return the sum of the squares for error (sse). Must call diagnose first.

Return the sum of the squares for error (sse). Must call diagnose first.

Attributes

Inherited from:
FitM
def summary(x_: MatrixD, fname: Array[String], b: VectorD, vifs: VectorD = ...): String

Produce a QoF summary for a model with diagnostics for each predictor 'x_j' and the overall Quality of Fit (QoF).

Produce a QoF summary for a model with diagnostics for each predictor 'x_j' and the overall Quality of Fit (QoF).

Value parameters

b

the parameters/coefficients for the model

fname

the array of feature/variable names

vifs

the Variance Inflation Factors (VIFs)

x_

the testing/full data/input matrix

Attributes

Inherited from:
FitM

Attributes

Inherited from:
RecurrentBase
def tanh_(x: MatrixD): MatrixD

Attributes

Inherited from:
RecurrentBase
def tanh_(t: VectorD): VectorD

Attributes

Inherited from:
RecurrentBase
protected def updateBatch(matrix: MatrixD, batch_start: Int, batch_end: Int, newBatch: MatrixD): Unit

Updates a specific batch of rows in a matrix with new values.

Updates a specific batch of rows in a matrix with new values.

Value parameters

batch_end

the ending index (exclusive) of the batch in the matrix

batch_start

the starting index of the batch in the matrix

matrix

the matrix to be updated

newBatch

the new matrix containing the values to be inserted

Attributes

Throws
IllegalArgumentException

If the size of newBatch doesn't match the specified batch size

Inherited from:
RecurrentBase
protected def update_params(batch_size: Int, leaky: Boolean = ...): Unit

Update the model parameters based on the gradients. This method applies momentum and updates the parameters using the specified learning rate and batch size.

Update the model parameters based on the gradients. This method applies momentum and updates the parameters using the specified learning rate and batch size.

Value parameters

batch_size

the size of the batch used for training

leaky

a boolean flag indicating whether to use leaky updates (default is true)

Attributes

Inherited from:
RNNCell
protected def zero_gradients(): Unit

Zero the gradients of the model parameters. This method sets all gradients of the weight matrices and bias vectors to zero.

Zero the gradients of the model parameters. This method sets all gradients of the weight matrices and bias vectors to zero.

Attributes

Inherited from:
RNNCell

Concrete fields

override val CLASSIF: Boolean
override val L: VectorD
override val batch_size: Int
override val eta: Double
override val loss_per_epoch: VectorD
override val max_epochs: Int
override val n_mem: Int
override val n_seq: Int
override val n_var: Int
override val seq_length: Int
override val threshold: Double
override val truncation_length: Int
override val x: TensorD
override val y: TensorD
override val β: Double

Inherited fields

lazy protected val H: HiddenState

Attributes

Inherited from:
RNNCell
lazy protected val U: ParamGroup[MatrixD]

Attributes

Inherited from:
RNNCell
lazy protected val V: ParamGroup[MatrixD]

Attributes

Inherited from:
RNNCell
lazy protected val W: ParamGroup[MatrixD]

Attributes

Inherited from:
RNNCell
lazy protected val b_h: ParamGroup[VectorD]

Attributes

Inherited from:
RNNCell
lazy protected val b_y: ParamGroup[VectorD]

Attributes

Inherited from:
RNNCell