deepxde.backend package

Subpackages

Submodules

deepxde.backend.backend module

This file defines the unified tensor framework interface required by DeepXDE.

The principles of this interface: * There should be as few interfaces as possible. * The interface is used by DeepXDE system so it is more important to have

clean definition rather than convenient usage.

  • Default arguments should be avoided.

  • Keyword or positional arguments should be avoided.

  • Argument type should be easier to understand.

It is recommended the frameworks implement all the interfaces. However, it is also OK to skip some. The generated backend module has an is_enabled function that returns whether the interface is supported by the framework or not.

deepxde.backend.backend.Variable(initial_value, dtype=None)[source]

Return a trainable variable.

Parameters:
  • initial_value – The initial value of the variable.

  • dtype – The desired data type of returned tensor. Default: if None, infers data type from data.

deepxde.backend.backend.abs(x)[source]

Computes the absolute value element-wise.

deepxde.backend.backend.as_tensor(data, dtype=None)[source]

Convert the data to a Tensor.

If the data is already a tensor and has the same dtype, directly return.

Parameters:
  • object (data. Tensor)

  • array (numpy)

  • list (Python)

  • scalar. (and Python)

  • dtype (data type, optional) – If None, infers data type from data.

Returns:

Tensor. A framework-specific tensor.

deepxde.backend.backend.concat(values, axis)[source]

Returns the concatenation of the input tensors along the given dim.

Parameters:
  • values (list or tuple of Tensor)

  • axis (int)

Returns:

Concatenated tensor.

Return type:

Tensor

deepxde.backend.backend.cos(x)[source]

Computes cosine of x element-wise.

deepxde.backend.backend.data_type_dict()[source]

Returns a dictionary from data type string to the data type.

The dictionary should include at least: float16 float32 float64 uint8 int8 int16 int32 int64 bool

This function will be called only once during the initialization of the backend module. The returned dictionary will become the attributes of the backend module.

Examples

>>> import tensorflow as tf
>>> def data_type_dict():
>>>     return {'float16': tf.float16, 'float32': tf.float32, ...}

After the module is initialized.

>>> import backend as bkd
>>> bkd.float16  # this will point to tf.float16
Returns:

dict of str to data type. The data type dict.

deepxde.backend.backend.elu(x)[source]

Computes the exponential linear function.

deepxde.backend.backend.exp(x)[source]

Computes exponential of x element-wise.

deepxde.backend.backend.expand_dims(tensor, axis)[source]

Expand dim for tensor along given axis.

Parameters:
  • tensor (Tensor)

  • axis (int)

Returns:

Expanded tensor.

Return type:

Tensor

deepxde.backend.backend.from_numpy(np_array)[source]

Create a tensor that shares the underlying numpy array memory, if possible.

Parameters:

np_array (numpy.ndarray)

Returns:

Tensor. A framework-specific tensor.

deepxde.backend.backend.gelu(x)[source]

Computes Gaussian Error Linear Unit function.

deepxde.backend.backend.is_gpu_available()[source]

Returns a bool indicating if GPU is currently available.

Returns:

True if a GPU device is available.

deepxde.backend.backend.is_tensor(obj)[source]

Returns True if obj is a backend-native type tensor.

deepxde.backend.backend.lgamma(x)[source]

Computes the natural logarithm of the absolute value of the gamma function of x element-wise.

deepxde.backend.backend.matmul(x, y)[source]

Compute matrix multiplication for two matrices x and y.

Parameters:
  • x (Tensor)

  • y (Tensor)

Returns:

The multiplication result.

Return type:

Tensor

deepxde.backend.backend.max(input_tensor, dim, keepdims=False)[source]

Returns the maximum of the input tensor along the given dim.

Parameters:
  • input_tensor (Tensor)

  • dim (int)

  • keepdims (bool)

Returns:

A framework-specific tensor.

Return type:

Tensor

deepxde.backend.backend.mean(input_tensor, dim, keepdims=False)[source]

Returns the mean value of the input tensor in the given dimension dim.

deepxde.backend.backend.min(input_tensor, dim, keepdims=False)[source]

Returns the minimum of the input tensor along the given dim.

Parameters:
  • input_tensor (Tensor)

  • dim (int)

  • keepdims (bool)

Returns:

A framework-specific tensor.

Return type:

Tensor

deepxde.backend.backend.minimum(x, y)[source]

Returns the minimum of x and y (i.e. x < y ? x : y) element-wise.

deepxde.backend.backend.ndim(input_tensor)[source]

Returns the number of dimensions of the tensor.

Parameters:

input (Tensor) – The input tensor.

Returns:

The number of dimensions.

Return type:

int

deepxde.backend.backend.norm(tensor, ord=None, axis=None, keepdims=False)[source]

Computes a vector norm.

Due to the incompatibility of different backends, only some vector norms are supported, and matrix norm is not supported now. This API follows numpy.linalg.norm().

Parameters:
  • tensor (Tensor)

  • ord (int, float, inf) – 1, 2, inf, and any positive real number. Default is 2-norm for vectors.

  • axis (int) – to compute the vector norms. If axis is None, then a vector norm (when tensor is 1-D) is returned. The default is None.

  • keepdims (bool) – in the result as dimensions with size one.

Returns:

Norm of the vector(s).

Return type:

Tensor

deepxde.backend.backend.pow(x, y)[source]

Computes the power of one value to another: x ^ y.

deepxde.backend.backend.prod(input_tensor, dim, keepdims=False)[source]

Returns the product of the input tensor along the given dim.

Parameters:
  • input_tensor (Tensor)

  • dim (int)

  • keepdims (bool)

Returns:

A framework-specific tensor.

Return type:

Tensor

deepxde.backend.backend.reduce_max(input_tensor)[source]

Returns the maximum of all elements in the input tensor.

Parameters:

input_tensor (Tensor)

Returns:

Tensor.

deepxde.backend.backend.reduce_mean(input_tensor)[source]

Returns the mean value of all elements in the input tensor.

deepxde.backend.backend.reduce_min(input_tensor)[source]

Returns the minimum of all elements in the input tensor.

Parameters:

input_tensor (Tensor)

Returns:

Tensor.

deepxde.backend.backend.reduce_prod(input_tensor)[source]

Returns the product of all elements in the input tensor.

Parameters:

input_tensor (Tensor)

Returns:

Tensor.

deepxde.backend.backend.reduce_sum(input_tensor)[source]

Returns the sum of all elements in the input tensor.

Parameters:

input_tensor (Tensor)

Returns:

Tensor.

deepxde.backend.backend.relu(x)[source]

Applies the rectified linear unit activation function.

deepxde.backend.backend.reshape(tensor, shape)[source]

Gives a new shape to a tensor without changing its data.

Parameters:
  • tensor (Tensor) – The tensor to be reshaped.

  • shape (tuple of ints) – The new shape should be compatible with the original shape.

Returns:

Reshaped tensor. This will be a new view object if possible.

deepxde.backend.backend.reverse(tensor, axis)[source]

Reverse the order of elements along the given axis.

Parameters:
  • tensor (Tensor)

  • axis (int)

Returns:

Tensor which is flipped along given axis.

Return type:

Tensor

deepxde.backend.backend.roll(tensor, shift, axis)[source]

Roll the tensor along the given axis (axes).

Parameters:
  • tensor (Tensor)

  • shift (int or tuple of ints) – tensor are shifted.

  • axis (int or tuple of ints). Axis (axes)

Returns:

Rolled tensor.

Return type:

Tensor

deepxde.backend.backend.selu(x)[source]

Computes scaled exponential linear.

deepxde.backend.backend.shape(input_tensor)[source]

Return the shape of the tensor.

Parameters:

input (Tensor) – The input tensor.

Returns:

The tensor shape.

Return type:

tuple or list of ints

deepxde.backend.backend.sigmoid(x)[source]

Computes sigmoid of x element-wise.

deepxde.backend.backend.silu(x)[source]

Sigmoid Linear Unit (SiLU) function, also known as the swish function. silu(x) = x * sigmoid(x).

deepxde.backend.backend.sin(x)[source]

Computes sine of x element-wise.

deepxde.backend.backend.size(input_tensor)[source]

Return the total number of elements in the input tensor.

Parameters:

input_tensor (Tensor)

Returns:

The total number of elements in the input tensor.

Return type:

int

deepxde.backend.backend.sparse_dense_matmul(x, y)[source]

Compute matrix multiplication of a sparse matrix x and a sparse/dense matrix y.

Parameters:
  • x (Sparse Tensor)

  • y (Sparse Tensor or Tensor)

Returns:

The multiplication result.

Return type:

Tensor

deepxde.backend.backend.sparse_tensor(indices, values, shape)[source]

Construct a sparse tensor based on given indices, values and shape.

Parameters:
  • indices (list of tuple) – the indices of the elements in the sparse tensor that contain nonzero values (elements are zero-indexed), such as [(x1, y1), (x2, y2), …, (xN, yN)].

  • values (Tensor)

  • shape (list or tuple)

