Class Tensor¶
Defined in File TensorBody.h
Page Contents
Class Documentation¶
-
class
at
::
Tensor
¶ Public Types
Public Functions
-
Tensor
()¶
-
Tensor
(c10::intrusive_ptr<TensorImpl, UndefinedTensorImpl> tensor_impl)¶
-
int64_t
dim
() const¶
-
int64_t
storage_offset
() const¶
-
bool
is_complex
() const¶
-
int64_t
size
(int64_t dim) const¶
-
int64_t
stride
(int64_t dim) const¶
-
TensorImpl *
unsafeGetTensorImpl
() const¶
-
TensorImpl *
unsafeReleaseTensorImpl
()¶
-
const c10::intrusive_ptr<TensorImpl, UndefinedTensorImpl> &
getIntrusivePtr
() const¶
-
c10::intrusive_ptr<TensorImpl, UndefinedTensorImpl>
unsafeReleaseIntrusivePtr
()¶
-
bool
defined
() const¶
-
void
reset
()¶
-
size_t
use_count
() const noexcept¶
-
size_t
weak_use_count
() const noexcept¶
-
IntArrayRef
sizes
() const¶
-
IntArrayRef
strides
() const¶
-
DimnameList
names
() const¶
-
int64_t
ndimension
() const¶
-
bool
is_contiguous
(at::MemoryFormat memory_format = at::MemoryFormat::Contiguous) const¶
-
bool
is_non_overlapping_and_dense
() const¶
-
at::MemoryFormat
suggest_memory_format
(bool channels_last_strides_exact_match = false) const¶
-
size_t
nbytes
() const¶
-
int64_t
numel
() const¶
-
size_t
itemsize
() const¶
-
int64_t
element_size
() const¶
-
C10_DEPRECATED_MESSAGE ("Tensor.type() is deprecated. Instead use Tensor.options(), which in many cases (e.g. in a constructor) is a drop-in replacement. If you were using data from type(), that is now available from Tensor itself, so instead of tensor.type().scalar_type(), use tensor.scalar_type() instead and instead of tensor.type().backend() use tensor.device().") DeprecatedTypeProperties &type() const
-
DispatchKeySet
key_set
() const¶
-
ScalarType
scalar_type
() const¶
-
bool
has_storage
() const¶
-
const Storage &
storage
() const¶
-
C10_DEPRECATED_MESSAGE ("Tensor.is_variable() is deprecated; everything is a variable now. (If you want to assert that variable has been appropriately handled already, use at::impl::variable_excluded_from_dispatch())") bool is_variable() const noexcept
-
caffe2::TypeMeta
dtype
() const noexcept¶ Returns a
Tensor
’s dtype (TypeMeta
). Defined in TensorMethods.cpp.
-
bool
is_meta
() const¶ Returns if a
Tensor
is a meta tensor.Meta tensors can also have other designations.
-
QuantizerPtr
quantizer
() const¶ If a tensor is a quantized tensor, returns its quantizer TODO: it’s not in native_functions.yaml yet as it’s not exposed to python.
-
const NamedTensorMeta *
get_named_tensor_meta
() const¶ Returns a
Tensor
’s dimension names data structure.
-
NamedTensorMeta *
get_named_tensor_meta
()¶
-
TensorOptions
options
() const¶ Returns the
TensorOptions
corresponding to thisTensor
.Defined in TensorOptions.h.
-
void *
data_ptr
() const¶
-
template<typename T> C10_DEPRECATED_MESSAGE ("Tensor.data<T>() is deprecated. Please use Tensor.data_ptr<T>() instead.") T *data() const
-
void
print
() const¶
-
template<typename
T
, size_tN
, template<typename U> classPtrTraits
= DefaultPtrTraits, typenameindex_t
= int64_t>
GenericPackedTensorAccessor<T, N, PtrTraits, index_t>generic_packed_accessor
() const &¶
-
template<typename
T
, size_tN
, template<typename U> classPtrTraits
= DefaultPtrTraits, typenameindex_t
= int64_t>
GenericPackedTensorAccessor<T, N>generic_packed_accessor
() && = delete¶
-
template<typename
T
, size_tN
, template<typename U> classPtrTraits
= DefaultPtrTraits>
PackedTensorAccessor32<T, N, PtrTraits>packed_accessor32
() const &¶
-
template<typename
T
, size_tN
, template<typename U> classPtrTraits
= DefaultPtrTraits>
PackedTensorAccessor32<T, N, PtrTraits>packed_accessor32
() && = delete¶
-
template<typename
T
, size_tN
, template<typename U> classPtrTraits
= DefaultPtrTraits>
PackedTensorAccessor64<T, N, PtrTraits>packed_accessor64
() const &¶
-
template<typename
T
, size_tN
, template<typename U> classPtrTraits
= DefaultPtrTraits>
PackedTensorAccessor64<T, N, PtrTraits>packed_accessor64
() && = delete¶
-
template<typename T, size_t N, template< typename U > class PtrTraits = DefaultPtrTraits, typename index_t = int64_t> C10_DEPRECATED_MESSAGE ("packed_accessor is deprecated, use packed_accessor32 or packed_accessor64 instead") GenericPackedTensorAccessor<T
-
index_t
packed_accessor
() const &¶
-
template<typename T, size_t N, template< typename U > class PtrTraits = DefaultPtrTraits, typename index_t = int64_t> C10_DEPRECATED_MESSAGE ("packed_accessor is deprecated, use packed_accessor32 or packed_accessor64 instead") GenericPackedTensorAccessor<T
-
index_t
packed_accessor
() && = delete¶
-
void
backward
(const Tensor &gradient = {}, c10::optional<bool> retain_graph = c10::nullopt, bool create_graph = false, c10::optional<TensorList> inputs = c10::nullopt) const¶ Computes the gradient of current tensor with respect to graph leaves.
The graph is differentiated using the chain rule. If the tensor is non-scalar (i.e. its data has more than one element) and requires gradient, the function additionally requires specifying
gradient
. It should be a tensor of matching type and location, that contains the gradient of the differentiated function w.r.t. this Tensor.This function accumulates gradients in the leaves - you might need to zero them before calling it.
- Parameters
gradient
: Gradient w.r.t. the tensor. If it is a tensor, it will be automatically converted to a Tensor that does not require grad unlesscreate_graph
is True. None values can be specified for scalar Tensors or ones that don’t require grad. If a None value would be acceptable then this argument is optional.retain_graph
: Iffalse
, the graph used to compute the grads will be freed. Note that in nearly all cases setting this option to True is not needed and often can be worked around in a much more efficient way. Defaults to the value ofcreate_graph
.create_graph
: Iftrue
, graph of the derivative will be constructed, allowing to compute higher order derivative products. Defaults tofalse
.inputs
: Inputs w.r.t. which the gradient will be accumulated intoat::Tensor::grad
. All other Tensors will be ignored. If not provided, the gradient is accumulated into all the leaf Tensors that were used to compute the current tensor. All the provided inputs must be leaf Tensors.
-
bool
requires_grad
() const¶
-
Tensor &
mutable_grad
()¶ Return a mutable reference to the gradient.
This is conventionally used as
t.grad() = x
to set a gradient to a completely new tensor. Note that this function work with a non-const Tensor and is not thread safe.
-
const Tensor &
grad
() const¶ This function returns an undefined tensor by default and returns a defined tensor the first time a call to
backward()
computes gradients for this Tensor.The attribute will then contain the gradients computed and future calls to
backward()
will accumulate (add) gradients into it.
-
const Tensor &
fw_grad
(uint64_t level) const¶ This function returns the forward gradient for this Tensor at the given level.
-
void
set_fw_grad
(const Tensor &new_grad, uint64_t level, bool is_inplace_op)¶ This function can be used to set the value of the forward grad.
Note that the given new_grad might not be used directly if it has different metadata (size/stride/storage offset) compared to this Tensor. In that case, new_grad content will be copied into a new Tensor
-
void
_backward
(TensorList inputs, const c10::optional<Tensor> &gradient = {}, c10::optional<bool> retain_graph = c10::nullopt, bool create_graph = false) const¶
-
bool
is_leaf
() const¶ All Tensors that have
requires_grad()
which isfalse
will be leaf Tensors by convention.For Tensors that have
requires_grad()
which istrue
, they will be leaf Tensors if they were created by the user. This means that they are not the result of an operation and sograd_fn()
isnullptr
.Only leaf Tensors will have their
grad()
populated during a call tobackward()
. To getgrad()
populated for non-leaf Tensors, you can useretain_grad()
.Example:
auto a = torch::rand(10, torch::requires_grad()); std::cout << a.is_leaf() << std::endl; // prints `true` auto b = torch::rand(10, torch::requires_grad()).to(torch::kCUDA); std::cout << b.is_leaf() << std::endl; // prints `false` // b was created by the operation that cast a cpu Tensor into a cuda Tensor auto c = torch::rand(10, torch::requires_grad()) + 2; std::cout << c.is_leaf() << std::endl; // prints `false` // c was created by the addition operation auto d = torch::rand(10).cuda(); std::cout << d.is_leaf() << std::endl; // prints `true` // d does not require gradients and so has no operation creating it (that is tracked by the autograd engine) auto e = torch::rand(10).cuda().requires_grad_(); std::cout << e.is_leaf() << std::endl; // prints `true` // e requires gradients and has no operations creating it auto f = torch::rand(10, torch::device(torch::kCUDA).requires_grad(true)); std::cout << f.is_leaf() << std::endl; // prints `true` // f requires grad, has no operation creating it
-
int64_t
output_nr
() const¶
-
int64_t
_version
() const¶
-
bool
allclose
(const Tensor &other, double rtol = 1e-05, double atol = 1e-08, bool equal_nan = false) const¶
-
Tensor
as_strided
(IntArrayRef size, IntArrayRef stride, c10::optional<int64_t> storage_offset = c10::nullopt) const¶
-
Tensor &
as_strided_
(IntArrayRef size, IntArrayRef stride, c10::optional<int64_t> storage_offset = c10::nullopt) const¶
-
Tensor
baddbmm
(const Tensor &batch1, const Tensor &batch2, Scalar beta = 1, Scalar alpha = 1) const¶
-
Tensor &
baddbmm_
(const Tensor &batch1, const Tensor &batch2, Scalar beta = 1, Scalar alpha = 1) const¶
-
Tensor
clamp
(c10::optional<Scalar> min = c10::nullopt, c10::optional<Scalar> max = c10::nullopt) const¶
-
Tensor &
clamp_
(c10::optional<Scalar> min = c10::nullopt, c10::optional<Scalar> max = c10::nullopt) const¶
-
Tensor
clip
(c10::optional<Scalar> min = c10::nullopt, c10::optional<Scalar> max = c10::nullopt) const¶
-
Tensor &
clip_
(c10::optional<Scalar> min = c10::nullopt, c10::optional<Scalar> max = c10::nullopt) const¶
-
Tensor
diff
(int64_t n = 1, int64_t dim = -1, const c10::optional<Tensor> &prepend = {}, const c10::optional<Tensor> &append = {}) const¶
-
Tensor
new_empty
(IntArrayRef size, c10::optional<ScalarType> dtype, c10::optional<Layout> layout, c10::optional<Device> device, c10::optional<bool> pin_memory) const¶
-
Tensor
new_empty_strided
(IntArrayRef size, IntArrayRef stride, c10::optional<ScalarType> dtype, c10::optional<Layout> layout, c10::optional<Device> device, c10::optional<bool> pin_memory) const¶
-
Tensor
new_full
(IntArrayRef size, Scalar fill_value, c10::optional<ScalarType> dtype, c10::optional<Layout> layout, c10::optional<Device> device, c10::optional<bool> pin_memory) const¶
-
Tensor
new_zeros
(IntArrayRef size, c10::optional<ScalarType> dtype, c10::optional<Layout> layout, c10::optional<Device> device, c10::optional<bool> pin_memory) const¶
-
Tensor
unflatten
(int64_t dim, IntArrayRef sizes, c10::optional<DimnameList> names = c10::nullopt) const¶
-
Tensor &
index_put_
(const c10::List<c10::optional<Tensor>> &indices, const Tensor &values, bool accumulate = false) const¶
-
Tensor
index_put
(const c10::List<c10::optional<Tensor>> &indices, const Tensor &values, bool accumulate = false) const¶
-
Tensor
isclose
(const Tensor &other, double rtol = 1e-05, double atol = 1e-08, bool equal_nan = false) const¶
-
bool
is_distributed
() const¶
-
bool
is_floating_point
() const¶
-
bool
__dispatch_is_complex
() const¶
-
bool
is_nonzero
() const¶
-
bool
is_signed
() const¶
-
Tensor
nan_to_num
(c10::optional<double> nan = c10::nullopt, c10::optional<double> posinf = c10::nullopt, c10::optional<double> neginf = c10::nullopt) const¶
-
Tensor &
nan_to_num_
(c10::optional<double> nan = c10::nullopt, c10::optional<double> posinf = c10::nullopt, c10::optional<double> neginf = c10::nullopt) const¶
-
Tensor
mean
(IntArrayRef dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt) const¶
-
Tensor
mean
(DimnameList dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt) const¶
-
bool
is_pinned
() const¶
-
Tensor
detach
() const¶ Returns a new Tensor, detached from the current graph.
The result will never require gradient.
-
Tensor &
detach_
() const¶ Detaches the Tensor from the graph that created it, making it a leaf.
Views cannot be detached in-place.
-
int64_t
size
(Dimname dim) const¶
-
Tensor
slice
(int64_t dim = 0, c10::optional<int64_t> start = 0, c10::optional<int64_t> end = 9223372036854775807, int64_t step = 1) const¶
-
Tensor
stft
(int64_t n_fft, c10::optional<int64_t> hop_length = c10::nullopt, c10::optional<int64_t> win_length = c10::nullopt, const c10::optional<Tensor> &window = {}, bool normalized = false, c10::optional<bool> onesided = c10::nullopt, c10::optional<bool> return_complex = c10::nullopt) const¶
-
Tensor
istft
(int64_t n_fft, c10::optional<int64_t> hop_length = c10::nullopt, c10::optional<int64_t> win_length = c10::nullopt, const c10::optional<Tensor> &window = {}, bool center = true, bool normalized = false, c10::optional<bool> onesided = c10::nullopt, c10::optional<int64_t> length = c10::nullopt, bool return_complex = false) const¶
-
int64_t
stride
(Dimname dim) const¶
-
Tensor
sum
(IntArrayRef dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt) const¶
-
Tensor
sum
(DimnameList dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt) const¶
-
Tensor
nansum
(IntArrayRef dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt) const¶
-
Tensor
prod
(int64_t dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt) const¶
-
Tensor
prod
(Dimname dim, bool keepdim = false, c10::optional<ScalarType> dtype = c10::nullopt) const¶
-
Tensor &
resize_as_
(const Tensor &the_template, c10::optional<MemoryFormat> memory_format = c10::nullopt) const¶
-
int64_t
sparse_dim
() const¶
-
int64_t
_dimI
() const¶
-
int64_t
dense_dim
() const¶
-
int64_t
_dimV
() const¶
-
int64_t
_nnz
() const¶
-
bool
is_coalesced
() const¶
-
double
q_scale
() const¶
-
int64_t
q_zero_point
() const¶
-
int64_t
q_per_channel_axis
() const¶
-
QScheme
qscheme
() const¶
-
Tensor
to
(TensorOptions options = {}, bool non_blocking = false, bool copy = false, c10::optional<MemoryFormat> memory_format = c10::nullopt) const¶
-
Tensor
to
(c10::optional<ScalarType> dtype, c10::optional<Layout> layout, c10::optional<Device> device, c10::optional<bool> pin_memory, bool non_blocking, bool copy, c10::optional<MemoryFormat> memory_format) const¶
-
Tensor
to
(Device device, ScalarType dtype, bool non_blocking = false, bool copy = false, c10::optional<MemoryFormat> memory_format = c10::nullopt) const¶
-
Tensor
to
(ScalarType dtype, bool non_blocking = false, bool copy = false, c10::optional<MemoryFormat> memory_format = c10::nullopt) const¶
-
Tensor
to
(const Tensor &other, bool non_blocking = false, bool copy = false, c10::optional<MemoryFormat> memory_format = c10::nullopt) const¶
-
Scalar
item
() const¶
-
Tensor &
set_
(Storage source, int64_t storage_offset, IntArrayRef size, IntArrayRef stride = {}) const¶
-
Tensor &
addbmm_
(const Tensor &batch1, const Tensor &batch2, Scalar beta = 1, Scalar alpha = 1) const¶
-
Tensor &
random_
(int64_t from, c10::optional<int64_t> to, c10::optional<Generator> generator = c10::nullopt) const¶
-
Tensor &
uniform_
(double from = 0, double to = 1, c10::optional<Generator> generator = c10::nullopt) const¶
-
Tensor &
cauchy_
(double median = 0, double sigma = 1, c10::optional<Generator> generator = c10::nullopt) const¶
-
Tensor &
log_normal_
(double mean = 1, double std = 2, c10::optional<Generator> generator = c10::nullopt) const¶
-
std::tuple<Tensor, Tensor>
triangular_solve
(const Tensor &A, bool upper = true, bool transpose = false, bool unitriangular = false) const¶
-
Tensor
ormqr
(const Tensor &input2, const Tensor &input3, bool left = true, bool transpose = false) const¶
-
Tensor
multinomial
(int64_t num_samples, bool replacement = false, c10::optional<Generator> generator = c10::nullopt) const¶
-
Tensor
quantile
(const Tensor &q, c10::optional<int64_t> dim = c10::nullopt, bool keepdim = false) const¶
-
Tensor
nanquantile
(double q, c10::optional<int64_t> dim = c10::nullopt, bool keepdim = false) const¶
-
Tensor
nanquantile
(const Tensor &q, c10::optional<int64_t> dim = c10::nullopt, bool keepdim = false) const¶
-
std::tuple<Tensor, Tensor>
sort
(c10::optional<bool> stable, int64_t dim = -1, bool descending = false) const¶
-
std::tuple<Tensor, Tensor>
sort
(c10::optional<bool> stable, Dimname dim, bool descending = false) const¶
-
std::tuple<Tensor, Tensor>
topk
(int64_t k, int64_t dim = -1, bool largest = true, bool sorted = true) const¶
-
Tensor &
normal_
(double mean = 0, double std = 1, c10::optional<Generator> generator = c10::nullopt) const¶
-
Tensor
to
(Device device, caffe2::TypeMeta type_meta, bool non_blocking = false, bool copy = false) const¶
-
at::Tensor
tensor_data
() const¶ NOTE: This is similar to the legacy
.data()
function onVariable
, and is intended to be used from functions that need to access theVariable
’s equivalentTensor
(i.e.Tensor
that shares the same storage and tensor metadata with theVariable
).One notable difference with the legacy
.data()
function is that changes to the returnedTensor
’s tensor metadata (e.g. sizes / strides / storage / storage_offset) will not update the originalVariable
, due to the fact that this function shallow-copies theVariable
’s underlying TensorImpl.
-
at::Tensor
variable_data
() const¶ NOTE:
var.variable_data()
in C++ has the same semantics astensor.data
in Python, which create a newVariable
that shares the same storage and tensor metadata with the originalVariable
, but with a completely new autograd history.NOTE: If we change the tensor metadata (e.g. sizes / strides / storage / storage_offset) of a variable created from
var.variable_data()
, those changes will not update the original variablevar
. In.variable_data()
, we setallow_tensor_metadata_change_
to false to make such changes explicitly illegal, in order to prevent users from changing metadata ofvar.variable_data()
and expecting the original variablevar
to also be updated.
-
const std::shared_ptr<torch::autograd::Node> &
grad_fn
() const¶ Gets the gradient function of the
Variable
.If this is a leaf variable, the pointer returned will be null.
For View Variables: Gets the up-to-date grad_fn. If the shared data or base was modified, we re-create the grad_fn to express the up-to-date view relationship between this and the base Variable.
-
template<typename
T
>
hook_return_void_t<T>register_hook
(T &&hook) const¶ Registers a backward hook.
The hook will be called every time a gradient with respect to the Tensor is computed. The hook should have one of the following signature:
hook(Tensor grad) -> Tensor
The hook should not modify its argument, but it can optionally return a new gradient which will be used in place ofhook(Tensor grad) -> void
grad
.This function returns the index of the hook in the list which can be used to remove hook.
Example:
auto v = torch::tensor({0., 0., 0.}, torch::requires_grad()); auto h = v.register_hook([](torch::Tensor grad){ return grad * 2; }); // double the gradient v.backward(torch::tensor({1., 2., 3.})); // This prints: // ``` // 2 // 4 // 6 // [ CPUFloatType{3} ] // ``` std::cout << v.grad() << std::endl; v.remove_hook(h); // removes the hook
-
template<typename
T
>
hook_return_var_t<T>register_hook
(T &&hook) const¶
-
void
remove_hook
(unsigned pos) const¶ Remove hook at given position.
-
bool
is_view
() const¶ Returns true if this
Variable
is a view of anotherVariable
.
-
const Tensor &
_base
() const¶ Returns the
Variable
that thisVariable
is a view of.If this
Variable
is not a view, throw astd::runtime_error
.
-
template<typename
T
>
autoregister_hook
(T &&hook) const -> Tensor::hook_return_void_t<T>¶
Public Members
-
N
-
PtrTraits
Public Static Functions
Protected Functions
-
void
enforce_invariants
()¶
Protected Attributes
-
c10::intrusive_ptr<TensorImpl, UndefinedTensorImpl>
impl_
¶
-