Open3D (C++ API)
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Member Functions | Static Public Member Functions
open3d::Tensor Class Reference

#include <Tensor.h>

Public Member Functions

 Tensor ()
 
 Tensor (const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
 Constructor for creating a contiguous Tensor. More...
 
template<typename T >
 Tensor (const std::vector< T > &init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
 Constructor for creating a contiguous Tensor with initial values. More...
 
 Tensor (const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
 The fully specified constructor. More...
 
 Tensor (const Tensor &other)
 Shallow copy constructor with lvalue input, e.g. Tensor dst(src). More...
 
 Tensor (Tensor &&other)
 Shallow copy constructor with rvalue input, e.g. Tensor dst(src[0]). More...
 
Tensoroperator= (const Tensor &other) &
 Tensor assignment lvalue = lvalue, e.g. tensor_a = tensor_b More...
 
Tensoroperator= (Tensor &&other) &
 Tensor assignment lvalue = rvalue, e.g. tensor_a = tensor_b[0] More...
 
Tensoroperator= (const Tensor &other) &&
 Tensor assignment rvalue = lvalue, e.g. tensor_a[0] = tensor_b More...
 
Tensoroperator= (Tensor &&other) &&
 Tensor assignment rvalue = rvalue, e.g. tensor_a[0] = tensor_b[0] More...
 
template<typename T >
Tensoroperator= (const T &v) &&
 
Tensor GetItem (const TensorKey &tk) const
 
Tensor GetItem (const std::vector< TensorKey > &tks) const
 
Tensor SetItem (const Tensor &value)
 Set all items. Equivalent to tensor[:] = value in Python. More...
 
Tensor SetItem (const TensorKey &tk, const Tensor &value)
 
Tensor SetItem (const std::vector< TensorKey > &tks, const Tensor &value)
 
DLManagedTensorToDLPack () const
 
void Assign (const Tensor &other)
 Assign (copy) values from another Tensor, shape, dtype, device may change. More...
 
template<typename T >
void Fill (T v)
 Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor's dtype. More...
 
Tensor Broadcast (const SizeVector &dst_shape) const
 Broadcast Tensor to a new broadcastable shape. More...
 
Tensor Expand (const SizeVector &dst_shape) const
 
Tensor Reshape (const SizeVector &dst_shape) const
 
Tensor View (const SizeVector &dst_shape) const
 
Tensor Copy (const Device &device) const
 
void CopyFrom (const Tensor &other)
 Copy Tensor values to current tensor for source tensor. More...
 
void ShallowCopyFrom (const Tensor &other)
 Shallow copy a tensor, returning a tensor sharing the same memory. More...
 
Tensor To (Dtype dtype, bool copy=false) const
 
std::string ToString (bool with_suffix=true, const std::string &indent="") const
 
Tensor operator[] (int64_t i) const
 Extract the i-th Tensor along the first axis, returning a new view. More...
 
Tensor IndexExtract (int64_t dim, int64_t idx) const
 
Tensor Slice (int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
 Slice Tensor. More...
 
Tensor AsRvalue () const
 
template<>
 Tensor (const std::vector< bool > &init_vals, const SizeVector &shape, Dtype dtype, const Device &device)
 

Static Public Member Functions

static Tensor Empty (const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
 Create a tensor with uninitilized values. More...
 
template<typename T >
static Tensor Full (const SizeVector &shape, T fill_value, Dtype dtype, const Device &device=Device("CPU:0"))
 Create a tensor fill with specified value. More...
 
static Tensor Zeros (const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
 Create a tensor fill with zeros. More...
 
static Tensor Ones (const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
 Create a tensor fill with ones. More...
 
static Tensor FromDLPack (DLManagedTensor *src)
 

Detailed Description

A Tensor is a "view" of a data Blob with shape, stride, data_ptr. Tensor can also be used to perform numerical operations.

Constructor & Destructor Documentation

◆ Tensor() [1/7]

open3d::Tensor::Tensor ( )
inline

◆ Tensor() [2/7]

open3d::Tensor::Tensor ( const SizeVector shape,
Dtype  dtype,
const Device device = Device("CPU:0") 
)
inline

Constructor for creating a contiguous Tensor.

◆ Tensor() [3/7]

template<typename T >
open3d::Tensor::Tensor ( const std::vector< T > &  init_vals,
const SizeVector shape,
Dtype  dtype,
const Device device = Device("CPU:0") 
)
inline

Constructor for creating a contiguous Tensor with initial values.

◆ Tensor() [4/7]

open3d::Tensor::Tensor ( const SizeVector shape,
const SizeVector strides,
void *  data_ptr,
Dtype  dtype,
const std::shared_ptr< Blob > &  blob 
)
inline

The fully specified constructor.

◆ Tensor() [5/7]

open3d::Tensor::Tensor ( const Tensor other)
inline

Shallow copy constructor with lvalue input, e.g. Tensor dst(src).

◆ Tensor() [6/7]

open3d::Tensor::Tensor ( Tensor &&  other)
inline

Shallow copy constructor with rvalue input, e.g. Tensor dst(src[0]).

◆ Tensor() [7/7]

template<>
open3d::Tensor::Tensor ( const std::vector< bool > &  init_vals,
const SizeVector shape,
Dtype  dtype,
const Device device 
)
inline

Member Function Documentation

◆ AsRvalue()

Tensor open3d::Tensor::AsRvalue ( ) const
inline

Convert to rvalue such that the Tensor can be assigned. E.g. in numpy tensor_a = tensor_b # tensor_a is lvalue, tensor_a variable will

now referecne tensor_b, that is, tensor_a

and tensor_b share exactly the same memory.

tensor_a[:] = tensor_b # tensor_a[:] is rvalue, tensor_b's values are

assigned to tensor_a's memory.

◆ Assign()

void open3d::Tensor::Assign ( const Tensor other)

Assign (copy) values from another Tensor, shape, dtype, device may change.

Assign (copy) values from another Tensor, shape, dtype, device may change. Slices of the original Tensor still keeps the original memory. After assignment, the Tensor will be contiguous.

◆ Broadcast()

Tensor open3d::Tensor::Broadcast ( const SizeVector dst_shape) const

Broadcast Tensor to a new broadcastable shape.

◆ Copy()

Tensor open3d::Tensor::Copy ( const Device device) const

Copy Tensor to a specified device The resulting Tensor will be compacted and contiguous

◆ CopyFrom()

void open3d::Tensor::CopyFrom ( const Tensor other)

Copy Tensor values to current tensor for source tensor.

◆ Empty()

Tensor open3d::Tensor::Empty ( const SizeVector shape,
Dtype  dtype,
const Device device = Device("CPU:0") 
)
static

Create a tensor with uninitilized values.

◆ Expand()

Tensor open3d::Tensor::Expand ( const SizeVector dst_shape) const

Expand Tensor to a new broadcastable shape, returning a new view.

Tensors can be expanded to broadcastable shape by setting dimension of size 1 to have stride 0, without allocating new memory.

◆ Fill()

template<typename T >
void open3d::Tensor::Fill ( v)
inline

Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor's dtype.

◆ FromDLPack()

static Tensor open3d::Tensor::FromDLPack ( DLManagedTensor src)
inlinestatic

◆ Full()

template<typename T >
static Tensor open3d::Tensor::Full ( const SizeVector shape,
fill_value,
Dtype  dtype,
const Device device = Device("CPU:0") 
)
inlinestatic

Create a tensor fill with specified value.

◆ GetItem() [1/2]

Tensor open3d::Tensor::GetItem ( const TensorKey tk) const

Pythonic getitem for tensor.

Returns a view of the original tensor, if TensorKey is TensorKeyMode::Index or TensorKeyMode::Slice. Returns a copy if the TensorKey contains TensorKeyMode::IndexTensor (advanced indexing).

For example, in numpy:

t = np.empty((4, 5), dtype=np.float32)
t1 = t[2]
t2 = t[0:4:2]

The equivalent Open3D C++ calls:

Tensor t1 = t.GetItem(TensorIndex(2));
Tensor t2 = t.GetItem(TensorSlice(0, 4, 2));

◆ GetItem() [2/2]

Tensor open3d::Tensor::GetItem ( const std::vector< TensorKey > &  tks) const

Pythonic getitem for tensor.

Returns a view of the original tensor, if TensorKey only contains TensorKeyMode::Index or TensorKeyMode::Slice. Returns a copy if the TensorKey contains IndexTensor (advanced indexing).

For example, in numpy:

t = np.empty((4, 5), dtype=np.float32)
t1 = t[1, 0:4:2]

The equivalent Open3D C++ calls:

Tensor t1 = t.GetItem({TensorIndex(2), TensorSlice(0, 4, 2)});

◆ IndexExtract()

Tensor open3d::Tensor::IndexExtract ( int64_t  dim,
int64_t  idx 
) const

Extract the idx -th sub-tensor in dimension dim. After IndexExtract, the dimension dim will be removed.

◆ Ones()

Tensor open3d::Tensor::Ones ( const SizeVector shape,
Dtype  dtype,
const Device device = Device("CPU:0") 
)
static

Create a tensor fill with ones.

◆ operator=() [1/5]

Tensor & open3d::Tensor::operator= ( const Tensor other) &

Tensor assignment lvalue = lvalue, e.g. tensor_a = tensor_b

Tensor assignment lvalue = lvalue, e.g. tensor_a = tensor_b, resulting in a "shallow" copy.

◆ operator=() [2/5]

Tensor & open3d::Tensor::operator= ( Tensor &&  other) &

Tensor assignment lvalue = rvalue, e.g. tensor_a = tensor_b[0]

Tensor assignment lvalue = rvalue, e.g. tensor_a = tensor_b[0], resulting in a "shallow" copy.

◆ operator=() [3/5]

Tensor & open3d::Tensor::operator= ( const Tensor other) &&

Tensor assignment rvalue = lvalue, e.g. tensor_a[0] = tensor_b

◆ operator=() [4/5]

Tensor & open3d::Tensor::operator= ( Tensor &&  other) &&

Tensor assignment rvalue = rvalue, e.g. tensor_a[0] = tensor_b[0]

◆ operator=() [5/5]

template<typename T >
Tensor& open3d::Tensor::operator= ( const T &  v) &&
inline

Tensor assignment rvalue = rvalue_scalar, e.g. tensor_a[0] = 100 Implicit casting is performed to the underlying dtype.

Note that we don't have lvalue = rvalue_scalar, e.g. we don't support Tensor a_slice = tensor_a[0]; a_slice = 100;

◆ operator[]()

Tensor open3d::Tensor::operator[] ( int64_t  i) const

Extract the i-th Tensor along the first axis, returning a new view.

◆ Reshape()

Tensor open3d::Tensor::Reshape ( const SizeVector dst_shape) const

Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy.

Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.

Ref: https://pytorch.org/docs/stable/tensors.html aten/src/ATen/native/TensorShape.cpp aten/src/ATen/TensorUtils.cpp

◆ SetItem() [1/3]

Tensor open3d::Tensor::SetItem ( const Tensor value)

Set all items. Equivalent to tensor[:] = value in Python.

◆ SetItem() [2/3]

Tensor open3d::Tensor::SetItem ( const TensorKey tk,
const Tensor value 
)

Pythonic setitem for tensor.

For example, in numpy:

t = np.empty((4, 5), dtype=np.float32)
t[2] = np.empty((5,), dtype=np.float32)
t[0:4:2] = np.empty((2, 5), dtype=np.float32)

The equivalent Open3D C++ calls:

t.SetItem(TensorIndex(2), Tensor({5}, Dtype::Float32));
t.SetItem(TensorSlice(0, 4, 2), Tensor({2, 5}, Dtype::Float32));

◆ SetItem() [3/3]

Tensor open3d::Tensor::SetItem ( const std::vector< TensorKey > &  tks,
const Tensor value 
)

Pythonic setitem for tensor.

For example, in numpy:

t = np.empty((4, 5), dtype=np.float32)
t[2, 0:4:2] = np.empty((2, 5), dtype=np.float32)

The equivalent Open3D C++ calls:

t.SetItem({TensorIndex(2), TensorSlice(0, 4, 2)},

◆ ShallowCopyFrom()

void open3d::Tensor::ShallowCopyFrom ( const Tensor other)

Shallow copy a tensor, returning a tensor sharing the same memory.

◆ Slice()

Tensor open3d::Tensor::Slice ( int64_t  dim,
int64_t  start,
int64_t  stop,
int64_t  step = 1 
) const

Slice Tensor.

◆ To()

Tensor open3d::Tensor::To ( Dtype  dtype,
bool  copy = false 
) const

Returns a tensor with the specified dtype.

Parameters
dtypeThe targeted dtype to convert to.
copyIf true, a new tensor is always created; if false, the copy is avoided when the original tensor already have the targeted dtype.

◆ ToDLPack()

DLManagedTensor* open3d::Tensor::ToDLPack ( ) const
inline

◆ ToString()

std::string open3d::Tensor::ToString ( bool  with_suffix = true,
const std::string &  indent = "" 
) const

◆ View()

Tensor open3d::Tensor::View ( const SizeVector dst_shape) const

Returns a new tensor view with the same data but of a different shape.

The returned tensor shares the same data and must have the same number of elements, but may have a different size. For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions d, d+1, ..., d+kd,d+1, ..., d+k that satisfy the following contiguity-like condition that for all i = 0, ..., k-1, strides[i] = stride[i + 1] * shape[i + 1].

Otherwise, contiguous() needs to be called before the tensor can be viewed. See also: reshape(), which returns a view if the shapes are compatible, and copies (equivalent to calling contiguous()) otherwise.

Ref: https://pytorch.org/docs/stable/tensors.html aten/src/ATen/native/TensorShape.cpp aten/src/ATen/TensorUtils.cpp

◆ Zeros()

Tensor open3d::Tensor::Zeros ( const SizeVector shape,
Dtype  dtype,
const Device device = Device("CPU:0") 
)
static

Create a tensor fill with zeros.


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