Txeo v0.1
A Developer-Friendly TensorFlow C++ Wrapper
Loading...
Searching...
No Matches
txeo::TensorShape Class Reference

The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces. More...

#include <TensorShape.h>

Collaboration diagram for txeo::TensorShape:
Collaboration graph

Public Member Functions

 TensorShape (const TensorShape &shape)
 
 TensorShape (TensorShape &&shape) noexcept
 
 ~TensorShape ()
 
TensorShapeoperator= (const TensorShape &shape)
 
TensorShapeoperator= (TensorShape &&shape) noexcept
 
bool operator== (const TensorShape &shape) const
 
bool operator!= (const TensorShape &shape) const
 
 TensorShape (int number_of_axes, size_t dim)
 Constructs a tensor shape with axes having the same dimension.
 
 TensorShape (const std::vector< size_t > &shape)
 Constructs a tensor shape from a std::vector.
 
 TensorShape (std::vector< size_t > &&shape)
 Constructs a tensor shape from a std::vector.
 
 TensorShape (const std::initializer_list< size_t > &shape)
 Constructs a tensor shape from an initializer list.
 
int number_of_axes () const noexcept
 Returns the size of the tensor shape.
 
int size () const noexcept
 Synonym for txeo::TensorShape::number_of_axes()
 
int64_t axis_dim (int axis) const
 Returns the dimension of the specified axis.
 
const std::vector< size_t > & stride () const
 Returns the stride of each dimension in the tensor.
 
std::vector< int64_t > axes_dims () const noexcept
 Returns the collection of tensor shape dimensions.
 
bool is_fully_defined () const noexcept
 Indicates whether the tensor shape has any negative(undefined) dimensions or not.
 
void push_axis_back (size_t dim)
 Inserts a dimension after the last axis.
 
void insert_axis (int axis, size_t dim)
 Inserts a dimension at the specified axis.
 
void remove_axis (int axis)
 Removes a specified axis.
 
void remove_all_axes ()
 Removes all axes from this shape, resulting an empty shape.
 
void set_dim (int axis, size_t dim)
 Sets a dimension in a specified axis.
 
size_t calculate_capacity () const noexcept
 Calculates the number of available tensor elements specified by the tensor shape.
 
TensorShape clone () const
 Returns a clone of this tensor.
 

Friends

template<typename T >
class Tensor
 
template<typename T >
class Predictor
 
template<typename T >
class TensorAgg
 
class txeo::detail::TensorHelper
 
std::ostream & operator<< (std::ostream &os, const TensorShape &shape)
 

Detailed Description

The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces.

Each position of the tensor shape is an "axis", labeled starting from zero, and the value in this position is a "dimension". An empty tensor shape is the shape of a scalar value. In some some conditions, a negative dimension represents an undefined dimension, but this attribution is reserved to TensorFlow C++.

Definition at line 30 of file TensorShape.h.

Constructor & Destructor Documentation

◆ TensorShape() [1/6]

txeo::TensorShape::TensorShape ( const TensorShape shape)

◆ TensorShape() [2/6]

txeo::TensorShape::TensorShape ( TensorShape &&  shape)
noexcept

◆ ~TensorShape()

txeo::TensorShape::~TensorShape ( )

◆ TensorShape() [3/6]

txeo::TensorShape::TensorShape ( int  number_of_axes,
size_t  dim 
)
explicit

Constructs a tensor shape with axes having the same dimension.

Parameters
number_of_axesNumber of axes of the tensor
dimdimension in each axis
Exceptions
txeo::TensorShapeError

Example Usage:

#include <iostream>
int main() {
txeo::TensorShape shape(3, 5); // A tensor shape with 3 dimensions, each of size 5
std::cout << "TensorShape created: " << shape << std::endl;
return 0;
}
The shape of a tensor is an ordered collection of dimensions of mathematical vector spaces.
Definition TensorShape.h:30

◆ TensorShape() [4/6]

txeo::TensorShape::TensorShape ( const std::vector< size_t > &  shape)
explicit

Constructs a tensor shape from a std::vector.

Parameters
shapevector of dimensions

Example Usage:

#include <iostream>
int main() {
std::vector<size_t> dims = {3, 4, 5};
txeo::TensorShape shape(dims); // Creates a 3D shape with dimensions 3x4x5
std::cout << "TensorShape created: " << shape << std::endl;
return 0;
}

◆ TensorShape() [5/6]

txeo::TensorShape::TensorShape ( std::vector< size_t > &&  shape)
explicit

Constructs a tensor shape from a std::vector.

Parameters
shapevector of dimensions
  • Example Usage:
    #include <iostream>
    int main() {
    txeo::TensorShape shape({3, 4, 5}); // Creates a 3D shape with dimensions 3x4x5
    std::cout << "TensorShape created: " << shape << std::endl;
    return 0;
    }

◆ TensorShape() [6/6]

txeo::TensorShape::TensorShape ( const std::initializer_list< size_t > &  shape)
inlineexplicit

Constructs a tensor shape from an initializer list.

Parameters
shapevector of dimensions
  • Example Usage:
    #include <iostream>
    int main() {
    txeo::TensorShape shape({3, 4, 5}); // Creates a 3D shape with dimensions 3x4x5
    std::cout << "TensorShape created: " << shape << std::endl;
    return 0;
    }

Definition at line 123 of file TensorShape.h.

124 : TensorShape(std::vector<size_t>(shape)) {}
TensorShape(const TensorShape &shape)

Member Function Documentation

◆ axes_dims()

std::vector< int64_t > txeo::TensorShape::axes_dims ( ) const
noexcept

Returns the collection of tensor shape dimensions.

Returns
std::vector<int64_t>

