Creation Routines
Constructors
Create an uninitialized tensor
Tensor* t1 = new Tensor();
// []
-
explicit Tensor::Tensor(const vector<int> &shape, int dev = DEV_CPU)
Construct of an uninitialized tensor.
- Parameters
shape – Vector of ints specifying the shape of the tensor
dev – One of
DEV_CPU
orDEV_GPU
- Returns
a tensor
Tensor* t1 = new Tensor({2, 3});
// [
// [0.00 -0.00 0.00]
// [-0.00 0.00 0.00]
// ]
-
Tensor::Tensor(const vector<float> &data, const vector<int> &shape, int dev = DEV_CPU)
Construct tensor with initial data.
- Parameters
data – Vector with the data to initialize the tensor with.
shape – Vector of ints specifying the shape of the tensor.
dev – One of
DEV_CPU
orDEV_GPU
- Returns
a tensor
Tensor* t1 = new Tensor({1,2,3,4,5,6}, {2,3});
// [
// [1.00 2.00 3.00]
// [4.00 5.00 6.00]
// ]
Constructors & Initializers
Create tensor from generators
empty
-
static Tensor *Tensor::empty(const vector<int> &shape, int dev = DEV_CPU)
Create a tensor of the specified shape filled with uninitialized data.
- Parameters
shape – Shape of the tensor to create.
dev – Device to use. The possible values are:
DEV_CPU
andDEV_GPU
.
- Returns
Tensor of the specified shape filled with zeros
Tensor* t1 = Tensor::empty({2, 3});
// [
// [0.00 -36893488147419103232.00 0.00]
// [-36893488147419103232.00 0.00 0.00]
// ]
empty_like
-
static Tensor *Tensor::empty_like(Tensor *A)
Create a tensor with the shape and device of another one, but empty.
- Parameters
A – Input tensor from wich to take shape and device.
- Returns
Empty initialized A-shaped tensor
Tensor* t1 = Tensor::empty({2, 3});
Tensor* t2 = Tensor::empty_like(t1);
// [
// [0.00 -36893488147419103232.00 0.00]
// [-36893488147419103232.00 0.00 0.00]
// ]
zeros
-
static Tensor *Tensor::zeros(const vector<int> &shape, int dev = DEV_CPU)
Create a tensor of the specified shape and filled with zeros.
- Parameters
shape – Shape of the tensor to create.
dev – Device to use. The possible values are:
DEV_CPU
andDEV_GPU
.
- Returns
Tensor of the specified shape filled with zeros
Tensor* t1 = Tensor::zeros({2, 3});
// [
// [0.00 0.00 0.00]
// [0.00 0.00 0.00]
// ]
zeros_like
-
static Tensor *Tensor::zeros_like(Tensor *A)
Create a tensor with the shape and device of another one, initialized with zeros.
- Parameters
A – Input tensor from wich to take shape and device.
- Returns
Zeros initialized A-shaped tensor
Tensor* t1 = Tensor::empty({2, 3});
Tensor* t2 = Tensor::zeros_like(t1);
// [
// [0.00 0.00 0.00]
// [0.00 0.00 0.00]
// ]
ones
-
static Tensor *Tensor::ones(const vector<int> &shape, int dev = DEV_CPU)
Create a tensor of the specified shape and filled with ones.
- Parameters
shape – Shape of the tensor to create.
dev – Device to use. The possible values are:
DEV_CPU
andDEV_GPU
.
- Returns
Tensor of the specified shape filled with ones
Tensor* t1 = Tensor::ones({2, 3});
// [
// [1.00 1.00 1.00]
// [1.00 1.00 1.00]
// ]
ones_like
-
static Tensor *Tensor::ones_like(Tensor *A)
Create a tensor with the shape and device of another one, initialized with ones.
- Parameters
A – Input tensor from wich to take shape and device.
- Returns
Ones initialized A-shaped tensor
Tensor* t1 = Tensor::empty({2, 3});
Tensor* t2 = Tensor::ones_like(t1);
// [
// [1.00 1.00 1.00]
// [1.00 1.00 1.00]
// ]
full
-
static Tensor *Tensor::full(const vector<int> &shape, float value, int dev = DEV_CPU)
Create a tensor of the specified shape and filled with a specific value.
- Parameters
shape – Shape of the tensor to create.
value – Value to use to fill the tensor.
dev – Device to use. The possible values are:
DEV_CPU
andDEV_GPU
.
- Returns
Tensor of the specified shape filled with the value
Tensor* t1 = Tensor::full({2, 3}, 10.0f);
// [
// [10.00 10.00 10.00]
// [10.00 10.00 10.00]
// ]
full_like
-
static Tensor *Tensor::full_like(Tensor *A, float value)
Create a tensor with the shape an device of the input tensor and filled with a specific value.
- Parameters
A – Input tensor from wich to take shape and device.
value – Value to use to fill the tensor
- Returns
Value initialized A-shaped tensor
Tensor* t1 = Tensor::empty({2, 3});
Tensor* t2 = Tensor::full_like(t1, 10.0f);
// [
// [10.00 10.00 10.00]
// [10.00 10.00 10.00]
// ]
eye
-
static Tensor *Tensor::eye(int rows, int offset = 0, int dev = DEV_CPU)
- Parameters
rows – Number of rows of the tensor.
offset –
dev – Device to use. The possible values are:
DEV_CPU
andDEV_GPU
.
- Returns
Tensor of the specified shape filled with the value
Tensor* t1 = Tensor::eye(3);
// [
// [1.00 0.00 0.00]
// [0.00 1.00 0.00]
// [0.00 0.00 1.00]
// ]
Tensor* t1 = Tensor::eye(3, -1);
// [
// [0.00 0.00 0.00]
// [1.00 0.00 0.00]
// [0.00 1.00 0.00]
// ]
identity
-
static Tensor *Tensor::identity(int rows, int dev = DEV_CPU)
Create a tensor representing the identity matrix. Equivalent to calling function
eye
withoffset = 0
.- Parameters
rows – Shape of the tensor to create.
dev – Device to use. The possible values are:
DEV_CPU
andDEV_GPU
.
- Returns
Tensor of the specified shape filled with the value
Tensor* t1 = Tensor::identity(3);
// [
// [1.00 0.00 0.00]
// [0.00 1.00 0.00]
// [0.00 0.00 1.00]
// ]
Constructors from existing data
Create tensor from existing data
clone
-
Tensor *Tensor::clone()
Clone a tensor (same device). Similar to copy, but returning a new instance.
- Returns
Tensor
Tensor* tensor1 = Tensor::ones({2, 3});
// [
// [1.00 1.00 1.00]
// [1.00 1.00 1.00]
// ]
Tensor* tensor2 = tensor1->clone();
// [
// [1.00 1.00 1.00]
// [1.00 1.00 1.00]
// ]
reallocate
-
void Tensor::reallocate(Tensor *old_t, const vector<int> &shape)
Reallocates a tensor into this one. Replaces the pointer of this tensor, with the pointer of a reference tensor. Then, the attributes of this tensor (shape, size, device) are update with from the specified new shape.
- Parameters
old_t – Reference tensor
shape – Shape of the new tensor (optional)
- Returns
void
Tensor* t1 = new Tensor({1,2,3,4,5,6,7,8,9}, {3, 3});
// [
// [1.00 2.00 3.00]
// [4.00 5.00 6.00]
// [7.00 8.00 9.00]
// ]
Tensor* t2 = Tensor::zeros({1, 6});
// [
// [0.00 0.00 0.00 0.00 0.00 0.00]
// ]
t2->reallocate(t1);
// [
// [1.00 2.00 3.00 4.00 5.00 6.00]
// ]
t2->reallocate(t1, {2, 2});
// [
// [1.00 2.00]
// [3.00 4.00]
// ]
t2->reallocate(t1, {3, 2});
// [
// [1.00 2.00]
// [3.00 4.00]
// [5.00 6.00]
// ]
// Modify value of T1
t1->ptr[0] = 100.0f;
// Tensor 1
// [
// [100.00 2.00 3.00]
// [4.00 5.00 6.00]
// [7.00 8.00 9.00]
// ]
// Tensor 2
// [
// [100.00 2.00]
// [3.00 4.00]
// [5.00 6.00]
// ]
copy
-
static void Tensor::copy(Tensor *A, Tensor *B)
Copy data from tensor A to B.
- Parameters
A – Tensor
B – Tensor
- Returns
void
Tensor* t1 = Tensor::ones({4, 3});
// [
// [1.00 1.00 1.00]
// [1.00 1.00 1.00]
// [1.00 1.00 1.00]
// [1.00 1.00 1.00]
// ]
Tensor* t2 = Tensor::zeros({6, 2, 1});
// [
// [[0.00] [0.00]]
// [[0.00] [0.00]]
// [[0.00] [0.00]]
// [[0.00] [0.00]]
// [[0.00] [0.00]]
// [[0.00] [0.00]]
// ]
Tensor::copy(t1, t2);
// [
// [[1.00] [1.00]]
// [[1.00] [1.00]]
// [[1.00] [1.00]]
// [[1.00] [1.00]]
// [[1.00] [1.00]]
// [[1.00] [1.00]]
// ]
Constructors from numerical ranges
Create tensor from numerical ranges
arange
-
static Tensor *Tensor::arange(float start, float end, float step = 1.0f, int dev = DEV_CPU)
Create a 1-D tensor of size ceil(end - start) with values from start to end with step step.
- Parameters
start – Start index
end – End index
step – The gap between two values in the tensor.
dev – One of
DEV_CPU
orDEV_GPU
- Returns
The new tensor
Tensor* t1 = Tensor::arange(1.0, 4.0, 0.5);
// [1.00 1.50 2.00 2.50 3.00 3.50]
range
-
static Tensor *Tensor::range(float start, float end, float step = 1.0f, int dev = DEV_CPU)
Creates a 1-D tensor of size floor(end - start)/ + 1 with values from start to end with step step.
- Parameters
start – Start value
end – End value
step – The gap between two values in the tensor.
dev – One of
DEV_CPU
orDEV_GPU
- Returns
The new tensor
Tensor* t1 = Tensor::range(1.0, 4.0, 0.5);
// [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
linspace
-
static Tensor *Tensor::linspace(float start, float end, int steps = 100, int dev = DEV_CPU)
Creates a 1-D tensor with a sequence of num evenly-spaced values starting at start. If steps > 1, the values in the sequence increase by end - start / steps - 1, so that the last one is exactly end.
- Parameters
start – Start value
end – End value
steps – The gap between two values in the tensor.
dev – One of
DEV_CPU
orDEV_GPU
- Returns
The new tensor
Tensor* t1 = Tensor::linspace(3.0, 10.0, 5);
// [3.00, 4.75, 6.50, 8.25, 10.00]
logspace
-
static Tensor *Tensor::logspace(float start, float end, int steps = 100, float base = 10.0f, int dev = DEV_CPU)
Creates a 1-D tensor with a sequence of num logarithmic spaced values starting at start. If steps > 1, the values in the sequence increase by end - start / steps - 1, so that the last one is exactly end.
- Parameters
start – Start value
end – End value
steps – The gap between two values in the tensor.
base – The base of the logarithm to apply.
dev – One of
DEV_CPU
orDEV_GPU
- Returns
The new tensor
Tensor* t1 = Tensor::logspace(0.1, 1.0, 5, 10.0);
// [1.2589, 2.1135, 3.5481, 5.9566, 10.0000]
geomspace
-
static Tensor *Tensor::geomspace(float start, float end, int steps = 100, int dev = DEV_CPU)
Creates a 1-D tensor with a sequence of num geometrically spaced values starting at start. If steps > 1, the values in the sequence increase by end - start / steps - 1, so that the last one is exactly end.
- Parameters
start – Start value
end – End value
steps – The gap between two values in the tensor.
dev – One of
DEV_CPU
orDEV_GPU
- Returns
The new tensor
Tensor* t1 = Tensor::geomspace(1.0, 1000.0, 3);
// [1.0, 10.0, 100.0]
Constructors from random generators
Create tensor from generators
randu
-
static Tensor *Tensor::randu(const vector<int> &shape, int dev = DEV_CPU)
Initialise a tensor with random values following an uniform distribution.
- Parameters
shape – Shape of the tensor to create.
dev – Device to use. The possible values are:
DEV_CPU
andDEV_GPU
.
- Returns
Tensor of the specified shape filled
Tensor* t1 = Tensor::randu({3, 3});
// [
// [0.11 0.17 0.01]
// [0.19 0.61 0.45]
// [0.26 0.22 0.02]
// ]
randn
-
static Tensor *Tensor::randn(const vector<int> &shape, int dev = DEV_CPU)
Initialise a tensor with random values following an normal distribution.
- Parameters
shape – Shape of the tensor to create.
dev – Device to use. The possible values are:
DEV_CPU
andDEV_GPU
.
- Returns
Tensor of the specified shape filled
Tensor* t1 = Tensor::randn({2, 3});
// [
// [-0.88 0.79 0.84]
// [-0.64 0.35 0.04]
// ]
Constructors of matrices
-
Tensor *Tensor::diag(int k = 0)
Extract a diagonal or construct a diagonal array.
- Parameters
k – Offset. If k=0, main diagonal is selected. If k>0, a diagonal above the main diagonal is selected. If k<0, a diagonal below the main diagonal is selected.
- Returns
A new tensor with the elements on the selected diagonal.
Example:
Tensor* t1 = new Tensor({1,2,3,4,5,6,7,8,9}, {3, 3});
// [
// [1.00 2.00 3.00]
// [4.00 5.00 6.00]
// [7.00 8.00 9.00]
// ]
Tensor* t2 = t1->diag(0);
// [
// [1.00 0.00 0.00]
// [0.00 5.00 0.00]
// [0.00 0.00 9.00]
// ]
Tensor* t3 = t1->diag(1);
// [
// [0.00 2.00 0.00]
// [0.00 0.00 6.00]
// [0.00 0.00 0.00]
// ]