Returns:

A sparse tensor.

Return type:

SparseTensor

deepxde.backend.backend.square(x)[source]

Returns the square of the elements of input.

deepxde.backend.backend.stack(values, axis)[source]

Returns the stack of the input tensors along the given dim.

Parameters:
  • values (list or tuple of Tensor)

  • axis (int)

Returns:

Stacked tensor.

Return type:

Tensor

deepxde.backend.backend.sum(input_tensor, dim, keepdims=False)[source]

Returns the sum of the input tensor along the given dim.

Parameters:
  • input_tensor (Tensor)

  • dim (int)

  • keepdims (bool)

Returns:

A framework-specific tensor.

Return type:

Tensor

deepxde.backend.backend.tanh(x)[source]

Computes hyperbolic tangent of x element-wise.

deepxde.backend.backend.to_numpy(input_tensor)[source]

Create a numpy ndarray that shares the same underlying storage, if possible.

Parameters:

input_tensor (Tensor)

Returns:

np_array (numpy.ndarray). The numpy ndarray.

deepxde.backend.backend.transpose(tensor, axes=None)[source]

Reverse or permute the axes of a tensor; returns the modified array.

For a tensor with two axes, transpose gives the matrix transpose.

Parameters:
  • tensor (Tensor) – Input tensor.

  • axes (tuple of ints) – A permutation of the dimensions.

Returns:

A tensor with its axes permuted. A view is returned whenever possible.

deepxde.backend.backend.zeros(shape, dtype)[source]

Creates a tensor with all elements set to zero.

Parameters:
  • shape (tuple of ints)

  • dtype (data type)

Returns:

Tensor. The zero tensor.

deepxde.backend.backend.zeros_like(input_tensor)[source]

Create a zero tensor with the same shape, dtype and context of the given tensor.

Parameters:

input_tensor (Tensor)

Returns:

The result.

Return type:

Tensor

deepxde.backend.set_default_backend module

deepxde.backend.set_default_backend.set_default_backend(backend_name)[source]

deepxde.backend.utils module

deepxde.backend.utils.check_avx(platform)[source]

Check whether avx is supported.

deepxde.backend.utils.generate_cmd(py_exec, platform, cuda_version=None, has_rocm=False)[source]

Generate command.

Parameters:
  • py_exec (str) – python executable path.

  • platform (str) – User’s platform.

  • cuda_version (str) – Whether cuda is avaliable and its version if it is.

  • has_rocm (bool) – Whether ROCm4.0 has been installed.

deepxde.backend.utils.get_available_backend()[source]
deepxde.backend.utils.get_cuda(platform)[source]

Check whether cuda is avaliable and get its version.

Returns:

cuda_verion (str) or None

deepxde.backend.utils.get_platform()[source]

Get user’s platform.

Returns:

“windows”, “linux” or “darwin”

Return type:

platform (str)

deepxde.backend.utils.get_python_executable()[source]

Get user’s python executable.

Returns:

python exection path

Return type:

str

deepxde.backend.utils.get_rocm()[source]

Check whether ROCm4.0 is avaliable.

Returns:

bool

deepxde.backend.utils.import_jax()[source]
deepxde.backend.utils.import_paddle()[source]
deepxde.backend.utils.import_pytorch()[source]
deepxde.backend.utils.import_tensorflow()[source]
deepxde.backend.utils.import_tensorflow_compat_v1()[source]
deepxde.backend.utils.install_paddle()[source]

Generate command and install paddle.

deepxde.backend.utils.interactive_install_paddle()[source]

Ask the user for installing paddle.

deepxde.backend.utils.run_install(command)[source]

Send command to terminal and print it.

Parameters:

command (str) – command to be sent to terminal.

deepxde.backend.utils.verify_backend(backend_name)[source]

Verify if the backend is available. If it is available, do nothing, otherwise, raise RuntimeError.

Module contents

deepxde.backend.backend_message(backend_name)[source]

Show message about backend.

Parameters:

backend_name – which backend used

deepxde.backend.gelu(*args, **kwargs)
deepxde.backend.get_preferred_backend()[source]
deepxde.backend.is_enabled(api)[source]

Return true if the api is enabled by the current backend.

Parameters:

api (string) – The api name.

Returns:

True if the API is enabled by the current backend.

Return type:

bool

deepxde.backend.jax(*args, **kwargs)
deepxde.backend.load_backend(mod_name)[source]
deepxde.backend.paddle(*args, **kwargs)
deepxde.backend.to_numpy(*args, **kwargs)
deepxde.backend.torch(*args, **kwargs)