learn

class learn.Learner(model, batch_size=20, dt=0.1, neurons=64, layers=2, name='.', cuda=False, dissipative=False)

Bases: object

This is the fundamental class that provides the capability to learn dynamical systems, using various methods of learning (without Jacobi identity, with softly enforced Jacobi, and with implicitly valid Jacobi).

mov_loss_without(zn_tensor, zn2_tensor, mid_tensor)

The function calculates the movement loss using the “without” method.

Parameters:
  • zn_tensor – The zn_tensor parameter represents the current state of the system. It is a tensor that contains the values of the variables in the system at a particular time

  • zn2_tensor – The parameter zn2_tensor is a tensor representing the current state of the system at time t+dt. It is used to calculate the loss function for the movement of the system without considering any external forces or constraints

  • mid_tensor – The mid_tensor parameter is not used in the mov_loss_without function. It is not necessary for the calculation and can be removed from the function signature

Returns:

the result of the expression (zn_tensor - zn2_tensor)/self.dt + 1.0/2.0*(torch.matmul(Lz, E_z.unsqueeze(2)).squeeze() + torch.matmul(Lz2, E_z2.unsqueeze(2)).squeeze()).

mov_loss_without_with_jacobi(zn_tensor, zn2_tensor, mid_tensor, reduced_L)

The function calculates the movement loss including Jacobi identity the for a given input tensor.

Parameters:
  • zn_tensor – The zn_tensor is a tensor representing the current state of the system. It is used to calculate the energy and the Jacobian loss of the system

  • zn2_tensor – The zn2_tensor parameter is a tensor representing the state at time t+1. It is used to calculate the loss for the movement of the system without using the Jacobian matrix

  • mid_tensor – The mid_tensor parameter is a tensor representing the intermediate state between zn_tensor and zn2_tensor. It is used to calculate the loss function

  • reduced_L – The parameter “reduced_L” is a reduced Laplacian matrix. It is used in the calculation of the Jacobi loss

Returns:

two values: 1. The difference between zn_tensor and zn2_tensor divided by self.dt plus half of the sum of the matrix multiplication of Lz and E_z and the matrix multiplication of Lz2 and E_z2. 2. The jacobi_loss calculated using zn_tensor, `

jacobi_loss(zn_tensor, Lz, reduced_L)

The function jacobi_loss calculates the Jacobi loss (error in Jacobi identity) using the given inputs.

Parameters:
  • zn_tensor – The zn_tensor parameter is a tensor representing the input to the function. It is used to compute the Jacobian loss.

  • Lz – Lz is a tensor representing the Jacobian matrix of the output with respect to the input. It has shape (m, n, n), where m is the number of samples and n is the number of input variables.

  • reduced_L – The parameter reduced_L is a tensor representing the reduced loss function

Returns:

the sum of three terms: term1, term2, and term3.

mov_loss_soft(zn_tensor, zn2_tensor, mid_tensor, reduced_L)

The function mov_loss_soft calculates the movement loss and Jacobi loss for a given input tensor, using the “soft” method.

Parameters:
  • zn_tensor – The zn_tensor is a tensor representing the current state of the system. It is used to calculate the energy and gradient of the energy with respect to zn_tensor

  • zn2_tensor – The zn2_tensor parameter is a tensor representing the state at time t+1. It is used to calculate the movement loss and Jacobi loss in the mov_loss_soft function

  • mid_tensor – The mid_tensor parameter is not used in the mov_loss_soft function. It is not clear what its purpose is without further context.

  • reduced_L – The parameter “reduced_L” is a tensor representing the reduced Laplacian matrix

Returns:

two values: mov_loss and jacobi_loss.

mov_loss_implicit(zn_tensor, zn2_tensor, mid_tensor)

The function mov_loss_implicit calculates the loss for a motion model using the “implicit” method.

Parameters:
  • zn_tensor – The zn_tensor parameter is a tensor representing the current state of the system. It is used to calculate the energy and Jacobian vectors for the system.

  • zn2_tensor – The zn2_tensor parameter is a tensor representing the state of the system at time t + dt, where t is the current time and dt is the time step.

  • mid_tensor – The mid_tensor parameter is not used in the mov_loss_implicit function. It is not necessary for the calculation and can be removed from the function signature.

Returns:

The function mov_loss_implicit returns the result of the expression (zn_tensor - zn2_tensor)/self.dt + 1.0/2.0*(torch.cross(Jz, E_z, dim=1) + torch.cross(Jz2, E_z2, dim=1)).

learn(method='without', learning_rate=1e-05, epochs=10, prefactor=1.0, jac_prefactor=1.0, scheme='IMR')

The learn function is used to train a model using different methods and parameters, and it saves the trained models and error metrics.

Parameters:
  • method – The method parameter determines the learning method to be used. It can take one of three values: “without”, “soft”, or “implicit”, defaults to without (optional)

  • learning_rate – The learning rate determines the step size at which the optimizer adjusts the model’s parameters during training. It controls how quickly or slowly the model learns from the training data

  • epochs – The “epochs” parameter determines the number of times the model will iterate over the entire training dataset during the learning process. Each iteration is called an epoch

  • prefactor – The prefactor parameter is a scaling factor that is applied to the movement loss during training. It allows you to control the relative importance of the movement loss compared to other losses or metrics. By adjusting the value of prefactor, you can increase or decrease the impact of the movement loss on

  • jac_prefactor – The jac_prefactor parameter is used as a scaling factor for the regularization term in the loss function. It determines the relative importance of the regularization term compared to the movement term in the loss function. A higher value of jac_prefactor will give more weight to the regularization term, while

  • scheme – The “scheme” parameter is a string that specifies the numerical scheme used for solving the equations. It can take one of the following values:, defaults to IMR (optional)

class learn.LearnerIMR(model, batch_size=20, dt=0.1, neurons=64, layers=2, name='.', cuda=False, dissipative=False)

Bases: Learner

mov_loss_without(zn_tensor, zn2_tensor, mid_tensor)

The function calculates the loss for a given input tensor by computing the energy, and then combining them with other tensors.

Parameters:
  • zn_tensor – The zn_tensor parameter is a tensor representing the current state of the system

  • zn2_tensor – The zn2_tensor parameter is a tensor representing the state at time t-2*dt

  • mid_tensor – The mid_tensor parameter is a tensor representing the intermediate state of the system. It is used to compute various quantities such as energy (En), and the gradient of energy (E_z) with respect to mid_tensor. These quantities

Returns:

returns (zn_tensor - zn2_tensor)/self.dt + ham.

mov_loss_without_with_jacobi(zn_tensor, zn2_tensor, mid_tensor, reduced_L)

The function calculates the moving loss and Jacobi loss for given tensors.

Parameters:
  • zn_tensor – The zn_tensor is a tensor representing the current state of the system. It is used as input to calculate the mov_loss and jacobi_loss

  • zn2_tensor – The zn2_tensor parameter is a tensor representing the second frame of a video sequence

  • mid_tensor – The mid_tensor parameter is a tensor that represents the intermediate state of the model during training. It is typically used to calculate losses or perform other operations

  • reduced_L – The parameter “reduced_L” is a reduced version of the L tensor. It is used in the calculation of the Jacobi loss

Returns:

two values: mov_loss and jacobi_loss.

mov_loss_soft(zn_tensor, zn2_tensor, mid_tensor, reduced_L)

The function calculates the moving loss and Jacobi loss for a given set of tensors.

Parameters:
  • zn_tensor – The zn_tensor is a tensor representing the first set of input data for the mov_loss_soft function

  • zn2_tensor – The zn2_tensor parameter is a tensor representing the second zero-normalized tensor

  • mid_tensor – The mid_tensor parameter is a tensor that represents the intermediate output of a neural network model. It is used as input to calculate the moving loss and Jacobi loss

  • reduced_L – The parameter “reduced_L” is a reduced version of the L tensor. It is used in the calculation of the Jacobi loss

Returns:

two values: mov_loss and jacobi_loss.

mov_loss_implicit(zn_tensor, zn2_tensor, mid_tensor)

The function calculates the implicit loss for a given input tensor.

Parameters:
  • zn_tensor – The zn_tensor parameter represents the current state of the system at time n

  • zn2_tensor – The zn2_tensor parameter is a tensor representing the state at time t - dt

  • mid_tensor – The mid_tensor parameter represents the input tensor for which the energy and Jacobian vectors are calculated

Returns:

the result of the expression (zn_tensor - zn2_tensor)/self.dt + torch.cross(Jz, E_z, dim=1).

learn.check_folder(name)

The function check_folder checks if the specified folder exists, and if not, creates it along with two subfolders named “data” and “saved_models”.

Parameters:

name – The name parameter is the name of the folder that you want to check and create if it doesn’t exist