Mathematical functions

Point-wise

Abs

Tensor *Tensor::abs()

Element-wise abs operation.

Returns

A new tensor with abs applied over A

Tensor* t1 = Tensor::randn({2, 3});
// [
// [0.31 0.08 1.59]
// [-0.77 -0.46 -0.39]
// ]

t1->abs_();  // In-place
// [
// [0.31 0.08 1.59]
// [0.77 0.46 0.39]
// ]

// Other ways
Tensor* t2 = t1->abs(); // returns a new tensor
Tensor::abs(t1, t2); // static

Acos

Tensor *Tensor::acos()

In-place element-wise acos operation.

Returns

A new tensor with the result of acos operation

Tensor* t1 = Tensor::randn({2, 3});
// [
// [0.42 -0.80 0.27]
// [0.42 -0.80 0.27]
// ]

t1->acos_(); // In-place
// [
// [1.13 2.51 1.29]
// [1.12 0.78 1.77]
// ]

// Other ways
Tensor* t2 = t1->acos(); // returns a new tensor
Tensor::acos(t1, t2); // static

Add

Tensor *Tensor::add(float v)

Element-wise add operation of a tensor and a real value.

Parameters

v – The real number to add

Returns

A new tensor with the sum

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

t1->add_(2.0f); // In-place
// [
// [7.00 7.00 7.00]
// [7.00 7.00 7.00]
// ]

// Other ways
Tensor* t2 = t1->add(2.0f); // returns new tensor
Tensor::add(t1, t2, 2.0f); // static

Asin

Tensor *Tensor::asin()

Element-wise asin operation.

Returns

A new tensor with the result

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-0.99 0.24 0.39]
// [-0.01 1.64 0.01]
// ]

t1->asin_(); // In-place
// [
// [-1.54 0.24 0.40]
// [-0.01 nan 0.01]
// ]

// Other ways
Tensor* t2 = t1->asin(); // returns a new tensor
Tensor::asin(t1, t2); // static

Atan

Tensor *Tensor::atan()

Element-wise atan operation.

Returns

A new tensor with the result

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-0.82 -0.04 0.29]
// [-0.80 -0.03 0.51]
// ]

t1->atan_(); // In-place
// [
// [-0.68 -0.04 0.29]
// [-0.67 -0.03 0.47]
// ]

// Other ways
Tensor* t2 = t1->atan(); // returns a new tensor
Tensor::atan(t1, t2); // static

Ceil

Tensor *Tensor::ceil()

Element-wise ceil operation.

Returns

A new tensor with the result

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-0.15 0.63 -0.76]
// [0.18 -0.12 0.18]
// ]

t1->ceil_(); // In-place
// [
// [-0.00 1.00 -0.00]
// [1.00 -0.00 1.00]
// ]

// Other ways
Tensor* t2 = t1->ceil(); // returns a new tensor
Tensor::ceil(t1, t2); // static

Clamp

Tensor *Tensor::clamp(float min, float max)

Clamp all elements in the input tensor to the range [min, max].

Parameters
  • min – The lower bound of the clamping range.

  • max – The upper bound of the clamping range.

Returns

A new tensor with the clamped values in the input tensor.

Tensor* t1 = Tensor::range(-5.0f, 5.0f);
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->clamp_(-2.0f, 3.0f); // In-place
// [-2.00 -2.00 -2.00 -2.00 -1.00 0.00 1.00 2.00 3.00 3.00 3.00]

// Other ways
Tensor* t2 = t1->clamp(-2.0f, 3.0f); // returns a new tensor
Tensor::clamp(t1, t2, -2.0f, 3.0f); // static

Clampmax

Tensor *Tensor::clampmax(float max)

Clamp all elements in the input tensor to the range [-infty, max].

Parameters

max – The upper bound of the clamping range.

Returns

A new tensor with the clamped values.

Tensor* t1 = Tensor::range(-5.0f, 5.0f);
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->clampmax_(3.0f); // In-place
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 3.00 3.00]

// Other ways
Tensor* t2 = t1->clampmax(3.0f); // returns a new tensor
Tensor::clampmax(t1, t2, 3.0f); // static

Clampmin

Tensor *Tensor::clampmin(float min)

Clamp all elements in the input tensor to the range [min, +infty].

