Skip to main content
Version: 0.2.2

Interface: Tensor

torchlive/torch.Tensor

Indexable

[index: number]: Tensor

Access tensor with index.

const tensor = torch.rand([2]);
console.log(tensor.data, tensor[0].data);
// [0.8339180946350098, 0.17733973264694214], [0.8339180946350098]

https://pytorch.org/cppdocs/notes/tensor_indexing.html

Properties

dtype

dtype: Dtype

A dtype is an string that represents the data type of a torch.Tensor.

https://pytorch.org/docs/1.12/tensor_attributes.html


shape

shape: number[]

Returns the size of the tensor.

https://pytorch.org/docs/1.12/generated/torch.Tensor.size.html

Methods

abs

abs(): Tensor

Computes the absolute value of each element in input.

https://pytorch.org/docs/1.12/generated/torch.Tensor.abs.html

Returns

Tensor


add

add(other, options?): Tensor

Add a scalar or tensor to this tensor.

https://pytorch.org/docs/1.12/generated/torch.Tensor.add.html

Parameters

NameTypeDescription
othernumber | TensorScalar or tensor to be added to each element in this tensor.
options?Object-
options.alpha?NumberThe multiplier for other. Default: 1.

Returns

Tensor


argmax

argmax(options?): Tensor

Returns the indices of the maximum value of all elements in the input tensor.

https://pytorch.org/docs/1.12/generated/torch.Tensor.argmax.html

Parameters

NameTypeDescription
options?Objectargmax Options as keywords argument in pytorch
options.dim?numberThe dimension to reduce. If undefined, the argmax of the flattened input is returned.
options.keepdim?booleanWhether the output tensor has dim retained or not. Ignored if dim is undefined.

Returns

Tensor


argmin

argmin(options?): Tensor

Returns the indices of the minimum value(s) of the flattened tensor or along a dimension

https://pytorch.org/docs/1.12/generated/torch.Tensor.argmin.html

Parameters

NameTypeDescription
options?Objectargmin Options as keywords argument in pytorch
options.dim?numberThe dimension to reduce. If undefined, the argmin of the flattened input is returned.
options.keepdim?booleanWhether the output tensor has dim retained or not. Ignored if dim is undefined.

Returns

Tensor


clamp

clamp(min, max?): Tensor

Clamps all elements in input into the range [ min, max ].

If min is undefined, there is no lower bound. Or, if max is undefined there is no upper bound.

https://pytorch.org/docs/1.12/generated/torch.Tensor.clamp.html

Parameters

NameTypeDescription
minnumber | TensorLower-bound of the range to be clamped to
max?number | TensorUpper-bound of the range to be clamped to

Returns

Tensor

clamp(options): Tensor

Clamps all elements in input into the range [ min, max ].

If min is undefined, there is no lower bound. Or, if max is undefined there is no upper bound.

https://pytorch.org/docs/1.12/generated/torch.Tensor.clamp.html

Parameters

NameTypeDescription
optionsObject-
options.max?number | TensorUpper-bound of the range to be clamped to
options.min?number | TensorLower-bound of the range to be clamped to

Returns

Tensor


contiguous

contiguous(options?): Tensor

Returns a contiguous in memory tensor containing the same data as this tensor. If this tensor is already in the specified memory format, this function returns this tensor.

Parameters

NameTypeDescription
options?Object-
options.memoryFormatMemoryFormatThe desired memory format of returned Tensor. Default: torch.contiguousFormat. https://pytorch.org/docs/1.12/generated/torch.Tensor.contiguous.html

Returns

Tensor


data

data(): TypedArray

Returns the tensor data as TypedArray buffer.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

A valid TypeScript expression is as follows:

torch.rand([2, 3]).data()[3];
note

The function only exists in JavaScript.

experimental

Returns

TypedArray


div

div(other, options?): Tensor

Divides each element of the input input by the corresponding element of other.

https://pytorch.org/docs/1.12/generated/torch.Tensor.div.html

Parameters

NameTypeDescription
othernumber | TensorScalar or tensor that divides each element in this tensor.
options?Object-
options.roundingMode?"trunc" | "floor"Type of rounding applied to the result

Returns

Tensor


expand

expand(sizes): Tensor

Returns a new view of the tensor expanded to a larger size.

https://pytorch.org/docs/stable/generated/torch.Tensor.expand.html

Parameters

NameTypeDescription
sizesnumber[]The expanded size, eg: ([3, 4]).

Returns

Tensor


flip

flip(dims): Tensor

Reverse the order of a n-D tensor along given axis in dims.

https://pytorch.org/docs/1.12/generated/torch.Tensor.flip.html

Parameters