◆ axis_dim()

int64_t txeo::TensorShape::axis_dim ( int  axis) const

Returns the dimension of the specified axis.

Parameters
axisAxis whose dimension is to be returned
Returns
int64_t Dimension
Exceptions
txeo::TensorShapeError

◆ calculate_capacity()

size_t txeo::TensorShape::calculate_capacity ( ) const
noexcept

Calculates the number of available tensor elements specified by the tensor shape.

Returns
int64_t Number of tensor elements

◆ clone()

TensorShape txeo::TensorShape::clone ( ) const

Returns a clone of this tensor.

Returns
TensorShape Clone of this tensor

◆ insert_axis()

void txeo::TensorShape::insert_axis ( int  axis,
size_t  dim 
)

Inserts a dimension at the specified axis.

Parameters
axisSpecified axis
dimSpecified dimension
Exceptions
txeo::TensorShapeError

Example Usage:

#include <iostream>
int main() {
txeo::TensorShape shape({2, 3}); // Initial shape (2,3)
shape.insert_axis(1, 4); // Inserts a new axis at index 1, resulting in shape (2,4,3)
std::cout << "Updated TensorShape: " << shape << std::endl;
return 0;
}
void insert_axis(int axis, size_t dim)
Inserts a dimension at the specified axis.

◆ is_fully_defined()

bool txeo::TensorShape::is_fully_defined ( ) const
noexcept

Indicates whether the tensor shape has any negative(undefined) dimensions or not.

Returns
true Does not have a negative dimension
false Has a negative dimension

◆ number_of_axes()

int txeo::TensorShape::number_of_axes ( ) const
noexcept

Returns the size of the tensor shape.

Returns
int Number of axes
Here is the caller graph for this function:

◆ operator!=()

bool txeo::TensorShape::operator!= ( const TensorShape shape) const

◆ operator=() [1/2]

TensorShape & txeo::TensorShape::operator= ( const TensorShape shape)

◆ operator=() [2/2]

TensorShape & txeo::TensorShape::operator= ( TensorShape &&  shape)
noexcept

◆ operator==()

bool txeo::TensorShape::operator== ( const TensorShape shape) const

◆ push_axis_back()

void txeo::TensorShape::push_axis_back ( size_t  dim)

Inserts a dimension after the last axis.

Parameters
dimSpecified dimension

Example Usage:

#include <iostream>
int main() {
txeo::TensorShape shape({2, 3}); // Initial shape (2,3)
shape.push_axis_back(4); // Adds a new axis, resulting in shape (2,3,4)
std::cout << "Updated TensorShape: " << shape << std::endl;
return 0;
}
void push_axis_back(size_t dim)
Inserts a dimension after the last axis.

◆ remove_all_axes()

void txeo::TensorShape::remove_all_axes ( )

Removes all axes from this shape, resulting an empty shape.

◆ remove_axis()

void txeo::TensorShape::remove_axis ( int  axis)

Removes a specified axis.

Parameters
axisSpecified axis
Exceptions
txeo::TensorShapeError

Example Usage:

#include <iostream>
int main() {
txeo::TensorShape shape({2, 3, 4}); // Initial shape (2,3,4)
shape.remove_axis(1); // Removes the axis at index 1, resulting in shape (2,4)
std::cout << "Updated TensorShape: " << shape << std::endl;
return 0;
}
void remove_axis(int axis)
Removes a specified axis.

◆ set_dim()

void txeo::TensorShape::set_dim ( int  axis,
size_t  dim 
)

Sets a dimension in a specified axis.

Parameters
axisAxis whose dimension will be changed
dimNew dimension
Exceptions
txeo::TensorShapeError

Example Usage:

#include <iostream>
int main() {
txeo::TensorShape shape({2, 3, 4}); // Initial shape (2,3,4)
shape.set_dim(1, 5); // Changes the second axis size from 3 to 5
std::cout << "Updated TensorShape: " << shape << std::endl;
return 0;
}
void set_dim(int axis, size_t dim)
Sets a dimension in a specified axis.

◆ size()

int txeo::TensorShape::size ( ) const
inlinenoexcept

Synonym for txeo::TensorShape::number_of_axes()

Returns
int Size of this shape

Definition at line 138 of file TensorShape.h.

138{ return this->number_of_axes(); };
int number_of_axes() const noexcept
Returns the size of the tensor shape.
Here is the call graph for this function:

◆ stride()

const std::vector< size_t > & txeo::TensorShape::stride ( ) const

Returns the stride of each dimension in the tensor.

The stride represents the step size needed to move along each dimension of the tensor in memory layout. It is useful for operations requiring efficient indexing.

Returns
The stride for each dimension.

Example Usage:

#include <iostream>
#include "txeo/Tensor.h"
int main() {
txeo::Tensor<int> tensor({3, 4, 5}); // Create a 3x4x5 tensor
const std::vector<size_t>& strides = tensor.stride();
std::cout << "Tensor strides: ";
for (size_t s : strides) {
std::cout << s << " ";
}
std::cout << std::endl;
return 0;
}
Implements the mathematical concept of tensor, which is a magnitude of multiple order....
Definition Tensor.h:48

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const TensorShape shape 
)
friend

◆ Predictor

template<typename T >
friend class Predictor
friend

Definition at line 314 of file TensorShape.h.

◆ Tensor

template<typename T >
friend class Tensor
friend

Definition at line 311 of file TensorShape.h.

◆ TensorAgg

template<typename T >
friend class TensorAgg
friend

Definition at line 317 of file TensorShape.h.

◆ txeo::detail::TensorHelper

friend class txeo::detail::TensorHelper
friend

Definition at line 319 of file TensorShape.h.


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