Tensor

class dragon.vm.torch.Tensor(
  *args,
  **kwargs
)[source]

A multi-dimensional array containing elements of a single data type.

To create a tensor from constant value, use torch.tensor(...):

# Create a constant tensor
# The value is 1, dimensions is (0,)
const_tensor = torch.tensor(1, dtype=torch.float32)

Besides, following initializers can also be used:

# Create an empty float32 tensor
# The dimensions is (1, 2)
empty_tensor = torch.empty(1, 2, dtype=torch.float32)

# Create a float32 tensor filling ``one`` or ``zero``
ones = torch.ones(1, 2, dtype=torch.float32)
zeros = torch.zeros(1, 2, dtype=torch.float32)

Construct a tensor with device and grad will sometimes be helpful:

# Initialize a weight and bias on the gpu:0, whose gradient should not be ignored
weight = torch.ones(1, 2, device=torch.device('cuda', 0), requires_grad=True)
bias = torch.tensor(0, device=torch.device('cuda', 0), requires_grad=True)

Be careful to store a tensor object, or the memory will not be free:

# The memory of ``my_tensor`` will be held
# until the reference count decrease to zero
my_object.tensors = []
my_object.tensors.append(my_tensor)

Properties

data

Tensor.data

Return a data reference detaching the grad.

Returns:
dragon.vm.torch.Tensor – The data tensor.

dtype

Tensor.dtype

Return the data type.

Returns:
str – The data type.

device

Tensor.device

Return the device of this tensor.

Returns:
dragon.vm.torch.device – The device.

grad

Tensor.grad

Return the grad of this tensor.

Returns:
dragon.vm.torch.Tensor – The grad tensor.

id

Tensor.id

Return the tensor identity.

Returns:
str – The identity.

is_leaf

Tensor.is_leaf

Return whether tensor is a leaf.

Returns:
boolTrue if this is a leaf tensor otherwise False.

requires_grad

Tensor.requires_grad

Return whether the gradient will be recorded.

Returns:
boolTrue to record gradient otherwise False.

shape

Tensor.shape

Return the shape of this tensor.

Returns:
dragon.vm.torch.Size – The shape.

T

Tensor.T

Return a tensor with dimensions reversed.

Returns:
dragon.vm.torch.Tensor – The output tensor.

Methods

abs

Tensor.abs()[source]

Return a tensor with the absolute value.

