Skip to content

Base

Contains the base class for the Dataset (1 and 2 dimensional) and a function that takes a Pytorch tensor and converts it to a numpy array

Dataset

This class automatically generates all the necessary proporties of a predefined data set with a single spatial dimension as input. In particular it calculates the solution, the time derivative and the library. Note that all the pytorch opperations such as automatic differentiation can be used on the results.

__init__(self, solution, **kwargs) special

Create a dataset and add a solution (data) to it

Parameters:

Name Type Description Default
solution

give the solution, the actual dataset u

required
parameters

additional parameters in keyword format

required
Source code in deepymod/data/base.py
def __init__(self, solution, **kwargs):
    """ Create a dataset and add a solution (data) to it
        Args:
            solution: give the solution, the actual dataset u
            parameters: additional parameters in keyword format
    """
    self.solution = solution  # set solution
    self.parameters = kwargs  # set solution parameters
    self.scaling_factor = None

create_dataset(self, x, t, n_samples, noise, random=True, normalize=True, return_idx=False, random_state=42)

Function creates the data set in the precise format used by DeepMoD

Parameters:

Name Type Description Default
x Tensor

Input vector of spatial coordinates

required
t Tensor

Input vector of temporal coordinates

required
n_samples int

Number of samples, set n_samples=0 for all.

required
noise float

Noise level in percentage of std.

required
random bool

When true, data set is randomised. Defaults to True.

True
normalize bool

When true, data set is normalized. Defaults to True.

True
return_idx bool

When true, the id of the data, before randomizing is returned. Defaults to False.

False
random_state int

Seed of the randomisation. Defaults to 42.

42

Returns:

Type Description
[type]

Tensor containing the input and output and optionally the randomisation.

Source code in deepymod/data/base.py
def create_dataset(self, x, t, n_samples, noise, random=True, normalize=True, return_idx=False, random_state=42):
    """Function creates the data set in the precise format used by DeepMoD

    Args:
        x (Tensor): Input vector of spatial coordinates
        t (Tensor): Input vector of temporal coordinates
        n_samples (int): Number of samples, set n_samples=0 for all.
        noise (float): Noise level in percentage of std.
        random (bool, optional): When true, data set is randomised. Defaults to True.
        normalize (bool, optional): When true, data set is normalized. Defaults to True.
        return_idx (bool, optional): When true, the id of the data, before randomizing is returned. Defaults to False.
        random_state (int, optional): Seed of the randomisation. Defaults to 42.

    Returns:
        [type]: Tensor containing the input and output and optionally the randomisation.
    """
    assert ((x.shape[1] == 1) & (t.shape[1] == 1)), 'x and t should have shape (n_samples x 1)'
    u = self.generate_solution(x, t)

    X = np.concatenate([t, x], axis=1)
    if random_state is None:
        y = u + noise * np.std(u, axis=0) * np.random.normal(size=u.shape)
    else:
        y = u + noise * np.std(u, axis=0) *  np.random.RandomState(seed=random_state).normal(size=u.shape)


    # creating random idx for samples
    N = y.shape[0] if n_samples == 0 else n_samples

    if random is True:
        if random_state is None:
            rand_idx = np.random.permutation(y.shape[0])[:N]
        else:
            rand_idx = np.random.RandomState(seed=random_state).permutation(y.shape[0])[:N]
    else:
        rand_idx = np.arange(y.shape[0])[:N]

    # Normalizing
    if normalize:
        if (self.scaling_factor is None):
            self.scaling_factor = (-(np.max(X, axis=0) + np.min(X, axis=0))/2, (np.max(X, axis=0) - np.min(X, axis=0))/2) # only calculate the first time
        X = (X + self.scaling_factor[0]) / self.scaling_factor[1] 

    # Building dataset
    X_train = torch.tensor(X[rand_idx, :], dtype=torch.float32)
    y_train = torch.tensor(y[rand_idx, :], dtype=torch.float32)

    if return_idx is False:
        return X_train, y_train
    else:
        return X_train, y_train, rand_idx

generate_solution(self, *args, **kwargs)

Evaluate function, Assign arugments and keyword arguments to a Pytorch function and return it as a numpy array.

Args: args: argument *kwargs: keyword arguments Return (np.array): output of function Tensor converted to numpy array

Source code in deepymod/data/base.py
def wrapper(self, *args, **kwargs):
    """ Evaluate function, Assign arugments and keyword arguments to a
     Pytorch function and return it as a numpy array.

    Args: 
        *args: argument
        **kwargs: keyword arguments
    Return
        (np.array): output of function Tensor converted to numpy array"""
    torch_args = [torch.tensor(arg, requires_grad=True, dtype=torch.float64) if type(arg) is ndarray else arg for arg in args]
    torch_kwargs = {key: torch.tensor(kwarg, requires_grad=True, dtype=torch.float64) if type(kwarg) is ndarray else kwarg for key, kwarg in kwargs.items()}
    result = function(self, *torch_args, **torch_kwargs)
    return result.cpu().detach().numpy()

