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 if computed.

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 grad is required.

Returns:
boolTrue if requiring grad otherwise False.

shape

Tensor.shape

Return the shape of this tensor.

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

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 self.

See also

torch.add(…)

argmax

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

Return the index of maximum elements.

Parameters:
  • dim (int, optional) – 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=None,
  keepdim=False
)[source]

Return the index of minimum elements.

Parameters:
  • dim (int, optional) – 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.

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_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 self.

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 self.

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 self.

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 self.

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 self.

chunk

Tensor.chunk(
  chunks,
  dim=0
)[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 dim to split.
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 self.

See also

torch.clamp(…)

copy_

Tensor.copy_(src)[source]

Copy the elements into this tensor.

Parameters:
Returns:

dragon.vm.torch.Tensor – The self.

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 self.

cuda

Tensor.cuda(device=None)[source]

Copy memory to the specified cuda device.

Parameters:
Returns:

dragon.vm.torch.Tensor – The self.

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 self.

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 self.

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(…)

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 self.

flatten

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

Return a new 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 self.

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 self.

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 self.

See also

torch.floor(…)

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 self.

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 self.

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.

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.

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 self.

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 self.

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.

masked_select

Tensor.masked_select(mask)[source]

Select the elements where mask is 1.

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.

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 self.

See also

torch.mul(…)

multinomial

Tensor.multinomial(
  num_samples,
  epsilon=0
)[source]

Return a tensor with index sampled from multinomial distribution.

Parameters:
  • num_samples (int) – The number of samples.
  • epsilon (float, optional, default=0) – The epsilon value to apply e-greedy strategy.
Returns:

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

narrow

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

Return a new tensor that is a narrowed version of input 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 self.

See also

torch.neg(…)

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_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_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 self.

numel

Tensor.numel()[source]

Return the total number of elements.

Returns:
int – The total count.

numpy

Tensor.numpy(readonly=True)[source]

Create a numpy array sharing the data.

Parameters:
  • readonly (bool, optional, default=True) – False to sync the content with device.
Returns:

numpy.ndarray – The numpy array.

one_

Tensor.one_()[source]

Fill self with ones.

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

permute

Tensor.permute(*dims)[source]

Return a new tensor with the specific order of 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 self.

repeat

Tensor.repeat(*sizes)[source]

Repeat elements along the specified dimensions.

Parameters:
  • sizes (Union[Sequence[int], int...]) – The number of times to repeat.
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 self.

retain_grad

Tensor.retain_grad()[source]

Retain grad for the non-leaf tensor.

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 self.

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 self.

See also

torch.rsqrt(…)

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 self.

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(…)

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 self.

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 self.

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 self.

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.

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 new 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.

type

Tensor.type(dtype=None)[source]

Return the data type.

If dtype is not None, cast self to the new tensor.

Parameters:
  • dtype (str, optional) – The specified type.
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 self.

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 self.

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 shape with the same data.

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

dragon.vm.torch.Tensor – The self.

view_as

Tensor.view_as(other)[source]
Return a new tensor with the same data
but a different size as the given tensor.
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 self.