Parameters

min – The lower bound of the clamping range.

Returns

A new tensor with the clamped values.

 Tensor* t1 = Tensor::range(-5.0f, 5.0f);
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->clampmin_(3.0f); // In-place
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 3.00 3.00]

// Other ways
Tensor* t2 = t1->clampmin(3.0f); // returns a new tensor
Tensor::clampmin(t1, t2, 3.0f); // static

Cos

Tensor *Tensor::cos()

Element-wise cos operation.

Returns

A new tensor with cos applied.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-0.58 0.45 -1.14]
// [-0.24 -1.15 -1.33]
// ]

t1->cos_(); // In-place
// [
// [0.83 0.90 0.41]
// [0.97 0.41 0.23]
// ]

// Other ways
Tensor* t2 = t1->cos(); // returns a new tensor
Tensor::cos(t1, t2); // static

Cosh

Tensor *Tensor::cosh()

Element-wise cosh operation.

Returns

A new tensor with cosh applied.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-1.52 -0.52 0.31]
// [0.85 1.06 0.26]
// ]

t1->cosh_(); // In-place
// [
// [2.40 1.14 1.05]
// [1.39 1.62 1.04]
// ]

// Other ways
Tensor* t2 = t1->cosh(); // returns a new tensor
Tensor::cosh(t1, t2); // static

Div

Tensor *Tensor::div(float v)

Element-wise division operation of a tensor and a real value.

Parameters

v – The real number to divide by.

Returns

A new tensor with the division.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

t1->div_(2.0f); // In-place
// [
// [2.50 2.50 2.50]
// [2.50 2.50 2.50]
// ]

// Other ways
Tensor* t2 = t1->div(2.0f); // returns new tensor
Tensor::div(t1, t2, 2.0f); // static

Exp

Tensor *Tensor::exp()

Element-wise exp operation of a tensor.

Returns

A new tensor with the exp operation applied

Tensor* t1 = Tensor::range({-5.0f, 5.0f);
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->exp_(); // In-place
// [0.01 0.02 0.05 0.14 0.37 1.00 2.72 7.39 20.09 54.60 148.41]

// Other ways
Tensor* t2 = t1->exp(); // returns new tensor
Tensor::exp(t1, t2); // static

Floor

Tensor *Tensor::floor()

Element-wise floor operation.

Returns

A new tensor with the floor operation applied.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [0.47 1.39 0.94]
// [0.98 1.16 0.40]
// ]

t1->floor_(); // In-place
// [
// [0.00 1.00 0.00]
// [0.00 1.00 0.00]
// ]

// Other ways
Tensor* t2 = t1->floor(); // returns new tensor
Tensor::floor(t1, t2); // static

Inv

Tensor *Tensor::inv(float v = 1.0f)

Element-wise 1/x operation.

Parameters

v – the value multiplying the inverse

Returns

A new tensor with the result.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [0.58 -0.49 0.04]
// [-1.25 -1.33 0.23]
// ]

t1->inv_(); // In-place
// [
// [1.72 -2.04 25.16]
// [-0.80 -0.75 4.34]
// ]

// Other ways
Tensor* t2 = t1->inv(); // returns new tensor
Tensor::inv(t1, t2); // static

Log

Tensor *Tensor::log()

Element-wise log operation.

Returns

A new tensor with the log operation applied

Tensor* t1 = Tensor::range(-1.0f, 5.0f);
// [-1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->log_(); // In-place
// [-nan -inf 0.00 0.69 1.10 1.39 1.61]

// Other ways
Tensor* t2 = t1->log(); // returns new tensor
Tensor::log(t1, t2); // static

Log2

Tensor *Tensor::log2()

Element-wise log2 operation.

Returns

A new tensor with the log2 operation applied.

Tensor* t1 = Tensor::range(-1.0f, 5.0f);
// [-1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->log2_(); // In-place
// [-nan -inf 0.00 1.00 1.58 2.00 2.32]

// Other ways
Tensor* t2 = t1->log2(); // returns new tensor
Tensor::log2(t1, t2); // static

Log10

Tensor *Tensor::log10()

Element-wise log10 operation.

Returns

A new tensor with the log10 operation applied.

Tensor* t1 = Tensor::range(-1.0f, 5.0f);
// [-1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->log10_(); // In-place
// [nan -inf 0.00 0.30 0.48 0.60 0.70]

// Other ways
Tensor* t2 = t1->log10(); // returns new tensor
Tensor::log10(t1, t2); // static

Logn

Tensor *Tensor::logn(float n)

Element-wise logn operation.

Parameters

n – The base of the logarithm.

Returns

A new tensor with the logn operation applied.

Tensor* t1 = Tensor::range(-1.0f, 5.0f);
// [-1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->logn_(5.0f); // In-place
// [-nan -inf 0.00 0.43 0.68 0.86 1.00]

// Other ways
Tensor* t2 = t1->logn(5.0f); // returns new tensor
Tensor::logn(t1, t2, 5.0f); // static

Maximum

Tensor *Tensor::maximum(float v)

Apply a lower bound to the elements in a tensor.

Parameters

v – Lower bound.

Returns

A new tensor with the values lower than v set to v.

Tensor* t1 = Tensor::randn({2,3});
// [
// [-0.63 -0.10 0.01]
// [-1.47 -0.84 0.54]
// ]


Tensor* t2 = t1->maximum(0.3f);  // returns a new tensor
// [
// [0.30 0.30 0.30]
// [0.30 0.30 0.54]
// ]

// Other ways
Tensor::maximum(t1, 0.3f) // source
Tensor::maximum(t1, t2, 0.3f); // static

Minimum

Tensor *Tensor::minimum(float v)

Apply a upper bound to the elements in a tensor.

Parameters

v – Lower bound.

Returns

A new tensor with the values higher than v set to v.

Tensor* t1 = Tensor::randn({2,3});
// [
// [-1.30 -1.05 0.49]
// [0.03 0.09 0.28]
// ]


Tensor* t2 = t1->minimum(0.3f);  // returns a new tensor
// [
// [-1.30 -1.05 0.30]
// [0.03 0.09 0.28]
// ]

// Other ways
Tensor::minimum(t1, 0.3f) // source
Tensor::minimum(t1, t2, 0.3f); // static

Mod

Tensor *Tensor::mod(float v)

Element-wise mod operation.

Parameters

v – The mod operator

Returns

A new tensor with the operation applied.

Tensor* t1 = Tensor::range(-5.0f, 5.0f);
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->mod_(2.0f); // In-place
// [-1.00 -0.00 -1.00 -0.00 -1.00 0.00 1.00 0.00 1.00 0.00 1.00]

// Other ways
Tensor* t2 = t1->mod(2.0f); // returns new tensor
Tensor::mod(t1, t2, 2.0f); // static

Mult

Tensor *Tensor::mult(float v)

Multiplication operation of a tensor by a scalar.

Parameters

v – The value to multiply by

Returns

A tensor with the result

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

t1->mult_(2.0); // In-place
// [
// [10.00 10.00 10.00]
// [10.00 10.00 10.00]
// ]

// Other ways
Tensor* t2 = t1->mult(2.0f); // returns new tensor
Tensor::mult(t1, t2, 2.0f); // static

Neg

Tensor *Tensor::neg()

Element-wise change of sign operation.

Returns

A tensor with the result.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

t1->neg_(); // In-place
// [
// [-5.00 -5.00 -5.00]
// [-5.00 -5.00 -5.00]
// ]

// Other ways
Tensor* t2 = t1->neg(); // returns new tensor
Tensor::neg(t1, t2); // static

Normalize

Tensor *Tensor::normalize(float min = 0.0f, float max = 1.0f)

In-place element-wise normalization of values in a given range.

Parameters
  • min – The lower bound of the new range.

  • max – The upper bound of the new range.

Returns

A tensor with the result.

Tensor* t1 = Tensor::range(-5.0f, 5.0f);
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->normalize_(0.0f, 1.0f); // In-place
// [0.00 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00]

// Other ways
Tensor* t2 = t1->normalize(0.0f, 1.0f); // returns new tensor
Tensor::normalize(t1, t2, 0.0f, 1.0f); // static

Pow

Tensor *Tensor::pow(float exp)

Element-wise power operation with base e.

Parameters

exp – The exponent.

Returns

A tensor with the result.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

t1->pow_(2.0f); // In-place
// [
// [25.00 25.00 25.00]
// [25.00 25.00 25.00]
// ]

// Other ways
Tensor* t2 = t1->pow(2.0f); // returns new tensor
Tensor::pow(t1, t2, 2.0f); // static

Powb

Tensor *Tensor::powb(float base)

Element-wise power operation.

Parameters

base – The base of the power.

Returns

A tensor with the result.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

t1->powb_(2.0f); // In-place
// [
// [32.00 32.00 32.00]
// [32.00 32.00 32.00]
// ]

// Other ways
Tensor* t2 = t1->powb(2.0f); // returns new tensor
Tensor::powb(t1, t2, 2.0f); // static

Reciprocal

Tensor *Tensor::reciprocal()

Element-wise reciprocal operation.

Returns

A tensor with the result

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

t1->reciprocal_(2.0f); // In-place
// [
// [0.20 0.20 0.20]
// [0.20 0.20 0.20]
// ]

// Other ways
Tensor* t2 = t1->reciprocal(); // returns new tensor
Tensor::reciprocal(t1, t2); // static

Remainder

Tensor *Tensor::remainder(float v)

Element-wise reminder operation.

Parameters

v – The real to divide A by

Returns

A tensor with Av

Tensor* t1 = Tensor::range(-5.0f, 5.0f);
// [-5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->remainder_(2.0f); // In-place
// [-1.00 -0.00 -1.00 -0.00 -1.00 0.00 1.00 0.00 1.00 0.00 1.00]

// Other ways
Tensor* t2 = t1->remainder(2.0f); // returns new tensor
Tensor::remainder(t1, t2, 2.0f); // static

Round

Tensor *Tensor::round()

Element-wise round operation.

Returns

A tensor with A rounded

Tensor* t1 = Tensor::randn({2, 3});
// [
// [1.14 -0.32 0.40]
// [0.21 -0.38 0.01]
// ]

t1->round_(); // In-place
// [
// [1.00 -0.00 0.00]
// [0.00 -0.00 0.00]
// ]

// Other ways
Tensor* t2 = t1->round(); // returns new tensor
Tensor::round(t1, t2); // static

Rsqrt

Tensor *Tensor::rsqrt()

Element-wise inverse square root operation.

Returns

A tensor with the result

Tensor* t1 = Tensor::range(-1.0f, 5.0f);
// [-1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->rsqrt_(); // In-place
// [-nan inf 1.00 0.71 0.58 0.50 0.45]

// Other ways
Tensor* t2 = t1->rsqrt(); // returns new tensor
Tensor::rsqrt(t1, t2); // static

Sigmoid

Tensor *Tensor::sigmoid()

Element-wise sigmoid operation.

Returns

A tensor with sigmoid(A)

Tensor* t1 = Tensor::randn({2,3});
// [
// [0.11 0.87 0.18]
// [2.13 -0.13 0.12]
// ]


t1->sigmoid_();  // In-place
// [
// [0.53 0.70 0.54]
// [0.89 0.47 0.53]
// ]

// Other ways
Tensor* t2 = t1->sigmoid(); // returns a new tensor
Tensor::sigmoid(t1, t2); // static

Sign

Tensor *Tensor::sign(float zero_sign = 0.0f)
Tensor* t1 = Tensor::linspace(-1.0f, 1.0f, 5.0f);
// [-1.00 -0.50 0.00 0.50 1.00]


t1->sign_(5.0f);  // In-place
// [-1.00 -1.00 5.00 1.00 1.00]

// Other ways
Tensor* t2 = t1->sign(5.0f); // returns a new tensor
Tensor::sign(t1, t2, 5.0f); // static

Sin

Tensor *Tensor::sin()

Element-wise sin operation.

Returns

A tensor with the result.

Tensor* t1 = Tensor::randn({2,3});
// [
// [0.11 0.87 0.18]
// [2.13 -0.13 0.12]
// ]


t1->sin_();  // In-place
// [
// [0.53 0.70 0.54]
// [0.89 0.47 0.53]
// ]

// Other ways
Tensor* t2 = t1->sin(); // returns a new tensor
Tensor::sin(t1, t2); // static

Sinh

Tensor *Tensor::sinh()

Element-wise sinh operation.

Returns

A tensor with the result.

Tensor* t1 = Tensor::randn({2,3});
// [
// [-0.35 -0.14 0.08]
// [0.64 0.46 1.49]
// ]


t1->sinh_();  // In-place
// [
// [-0.36 -0.15 0.08]
// [0.69 0.48 2.10]
// ]

// Other ways
Tensor* t2 = t1->sinh(); // returns a new tensor
Tensor::sinh(t1, t2); // static

Sqr

Tensor *Tensor::sqr()

Element-wise square operation. More efficient than using pow(A, 2).

Returns

A tensor with the result

Tensor* t1 = Tensor::range(-1.0f, 5.0f);
// [-1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->sqr_(); // In-place
// [1.00 0.00 1.00 4.00 9.00 16.00 25.00]

// Other ways
Tensor* t2 = t1->sqr(); // returns new tensor
Tensor::sqr(t1, t2); // static

Sqrt

Tensor *Tensor::sqrt()

Element-wise square operation.

Returns

A tensor with the result.

Tensor* t1 = Tensor::range(-1.0f, 5.0f);
// [-1.00 0.00 1.00 2.00 3.00 4.00 5.00]

t1->sqrt_(); // In-place
// [-nan 0.00 1.00 1.41 1.73 2.00 2.24]

// Other ways
Tensor* t2 = t1->sqrt(); // returns new tensor
Tensor::sqrt(t1, t2); // static

Sub

Tensor *Tensor::sub(float v)

Element-wise substraction operation of a tensor and a scalar.

Parameters

v – The value to substract to the input tensor.

Returns

A tensor with the result.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

t1->sub_(2.0f); // In-place
// [
// [3.00 3.00 3.00]
// [3.00 3.00 3.00]
// ]

// Other ways
Tensor* t2 = t1->sub(2.0f); // returns new tensor
Tensor::sub(t1, t2, 2.0f); // static

Tan

Tensor *Tensor::tan()

Element-wise tan operation.

Returns

A tensor with the result.

Tensor* t1 = Tensor::randn({2,3});
// [
// [0.20 0.97 1.08]
// [0.12 1.06 -0.67]
// ]


t1->tan_();  // In-place
// [
// [0.20 1.45 1.85]
// [0.12 1.78 -0.80]
// ]

// Other ways
Tensor* t2 = t1->tan(); // returns a new tensor
Tensor::tan(t1, t2); // static

Tanh

Tensor *Tensor::tanh()

Element-wise tanh operation.

Returns

A tensor with the result.

Tensor* t1 = Tensor::randn({2,3});
// [
// [-2.07 -0.41 -0.83]
// [0.12 0.45 0.90]
// ]


t1->tanh_();  // In-place
// [
// [-0.97 -0.39 -0.68]
// [0.12 0.42 0.72]
// ]

// Other ways
Tensor* t2 = t1->tanh(); // returns a new tensor
Tensor::tanh(t1, t2); // static

Trunc

Tensor *Tensor::trunc()

Element-wise truncate operation.

Returns

A tensor with the result.

Tensor* t1 = Tensor::randn({2,3});
// [
// [0.40 -0.95 0.49]
// [1.13 -0.08 -0.32]
// ]


t1->trunc_();  // In-place
// [
// [0.00 -0.00 0.00]
// [1.00 -0.00 -0.00]
// ]

// Other ways
Tensor* t2 = t1->trunc(); // returns a new tensor
Tensor::trunc(t1, t2); // static

Element-wise

Add

static Tensor *Tensor::add(Tensor *A, Tensor *B)

Element-wise add operation of two tensors.

Parameters
  • A – A tensor.

  • B – Another tensor.

Returns

A new tensor C = A + B.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

Tensor* t2 = Tensor::full({2, 3}, 2.0f);
// [
// [2.00 2.00 2.00]
// [2.00 2.00 2.00]
// ]

t1->add_(t2);  // In-place
// [
// [7.00 7.00 7.00]
// [7.00 7.00 7.00]
// ]

// Other ways
Tensor* t3 = t1->add(t2);  // returns new tensor
Tensor::add(t1, t2, t3) //source

Div

static Tensor *Tensor::div(Tensor *A, Tensor *B)

Element-wise division operation of two tensors.

Parameters
  • A – A tensor.

  • B – Another tensor.

Returns

A new tensor C = A / B.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

Tensor* t2 = Tensor::full({2, 3}, 2.0f);
// [
// [2.00 2.00 2.00]
// [2.00 2.00 2.00]
// ]

t1->div_(t2);  // In-place
// [
// [2.50 2.50 2.50]
// [2.50 2.50 2.50]
// ]

// Other ways
Tensor* t3 = t1->div(t2);  // returns new tensor
Tensor::add(t1, t2, t3) //source

Maximum

static Tensor *Tensor::maximum(Tensor *A, Tensor *B)

Element-wise selection of the maximum values in the same position in two tensors.

Parameters
  • A – Input tensor.

  • B – Input tensor.

Returns

A tensor with the higher value in the same position between A and B.

Tensor* t1 = Tensor::randn({2,3});
// [
// [-0.25 -1.21 -0.04]
// [-0.74 0.12 -0.59]
// ]

Tensor* t2 = Tensor::randn({2,3});
// [
// [0.05 -1.72 -0.50]
// [0.17 -0.00 0.11]
// ]

Tensor* t3 = Tensor::maximum(t1, t2);  // returns a new tensor
// [
// [0.05 -1.21 -0.04]
// [0.17 0.12 0.11]
// ]

// Other ways
Tensor::maximum(t1, t2, t3); // static

Minimum

static Tensor *Tensor::minimum(Tensor *A, Tensor *B)

Element-wise selection of the minimum values in the same position in two tensors.

Parameters
  • A – Input tensor.

  • B – Input tensor.

Returns

A tensor with the lower value in the same position between A and B.

Tensor* t1 = Tensor::randn({2,3});
// [
// [-0.25 -0.75 -0.53]
// [0.29 0.88 1.98]
// ]

Tensor* t2 = Tensor::randn({2,3});
// [
// [-1.37 0.95 -0.59]
// [-1.26 1.16 -0.21]
// ]

Tensor* t3 = Tensor::minimum(t1, t2);  // returns a new tensor
// [
// [-1.37 -0.75 -0.59]
// [-1.26 0.88 -0.21]
// ]

// Other ways
Tensor::minimum(t1, t2, t3); // static

Mult

static Tensor *Tensor::mult(Tensor *A, Tensor *B)

Element-wise multiplication operation of two tensors.

Parameters
  • A – A tensor.

  • B – Another tensor.

Returns

A new tensor C = A * B.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

Tensor* t2 = Tensor::full({2, 3}, 2.0f);
// [
// [2.00 2.00 2.00]
// [2.00 2.00 2.00]
// ]

t1->mult_(t2);  // In-place
// [
// [10.00 10.00 10.00]
// [10.00 10.00 10.00]
// ]

// Other ways
Tensor* t3 = t1->mult(t2);  // returns new tensor
Tensor::mult(t1, t2, t3) //source

Sub

static Tensor *Tensor::sub(Tensor *A, Tensor *B)

Element-wise substraction operation of two tensors.

Parameters
  • A – A tensor.

  • B – Another tensor.

Returns

A new tensor C = A - B.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

Tensor* t2 = Tensor::full({2, 3}, 2.0f);
// [
// [2.00 2.00 2.00]
// [2.00 2.00 2.00]
// ]

t1->sub_(t2);  // In-place
// [
// [3.00 3.00 3.00]
// [3.00 3.00 3.00]
// ]

// Other ways
Tensor* t3 = t1->sub(t2);  // returns new tensor
Tensor::sub(t1, t2, t3) //source

Reductions

Argmax

int Tensor::argmax()

Obtain the index of the maximum value in the tensor.

Returns

The desired index.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [2.13 -0.14 -0.15]
// [1.53 0.96 0.31]
// ]

// Global (reduce on all axis)
int n1 = t1->argmax();
// 0

// Reduced on axis 0
Tensor* t2 = t1->argmax({0}, false); // keepdims==false
// [0 1 1]

// Other ways
int n2 = Tensor::argmax(t1); // static (returns int)
Tensor* t3 = t1->argmax({0}, true); // axis (returns tensor)

Argmin

int Tensor::argmin()

Obtain the index of the minimum value in the tensor.

Returns

The desired index.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-0.34 -0.13 -0.41]
// [-1.09 -0.08 -1.85]
// ]

// Global (reduce on all axis)
int n1 = t1->argmin();
// 5

// Reduced on axis 0
Tensor* t2 = t1->argmin({0}, false); // keepdims==false
t2->print(2);

// Other ways
int n2 = Tensor::argmin(t1); // static (returns int)
Tensor* t3 = t1->argmin({0}, true); // axis (returns tensor)

Max

float Tensor::max()

Obtain the maximum value in the tensor.

Returns

float. The maximum value in the tensor

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-0.79 -0.54 0.28]
// [1.12 0.38 -1.25]
// ]

// Global (reduce on all axis)
float n1 = t1->max();
// 1.12

// Reduced on axis 0
Tensor* t2 = t1->max({0}, false); // keepdims==false
// [1.12 0.38 0.28]

// Other ways
float n2 = Tensor::max(t1); // static (returns float)
Tensor* t3 = t1->max({0}, true); // axis (returns tensor)

Mean

float Tensor::mean()

Obtain the mean of all the values in the tensor.

Returns

The mean of all the elements in the tensor.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-0.01 0.40 -0.07]
// [1.14 0.13 -0.49]
// ]

// Global (reduce on all axis)
float n1 = t1->mean();
// 0.18

// Reduced on axis 0
Tensor* t2 = t1->mean({0}, false); // keepdims==false
// [0.57 0.26 -0.28]