library(self, *args, **kwargs)

Evaluate function, Assign arugments and keyword arguments to a Pytorch function and return it as a numpy array.

Args: args: argument *kwargs: keyword arguments Return (np.array): output of function Tensor converted to numpy array

Source code in deepymod/data/base.py
def wrapper(self, *args, **kwargs):
    """ Evaluate function, Assign arugments and keyword arguments to a
     Pytorch function and return it as a numpy array.

    Args: 
        *args: argument
        **kwargs: keyword arguments
    Return
        (np.array): output of function Tensor converted to numpy array"""
    torch_args = [torch.tensor(arg, requires_grad=True, dtype=torch.float64) if type(arg) is ndarray else arg for arg in args]
    torch_kwargs = {key: torch.tensor(kwarg, requires_grad=True, dtype=torch.float64) if type(kwarg) is ndarray else kwarg for key, kwarg in kwargs.items()}
    result = function(self, *torch_args, **torch_kwargs)
    return result.cpu().detach().numpy()

time_deriv(self, *args, **kwargs)

Evaluate function, Assign arugments and keyword arguments to a Pytorch function and return it as a numpy array.

Args: args: argument *kwargs: keyword arguments Return (np.array): output of function Tensor converted to numpy array

Source code in deepymod/data/base.py
def wrapper(self, *args, **kwargs):
    """ Evaluate function, Assign arugments and keyword arguments to a
     Pytorch function and return it as a numpy array.

    Args: 
        *args: argument
        **kwargs: keyword arguments
    Return
        (np.array): output of function Tensor converted to numpy array"""
    torch_args = [torch.tensor(arg, requires_grad=True, dtype=torch.float64) if type(arg) is ndarray else arg for arg in args]
    torch_kwargs = {key: torch.tensor(kwarg, requires_grad=True, dtype=torch.float64) if type(kwarg) is ndarray else kwarg for key, kwarg in kwargs.items()}
    result = function(self, *torch_args, **torch_kwargs)
    return result.cpu().detach().numpy()

Dataset_2D

This class automatically generates all the necessary proporties of a predifined data set with two spatial dimension as input. In particular it calculates the solution, the time derivative and the library. Note that all the pytorch opperations such as automatic differentiation can be used on the results.

__init__(self, solution, **kwargs) special

Create a 2D dataset and add a solution (data) to it

Parameters:

Name Type Description Default
solution

give the solution, the actual dataset u

required
parameters

additional parameters in keyword format

required
Source code in deepymod/data/base.py
def __init__(self, solution, **kwargs):
    """ Create a 2D dataset and add a solution (data) to it
        Args:
            solution: give the solution, the actual dataset u
            parameters: additional parameters in keyword format
    """
    self.solution = solution  # set solution
    self.parameters = kwargs  # set solution parameters

create_dataset(self, x, t, n_samples, noise, random=True, return_idx=False, random_state=42)

Function creates the data set in the precise format used by DeepMoD

Parameters:

Name Type Description Default
x Tensor

Input vector of spatial coordinates

required
t Tensor

Input vector of temporal coordinates

required
n_samples int

Number of samples, set n_samples=0 for all.

required
noise float

Noise level in percentage of std.

required
random bool

When true, data set is randomised. Defaults to True.

True
normalize bool

When true, data set is normalized. Defaults to True.

required
return_idx bool

When true, the id of the data, before randomizing is returned. Defaults to False.

False
random_state int

Seed of the randomisation. Defaults to 42.

42

Returns:

Type Description
[type]

Tensor containing the input and output and optionally the randomisation.

Source code in deepymod/data/base.py
def create_dataset(self, x, t, n_samples, noise, random=True, return_idx=False, random_state=42):
    """Function creates the data set in the precise format used by DeepMoD

    Args:
        x (Tensor): Input vector of spatial coordinates
        t (Tensor): Input vector of temporal coordinates
        n_samples (int): Number of samples, set n_samples=0 for all.
        noise (float): Noise level in percentage of std.
        random (bool, optional): When true, data set is randomised. Defaults to True.
        normalize (bool, optional): When true, data set is normalized. Defaults to True.
        return_idx (bool, optional): When true, the id of the data, before randomizing is returned. Defaults to False.
        random_state (int, optional): Seed of the randomisation. Defaults to 42.

    Returns:
        [type]: Tensor containing the input and output and optionally the randomisation.
    """
    assert ((x.shape[1] == 2) & (t.shape[1] == 1)), 'x and t should have shape (n_samples x 1)'
    u = self.generate_solution(x, t)

    X = np.concatenate([t, x], axis=1)
    y = u + noise * np.std(u, axis=0) * np.random.normal(size=u.shape)

    # creating random idx for samples
    N = y.shape[0] if n_samples == 0 else n_samples

    if random is True:
        rand_idx = np.random.RandomState(seed=random_state).permutation(y.shape[0])[:N] # so we can get similar splits for different noise levels
    else:
        rand_idx = np.arange(y.shape[0])[:N]

    # Building dataset
    X_train = torch.tensor(X[rand_idx, :], requires_grad=True, dtype=torch.float32)
    y_train = torch.tensor(y[rand_idx, :], requires_grad=True, dtype=torch.float32)

    if return_idx is False:
        return X_train, y_train
    else:
        return X_train, y_train, rand_idx

