Class nuTens::Tensorο
-
class Tensorο
Constructors
Use these methods to construct tensors
-
inline Tensor()ο
Default constructor with no initialisation.
-
Tensor(const std::vector<float> &values, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = true)ο
Construct a 1-d array with specified values.
values The values to include in the tensor
-
static Tensor eye(int n, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = true)ο
Construct an identity tensor (has to be a 2d square tensor)
n The size of one of the sides of the tensor
type The data type of the tensor
-
static Tensor rand(const std::vector<long int> &shape, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = true)ο
Construct a tensor with entries randomly initialised in the range [0, 1].
shape The desired shape of the intitalised tensor
type The data type of the tensor
-
static Tensor diag(const Tensor &diag)ο
Construct a tensor diag values along the diagonal, and zero elsewhere.
diag A 1-d tensor which represents the desired diagonal values
-
static Tensor ones(const std::vector<long int> &shape, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = true)ο
Construct a tensor with ones.
shape The desired shape of the intitalised tensor
type The data type of the tensor
-
static Tensor zeros(const std::vector<long int> &shape, dtypes::scalarType type = dtypes::kFloat, dtypes::deviceType device = dtypes::kCPU, bool requiresGrad = true)ο
Construct a tensor with zeros.
shape The desired shape of the intitalised tensor
type The data type of the tensor
Setters
Set the underlying data type of this tensor
-
Tensor &dType(dtypes::scalarType type)ο
-
Tensor &device(dtypes::deviceType device)ο
Set the device that this tensor lives on.
Matrix Arithmetic
Generally there are static functions with the pattern <function>(Mat1, Mat2) which will return a new matrix and inline equivalents with the pattern <function>_(Mat2) which will affect the object they are called by
-
void matmul_(const Tensor &t2)ο
Inline matrix multiplication.
t2 Right hand matrix to multiply with this one
-
void scale_(float s)ο
Inline matrix scaling.
s The scalar
-
void scale_(std::complex<float> s)ο
Inline complex matrix scaling.
s The scalar
-
void pow_(float s)ο
Inline raise to scalar power.
s The scalar
-
void pow_(std::complex<float> s)ο
Inline raise to scalar power.
s The scalar
-
void exp_()ο
Inline element-wise exponential.
-
void transpose_(int dim1, int dim2)ο
Inline transpose.
dim1 The first dimension to swap
dim2 The second dimension to swap
-
static Tensor matmul(const Tensor &t1, const Tensor &t2)ο
Multiply two matrices together.
t1 Left hand tensor
t2 Right hand tensor
-
static Tensor outer(const Tensor &t1, const Tensor &t2)ο
Outer product of two 1D tensors.
t1 Left hand tensor
t2 Right hand tensor
-
static Tensor mul(const Tensor &t1, const Tensor &t2)ο
Element-wise multiplication of two tensors.
t1 Left hand tensor
t2 Right hand tensor
-
static Tensor div(const Tensor &t1, const Tensor &t2)ο
Element-wise division of two tensors.
t1 Numerator
t2 Denominator
-
static Tensor pow(const Tensor &t, float s)ο
Raise a matrix to a scalar power.
t The tensor
s The scalar
-
static Tensor pow(const Tensor &t, std::complex<float> s)ο
Raise a matrix to a scalar power.
t The tensor
s The scalar
-
static Tensor transpose(const Tensor &t, int dim1, int dim2)ο
Get the transpose of a tensor.
t The tensor
dim1 The first dimension to swap
dim2 The second dimension to swap
-
static Tensor scale(const Tensor &t, float s)ο
Scale a matrix by some scalar.
s The scalar
t The tensor
-
static Tensor scale(const Tensor &t, double s)ο
Scale a matrix by some scalar.
s The scalar
t The tensor
Mathematical
mathematical function overrides, generally work as expected, unless otherwise noted
Gradients
-
void backward() constο
Compute gradients of this tensor with respect to leaves Those can then be accessed using gradient()
-
Tensor grad() constο
Return a tensor containing the accumulated gradients calculated for this tensor after calling backward()
Linear Algebra
-
static void eig(const Tensor &t, Tensor &eVals, Tensor &eVecs)ο
Get eigenvalues and vectors of a tensor.
t The tensor
- Parameters:
eVals β [out] The eigenvalues
eVecs β [out] The eigenvectors
-
static void eigh(const Tensor &t, Tensor &eVals, Tensor &eVecs)ο
Get eigenvalues and vectors of a hermitian matrix.
t The tensor
- Parameters:
eVals β [out] The eigenvalues
eVecs β [out] The eigenvectors This is in general faster and more stable than Tensor::eig and should be preferred in basically all cases where it can be used
-
static void eigvals(const Tensor &t, Tensor &eVals)ο
Get eigenvalues of a tensor.
t The tensor
- Parameters:
eVals β [out] The eigenvalues
-
static void eigvalsh(const Tensor &t, Tensor &eVals)ο
Get eigenvalues of a hermitian matrix.
t The tensor
- Parameters:
eVals β [out] The eigenvalues This is in general faster and more stable than Tensor::eigvals and should be preferred in basically all cases where it can be used
Trigonometric
Public Types
-
using indexType = std::variant<int, std::string>ο
Holds the possible βindexβ types, this allows us to pass integers OR strings as index values which allows us to do some basic slicing of tensors similar to python
-
using variantType = std::variant<int, float, double, std::complex<float>, std::complex<double>>ο
Container that holds all allowed types that can be returned by a tensor.
Public Functions
-
Tensor &addBatchDim()ο
If the tensor does not already have a batch dimension (as set by hasBatchDim()) this will add one.
-
Tensor conj() constο
Get the complex conjugate of this tensor. If the underlying tensor is not complex, this will just return the tensor.
-
Tensor cumsum(int dim) constο
Get the cumulative sum over some dimension.
- Parameters:
dim β The dimension to sum over
-
Tensor sum(const std::vector<long int> &dims) constο
Get the result of summing this tensor over all dimensions.
- Parameters:
dims β The dimensions to sum over
-
std::string toString() constο
Print this object to a summary string.
-
void setValue(const Tensor &indices, const Tensor &value)ο
Set the value at a particular index of the tensor.
indices The indices of the value to set
value The value to set it to
-
void setValue(const std::vector<int> &indices, float value)ο
-
void setValue(const std::vector<int> &indices, double value)ο
-
void setValue(const std::vector<int> &indices, std::complex<float> value)ο
-
void setValue(const std::vector<int> &indices, std::complex<double> value)ο
-
Tensor getValues(const std::vector<indexType> &indices) constο
Get the value at a certain entry in the tensor.
- Parameters:
indices β The index of the entry to get
-
variantType getVariantValue(const std::vector<int> &indices) constο
Get the value at a certain entry in the tensor as an std::variant.
This mainly exists so we can get the values of a tensor in python as pybind11 DOES NOT like templated functions If using the c++ interface it is probably easier, faster and safer to use the templated getValue() function.
-
size_t getNdim() constο
Get the number of dimensions in the tensor.
-
int getBatchDim() constο
Get the size of the batch dimension of the tensor.
-
bool getHasBatchDim() constο
Check if tensor has batch dimension prepended.
-
std::vector<int> getShape() constο
Get the shape of the tensor.
Public Static Functions
-
static inline Tensor cumsum(const Tensor &t, int dim)ο
Get the cumulative sum over some dimension.
- Parameters:
dim β The dimension to sum over
-
static inline Tensor sum(const Tensor &t)ο
Get the result of summing this tensor over all dimensions.
-
static inline Tensor sum(const Tensor &t, const std::vector<long int> &dims)ο
Get the result of summing this tensor over all dimensions.
- Parameters:
dims β The dimensions to sum over
-
static std::string getTensorLibrary()ο
Get the name of the backend library used to deal with tensors.
-
inline Tensor()ο