// Other ways
float n2 = Tensor::mean(t1); // static (returns float)
Tensor* t3 = t1->mean({0}, true); // axis (returns tensor)

Median

float Tensor::median()

Obtain the median value of all the elements in the tensor.

Returns

float The median value.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [0.24 -0.86 0.36]
// [0.18 -0.93 -1.07]
// ]

// Global (reduce on all axis)
float n1 = t1->median();
// -0.34

// Reduced on axis 0
Tensor* t2 = t1->median({0}, false); // keepdims==false
// [0.21 -0.90 -0.35]

// Reduced on axis 1
Tensor* t3 = t1->median({1}, false); // keepdims==false
// [0.24 -0.93]

// Other ways
float n2 = Tensor::mean(t1); // static (returns float)
Tensor* t4 = t1->median({0}, true); // axis (returns tensor)

Min

float Tensor::min()

Obtain the minimum value in the tensor.

Returns

float. The minimum value in the tensor

Tensor* t1 = Tensor::randn({2, 3});
// [
// [-1.11 -0.00 0.34]
// [-0.16 0.13 -0.39]
// ]

// Global (reduce on all axis)
float n1 = t1->min();
// -1.11

// Reduced on axis 0
Tensor* t2 = t1->min({0}, false); // keepdims==false
// [-1.11 -0.00 -0.39]