generate_solution(self, *args, **kwargs)

Evaluate function, Assign arugments and keyword arguments to a Pytorch function and return it as a numpy array.

Args: args: argument *kwargs: keyword arguments Return (np.array): output of function Tensor converted to numpy array

Source code in deepymod/data/base.py
def wrapper(self, *args, **kwargs):
    """ Evaluate function, Assign arugments and keyword arguments to a
     Pytorch function and return it as a numpy array.

    Args: 
        *args: argument
        **kwargs: keyword arguments
    Return
        (np.array): output of function Tensor converted to numpy array"""
    torch_args = [torch.tensor(arg, requires_grad=True, dtype=torch.float64) if type(arg) is ndarray else arg for arg in args]
    torch_kwargs = {key: torch.tensor(kwarg, requires_grad=True, dtype=torch.float64) if type(kwarg) is ndarray else kwarg for key, kwarg in kwargs.items()}
    result = function(self, *torch_args, **torch_kwargs)
    return result.cpu().detach().numpy()

library(self, *args, **kwargs)

Evaluate function, Assign arugments and keyword arguments to a Pytorch function and return it as a numpy array.

Args: args: argument *kwargs: keyword arguments Return (np.array): output of function Tensor converted to numpy array

Source code in deepymod/data/base.py
def wrapper(self, *args, **kwargs):
    """ Evaluate function, Assign arugments and keyword arguments to a
     Pytorch function and return it as a numpy array.

    Args: 
        *args: argument
        **kwargs: keyword arguments
    Return
        (np.array): output of function Tensor converted to numpy array"""
    torch_args = [torch.tensor(arg, requires_grad=True, dtype=torch.float64) if type(arg) is ndarray else arg for arg in args]
    torch_kwargs = {key: torch.tensor(kwarg, requires_grad=True, dtype=torch.float64) if type(kwarg) is ndarray else kwarg for key, kwarg in kwargs.items()}
    result = function(self, *torch_args, **torch_kwargs)
    return result.cpu().detach().numpy()

time_deriv(self, *args, **kwargs)

Evaluate function, Assign arugments and keyword arguments to a Pytorch function and return it as a numpy array.

Args: args: argument *kwargs: keyword arguments Return (np.array): output of function Tensor converted to numpy array

Source code in deepymod/data/base.py
def wrapper(self, *args, **kwargs):
    """ Evaluate function, Assign arugments and keyword arguments to a
     Pytorch function and return it as a numpy array.

    Args: 
        *args: argument
        **kwargs: keyword arguments
    Return
        (np.array): output of function Tensor converted to numpy array"""
    torch_args = [torch.tensor(arg, requires_grad=True, dtype=torch.float64) if type(arg) is ndarray else arg for arg in args]
    torch_kwargs = {key: torch.tensor(kwarg, requires_grad=True, dtype=torch.float64) if type(kwarg) is ndarray else kwarg for key, kwarg in kwargs.items()}
    result = function(self, *torch_args, **torch_kwargs)
    return result.cpu().detach().numpy()

pytorch_func(function)

Decorator to automatically transform arrays to tensors and back

Parameters:

Name Type Description Default
function Tensor

Pytorch tensor

required

Returns:

Type Description
(wrapper)

function that can evaluate the Pytorch function for extra (keyword) arguments, returning (np.array)

Source code in deepymod/data/base.py
def pytorch_func(function):
    """Decorator to automatically transform arrays to tensors and back

    Args:
        function (Tensor): Pytorch tensor 

    Returns:
        (wrapper): function that can evaluate the Pytorch function for extra 
        (keyword) arguments, returning (np.array)
    """
    def wrapper(self, *args, **kwargs):
        """ Evaluate function, Assign arugments and keyword arguments to a
         Pytorch function and return it as a numpy array.

        Args: 
            *args: argument
            **kwargs: keyword arguments
        Return
            (np.array): output of function Tensor converted to numpy array"""
        torch_args = [torch.tensor(arg, requires_grad=True, dtype=torch.float64) if type(arg) is ndarray else arg for arg in args]
        torch_kwargs = {key: torch.tensor(kwarg, requires_grad=True, dtype=torch.float64) if type(kwarg) is ndarray else kwarg for key, kwarg in kwargs.items()}
        result = function(self, *torch_args, **torch_kwargs)
        return result.cpu().detach().numpy()
    return wrapper