NameTypeDescription
dimsnumber[]Axis to flip on.

Returns

Tensor


item

item(): number

Returns the value of this tensor as a number. This only works for tensors with one element.

https://pytorch.org/docs/1.12/generated/torch.Tensor.item.html

Returns

number


mul

mul(other): Tensor

Multiplies input by other scalar or tensor.

https://pytorch.org/docs/1.12/generated/torch.Tensor.mul.html

Parameters

NameTypeDescription
othernumber | TensorScalar or tensor multiplied with each element in this tensor.

Returns

Tensor


permute

permute(dims): Tensor

Returns a view of the original tensor input with its dimensions permuted.

https://pytorch.org/docs/1.12/generated/torch.Tensor.permute.html

Parameters

NameTypeDescription
dimsnumber[]The desired ordering of dimensions.

Returns

Tensor


reshape

reshape(shape): Tensor

Returns a tensor with the same data and number of elements as input, but with the specified shape.

https://pytorch.org/docs/1.12/generated/torch.Tensor.reshape.html

Parameters

NameTypeDescription
shapenumber[]The new shape.

Returns

Tensor


size

size(): number[]

Returns the size of the tensor.

https://pytorch.org/docs/1.12/generated/torch.Tensor.size.html

Returns

number[]


softmax

softmax(dim): Tensor

Applies a softmax function. It is applied to all slices along dim, and will re-scale them so that the elements lie in the range [0, 1] and sum to 1.

https://pytorch.org/docs/1.12/generated/torch.nn.functional.softmax.html

Parameters

NameTypeDescription
dimnumberA dimension along which softmax will be computed.

Returns

Tensor


sqrt

sqrt(): Tensor

Computes the square-root value of each element in input.

https://pytorch.org/docs/1.12/generated/torch.Tensor.sqrt.html

Returns

Tensor


squeeze

squeeze(dim?): Tensor

Returns a tensor with all the dimensions of input of size 1 removed.

https://pytorch.org/docs/1.12/generated/torch.Tensor.squeeze.html

Parameters

NameTypeDescription
dim?numberIf given, the input will be squeezed only in this dimension.

Returns

Tensor


stride

stride(): number[]

Returns the stride of the tensor.

https://pytorch.org/docs/1.12/generated/torch.Tensor.stride.html

Returns

number[]

stride(dim): number

Returns the stride of the tensor.

https://pytorch.org/docs/1.12/generated/torch.Tensor.stride.html

Parameters

NameTypeDescription
dimnumberThe desired dimension in which stride is required.

Returns

number


sub

sub(other, options?): Tensor

Subtracts other from input.

https://pytorch.org/docs/1.12/generated/torch.Tensor.sub.html

Parameters

NameTypeDescription
othernumber | TensorThe scalar or tensor to subtract from input.
options?Object-
options.alpha?NumberThe multiplier for other. Default: 1.

Returns

Tensor


sum

sum(): Tensor

Returns the sum of all elements in the input tensor.

https://pytorch.org/docs/1.12/generated/torch.Tensor.sum.html

Returns

Tensor

sum(dim, options?): Tensor

Returns the sum of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them.

https://pytorch.org/docs/1.12/generated/torch.Tensor.sum.html

Parameters

NameTypeDescription
dimnumber | number[]The dimension or dimensions to reduce.
options?Object-
options.keepdim?booleanWhether the output tensor has dim retained or not.

Returns

Tensor


to

to(options): Tensor

Performs Tensor conversion.

https://pytorch.org/docs/1.12/generated/torch.Tensor.to.html

Parameters

NameTypeDescription
optionsTensorOptionsTensor options.

Returns

Tensor


topk

topk(k, options?): [Tensor, Tensor]

Returns a list of two Tensors where the first represents the k largest elements of the given input tensor, and the second represents the indices of the k largest elements.

https://pytorch.org/docs/1.12/generated/torch.Tensor.topk.html

Parameters

NameTypeDescription
knumberThe k in "top-k"
options?Objecttopk Options as keywords argument in pytorch
options.dim?numberThe dimension to sort along. If dim is not given, the last dimension of the input is chosen.
options.largest?booleanControls whether to return largest or smallest elements. It is set to True by default.
options.sorted?booleanControls whether to return the elements in sorted order. It is set to True by default.

Returns

[Tensor, Tensor]


unsqueeze

unsqueeze(dim): Tensor

Returns a new tensor with a dimension of size one inserted at the specified position.

https://pytorch.org/docs/1.12/generated/torch.Tensor.unsqueeze.html

Parameters

NameTypeDescription
dimnumberThe index at which to insert the singleton dimension.

Returns

Tensor