// Other ways
float n2 = Tensor::min(t1); // static (returns float)
Tensor* t3 = t1->min({0}, true); // axis (returns tensor)

Mode

int Tensor::mode()

Obtain the mode of all the values in the tensor.

Returns

The mode of all the elements in the tensor.

Tensor* t1 = new Tensor({1, 1, 1, 3, 2, 3, 3, 2, 3}, {3, 3});
// [
// [1.00 1.00 1.00]
// [3.00 2.00 3.00]
// [3.00 2.00 3.00]
// ]

// Global (reduce on all axis)
float n1 = t1->mode();
// 3

// Reduced on axis 0
Tensor* t2 = t1->mode({0}, false); // keepdims==false
// [3.00 2.00 3.00]

// Reduced on axis 1
Tensor* t3 = t1->mode({1}, false); // keepdims==false
// [1.00 3.00 3.00]

// Other ways
float n2 = Tensor::mode(t1); // static (returns float)
Tensor* t4 = t1->mode({0}, true); // axis (returns tensor)

Norm

Tensor *Tensor::norm(vector<int> axis, bool keepdims, string ord = "fro")

Compute the norm of a selected axis of a tensor.

Parameters
  • axis – Vector with the axis selected to compute the norm.

  • keepdims – Whether to keep dimensions of the input vector in the output vector.

  • ord – The order of the norm. One of:

    • ”fro”: Frobenius norm

