11#include <initializer_list>
19concept c_numeric = std::is_arithmetic_v<T> && !std::is_same_v<T, bool>;
240 explicit Tensor(
const std::vector<size_t> &
shape,
const T &fill_value);
261 explicit Tensor(std::vector<size_t> &&
shape,
const T &fill_value);
282 explicit Tensor(
const std::initializer_list<size_t> &
shape,
const T &fill_value)
283 :
Tensor(std::vector<size_t>(
shape), fill_value) {}
334 explicit Tensor(
const std::vector<size_t> &
shape,
const std::vector<T> &values);
359 explicit Tensor(
const std::initializer_list<size_t> &
shape,
const std::vector<T> &values)
381 explicit Tensor(
const std::initializer_list<std::initializer_list<T>> &values);
411 const std::initializer_list<std::initializer_list<std::initializer_list<T>>> &values);
425 constexpr std::type_identity_t<T>
type()
const;
439 [[nodiscard]]
size_t dim()
const;
518 template <
typename U>
556 template <
typename... Args>
557 requires(std::convertible_to<Args, size_t> && ...)
558 T &operator()(Args... args);
597 template <typename... Args>
598 requires(std::convertible_to<Args,
size_t> && ...)
609 const T &operator()() const;
634 template <typename... Args>
635 requires(std::convertible_to<Args,
size_t> && ...)
636 const T &operator()(Args... args) const;
673 template <typename... Args>
674 requires(std::convertible_to<Args,
size_t> && ...)
675 const T &
at(Args... args) const;
835 const size_t &seed2);
884 [[nodiscard]]
const T *
data()
const;
913 template <
typename U>
916 template <
typename U>
936 template <
typename U>
954 template <
typename U>
972 template <
typename U>
989 template <
typename U>
1006 template <
typename U>
1024 template <
typename U>
1050 template <
typename P>
1053 void fill_data_shape(
const std::initializer_list<std::initializer_list<T>> &list,
1054 std::vector<T> &flat_data, std::vector<size_t> &
shape);
1057 const std::initializer_list<std::initializer_list<std::initializer_list<T>>> &list,
1058 std::vector<T> &flat_data, std::vector<size_t> &
shape);
1071 using std::runtime_error::runtime_error;
1074template <
typename T>
1075template <
typename... Args>
1076 requires(std::convertible_to<Args, size_t> && ...)
1077inline T &
Tensor<T>::operator()(Args... args) {
1078 size_t indexes[] = {
static_cast<size_t>(args)...};
1079 size_t size = this->order();
1080 auto *stride = this->shape().stride().data();
1081 size_t flat_index{indexes[size - 1]};
1083 for (
size_t i = 0; i < size - 1; ++i)
1084 flat_index += indexes[i] * stride[i];
1086 return this->data()[flat_index];
1089template <
typename T>
1090template <
typename... Args>
1091 requires(std::convertible_to<Args, size_t> && ...)
1092inline const T &
Tensor<T>::operator()(Args... args)
const {
1093 size_t indexes[] = {
static_cast<size_t>(args)...};
1094 size_t size = this->order();
1095 auto *stride = this->shape().stride().data();
1096 size_t flat_index{indexes[size - 1]};
1098 for (
size_t i = 0; i < size - 1; ++i)
1099 flat_index += indexes[i] * stride[i];
1101 return this->data()[flat_index];
1104template <
typename T>
1105template <
typename... Args>
1106 requires(std::convertible_to<Args, size_t> && ...)
1108 if (this->order() !=
sizeof...(Args))
1109 throw TensorError(
"The number of axes specified and the order of this tensor do no match.");
1111 check_indexes({
static_cast<size_t>(args)...});
1112 }
catch (std::exception e) {
1116 return (*
this)(args...);
1119template <
typename T>
1120template <
typename... Args>
1121 requires(std::convertible_to<Args, size_t> && ...)
1122inline const T &
Tensor<T>::at(Args... args)
const {
1123 if (this->order() !=
sizeof...(Args))
1124 throw TensorError(
"The number of axes specified and the order of this tensor do no match.");
1126 check_indexes({
static_cast<size_t>(args)...});
1127 }
catch (std::exception e) {
1131 return (*
this)(args...);
1134template <
typename T>
1136 std::vector<T> &flat_data, std::vector<size_t> &shape) {
1138 shape.emplace_back(list.size());
1139 std::vector<std::initializer_list<T>> v_list(list);
1140 for (
size_t i{1}; i < v_list.size(); ++i)
1141 if (v_list[i].size() != v_list[i - 1].size())
1144 shape.emplace_back(v_list[0].size());
1145 for (
auto &item : v_list)
1146 for (
auto &subitem : item)
1147 flat_data.emplace_back(subitem);
1150template <
typename T>
1152 const std::initializer_list<std::initializer_list<std::initializer_list<T>>> &list,
1153 std::vector<T> &flat_data, std::vector<size_t> &shape) {
1154 shape.emplace_back(list.size());
1155 std::vector<std::initializer_list<std::initializer_list<T>>> v_list(list);
1156 for (
size_t i{1}; i < v_list.size(); ++i)
1157 if (v_list[i].size() != v_list[i - 1].size())
1160 shape.emplace_back(v_list[0].size());
1161 bool emplaced{
false};
1162 for (
size_t i{0}; i < v_list.size(); ++i) {
1163 std::vector<std::initializer_list<T>> v_sublist(v_list[i]);
1164 for (
size_t i{1}; i < v_sublist.size(); ++i)
1165 if (v_sublist[i].size() != v_sublist[i - 1].size())
1169 shape.emplace_back(v_sublist[0].size());
1172 for (
auto &item : v_sublist)
1173 for (
auto &subitem : item)
1174 flat_data.emplace_back(subitem);
Class that deals with the main tasks of prediction (inference)
A utility class for aggregation functions on tensors.
Exceptions concerning txeo::Tensor.
A utility class for common math functions on tensors.
Implements the mathematical concept of tensor, which is a magnitude of multiple order....
Tensor(Tensor &&tensor) noexcept
Tensor(txeo::TensorShape &&shape)
Constructs a Tensor from a specified txeo::TensorShape.
Tensor(const std::vector< size_t > &shape, const T &fill_value)
Constructs a Tensor from a specified shape std::vector and fills it with a value.
friend std::ostream & operator<<(std::ostream &os, const Tensor< U > &tensor)
friend txeo::Tensor< U > operator+(const txeo::Tensor< U > &left, const U &right)
Tensor< T > clone() const
Returns a clone of this tensor.
T * data()
Acesses the raw data of this tensor.
void fill_with_uniform_random(const T &min, const T &max)
Fills the tensor with uniformly distributed random values ranging according to the specified interval...
Tensor< T > & operator/=(const T &scalar)
bool operator!=(const Tensor &tensor)
Tensor(std::vector< size_t > &&shape, const T &fill_value)
Constructs a Tensor from a specified shape std::vector and fills it with a value.
Tensor & operator=(const Tensor &tensor)
void squeeze()
Reshapes this tensor by removing all the axes of dimension one.
Tensor(const std::initializer_list< std::initializer_list< std::initializer_list< T > > > &values)
Constructs a third order Tensor from a nested std::initializer_list.
std::unique_ptr< Impl > _impl
bool operator==(const Tensor &tensor)
Tensor & operator=(Tensor &&tensor) noexcept
friend txeo::Tensor< U > operator-(const txeo::Tensor< U > &left, const U &right)
Element-wise tensor-scalar addition operator.
void create_from_shape(P &&shape)
Tensor< T > flatten() const
Returns a first order reshaped view of this tensor.
Tensor< T > & operator+=(const Tensor< T > &tensor)
constexpr std::type_identity_t< T > type() const
Returns the data type of this tensor.
void shuffle()
Shuffles the elements of this tensor.
Tensor(const txeo::TensorShape &shape, const std::vector< T > &values)
Constructs a Tensor object from a specified txeo::TensorShape and fills it with a std::vector of valu...
Tensor< T > & operator-=(const Tensor< T > &tensor)
friend class txeo::detail::TensorHelper
Tensor(const std::initializer_list< size_t > &shape, const T &fill_value)
Constructs a Tensor from a specified initializer list and fills it with a value.
Tensor< T > & operator*=(const T &scalar)
bool is_equal_shape(const Tensor< U > &other) const
Compares the shape of this tensor with the shape of the specified tensor.
friend txeo::Tensor< U > operator+(const txeo::Tensor< U > &left, const txeo::Tensor< U > &right)
Returns the sum of two tensors.
txeo::TensorIterator< const T > begin() const
friend txeo::Tensor< U > operator/(const txeo::Tensor< U > &left, const U &right)
Element-wise division operator (tensor / scalar)
Tensor(const std::initializer_list< std::initializer_list< T > > &values)
Constructs a second order Tensor from a nested std::initializer_list.
Tensor(const std::initializer_list< size_t > &shape)
Constructs a Tensor from a specified shape std::vector.
friend txeo::Tensor< U > operator-(const txeo::Tensor< U > &left, const txeo::Tensor< U > &right)
Returns the subtraction of two tensors.
Tensor(std::vector< size_t > &&shape)
Constructs a Tensor from a specified shape std::vector.
Tensor(const txeo::TensorShape &shape, const T &fill_value)
Constructs a Tensor from a specified txeo::TensorShape and fills it with a value.
friend txeo::Tensor< U > operator/(const U &left, const txeo::Tensor< U > &right)
Element-wise scalar-tensor division operator.
txeo::TensorIterator< T > end()
const T * data() const
Reads the raw data of this tensor.
void fill(const T &value)
Fills this tensor with the specified value.
Tensor< T > & operator=(const T &value)
Assigns a specified value to this tensor elements.
Tensor(txeo::TensorShape &&shape, const T &fill_value)
Constructs a Tensor from a specified txeo::TensorShape and fills it with a value.
void reshape(const txeo::TensorShape &shape)
Reshapes this tensor if the specified shape defines a number of elements equal to this tensor order.
txeo::TensorIterator< const T > end() const
Tensor(const Tensor &tensor)
T & operator()()
Accesses the value of this tensor if it is a scalar (order zero).
Tensor(const std::initializer_list< size_t > &shape, const std::vector< T > &values)
Constructs a Tensor object from a specified initializer list and fills it with a std::vector of value...
friend txeo::Tensor< U > operator*(const txeo::Tensor< U > &tensor, const U &scalar)
Returns the scalar multiplication of a tensor.
Tensor< T > & operator+=(const T &tensor)
Tensor< T > & operator-=(const T &tensor)
Tensor(const std::vector< size_t > &shape)
Constructs a Tensor from a specified shape std::vector.
const txeo::TensorShape & shape() const
Returns the shape of this tensor.
void view_of(const Tensor< T > &tensor, const txeo::TensorShape &shape)
Views the content of the specified tensor according to the specified shape. There is no element copyi...
txeo::TensorIterator< T > begin()
void fill_with_uniform_random(const T &min, const T &max, const size_t &seed1, const size_t &seed2)
Fills the tensor with uniformly distributed random values ranging according to the specified interval...
size_t memory_size() const
Returns the number of bytes occupied by this tensor.
void check_indexes(const std::vector< size_t > &indexes)
Tensor(const txeo::TensorShape &shape)
Constructs a tensor from a specified txeo::TensorShape.
T & at()
Accesses the value of this tensor if it is a scalar (order zero).
Tensor(const std::vector< size_t > &shape, const std::vector< T > &values)
Constructs a Tensor object from a specified std::vector<size_t> and fills it with a std::vector of va...
size_t dim() const
Returns the dimension of this tensor.
friend txeo::Tensor< U > operator-(const U &left, const txeo::Tensor< U > &right)
Element-wise scalar-tensor subtraction operator.
size_t number_of_elements() const
Returns the number of elements of this tensor, which corresponds to the dimension of this tensor.
Tensor< T > slice(size_t first_axis_begin, size_t first_axis_end) const
Returns a view ot this tensor from a specified range of dimensions of the first axis.
int order() const
Returns the order of this tensor.
void fill_data_shape(const std::initializer_list< std::initializer_list< T > > &list, std::vector< T > &flat_data, std::vector< size_t > &shape)
A utility class for performing operations on tensors and vectors.
A utility class for partitioning tensors.
The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces.