tensorflow::TensorShape

#include <tensor_shape.h>

Represents the shape of a Tensor.

Summary

A tensor's shape is denoted by its number of dimensions and a size for each dimension. For example, a Tensor represented by a 3 x 4 matrix would have a shape of 2-D, [3,4].

If you know the exact shape of your Tensor when you create the TensorShape object, you can specify it then, or you can create a TensorShape with zero dimensions and one element, and call AddDim() to add dimensions later.

Constructors and Destructors

TensorShape(gtl::ArraySlice< int64 > dim_sizes)
Construct a TensorShape from the provided sizes.
TensorShape(std::initializer_list< int64 > dim_sizes)
TensorShape(const TensorShapeProto & proto)
REQUIRES: IsValid(proto)
TensorShape()
Create a tensor shape with no dimensions and one element, which you can then call AddDim() on.
TensorShape(const TensorShape & b)
Copy the specified shape.
TensorShape(TensorShape && b)
Move the specified shape. After moving, is safe for destruction and.
~TensorShape()

Friend classes

Tensor
friend class
TensorShapeTestHelper
friend class

Public attributes

buf[16]
uint8
unused_aligner
Rep64 *

Public functions

AddDim(int64 size)
void
Add a dimension to the end ("inner-most").
AppendShape(const TensorShape & shape)
void
Appends all the dimensions from shape.
AsEigenDSizes() const
Eigen::DSizes< Eigen::DenseIndex, NDIMS >
Fill *dsizes from *this.
AsEigenDSizesWithPadding() const
Eigen::DSizes< Eigen::DenseIndex, NDIMS >
Same as AsEigenDSizes() but allows for NDIMS > dims() in which case we pad the rest of the sizes with 1.
AsProto(TensorShapeProto *proto) const
void
Fill *proto from *this.
Clear()
void
Clear a tensor shape.
DebugString() const
string
For error messages.
DumpRep() const
void
InsertDim(int d, int64 size)
void
Insert a dimension somewhere in the TensorShape.
IsSameSize(const TensorShape & b) const
bool
Returns true if *this and b have the same sizes.
RemoveDim(int d)
void
Removes dimension d from the TensorShape.
begin() const
TensorShapeIter
For iterating through the dimensions.
dim_size(int d) const
int64
Returns the number of elements in dimension d.
dim_sizes() const
gtl::InlinedVector< int64, 4 >
Returns sizes of all dimensions.
dims() const
int
Return the number of dimensions in the tensor.
end() const
TensorShapeIter
num_elements() const
int64
Returns the number of elements in the tensor.
operator!=(const TensorShape & b) const
bool
operator=(const TensorShape & b)
void
operator=(TensorShape && b)
void
operator==(const TensorShape & b) const
bool
set_dim(int d, int64 size)
void
Modifies the size of the dimension d to be size REQUIRES: 0 <= d < dims() REQUIRES: size >= 0

Public static functions

DebugString(const TensorShapeProto & proto)
string
Same as TensorShape(proto).DebugString() but doesn't crash for invalid protos.
IsValid(const TensorShapeProto & proto)
bool
Returns true iff proto is a valid tensor shape.
IsValidShape(const TensorShapeProto & proto)
Returns OK iff proto is a valid tensor shape, and a descriptive error status otherwise.
MaxDimensions()
constexpr int

Friend classes

Tensor

friend class Tensor

TensorShapeTestHelper

friend class TensorShapeTestHelper

Public attributes

buf

uint8 buf[16]

unused_aligner

Rep64 * unused_aligner

Public functions

AddDim

void AddDim(
  int64 size
)

Add a dimension to the end ("inner-most").

REQUIRES: size >= 0

AppendShape

void AppendShape(
  const TensorShape & shape
)

Appends all the dimensions from shape.

AsEigenDSizes

Eigen::DSizes< Eigen::DenseIndex, NDIMS > AsEigenDSizes() const 

Fill *dsizes from *this.

AsEigenDSizesWithPadding

Eigen::DSizes< Eigen::DenseIndex, NDIMS > AsEigenDSizesWithPadding() const 

Same as AsEigenDSizes() but allows for NDIMS > dims() in which case we pad the rest of the sizes with 1.

AsProto

void AsProto(
  TensorShapeProto *proto
) const 

Fill *proto from *this.

Clear

void Clear()

Clear a tensor shape.

DebugString

string DebugString() const 

For error messages.

DumpRep

void DumpRep() const 

InsertDim

void InsertDim(
  int d,
  int64 size
)

Insert a dimension somewhere in the TensorShape.

REQUIRES: 0 <= d <= dims() REQUIRES: size >= 0

IsSameSize

bool IsSameSize(
  const TensorShape & b
) const 

Returns true if *this and b have the same sizes.

Ignores dimension names.

RemoveDim

void RemoveDim(
  int d
)

Removes dimension d from the TensorShape.

REQUIRES: 0 <= d < dims()

TensorShape

 TensorShape(
  gtl::ArraySlice< int64 > dim_sizes
)

Construct a TensorShape from the provided sizes.

REQUIRES: dim_sizes[i] >= 0

TensorShape

 TensorShape(
  std::initializer_list< int64 > dim_sizes
)

TensorShape

 TensorShape(
  const TensorShapeProto & proto
)

REQUIRES: IsValid(proto)

TensorShape

 TensorShape()

Create a tensor shape with no dimensions and one element, which you can then call AddDim() on.

TensorShape

 TensorShape(
  const TensorShape & b
)

Copy the specified shape.

TensorShape

 TensorShape(
  TensorShape && b
)

Move the specified shape. After moving, is safe for destruction and.

begin

TensorShapeIter begin() const 

For iterating through the dimensions.

dim_size

int64 dim_size(
  int d
) const 

Returns the number of elements in dimension d.

REQUIRES: 0 <= d < dims()

dim_sizes

gtl::InlinedVector< int64, 4 > dim_sizes() const 

Returns sizes of all dimensions.

dims

int dims() const 

Return the number of dimensions in the tensor.

end

TensorShapeIter end() const 

num_elements

int64 num_elements() const 

Returns the number of elements in the tensor.

We use int64 and not size_t to be compatible with Eigen::Tensor which uses ptrdiff_t.

operator!=

bool operator!=(
  const TensorShape & b
) const 

operator=

void operator=(
  const TensorShape & b
)

operator=

void operator=(
  TensorShape && b
)

operator==

bool operator==(
  const TensorShape & b
) const 

set_dim

void set_dim(
  int d,
  int64 size
)

Modifies the size of the dimension d to be size REQUIRES: 0 <= d < dims() REQUIRES: size >= 0

~TensorShape

 ~TensorShape()

Public static functions

DebugString

string DebugString(
  const TensorShapeProto & proto
)

Same as TensorShape(proto).DebugString() but doesn't crash for invalid protos.

IsValid

bool IsValid(
  const TensorShapeProto & proto
)

Returns true iff proto is a valid tensor shape.

IsValidShape

Status IsValidShape(
  const TensorShapeProto & proto
)

Returns OK iff proto is a valid tensor shape, and a descriptive error status otherwise.

MaxDimensions

constexpr int MaxDimensions()