Returns

A tensor with the norms computed over the selected axis.

Example:

Tensor* t1 = new Tensor({1,2,3,4,5,6}, {3, 2});
// [
// [1.00 2.00 3.00]
// [4.00 5.00 6.00]
// ]

// Global (reduce on all axis)
float n1 = t1->norm();
// 9.53939

// Reduced on axis 0
Tensor* t2 = t1->norm({0}, false); // keepdims==false
// [4.12 5.39 6.71]

// Other ways
Tensor::norm(t1, "fro");

Prod

float Tensor::prod()

Obtain the product of all the values in the tensor.

Returns

The product of all the elements in the tensor.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

// Global (reduce on all axis)
float n1 = t1->prod();
// 15625

// Reduced on axis 0
Tensor* t2 = t1->prod({0}, false); // keepdims==false
// [25.00 25.00 25.00]

// Reduced on axis 1
Tensor* t3 = t1->prod({1}, false); // keepdims==false
// [125.00 125.00]

// Other ways
float n2 = Tensor::prod(t1); // static (returns float)
Tensor* t4 = t1->prod({0}, true); // axis (returns tensor)

Std

float Tensor::std(bool unbiased = true)

Obtain the standard deviation of all the values in the tensor.

Parameters

unbiased – Whether the standard deviation is computed using the unbiased estimation or not.