\[\text{out} = \left| \text{self} \right| \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.abs(…)

add

Tensor.add(other)[source]

Compute the element-wise addition.

\[\text{out} = \text{self} + \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.add(…)

add_

Tensor.add_(other)[source]

Compute the element-wise addition.

\[\text{self} \mathrel{+}= \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.add(…)

addmm

Tensor.addmm(
  mat1,
  mat2,
  beta=1,
  alpha=1
)[source]

Add the result of matrix-matrix multiplication.

\[\text{out} = \alpha (\text{mat1} \times \text{mat2}) + \beta \text{self} \]
Parameters:
  • mat1 (dragon.vm.torch.Tensor) – The first matrix.
  • mat2 (dragon.vm.torch.Tensor) – The second matrix.
  • beta (float, optional, default=1) – The value to \(\beta\).
  • alpha (float, optional, default=1) – The value to \(\alpha\).
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.addmm(…)

argmax

Tensor.argmax(
  dim,
  keepdim=False
)[source]

Return the index of maximum elements.

Parameters:
  • dim (int) – The dimension to reduce.
  • keepdim (bool, optional, default=False) – Keep the reduced dimension or not.
Returns:

dragon.vm.torch.Tensor – The index of maximum elements.

argmin

Tensor.argmin(
  dim,
  keepdim=False
)[source]

Return the index of minimum elements.

Parameters:
  • dim (int) – The dimension to reduce.
  • keepdim (bool, optional, default=False) – Keep the reduced dimension or not.
Returns:

dragon.vm.torch.Tensor – The index of minimum elements.

argsort

Tensor.argsort(
  dim=- 1,
  descending=False
)[source]

Return the index of sorted elements.

Parameters:
  • dim (int, optional, default=-1) – The dimension to sort elements.
  • descending (bool, optional, default=False) – Sort in the descending order or not.
Returns:

dragon.vm.torch.Tensor – The output tensor.

backward

Tensor.backward(
  gradient=None,
  retain_graph=False
)[source]

Compute the derivatives of this tensor w.r.t. graph leaves.

Parameters:
  • gradient (dragon.vm.torch.Tensor, optional) – The optional gradient of this tensor.
  • retain_graph (bool, optional, default=False) – False to free the graph used to compute grad.

baddbmm

Tensor.baddbmm(
  batch1,
  batch2,
  beta=1,
  alpha=1
)[source]

Add the result of batched matrix-matrix multiplication.

\[\text{out}_{i} = \alpha (\text{batch1}_{i} \times \text{batch2}_{i}) + \beta \text{self}_{i} \]
Parameters:
  • batch1 (dragon.vm.torch.Tensor) – The first batch of matrices.
  • batch2 (dragon.vm.torch.Tensor) – The second batch of matrices.
  • beta (float, optional, default=1) – The value to \(\beta\).
  • alpha (float, optional, default=1) – The value to \(\alpha\).
Returns:

dragon.vm.torch.Tensor – The output tensor.

baddbmm_

Tensor.baddbmm_(
  batch1,
  batch2,
  beta=1,
  alpha=1
)[source]

Add the result of batched matrix-matrix multiplication.

\[\text{self}_{i} = \alpha (\text{batch1}_{i} \times \text{batch2}_{i}) + \beta \text{self}_{i} \]
Parameters:
  • batch1 (dragon.vm.torch.Tensor) – The first batch of matrices.
  • batch2 (dragon.vm.torch.Tensor) – The second batch of matrices.
  • beta (float, optional, default=1) – The value to \(\beta\).
  • alpha (float, optional, default=1) – The value to \(\alpha\).
Returns:

dragon.vm.torch.Tensor – The output tensor.

bitwise_and

Tensor.bitwise_and(other)[source]

Compute the element-wise AND bitwise operation.

\[\text{out} = \text{self} \mathbin{\&} \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

bitwise_and_

Tensor.bitwise_and_(other)[source]

Compute the element-wise AND bitwise operation.

\[\text{self} = \text{self} \mathbin{\&} \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

bitwise_not

Tensor.bitwise_not()[source]

Compute the element-wise NOT bitwise operation.

\[\text{out} = \,\,\sim \text{self} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

bitwise_not_

Tensor.bitwise_not_()[source]

Compute the element-wise NOT bitwise operation.

\[\text{self} = \,\,\sim \text{self} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

bitwise_or

Tensor.bitwise_or(other)[source]

Compute the element-wise OR bitwise operation.

\[\text{out} = \text{self} \mathbin{|} \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

bitwise_or_

Tensor.bitwise_or_(other)[source]

Compute the element-wise OR bitwise operation.

\[\text{self} = \text{self} \mathbin{|} \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

bitwise_xor

Tensor.bitwise_xor(other)[source]

Compute the element-wise XOR bitwise operation.

\[\text{out} = \text{self} \oplus \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

bitwise_xor_

Tensor.bitwise_xor_(other)[source]

Compute the element-wise XOR bitwise operation.

\[\text{self} = \text{self} \oplus \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

bmm

Tensor.bmm(batch2)[source]

Compute the batched matrix multiplication.

\[\text{out}_{i} = \text{self}_{i} \times \text{batch2}_{i} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.bmm(…)

bool

Tensor.bool()[source]

Return a bool tensor with the same data.

Returns:
dragon.vm.torch.Tensor – The output tensor.

bool_

Tensor.bool_()[source]

Cast to a bool tensor.

Returns:
dragon.vm.torch.Tensor – The output tensor.

byte

Tensor.byte()[source]

Return an uint8 tensor with the same data.

Returns:
dragon.vm.torch.Tensor – The output tensor.

byte_

Tensor.byte_()[source]

Cast to an uint8 tensor.

Returns:
dragon.vm.torch.Tensor – The output tensor.

ceil

Tensor.ceil()[source]

Return a tensor taken the ceil of elements.

\[\text{out} = \lceil \text{self} \rceil \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.ceil(…)

ceil_

Tensor.ceil_()[source]

Set to the ceil of elements.

\[\text{self} = \lceil \text{self} \rceil \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.ceil(…)

char

Tensor.char()[source]

Return an int8 tensor with the same data.

Returns:
dragon.vm.torch.Tensor – The output tensor.

char_

Tensor.char_()[source]

Cast to an int8 tensor.

Returns:
dragon.vm.torch.Tensor – The output tensor.

chunk

Tensor.chunk(
  chunks,
  dim=0,
  copy=True
)[source]

Split self into several parts along the given dim.

Parameters:
  • chunks (int) – The number of chunks to split.
  • dim (int, optional, default=0) – The dimension to split.
  • copy (bool, optional, default=True) – Copy or create the views of input.
Returns:

Sequence[dragon.vm.torch.Tensor] – The output chunks.

clamp

Tensor.clamp(
  min=None,
  max=None
)[source]

Return a tensor with elements clamped into a range.

Parameters:
  • min (number, optional) – The min value.
  • max (number, optional) – The max value.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.clamp(…)

clamp_

Tensor.clamp_(
  min=None,
  max=None
)[source]

Clamp elements into the a range.

Parameters:
  • min (number, optional) – The min value.
  • max (number, optional) – The max value.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.clamp(…)

contiguous

Tensor.contiguous()[source]

Return a tensor with contiguous memory.

Returns:
dragon.vm.torch.Tensor – The output tensor.

copy_

Tensor.copy_(src)[source]

Copy the elements into this tensor.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

cos

Tensor.cos()[source]

Compute the cos.

\[\text{out} = \cos(\text{self}) \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.cos(…)

cpu

Tensor.cpu()[source]

Switch the internal storage on cpu memory.

Returns:
dragon.vm.torch.Tensor – The output tensor.

cuda

Tensor.cuda(device=None)[source]

Copy memory to the specified cuda device.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

cumsum

Tensor.cumsum(dim)[source]

Return a tensor with the cumulative sum of elements.

Parameters:
  • dim (int) – The cumulative dimension.
Returns:

dragon.vm.torch.Tensor – The output tensor.

detach

Tensor.detach()[source]

Return a data reference detaching the grad.

Returns:
dragon.vm.torch.Tensor – The data tensor.

dim

Tensor.dim()[source]

Return the number of dimensions.

Returns:
int – The number of dimensions.

div

Tensor.div(other)[source]

Compute the element-wise division.

\[\text{out} = \text{self} \div \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.div(…)

div_

Tensor.div_(other)[source]

Compute the element-wise division.

\[\text{self} \mathrel{\div}= \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.div(…)

double

Tensor.double()[source]

Return a float64 tensor with the same data.

Returns:
dragon.vm.torch.Tensor – The output tensor.

double_

Tensor.double_()[source]

Cast to a float64 tensor.

Returns:
dragon.vm.torch.Tensor – The output tensor.

eq

Tensor.eq(other)[source]

Compute the element-wise equal comparison.

\[\text{out} = (\text{self} = \text{other}) \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.eq(…)

exp

Tensor.exp()[source]

Compute the exponential.

\[\text{out} = \exp(\text{self}) \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.exp(…)

exp_

Tensor.exp_()[source]

Set to the exponential of elements.

\[\text{self} = \exp(\text{self}) \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.exp(…)

expand

Tensor.expand(*sizes)[source]

Return a tensor with elements broadcast.

Parameters:
  • sizes (Union[Sequence[int], int...]) – The output dimensions to broadcast to.
Returns:

dragon.vm.torch.Tensor – The output tensor.

expand_as

Tensor.expand_as(other)[source]

Return a tensor with elements broadcast like the other.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

fill_

Tensor.fill_(value)[source]

Fill self with a scalar value.

\[\text{self} \leftarrow \text{value} \]
Parameters:
  • value (number) – The value to fill.
Returns:

dragon.vm.torch.Tensor – The output tensor.

flatten

Tensor.flatten(
  start_dim=0,
  end_dim=- 1
)[source]

Return a tensor with dimensions flattened.

Parameters:
  • start_dim (int, optional, default=0) – The start dimension to flatten.
  • end_dim (int, optional, default=-1) – The end dimension to flatten.
Returns:

dragon.vm.torch.Tensor – The output tensor.

flatten_

Tensor.flatten_(
  start_dim=0,
  end_dim=- 1
)[source]

Flatten the dimensions.

Parameters:
  • start_dim (int, optional, default=0) – The start dimension to flatten.
  • end_dim (int, optional, default=-1) – The end dimension to flatten.
Returns:

dragon.vm.torch.Tensor – The output tensor.

flip

Tensor.flip(dims)[source]

Return a tensor with elements reversed along the given dimension.

Parameters:
  • dims (Union[int, Sequence[int]]) – The dimension to reverse.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.flip(…)

fliplr

Tensor.fliplr()[source]

Return a tensor with elements reversed along the second dimension.

Returns:
dragon.vm.torch.Tensor – The output tensor.

flipud

Tensor.flipud()[source]

Return a tensor with elements reversed along the first dimension.

Returns:
dragon.vm.torch.Tensor – The output tensor.

float

Tensor.float()[source]

Return a float32 tensor with the same data.

Returns:
dragon.vm.torch.Tensor – The output tensor.

float_

Tensor.float_()[source]

Cast to a float32 tensor.

Returns:
dragon.vm.torch.Tensor – The output tensor.

floor

Tensor.floor()[source]

Return a tensor taken the floor of elements.

\[\text{out} = \lfloor \text{self} \rfloor \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.floor(…)

floor_

Tensor.floor_()[source]

Set to the floor of elements.

\[\text{self} = \lfloor \text{self} \rfloor \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.floor(…)

gather

Tensor.gather(
  dim,
  index
)[source]

Gather elements along the given dimension of index.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

ge

Tensor.ge(other)[source]

Compute the element-wise greater-equal comparison.

\[\text{out} = (\text{self} \geq \text{other}) \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.ge(…)

gt

Tensor.gt(other)[source]

Compute the element-wise greater comparison.

\[\text{out} = (\text{self} > \text{other}) \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.gt(…)

half

Tensor.half()[source]

Return a float16 tensor with the same data.

Returns:
dragon.vm.torch.Tensor – The output tensor.

half_

Tensor.half_()[source]

Cast to a float16 tensor.

Returns:
dragon.vm.torch.Tensor – The output tensor.

index_select

Tensor.index_select(
  dim,
  index
)[source]

Select the elements along the dim dim using index.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

int

Tensor.int()[source]

Return an int32 tensor with the same data.

Returns:
dragon.vm.torch.Tensor – The output tensor.

int_

Tensor.int_()[source]

Cast to an int32 tensor.

Returns:
dragon.vm.torch.Tensor – The output tensor.

isinf

Tensor.isinf()[source]

Return if the elements are infinite.

\[\text{out} = \text{isinf}(\text{self}) \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.isinf(…)

isnan

Tensor.isnan()[source]

Return if the elements are NaN.

\[\text{out} = \text{isnan}(\text{self}) \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.isnan(…)

is_contiguous

Tensor.is_contiguous()[source]

Return whether the memory is contiguous.

Returns:
boolTrue if the memory is contiguous otherwise False.

is_floating_point

Tensor.is_floating_point()[source]

Return whether the data type is floating.

Floating types contains: (float16, float32, float64)

Returns:
boolTrue if the data type is floating otherwise False.

item

Tensor.item()[source]

Return the value as a python number.

Returns:
number – The value.

le

Tensor.le(other)[source]

Compute the element-wise less-equal comparison.

\[\text{out} = (\text{self} \leq \text{other}) \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.le(…)

log

Tensor.log()[source]

Compute the natural logarithm.

\[\text{out} = \log(\text{self}) \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

log_

Tensor.log_()[source]

Set to the natural logarithm of elements.

\[\text{self} = \log(\text{self}) \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.log(…)

logical_and

Tensor.logical_and(other)[source]

Compute the element-wise AND logical operation.

\[\text{out} = \text{self} \mathbin{\&} \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

logical_not

Tensor.logical_not()[source]

Compute the element-wise NOT logical operation.

\[\text{out} = \,\,\sim \text{self} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

logical_or

Tensor.logical_or(other)[source]

Compute the element-wise OR logical operation.

\[\text{out} = \text{self} \mathbin{|} \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

logical_xor

Tensor.logical_xor(other)[source]

Compute the element-wise XOR logical operation.

\[\text{out} = \text{self} \oplus \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

logsumexp

Tensor.logsumexp(
  dim,
  keepdim=False
)[source]

Apply the composite of log, sum, and exp.

\[\text{out}_{i} = \log\sum_{j}\exp(\text{self}_{ij}) \]
Parameters:
  • dim (Union[int, Sequence[int]]) – The dimension(s) to reduce.
  • keepdim (bool, optional, default=False) – Whether the output tensor has dim retained or not.
Returns:

dragon.vm.torch.Tensor – The output tensor.

long

Tensor.long()[source]

Return an int64 tensor with the same data.

Returns:
dragon.vm.torch.Tensor – The output tensor.

long_

Tensor.long_()[source]

Cast to an int64 tensor.

Returns:
dragon.vm.torch.Tensor – The output tensor.

lt

Tensor.lt(other)[source]

Compute the element-wise less comparison.

\[\text{out} = (\text{self} < \text{other}) \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.lt(…)

masked_fill_

Tensor.masked_fill_(
  mask,
  value
)[source]

Fill self with the value where mask is 1.

\[\text{self}_{i} = \begin{cases} \text{value}_{i}, & \text{ if } \text{mask}_{i} = 1 \\ \text{self}_{i}, & \text{ otherwise } \end{cases} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

masked_select

Tensor.masked_select(mask)[source]

Select the elements where mask is 1.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

matmul

Tensor.matmul(tensor2)[source]

Compute the matrix multiplication.

\[\text{out} = \text{self} \times \text{tensor2} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

max

Tensor.max(
  dim=None,
  keepdim=False
)[source]

Compute the max value of elements along the given dimension.

Parameters:
  • dim (Union[int, Sequence[int]], optional) – The dimension(s) to reduce.
  • keepdim (bool, optional, default=False) – Keep the reduced dimensions or not.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.max(…)

maximum

Tensor.maximum(other)[source]

Compute the maximum value of inputs.

\[\text{out} = \max(\text{self}, \text{other}) \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

mean

Tensor.mean(
  dim=None,
  keepdim=False
)[source]

Compute the mean value of elements along the given dimension.

Parameters:
  • dim (Union[int, Sequence[int]], optional) – The dimension(s) to reduce.
  • keepdim (bool, optional, default=False) – Keep the reduced dimensions or not.
Returns:

dragon.vm.torch.Tensor – The output tensor.

min

Tensor.min(
  dim=None,
  keepdim=False
)[source]

Compute the min value of elements along the given dimension.

Parameters:
  • dim (Union[int, Sequence[int]], optional) – The dimension(s) to reduce.
  • keepdim (bool, optional, default=False) – Keep the reduced dimensions or not.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.min(…)

minimum

Tensor.minimum(other)[source]

Compute the minimum value of inputs.

\[\text{out} = \min(\text{self}, \text{other}) \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

mm

Tensor.mm(mat2)[source]

Compute the matrix-matrix multiplication.

\[\text{out} = \text{self} \times \text{mat2} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.mm(…)

mul

Tensor.mul(other)[source]

Compute the element-wise multiplication.

\[\text{out} = \text{self} \times \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.mul(…)

mul_

Tensor.mul_(other)[source]

Compute the element-wise multiplication.

\[\text{self} \mathrel{\times}= \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.mul(…)

multinomial

Tensor.multinomial(num_samples)[source]

Return a tensor with index sampled from multinomial distribution.

Parameters:
  • num_samples (int) – The number of samples.
Returns:

dragon.vm.torch.Tensor – The output tensor.

narrow

Tensor.narrow(
  dimension,
  start,
  length
)[source]

Return a narrowed tensor.

Parameters:
  • dimension (int) – The dimension to narrow.
  • start (int) – The starting position.
  • length (int) – The distance to the ending position.
Returns:

dragon.vm.torch.Tensor – The output tensor.

ndimension

Tensor.ndimension()[source]

Alias for Tensor.dim().

Returns:
int – The number of dimensions.

ne

Tensor.ne(other)[source]

Compute the element-wise not-equal comparison.

\[\text{out} = (\text{self} \neq \text{other}) \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.ne(…)

neg

Tensor.neg()[source]

Compute the element-wise negative.

\[\text{out} = -\text{self} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.neg(…)

neg_

Tensor.neg_()[source]

Compute the element-wise negative.

\[\text{self} = -\text{self} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.neg(…)

new_empty

Tensor.new_empty(
  *size,
  dtype=None,
  device=None,
  requires_grad=False
)[source]

Return a tensor filled with uninitialized data.

Refer this tensor if dtype and device not provided.

Parameters:
  • size (int...) – The size of output tensor.
  • dtype (str, optional) – The optional data type.
  • device (dragon.vm.torch.device, optional) – The optional device of returned tensor.
  • requires_grad (bool, optional, default=False) – True to record gradient for returned tensor.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.empty(…)

new_full

Tensor.new_full(
  size,
  fill_value,
  dtype=None,
  device=None,
  requires_grad=False
)[source]

Return a tensor filled with a scalar.

Refer this tensor if dtype and device not provided.

Parameters:
  • size (Sequence[int]) – The size of output tensor.
  • fill_value (number) – The scalar to fill.
  • dtype (str, optional) – The optional data type.
  • device (dragon.vm.torch.device, optional) – The optional device of returned tensor.
  • requires_grad (bool, optional, default=False) – True to record gradient for returned tensor.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.full(…)

new_ones

Tensor.new_ones(
  *size,
  dtype=None,
  device=None,
  requires_grad=False
)[source]

Return a tensor filled with with ones.

Refer this tensor if dtype and device not provided.

Parameters:
  • size (int...) – The size of output tensor.
  • dtype (str, optional) – The optional data type.
  • device (dragon.vm.torch.device, optional) – The optional device of returned tensor.
  • requires_grad (bool, optional, default=False) – True to record gradient for returned tensor.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.ones(…)

new_tensor

Tensor.new_tensor(
  data,
  dtype=None,
  device=None,
  requires_grad=False
)[source]

Return a tensor initializing from the given data.

Refer this tensor if dtype and device not provided.

Parameters:
  • data (array_like) – The data to initialize from.
  • dtype (str, optional) – The optional data type.
  • device (dragon.vm.torch.device, optional) – The optional device of returned tensor.
  • requires_grad (bool, optional, default=False) – True to record gradient for returned tensor.
Returns:

dragon.vm.torch.Tensor – The output tensor.

new_zeros

Tensor.new_zeros(
  *size,
  dtype=None,
  device=None,
  requires_grad=False
)[source]

Return a tensor filled with with zeros.

Refer this tensor if dtype and device not provided.

Parameters:
  • size (int...) – The size of output tensor.
  • dtype (str, optional) – The optional data type.
  • device (dragon.vm.torch.device, optional) – The optional device of returned tensor.
  • requires_grad (bool, optional, default=False) – True to record gradient for returned tensor.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.zeros(…)

nonzero

Tensor.nonzero()[source]

Return the index of non-zero elements.

\[\text{out} = \{i\}, \text{ if } \text{self}_{i} \neq 0 \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

normal_

Tensor.normal_(
  mean=0,
  std=1
)[source]

Fill self from a normal distribution.

\[\text{self} \sim \mathcal{N}(\mu, \sigma^{2}) \]
Parameters:
  • mean (number, optional, default=0) – The value to \(\mu\).
  • std (number, optional, default=1) – The value to \(\sigma\).
Returns:

dragon.vm.torch.Tensor – The output tensor.

numel

Tensor.numel()[source]

Return the total number of elements.

Returns:
int – The total count.

numpy

Tensor.numpy()[source]

Create a numpy array sharing the data.

Returns:
numpy.ndarray – The numpy array.

one_

Tensor.one_()[source]

Fill self with ones.

\[\text{self} \leftarrow 1 \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

permute

Tensor.permute(*dims)[source]

Return a tensor with the new order of dimensions.

Parameters:
  • dims (int...) – The output dimensions.
Returns:

dragon.vm.torch.Tensor – The output tensor.

permute_

Tensor.permute_(*dims)[source]

Reorder the dimensions.

Parameters:
  • dims (Union[Sequence[int], int...]) – The new order of dimensions.
Returns:

dragon.vm.torch.Tensor – The output tensor.

pow

Tensor.pow(exponent)[source]

Compute the power.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.pow(…)

reciprocal

Tensor.reciprocal()[source]

Compute the reciprocal.

\[\text{out} = \frac{1}{\text{self}} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

reciprocal_

Tensor.reciprocal_()[source]

Compute the reciprocal.

\[\text{self} = \frac{1}{\text{self}} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

repeat

Tensor.repeat(*sizes)[source]

Repeat elements along each dimension.

Parameters:
  • sizes (int...) – The repetition for each dimension.
Returns:

dragon.vm.torch.Tensor – The output tensor.

reshape

Tensor.reshape(shape)[source]

Return a tensor with the same data but a different shape.

Parameters:
  • shape (Sequence[int]) – The new shape.
Returns:

dragon.vm.torch.Tensor – The output tensor.

reshape_

Tensor.reshape_(shape)[source]

Change into a new shape with the same data.

Parameters:
  • shape (Sequence[int]) – The new shape.
Returns:

dragon.vm.torch.Tensor – The output tensor.

retain_grad

Tensor.retain_grad()[source]

Retain grad for the non-leaf tensor.

roll

Tensor.roll(
  shifts,
  dims=None
)[source]

Return a tensor of rolled elements.

Parameters:
  • shifts (Union[int, Sequence[int]]) – The rolling offset of each dimension.
  • dims (Union[int, Sequence[int]], optional) – The dimension to roll.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.roll(…)

round

Tensor.round()[source]

Return a tensor taken the round of elements.

\[\text{out} = \lfloor \text{self} \rceil \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.round(…)

round_

Tensor.round_()[source]

Set to the round of elements.

\[\text{self} = \lfloor \text{self} \rceil \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.round(…)

rsqrt

Tensor.rsqrt()[source]

Compute the reciprocal square root.

\[\text{out} = \frac{1}{\sqrt{\text{self}}} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.rsqrt(…)

rsqrt_

Tensor.rsqrt_()[source]

Compute the reciprocal square root.

\[\text{self} = \frac{1}{\sqrt{\text{self}}} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.rsqrt(…)

scatter

Tensor.scatter(
  dim,
  index,
  src
)[source]

Return a tensor with elements updated from the source.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

scatter_

Tensor.scatter_(
  dim,
  index,
  src,
  reduce=None
)[source]

Update elements from the source.

Parameters:
  • dim (int) – The dimension of index values.
  • index (dragon.vm.torch.Tensor) – The index tensor.
  • src (Union[dragon.vm.torch.Tensor, number]) – The tensor to update from.
  • reduce (str, optional) – 'add' or 'multiply'.
Returns:

dragon.vm.torch.Tensor – The output tensor.

scatter_add

Tensor.scatter_add(
  dim,
  index,
  src
)[source]

Return a tensor with elements added from the source.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

scatter_add_

Tensor.scatter_add_(
  dim,
  index,
  src
)[source]

Add elements from the source.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

sign

Tensor.sign()[source]

Return a tensor taken the sign indication of elements.

\[\text{out}_{i} = \begin{cases} -1, & \text{ if } \text{self}_{i} < 0 \\ 0, & \text{ if } \text{self}_{i} = 0 \\ 1, & \text{ if } \text{self}_{i} > 0 \end{cases} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.sign(…)

sign_

Tensor.sign_()[source]

Set to the sign indication of elements.

\[\text{self}_{i} = \begin{cases} -1, & \text{ if } \text{self}_{i} < 0 \\ 0, & \text{ if } \text{self}_{i} = 0 \\ 1, & \text{ if } \text{self}_{i} > 0 \end{cases} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.sign(…)

sin

Tensor.sin()[source]

Compute the sin.

\[\text{out} = \sin(\text{self}) \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.sin(…)

size

Tensor.size(axis=None)[source]

Return the size of this tensor.

Parameters:
  • axis (int, optional) – The optional axis.
Returns:

Union[int, dragon.vm.torch.Size] – The size.

sort

Tensor.sort(
  dim=- 1,
  descending=False
)[source]

Return the sorted elements.

Parameters:
  • dim (int, optional, default=-1) – The dimension to sort elements.
  • descending (bool, optional, default=False) – Sort in the descending order or not.
Returns:

Sequence[dragon.vm.torch.Tensor] – The value and index tensor.

See also

torch.sort(…)

split

Tensor.split(
  split_size_or_sections,
  dim=0,
  copy=True
)[source]

Return the split chunks along the given dimension.

Parameters:
  • split_size_or_sections (Union[int, Sequence[int]) – The number or size of chunks.
  • dim (int, optional, default=0) – The dimension to split.
Returns:

  • Sequence[dragon.vm.torch.Tensor] – The output tensors.
  • copy (bool, optional, default=True) – Copy or create the views of input.

See also

torch.split(…)

sqrt

Tensor.sqrt()[source]

Compute the square root.

\[\text{out} = \sqrt{\text{self}} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.sqrt(…)

sqrt_

Tensor.sqrt_()[source]

Compute the square root.

\[\text{self} = \sqrt{\text{self}} \]
Returns:
dragon.vm.torch.Tensor – The output tensor.

See also

torch.sqrt(…)

squeeze

Tensor.squeeze(dim=None)[source]

Return a tensor with dimensions of size 1 removed.

Parameters:
  • dim (Union[int, Sequence[int]], optional) – The dimension(s) to remove.
Returns:

dragon.vm.torch.Tensor – The output tensor.

squeeze_

Tensor.squeeze_(dim=None)[source]

Remove the dimensions with size 1.

Parameters:
  • dim (Union[int, Sequence[int]], optional) – The dimension(s) to remove.
Returns:

dragon.vm.torch.Tensor – The output tensor.

sum

Tensor.sum(
  dim=None,
  keepdim=False
)[source]

Compute the sum value of elements along the given dimension.

Parameters:
  • dim (Union[int, Sequence[int]], optional) – The dimension(s) to reduce.
  • keepdim (bool, optional, default=False) – Keep the reduced dimensions or not.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.sum(…)

sub

Tensor.sub(other)[source]

Compute the element-wise subtraction.

\[\text{out} = \text{self} - \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.sub(…)

sub_

Tensor.sub_(other)[source]

Compute the element-wise subtraction.

\[\text{self} \mathrel{-}= \text{other} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.sub(…)

to

Tensor.to(
  *args,
  **kwargs
)[source]

Convert to the specified data type or device.

The arguments could be torch.dtype or torch.device:

x = torch.FloatTensor(1)
x.to(torch.int32)  # Equivalent to ``x.int()``
x.to(torch.device('cpu'))  # Equivalent to ``x.cpu()``
x.to(torch.device('cuda'), torch.float32)  # Convert both

Or torch.Tensor to provide both dtype and device:

a, b = torch.tensor(1.), torch.tensor(2)
print(a.to(b))  # 1
Returns:
dragon.vm.torch.Tensor – The output tensor.

tolist

Tensor.tolist()[source]

Return the value as a python list.

Returns:
list – The value.

topk

Tensor.topk(
  k,
  dim=- 1,
  largest=True,
  sorted=True
)[source]

Return the top-K largest or smallest elements.

Parameters:
  • k (int) – The number of top elements to select.
  • dim (int, optional, default=-1) – The dimension to select elements.
  • largest (bool, optional, default=True) – Return largest or smallest elements.
  • sorted (bool, optional, default=True) – Whether to return in the sorted order.
Returns:

Sequence[dragon.vm.torch.Tensor] – The value and index tensor.

See also

torch.topk(…)

transpose

Tensor.transpose(
  dim0,
  dim1
)[source]

Return a tensor with two dimensions swapped.

Parameters:
  • dim0 (int) – The first dimension to be transposed.
  • dim1 (int) – The second dimension to be transposed.
Returns:

dragon.vm.torch.Tensor – The output tensor.

transpose_

Tensor.transpose_(
  dim0,
  dim1
)[source]

Swap two dimensions.

Parameters:
  • dim0 (int) – The first dimension to be transposed.
  • dim1 (int) – The second dimension to be transposed.
Returns:

dragon.vm.torch.Tensor – The output tensor.

tril

Tensor.tril(k=0)[source]

Return the lower triangular part.

\[\text{out}_{ij} = \begin{cases} 0, & \text{ if } j > i + k \\ \text{self}_{ij}, & \text{ otherwise } \end{cases} \]
Parameters:
  • k (int, optional, default=0) – Diagonal above which to zero elements.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.tril(…)

tril_

Tensor.tril_(k=0)[source]

Set to the lower triangular part.

\[\text{self}_{ij} = \begin{cases} 0, & \text{ if } j > i + k \\ \text{self}_{ij}, & \text{ otherwise } \end{cases} \]
Parameters:
  • k (int, optional, default=0) – Diagonal above which to zero elements.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.tril(…)

triu

Tensor.triu(k=0)[source]

Return the upper triangular part.

\[\text{out}_{ij} = \begin{cases} 0, & \text{ if } j < i + k \\ \text{self}_{ij}, & \text{ otherwise } \end{cases} \]
Parameters:
  • k (int, optional, default=0) – Diagonal below which to zero elements.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.triu(…)

triu_

Tensor.triu_(k=0)[source]

Set to the upper triangular part.

\[\text{self}_{ij} = \begin{cases} 0, & \text{ if } j < i + k \\ \text{self}_{ij}, & \text{ otherwise } \end{cases} \]
Parameters:
  • k (int, optional, default=0) – Diagonal below which to zero elements.
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.triu(…)

type

Tensor.type(dtype=None)[source]

Return the data type.

If dtype is not None, converts to a new tensor.

Parameters:
  • dtype (str, optional) – The data type to convert to.
Returns:

Union[str, dragon.vm.torch.Tensor] – The data type or new tensor.

uniform_

Tensor.uniform_(
  low=0,
  high=1
)[source]

Fill self from a uniform distribution.

\[\text{self} \sim \mathcal{U}(\alpha, \beta) \]
Parameters:
  • low (number, optional, default=0) – The value to \(\alpha\).
  • high (number, optional, default=1) – The value to \(\beta\).
Returns:

dragon.vm.torch.Tensor – The output tensor.

unique

Tensor.unique(
  return_inverse=False,
  return_counts=False,
  **kwargs
)[source]

Return the unique elements.

Parameters:
  • return_inverse (bool, optional, default=False) – Return the inverse index or not.
  • return_counts (bool, optional, default=False) – Return the counts or not.
Returns:

  • dragon.vm.torch.Tensor – The output tensor.
  • dragon.vm.torch.Tensor, optional – The inverse index tensor.
  • dragon.vm.torch.Tensor, optional – The counts tensor.

unsqueeze

Tensor.unsqueeze(dim)[source]

Return a tensor with dimensions of size 1 inserted.

Parameters:
  • dim (Union[int, Sequence[int]]) – The dimensions(s) to insert.
Returns:

dragon.vm.torch.Tensor – The output tensor.

unsqueeze_

Tensor.unsqueeze_(dim)[source]

Insert the dimensions of size 1.

Parameters:
  • dim (Union[int, Sequence[int]]) – The dimensions(s) to insert.
Returns:

dragon.vm.torch.Tensor – The output tensor.

view

Tensor.view(*shape)[source]

Return a tensor with the same data but a different shape.

Parameters:
  • shape (Union[Sequence[int], int...]) – The new shape
Returns:

dragon.vm.torch.Tensor – The output tensor.

view_

Tensor.view_(*shape)[source]

Change into a new size with the same data.

Parameters:
  • shape (Union[Sequence[int], int...]) – The new shape.
Returns:

dragon.vm.torch.Tensor – The output tensor.

view_as

Tensor.view_as(other)[source]

Return a tensor with the same data but a different size.

Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

where

Tensor.where(
  condition,
  y
)[source]

Select the elements from two branches under the condition.

\[\text{out}_{i} = \begin{cases} \text{self}_{i} & \text{ if } \text{condition}_{i} \\ y_{i}, & \text{ otherwise } \end{cases} \]
Parameters:
Returns:

dragon.vm.torch.Tensor – The output tensor.

See also

torch.where(…)

zero_

Tensor.zero_()[source]

Fill self with zeros.

\[\text{self} \leftarrow 0 \]
Returns:
dragon.vm.torch.Tensor – The output tensor.