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
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
Name | Type | Description |
---|---|---|
other | number | Tensor | Scalar or tensor to be added to each element in this tensor. |
options? | Object | - |
options.alpha? | Number | The multiplier for other . Default: 1 . |
Returns
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
Name | Type | Description |
---|---|---|
options? | Object | argmax Options as keywords argument in pytorch |
options.dim? | number | The dimension to reduce. If undefined , the argmax of the flattened input is returned. |
options.keepdim? | boolean | Whether the output tensor has dim retained or not. Ignored if dim is undefined . |
Returns
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
Name | Type | Description |
---|---|---|
options? | Object | argmin Options as keywords argument in pytorch |
options.dim? | number | The dimension to reduce. If undefined , the argmin of the flattened input is returned. |
options.keepdim? | boolean | Whether the output tensor has dim retained or not. Ignored if dim is undefined . |
Returns
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
Name | Type | Description |
---|---|---|
min | number | Tensor | Lower-bound of the range to be clamped to |
max? | number | Tensor | Upper-bound of the range to be clamped to |
Returns
▸ 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
Name | Type | Description |
---|---|---|
options | Object | - |
options.max? | number | Tensor | Upper-bound of the range to be clamped to |
options.min? | number | Tensor | Lower-bound of the range to be clamped to |
Returns
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
Name | Type | Description |
---|---|---|
options? | Object | - |
options.memoryFormat | MemoryFormat | The desired memory format of returned Tensor. Default: torch.contiguousFormat. https://pytorch.org/docs/1.12/generated/torch.Tensor.contiguous.html |
Returns
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];
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
Name | Type | Description |
---|---|---|
other | number | Tensor | Scalar or tensor that divides each element in this tensor. |
options? | Object | - |
options.roundingMode? | "trunc" | "floor" | Type of rounding applied to the result |
Returns
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
Name | Type | Description |
---|---|---|
sizes | number [] | The expanded size, eg: ([3, 4]). |
Returns
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
Name | Type | Description |
---|---|---|
dims | number [] | Axis to flip on. |
Returns
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
Name | Type | Description |
---|---|---|
other | number | Tensor | Scalar or tensor multiplied with each element in this tensor. |
Returns
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
Name | Type | Description |
---|---|---|
dims | number [] | The desired ordering of dimensions. |
Returns
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
Name | Type | Description |
---|---|---|
shape | number [] | The new shape. |
Returns
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
Name | Type | Description |
---|---|---|
dim | number | A dimension along which softmax will be computed. |
Returns
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
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
Name | Type | Description |
---|---|---|
dim? | number | If given, the input will be squeezed only in this dimension. |
Returns
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
Name | Type | Description |
---|---|---|
dim | number | The 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
Name | Type | Description |
---|---|---|
other | number | Tensor | The scalar or tensor to subtract from input. |
options? | Object | - |
options.alpha? | Number | The multiplier for other . Default: 1 . |
Returns
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
▸ 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
Name | Type | Description |
---|---|---|
dim | number | number [] | The dimension or dimensions to reduce. |
options? | Object | - |
options.keepdim? | boolean | Whether the output tensor has dim retained or not. |
Returns
to
▸ to(options
): Tensor
Performs Tensor conversion.
https://pytorch.org/docs/1.12/generated/torch.Tensor.to.html
Parameters
Name | Type | Description |
---|---|---|
options | TensorOptions | Tensor options. |
Returns
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
Name | Type | Description |
---|---|---|
k | number | The k in "top-k" |
options? | Object | topk Options as keywords argument in pytorch |
options.dim? | number | The dimension to sort along. If dim is not given, the last dimension of the input is chosen. |
options.largest? | boolean | Controls whether to return largest or smallest elements. It is set to True by default. |
options.sorted? | boolean | Controls whether to return the elements in sorted order. It is set to True by default. |
Returns
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
Name | Type | Description |
---|---|---|
dim | number | The index at which to insert the singleton dimension. |