Returns

The standard deviation of all the elements in the tensor.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [0.14 0.29 -0.51]
// [0.15 -0.82 0.21]
// ]

// Global (reduce on all axis)
float n1 = t1->std();
// 0.45

// Reduced on axis 0
Tensor* t2 = t1->std({0},  false, true); // keepdims==false, unbiased=true
// [0.01 0.79 0.50]

// Reduced on axis 1
Tensor* t3 = t1->std({1}, false, true); // keepdims==false, unbiased=true
// [0.42 0.58]

// Other ways
float n2 = Tensor::std(t1); // static (returns float)

Sum

float Tensor::sum()

Obtain the sum of all the values in the tensor.

Returns

The sum of all the elements in the tensor.

Tensor* t1 = Tensor::full({2, 3}, 5.0f);
// [
// [5.00 5.00 5.00]
// [5.00 5.00 5.00]
// ]

// Global (reduce on all axis)
float n1 = t1->sum();
// 30

// Reduced on axis 0
Tensor* t2 = t1->sum({0}, false); // keepdims==false
// [10.00 10.00 10.00]

// Reduced on axis 1
Tensor* t3 = t1->sum({1}, false); // keepdims==false
// [15.00 15.00]

// Other ways
float n2 = Tensor::sum(t1); // static (returns float)
Tensor* t4 = t1->sum({0}, true); // axis (returns tensor)

Sum Abs

float Tensor::sum_abs()

Obtain the absolute value sum of all the values in the tensor.

Returns

The absolute value sum of all the elements in the tensor.

Tensor* t1 = new Tensor({-5, 5, 5, -5, -5, 5}, {2, 3});
// [
// [-5.00 5.00 5.00]
// [-5.00 -5.00 5.00]
// ]

// Global (reduce on all axis)
float n1 = t1->sum_abs();
// 30

// Reduced on axis 0
Tensor* t2 = t1->sum_abs({0}, false); // keepdims==false
// [10.00 10.00 10.00]

// Reduced on axis 1
Tensor* t3 = t1->sum_abs({1}, false); // keepdims==false
// [15.00 15.00]

// Other ways
float n2 = Tensor::sum_abs(t1); // static (returns float)
Tensor* t4 = t1->sum_abs({0}, true); // axis (returns tensor)

Var

float Tensor::var(bool unbiased = true)

Obtain the variance of all the values in the tensor.

Parameters

unbiased – Whether the variance is computed using the unbiased estimation or not.

Returns

The variance of all the elements in the tensor.

Tensor* t1 = Tensor::randn({2, 3});
// [
// [0.23 -1.09 0.09]
// [0.09 1.68 -0.06]
// ]

// Global (reduce on all axis)
float n1 = t1->var();
// 0.78

// Reduced on axis 0
Tensor* t2 = t1->var({0},  false, true); // keepdims==false, unbiased=true
// [0.01 3.84 0.01]

// Reduced on axis 1
Tensor* t3 = t1->var({1}, false, true); // keepdims==false, unbiased=true
// [0.52 0.93]

// Other ways
float n2 = Tensor::var(t1); // static (returns int)