CppMatrix
Loading...
Searching...
No Matches
cpp_matrix::neural_network::Tensor< Matrix > Class Template Referenceexport

Tensor is a matrix supports auto grad. More...

Public Types

using ElementType = typename Matrix::ElementType
 

Public Member Functions

 Tensor (Matrix m)
 Initialize a tensor with matrix data.
 
 Tensor (std::initializer_list< std::initializer_list< ElementType > > initData)
 Initialize a tensor with init data.
 
 Tensor (const Tensor &other)
 
 Tensor (Tensor &&other)
 
size_t Row () const
 
size_t Column () const
 
Tensor operator- () const
 
Tensor operator+ (ElementType v) const
 
Tensor operator- (const Tensor &other) const
 
Tensor operator* (const Tensor &other) const
 
Tensor AddToRow (const Tensor &other) const
 
Tensor Pow (ElementType n) const
 
Tensor operator+ (const Tensor &other) const
 
Tensor operator* (ElementType n) const
 
Tensor operator/ (ElementType n) const
 
Tensor Exp () const
 
ElementType operator[] (size_t row, size_t column) const
 
Tensor DivBy (ElementType v) const
 Applies the element-wise function: v / x.
 
Tensor Sum () const
 Returns the sum of all elements in the tensor.
 
Tensor Derivative (const Tensor &x) const
 Get derivative of tensor at x.
 
Tensor SumX () const
 Returns the sum of all elements in the tensor.
 

Detailed Description

template<Backend Matrix>
class cpp_matrix::neural_network::Tensor< Matrix >

Tensor is a matrix supports auto grad.

Member Function Documentation

◆ Derivative()

template<Backend Matrix>
Tensor cpp_matrix::neural_network::Tensor< Matrix >::Derivative ( const Tensor< Matrix > & x) const
inlineexport

Get derivative of tensor at x.

This function will return derivative of tensor at x.

For example:

auto x = Tensor {
{ 1, 2, 3 },
{ 4, 5, 6 },
};
auto y = Tensor {
{ 0.1, 0.2, 0.3 },
{ 0.4, 0.5, 0.6 },
};
auto f = 3*x + 4*y;
auto dx = f.Derivative(x);
auto dy = f.Derivative(y);
Tensor(Matrix m)
Initialize a tensor with matrix data.
Definition tensor.cpp:32

Because:

\[\begin{aligned} f &= 3 \cdot x + 4 \cdot y \\\\ {d \over dx}f &= {d \over dx}(3 \cdot x) + {d \over dx}(4 \cdot y) \\ &= 3 \cdot {d \over dx}x + 4 \cdot {d \over dx}y \\ &= 3 \\\\ {d \over dy}f &= {d \over dy}(3 \cdot x) + {d \over dy}(4 \cdot y) \\ &= 3 \cdot {d \over dy}x + 4 \cdot {d \over dy}y \\ &= 4 \end{aligned} \]

So after executing, f, dx and dy should be:

\[\begin{aligned} f &= \begin{bmatrix} 3.4 & 6.8 & 10.2 \\ 13.6 & 17.0 & 20.4 \end{bmatrix} \\\\ dx &= \begin{bmatrix} 3 & 3 & 3 \\ 3 & 3 & 3 \end{bmatrix} \\\\ dy &= \begin{bmatrix} 4 & 4 & 4 \\ 4 & 4 & 4 \end{bmatrix} \end{aligned} \]

◆ Sum()

template<Backend Matrix>
Tensor cpp_matrix::neural_network::Tensor< Matrix >::Sum ( ) const
inlineexport

Returns the sum of all elements in the tensor.

Returns
Should be a 1x1 Tensor.

◆ SumX()

template<Backend Matrix>
Tensor cpp_matrix::neural_network::Tensor< Matrix >::SumX ( ) const
inlineexport

Returns the sum of all elements in the tensor.

Returns
Should be a 1x1 Tensor.

The documentation for this class was generated from the following file: