Skip to main content
Version: 0.2.0

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.11/tensor_attributes.html

Defined in

torchlive/torch.ts:236


shape

shape: number[]

Returns the size of the tensor.

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

Defined in

torchlive/torch.ts:274

Methods

abs

abs(): Tensor

Computes the absolute value of each element in input.

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

Returns

Tensor

Defined in

torchlive/torch.ts:145


add

add(other, options?): Tensor

Add a scalar or tensor to this tensor.

https://pytorch.org/docs/1.11/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

Defined in

torchlive/torch.ts:154


argmax

argmax(options?): Tensor

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

https://pytorch.org/docs/1.11/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

Defined in

torchlive/torch.ts:165


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.11/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

Defined in

torchlive/torch.ts:176

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.11/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

Defined in

torchlive/torch.ts:187


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.11/generated/torch.Tensor.contiguous.html

Returns

Tensor

Defined in

torchlive/torch.ts:197


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

Defined in

torchlive/torch.ts:217


div

div(other, options?): Tensor

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

https://pytorch.org/docs/1.11/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

Defined in

torchlive/torch.ts:227


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.11/generated/torch.Tensor.item.html

Returns

number

Defined in

torchlive/torch.ts:243


mul

mul(other): Tensor

Multiplies input by other scalar or tensor.

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

Parameters

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

Returns

Tensor

Defined in

torchlive/torch.ts:260


permute

permute(dims): Tensor

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

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

Parameters

NameTypeDescription
dimsnumber[]The desired ordering of dimensions.

Returns

Tensor

Defined in

torchlive/torch.ts:268


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.11/generated/torch.Tensor.reshape.html

Parameters

NameTypeDescription
shapenumber[]The new shape.

Returns

Tensor

Defined in

torchlive/torch.ts:252


size

size(): number[]

Returns the size of the tensor.

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

Returns

number[]

Defined in

torchlive/torch.ts:280


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.11/generated/torch.nn.functional.softmax.html

Parameters

NameTypeDescription
dimnumberA dimension along which softmax will be computed.

Returns

Tensor

Defined in

torchlive/torch.ts:290


sqrt

sqrt(): Tensor

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

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

Returns

Tensor

Defined in

torchlive/torch.ts:296


squeeze

squeeze(dim?): Tensor

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

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

Parameters

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

Returns

Tensor

Defined in

torchlive/torch.ts:304


stride

stride(): number[]

Returns the stride of the tensor.

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

Returns

number[]

Defined in

torchlive/torch.ts:310

stride(dim): number

Returns the stride of the tensor.

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

Parameters

NameTypeDescription
dimnumberThe desired dimension in which stride is required.

Returns

number

Defined in

torchlive/torch.ts:318


sub

sub(other, options?): Tensor

Subtracts other from input.

https://pytorch.org/docs/1.11/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

Defined in

torchlive/torch.ts:327


sum

sum(): Tensor

Returns the sum of all elements in the input tensor.

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

Returns

Tensor

Defined in

torchlive/torch.ts:333

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.11/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

Defined in

torchlive/torch.ts:343


to

to(options): Tensor

Performs Tensor conversion.

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

Parameters

NameTypeDescription
optionsTensorOptionsTensor options.

Returns

Tensor

Defined in

torchlive/torch.ts:351


topk

topk(k): [Tensor, Tensor]

Returns the k largest elements of the given input tensor along a given dimension.

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

Parameters

NameTypeDescription
knumberThe k in "top-k"

Returns

[Tensor, Tensor]

Defined in

torchlive/torch.ts:360


unsqueeze

unsqueeze(dim): Tensor

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

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

Parameters

NameTypeDescription
dimnumberThe index at which to insert the singleton dimension.

Returns

Tensor

Defined in

torchlive/torch.ts:369