Skip to main content
Version: Next

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

NameType
otherTensor
options?Object
options.alpha?Number

Returns

Tensor

add(other, options?): Tensor

Parameters

NameType
othernumber
options?Object
options.alpha?Number

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

NameType
options?Object
options.dim?number
options.keepdim?boolean

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

NameType
options?Object
options.dim?number
options.keepdim?boolean

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.

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

Parameters

NameType
options?Object
options.memoryFormat?MemoryFormat

Returns

Tensor


data

data(): TypedArray

Returns the tensor data as TypedArray buffer.

A valid TypeScript expression is as follows:

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

The function only exists in JavaScript.

experimental

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

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

NameType
otherTensor
optionsObject
options.roundingMode"trunc" | "floor"

Returns

Tensor

div(other, options): Tensor

Parameters

NameType
othernumber
optionsObject
options.roundingMode"trunc" | "floor"

Returns

Tensor

div(other): Tensor

Parameters

NameType
otherTensor

Returns

Tensor

div(other): Tensor

Parameters

NameType
othernumber

Returns

Tensor


expand

expand(size, options?): Tensor

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

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

Parameters

NameType
sizenumber[]
options?Object
options.implicit?boolean

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

NameType
dimsnumber[]

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


matmul

matmul(other): Tensor

Performs matrix multiplication with other tensor.

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

Parameters

NameType
otherTensor

Returns

Tensor


mul

mul(other): Tensor

Multiplies input by other scalar or tensor.

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

Parameters

NameType
otherTensor

Returns

Tensor

mul(other): Tensor

Parameters

NameType
othernumber

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

NameType
dimsnumber[]

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

NameType
shapenumber[]

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

NameType
otherTensor
options?Object
options.alpha?Number

Returns

Tensor

sub(other, options?): Tensor

Parameters

NameType
othernumber
options?Object
options.alpha?Number

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

NameType
knumber
options?Object
options.dim?number
options.largest?boolean
options.sorted?boolean

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

NameType
dimnumber

Returns

Tensor