Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
tensor.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <initializer_list>
4#include <iostream>
5#include <tuple>
6#include <vector>
7#include <xtensor/containers/xarray.hpp>
8#include <xtensor/core/xmath.hpp>
9#include <xtensor/generators/xrandom.hpp>
10#include <xtensor/io/xio.hpp>
11#include <xtensor/reducers/xreducer.hpp>
12#include <xtensor/views/xview.hpp>
13#include <xtensor/core/xshape.hpp>
14
22namespace utils {
47 template<typename T>
48 class Tensor {
49 public:
58 Tensor() : data_(xt::xarray<T>::from_shape({0})), rows_(0), cols_(0) {}
59
64 explicit Tensor(const std::vector<size_t>& shape);
65
71 Tensor(size_t rows, size_t cols);
72
78 Tensor(const std::vector<size_t>& shape, T value);
79
86 Tensor(size_t rows, size_t cols, T value);
87
92 Tensor(std::initializer_list<std::initializer_list<T>> list);
93
98 explicit Tensor(const xt::xarray<T>& data);
99
102 template<typename U>
103 friend std::ostream &operator<<(std::ostream &os, const Tensor<U> &tensor);
104 template<typename U>
105 friend Tensor<U> dot(const Tensor<U> &a, const Tensor<U> &b);
106 template<typename U>
107 friend U sum(const Tensor<U> &tensor);
108 template<typename U>
109 friend U mean(const Tensor<U> &tensor);
110
122 template<typename... Args>
123 T &operator()(Args... indices);
124
131 template<typename... Args>
132 const T &operator()(Args... indices) const;
133
141 T &at(size_t row, size_t col);
142
150 const T &at(size_t row, size_t col) const;
151
165 Tensor operator+(const Tensor &other) const;
166
173 Tensor operator-(const Tensor &other) const;
174
181 Tensor operator*(const Tensor &other) const;
182
189 Tensor matmul(const Tensor &other) const;
190
196 Tensor operator*(T scalar) const;
197
210 Tensor transpose() const;
211
217 Tensor transpose(const std::vector<size_t>& axes) const;
218
225 Tensor reshape(const std::vector<size_t>& new_shape) const;
226
234 Tensor reshape(size_t new_rows, size_t new_cols) const;
235
242 Tensor view(const std::vector<size_t>& new_shape) const;
243
249 Tensor squeeze(int axis = -1) const;
250
256 Tensor unsqueeze(size_t axis) const;
257
263 T determinant() const;
264
270 Tensor inverse() const;
271
277 auto eigenvalues() const;
278
290 [[nodiscard]] size_t rows() const { return rows_; }
291
296 [[nodiscard]] size_t cols() const { return cols_; }
297
302 [[nodiscard]] size_t size() const { return rows_ * cols_; }
303
308 [[nodiscard]] std::tuple<size_t, size_t> shape() const { return {rows_, cols_}; }
309
322 static Tensor zeros(const std::vector<size_t>& shape);
323
330 static Tensor zeros(size_t rows, size_t cols);
331
337 static Tensor ones(const std::vector<size_t>& shape);
338
345 static Tensor ones(size_t rows, size_t cols);
346
353 static Tensor full(const std::vector<size_t>& shape, T value);
354
360 static Tensor identity(size_t size);
361
367 static Tensor random(const std::vector<size_t>& shape);
368
376 static Tensor random(const std::vector<size_t>& shape, T min, T max);
377
384 static Tensor random(size_t rows, size_t cols);
385
394 static Tensor random(size_t rows, size_t cols, T min, T max);
395
401 static Tensor from_array(const xt::xarray<T>& array);
402
409 xt::xarray<T> &data() { return data_; }
410
415 const xt::xarray<T> &data() const { return data_; }
416
417 private:
421 xt::xarray<T> data_;
422
426 size_t rows_, cols_;
427 };
428
429 // Template method implementations
430 template<typename T>
431 template<typename... Args>
432 T& Tensor<T>::operator()(Args... indices) {
433 return data_(indices...);
434 }
435
436 template<typename T>
437 template<typename... Args>
438 const T& Tensor<T>::operator()(Args... indices) const {
439 return data_(indices...);
440 }
441
453 template<typename T>
454 std::ostream &operator<<(std::ostream &os, const Tensor<T> &tensor);
455
462 template<typename T>
463 Tensor<T> dot(const Tensor<T> &a, const Tensor<T> &b);
464
470 template<typename T>
471 T sum(const Tensor<T> &tensor);
472
478 template<typename T>
479 T mean(const Tensor<T> &tensor);
480
492
497
502
507
509} // namespace utils
friend std::ostream & operator<<(std::ostream &os, const Tensor< U > &tensor)
Tensor()
Default constructor creating an empty tensor.
Definition tensor.hpp:58
size_t cols() const
Get the number of columns.
Definition tensor.hpp:296
static Tensor full(const std::vector< size_t > &shape, T value)
Create a tensor filled with a specific value.
Definition tensor.cpp:289
Tensor reshape(const std::vector< size_t > &new_shape) const
Reshape the tensor to new dimensions.
Definition tensor.cpp:156
static Tensor ones(const std::vector< size_t > &shape)
Create a tensor filled with ones.
Definition tensor.cpp:277
Tensor transpose() const
Compute the transpose of the tensor (for 2D tensors)
Definition tensor.cpp:128
static Tensor from_array(const xt::xarray< T > &array)
Create a tensor from an existing xt::xarray.
Definition tensor.cpp:348
static Tensor identity(size_t size)
Create an identity matrix (2D tensor)
Definition tensor.cpp:294
std::tuple< size_t, size_t > shape() const
Get the shape of the matric in one step.
Definition tensor.hpp:308
const T & operator()(Args... indices) const
Access tensor element at specified position (const)
Definition tensor.hpp:438
size_t size() const
Get the total number of elements.
Definition tensor.hpp:302
friend Tensor< U > dot(const Tensor< U > &a, const Tensor< U > &b)
static Tensor random(const std::vector< size_t > &shape)
Create a random tensor with values between 0 and 1.
Definition tensor.cpp:302
const xt::xarray< T > & data() const
Get the underlying xtensor array (const)
Definition tensor.hpp:415
Tensor squeeze(int axis=-1) const
Squeeze dimensions of size 1.
Definition tensor.cpp:189
auto eigenvalues() const
Calculate eigenvalues of the matrix (for 2D square tensors)
Definition tensor.cpp:256
friend U sum(const Tensor< U > &tensor)
Tensor view(const std::vector< size_t > &new_shape) const
Create a view of the tensor with new shape.
Definition tensor.cpp:175
Tensor unsqueeze(size_t axis) const
Add a dimension of size 1.
Definition tensor.cpp:220
xt::xarray< T > & data()
Get the underlying xtensor array.
Definition tensor.hpp:409
size_t rows() const
Get the number of rows.
Definition tensor.hpp:290
Tensor operator-(const Tensor &other) const
Tensor element-wise subtraction operator.
Definition tensor.cpp:82
Tensor inverse() const
Calculate the inverse of the matrix (for 2D square tensors)
Definition tensor.cpp:243
T determinant() const
Calculate the determinant of the matrix (for 2D square tensors)
Definition tensor.cpp:233
Tensor operator*(const Tensor &other) const
Tensor element-wise multiplication operator.
Definition tensor.cpp:93
Tensor matmul(const Tensor &other) const
Matrix multiplication operator (for 2D tensors)
Definition tensor.cpp:111
static Tensor zeros(const std::vector< size_t > &shape)
Create a zero tensor.
Definition tensor.cpp:267
T & at(size_t row, size_t col)
Access 2D tensor element at specified position (mutable) - backward compatibility.
Definition tensor.cpp:55
friend U mean(const Tensor< U > &tensor)
T & operator()(Args... indices)
Access tensor element at specified position (mutable)
Definition tensor.hpp:432
Tensor operator+(const Tensor &other) const
Tensor element-wise addition operator.
Definition tensor.cpp:71
T mean(const Tensor< T > &tensor)
Calculate mean of all tensor elements.
Definition tensor.cpp:381
std::ostream & operator<<(std::ostream &os, const Tensor< T > &tensor)
Output stream operator for tensor visualization.
Definition tensor.cpp:354
Tensor< T > dot(const Tensor< T > &a, const Tensor< T > &b)
Compute dot product of two tensors.
Definition tensor.cpp:360
T sum(const Tensor< T > &tensor)
Calculate sum of all tensor elements.
Definition tensor.cpp:376