4.1.10. Tensor

class Tensor

Value with any dimensions.

Public Functions

Tensor()

Creates an invalid Tensor.

bool valid() const

Check whether the object is valid or not.

Return
true if the object is valid, false otherwise.
Remark
This returns false when the object is created through the default constructor or the object had been moved.

void check_valid() const

Check whether the object is valid or not.

Exceptions
  • primitiv::Error: This object is invalid.

Shape shape() const

Returns the shape of the Tensor.

Return
Shape of the Tensor.

Device &device() const

Returns the Device object related to the internal memory.

Return
Device object.

float to_float() const

Retrieves one internal value in the tensor.

Return
An internal float value.
Remark
This function can be used only when the tensor is a scalar and non-minibatched (i.e., shape() == Shape()).

std::vector<float> to_vector() const

Retrieves internal values in the tensor as a vector.

Return
A list of the internal values.
Remark
Each resulting values a re ordered by the column-major order, and the batch size is assumed as the last dimension of the tensor.

std::vector<std::uint32_t> argmax(std::uint32_t dim) const

Retrieves argmax indices along an axis.

Return
A list of integers that indicates positions of the maximum values.
Parameters
  • dim: A specified axis.

std::vector<std::uint32_t> argmin(std::uint32_t dim) const

Retrieves argmin indices along an axis.

Return
A list of integers that indicates positions of the minimum values.
Parameters
  • dim: A specified axis.

void invalidate()

Invalidates this object.

void reset(float k)

Reset internal values using a constant.

Parameters
  • k: A value to be used to initialize each element.

void reset_by_array(const float *values)

Reset internal values using a vector.

Remark
Length of values should be equal to shape().size(). Each element should be ordered by the column-major order, and the batch size is assumed as the last dimension.
Parameters
  • values: Array of values to be used to initialize each element.

void reset_by_vector(const std::vector<float> &values)

Reset internal values using a vector.

Remark
values.size() should be equal to shape().size(). Each element should be ordered by the column-major order, and the batch size is assumed as the last dimension.
Parameters
  • values: List of values to be used to initialize each element.

Tensor reshape(const Shape &new_shape) const

Returns a tensor which have the same values and different shape.

Return
A new tensor.
Parameters
  • new_shape: New shape with batch size 1.

Tensor flatten() const

Returns a flattened tensor.

Return
A new tensor.

Tensor &inplace_multiply_const(float k)

Directly multiplies a constant.

Return
*this
Parameters
  • k: A constant to multiply.

Tensor &inplace_add(const Tensor &x)

Directly adds a value.

Return
*this
Parameters
  • x: A tensor to add.

Tensor &inplace_subtract(const Tensor &x)

Directly subtracts a value.

Return
*this
Parameters
  • x: A tensor to subtract.