TensorOperator {BKTR}R Documentation

R6 singleton that contains the configuration for the tensor backend

Description

Tensor backend configuration and methods for all the tensor operations in BKTR

Super class

R6P::Singleton -> TensorOperator

Public fields

fp_type

The floating point type to use for the tensor operations

fp_device

The device to use for the tensor operations

Methods

Public methods


Method new()

Initialize the tensor operator with the given floating point type and device

Usage
TensorOperator$new(fp_type = "float64", fp_device = "cpu")
Arguments
fp_type

The floating point type to use for the tensor operations (either "float64" or "float32")

fp_device

The device to use for the tensor operations (either "cpu" or "cuda")

Returns

A new tensor operator instance


Method set_params()

Set the tensor operator parameters

Usage
TensorOperator$set_params(fp_type = NULL, fp_device = NULL, seed = NULL)
Arguments
fp_type

The floating point type to use for the tensor operations (either "float64" or "float32")

fp_device

The device to use for the tensor operations (either "cpu" or "cuda")

seed

The seed to use for the random number generator


Method get_default_jitter()

Get the default jitter value for the floating point type used by the tensor operator

Usage
TensorOperator$get_default_jitter()
Returns

The default jitter value for the floating point type used by the tensor operator


Method tensor()

Create a tensor from a vector or matrix of data with the tensor operator dtype and device

Usage
TensorOperator$tensor(tensor_data)
Arguments
tensor_data

The vector or matrix of data to create the tensor from

Returns

A new tensor with the tensor operator dtype and device


Method is_tensor()

Check if a provided object is a tensor

Usage
TensorOperator$is_tensor(tensor)
Arguments
tensor

The object to check

Returns

A boolean indicating if the object is a tensor


Method eye()

Create a tensor with a diagonal of ones and zeros with the tensor operator dtype and device for a given dimension

Usage
TensorOperator$eye(eye_dim)
Arguments
eye_dim

The dimension of the tensor to create

Returns

A new tensor with a diagonal of ones and zeros with the tensor operator dtype and device


Method ones()

Create a tensor of ones with the tensor operator dtype and device for a given dimension

Usage
TensorOperator$ones(tsr_dim)
Arguments
tsr_dim

The dimension of the tensor to create

Returns

A new tensor of ones with the tensor operator dtype and device


Method zeros()

Create a tensor of zeros with the tensor operator dtype and device for a given dimension

Usage
TensorOperator$zeros(tsr_dim)
Arguments
tsr_dim

The dimension of the tensor to create

Returns

A new tensor of zeros with the tensor operator dtype and device


Method rand()

Create a tensor of random uniform values with the tensor operator dtype and device for a given dimension

Usage
TensorOperator$rand(tsr_dim)
Arguments
tsr_dim

The dimension of the tensor to create

Returns

A new tensor of random values with the tensor operator dtype and device


Method randn()

Create a tensor of random normal values with the tensor operator dtype and device for a given dimension

Usage
TensorOperator$randn(tsr_dim)
Arguments
tsr_dim

The dimension of the tensor to create

Returns

A new tensor of random normal values with the tensor operator dtype and device


Method randn_like()

Create a tensor of random uniform values with the same shape as a given tensor with the tensor operator dtype and device

Usage
TensorOperator$randn_like(input_tensor)
Arguments
input_tensor

The tensor to use as a shape reference

Returns

A new tensor of random uniform values with the same shape as a given tensor


Method arange()

Create a tensor of a range of values with the tensor operator dtype and device for a given start and end

Usage
TensorOperator$arange(start, end)
Arguments
start

The start of the range

end

The end of the range

Returns

A new tensor of a range of values with the tensor operator dtype and device


Method rand_choice()

Choose random values from a tensor for a given number of samples

Usage
TensorOperator$rand_choice(
  choices_tsr,
  nb_sample,
  use_replace = FALSE,
  weights_tsr = NULL
)
Arguments
choices_tsr

The tensor to choose values from

nb_sample

The number of samples to choose

use_replace

A boolean indicating if the sampling should be done with replacement. Defaults to FALSE

weights_tsr

The weights to use for the sampling. If NULL, the sampling is uniform. Defaults to NULL

Returns

A new tensor of randomly chosen values from a tensor


Method kronecker_prod()

Efficiently compute the kronecker product of two matrices in tensor format

Usage
TensorOperator$kronecker_prod(a, b)
Arguments
a

The first tensor

b

The second tensor

Returns

The kronecker product of the two matrices


Method khatri_rao_prod()

Efficiently compute the khatri rao product of two matrices in tensor format having the same number of columns

Usage
TensorOperator$khatri_rao_prod(a, b)
Arguments
a

The first tensor

b

The second tensor

Returns

The khatri rao product of the two matrices


Method clone()

The objects of this class are cloneable with this method.

Usage
TensorOperator$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


# Set the seed, setup the tensor floating point type and device
TSR$set_params(fp_type='float64', fp_device='cpu', seed=42)
# Create a tensor from a vector
TSR$tensor(c(1, 2, 3))
# Create a tensor from a matrix
TSR$tensor(matrix(c(1, 2, 3, 4), nrow=2))
# Create a 3x3 tensor with a diagonal of ones and zeros elsewhere
TSR$eye(3)
# Create a tensor of ones (with 6 elements, 2 rows and 3 columns)
TSR$ones(c(2, 3))
# Create a tensor of zeros (with 12 elements, 3 rows and 4 columns)
TSR$zeros(c(3, 4))
# Create a tensor of random uniform values (with 6 elements)
TSR$rand(c(2, 3))
# Create a tensor of random normal values (with 6 elements)
TSR$randn(c(2, 3))
# Create a tensor of random normal values with the same shape as a given tensor
tsr_a <- TSR$randn(c(2, 3))
TSR$randn_like(tsr_a)
# Create a tensor of a range of values (1, 2, 3, 4)
TSR$arange(1, 4)
# Choose two random values from a given tensor without replacement
tsr_b <- TSR$rand(6)
TSR$rand_choice(tsr_b, 2)
# Use the tensor operator to compute the kronecker product of two 2x2 matrices
tsr_c <- TSR$tensor(matrix(c(1, 2, 3, 4), nrow=2))
tsr_d <- TSR$tensor(matrix(c(5, 6, 7, 8), nrow=2))
TSR$kronecker_prod(tsr_c, tsr_d) # Returns a 4x4 tensor
# Use the tensor operator to compute the khatri rao product of two 2x2 matrices
TSR$khatri_rao_prod(tsr_c, tsr_d) # Returns a 4x2 tensor
# Check if a given object is a tensor
TSR$is_tensor(tsr_d) # Returns TRUE
TSR$is_tensor(TSR$eye(2)) # Returns TRUE
TSR$is_tensor(1) # Returns FALSE


[Package BKTR version 0.1.1 Index]