torch

package module
v1.11.0-0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2023 License: MIT Imports: 6 Imported by: 6

README

GoTorch

GoTorch Go Report Card

GoTorch is a Golang front-end for pytorch.

Installation

GoTorch requires the installation of libtorch and libcgotorch (a C-binding written for CGo.) To install these libraries to /usr/local, use:

git clone --depth 1 --branch v1.11.0-0.1.4 https://github.com/Kautenja/gotorch /tmp/gotorch
/tmp/gotorch/install.sh
rm -rf /tmp/gotorch

You may refer to the example project for a working demonstration of this library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllClose

func AllClose(tensor, other *Tensor, rtol, atol float64) bool

Return true if the two tensors are approximately equal to one another with given relative and absolute tolerances.

This function checks if all `tensor` and `other` satisfy the condition:

|tensor − other| <= atol + rtol * |other|

func Equal

func Equal(tensor, other *Tensor) bool

Return true if the two tensors are precisely equal element-wise.

func IsComplex

func IsComplex(tensor *Tensor) bool

Return true if the tensor of a complex data-type.

func IsConj

func IsConj(tensor *Tensor) bool

Return true if the (complex) tensor is in conjugated form.

func IsDevice

func IsDevice(deviceName string) bool

Return true if the given device is valid, false otherwise.

func IsFloatingPoint

func IsFloatingPoint(tensor *Tensor) bool

Return true if the tensor of a floating-point data-type.

func IsGradEnabled

func IsGradEnabled() bool

Return the global gradient generation state.

func IsNonzero

func IsNonzero(tensor *Tensor) bool

Return true if the tensor is a single non-zero element (i.e., scalar.)

func ManualSeed

func ManualSeed(seed int64)

Set the random number generator seed.

func Numel

func Numel(tensor *Tensor) int64

Return the number of elements in the tensor.

func SetGradEnabled

func SetGradEnabled(value bool)

Set the global gradient generation state.

func SetNumThreads

func SetNumThreads(numThreads int32)

Set the number of threads used for intraop parallelism on CPU.

func StdMean

func StdMean(tensor *Tensor) (*Tensor, *Tensor)

Reduce a tensor to its mean value and standard deviation.

func StdMeanByDim

func StdMeanByDim(tensor *Tensor, dim int, unbiased, keep_dims bool) (*Tensor, *Tensor)

Reduce a tensor to its mean value and standard deviation along given dimension.

func ToSlice

func ToSlice(tensor *Tensor) interface{}

Convert a torch Tensor to a Go slice. This function implies a flattening of the tensor to return 1-dimensional vectors.

func TorchVersion

func TorchVersion() string

Return the version of the libtorch back-end.

func VarMean

func VarMean(tensor *Tensor) (*Tensor, *Tensor)

Reduce a tensor to its mean value and variance.

func VarMeanByDim

func VarMeanByDim(tensor *Tensor, dim int, unbiased, keep_dims bool) (*Tensor, *Tensor)

Reduce a tensor to its mean value and variance along given dimension.

func Version

func Version() string

Return the version of the GoTorch library.

Types

type Device

type Device struct {
	Pointer C.Device
}

Device wrapper a pointer to C.Device

func NewDevice

func NewDevice(deviceName string) (device *Device)

Create a new Device.

type Dtype

type Dtype int8

An enumeration of the data-types available in torch. Typically it would be standard for "Invalid" to correspond to value 0; however, these codes are one-to-one with C++ codes in torch.

const (
	Byte Dtype = iota
	Char
	Short
	Int
	Long
	Half
	Float
	Double
	ComplexHalf
	ComplexFloat
	ComplexDouble
	Bool
	QInt8
	QUInt8
	QInt32
	BFloat16
	Invalid Dtype = -1
)

func GetDtypeOfKind

func GetDtypeOfKind(kind reflect.Kind) Dtype

Map an element type kind to its associated Dtype.

func (Dtype) NumBytes

func (dtype Dtype) NumBytes() int64

Return the number of bytes consumed by each element of the given data-type.

type IValue

type IValue struct {
	Pointer C.IValue
}

IValue wraps a C.IValue.

func NewIValue

func NewIValue(data interface{}) (ivalue *IValue)

Create a new IValue from arbitrary data.

func (*IValue) IsBool

func (ivalue *IValue) IsBool() bool

Return true if the IValue is a boolean.

func (*IValue) IsBoolList

func (ivalue *IValue) IsBoolList() bool

Return true if the IValue is a list of booleans.

func (*IValue) IsCapsule

func (ivalue *IValue) IsCapsule() bool

Return true if the IValue is a capsule.

func (*IValue) IsComplexDouble

func (ivalue *IValue) IsComplexDouble() bool

Return true if the IValue is a complex double-precision floating-point value.

func (*IValue) IsComplexDoubleList

func (ivalue *IValue) IsComplexDoubleList() bool

Return true if the IValue is a list of complex double-precision floats.

func (*IValue) IsCustomClass

func (ivalue *IValue) IsCustomClass() bool

Return true if the IValue is a custom class instance.

func (*IValue) IsDevice

func (ivalue *IValue) IsDevice() bool

Return true if the IValue is a torch device.

func (*IValue) IsDouble

func (ivalue *IValue) IsDouble() bool

Return true if the IValue is a double-precision floating-point value.

func (*IValue) IsDoubleList

func (ivalue *IValue) IsDoubleList() bool

Return true if the IValue is a list of double-precision floats.

func (*IValue) IsEnum

func (ivalue *IValue) IsEnum() bool

Return true if the IValue is an enumeration.

func (*IValue) IsFuture

func (ivalue *IValue) IsFuture() bool

Return true if the IValue is a future.

func (*IValue) IsGenerator

func (ivalue *IValue) IsGenerator() bool

Return true if the IValue is a Python generator.

func (*IValue) IsGenericDict

func (ivalue *IValue) IsGenericDict() bool

Return true if the IValue is a dictionary.

func (*IValue) IsInt

func (ivalue *IValue) IsInt() bool

Return true if the IValue is an integer.

func (*IValue) IsIntList

func (ivalue *IValue) IsIntList() bool

Return true if the IValue is a list of integers.

func (*IValue) IsList

func (ivalue *IValue) IsList() bool

Return true if the IValue is a list.

func (*IValue) IsModule

func (ivalue *IValue) IsModule() bool

Return true if the IValue is a module.

func (*IValue) IsNil

func (ivalue *IValue) IsNil() bool

Return true if the IValue references a null pointer.

func (*IValue) IsObject

func (ivalue *IValue) IsObject() bool

Return true if the IValue is an object.

func (*IValue) IsPtrType

func (ivalue *IValue) IsPtrType() bool

Return true if the IValue is a void*.

func (*IValue) IsPyObject

func (ivalue *IValue) IsPyObject() bool

Return true if the IValue is a Python object instance.

func (*IValue) IsQuantizer

func (ivalue *IValue) IsQuantizer() bool

Return true if the IValue is a quantizer.

func (*IValue) IsRRef

func (ivalue *IValue) IsRRef() bool

Return true if the IValue is an RRef.

func (*IValue) IsScalar

func (ivalue *IValue) IsScalar() bool

Return true if the IValue is a scalar.

func (*IValue) IsStorage

func (ivalue *IValue) IsStorage() bool

Return true if the IValue is a storage medium.

func (*IValue) IsStream

func (ivalue *IValue) IsStream() bool

Return true if the IValue is a data stream (e.g., a CUDA stream.)

func (*IValue) IsString

func (ivalue *IValue) IsString() bool

Return true if the IValue is a string.

func (*IValue) IsTensor

func (ivalue *IValue) IsTensor() bool

Return true if the IValue is a tensor.

func (*IValue) IsTensorList

func (ivalue *IValue) IsTensorList() bool

Return true if the IValue is a list of tensors.

func (*IValue) IsTuple

func (ivalue *IValue) IsTuple() bool

Return true if the IValue is a tuple.

func (*IValue) LengthDict

func (ivalue *IValue) LengthDict() int64

If the ivalue is a dictionary, return the number of items in the dictionary.

func (*IValue) LengthList

func (ivalue *IValue) LengthList() int64

If the ivalue is a list, return the number of items in the list.

func (*IValue) LengthTuple

func (ivalue *IValue) LengthTuple() int64

If the ivalue is a tuple, return the number of items in the tuple.

func (*IValue) ToBool

func (ivalue *IValue) ToBool() bool

Convert the IValue to a boolean.

func (*IValue) ToComplexDouble

func (ivalue *IValue) ToComplexDouble() complex128

Convert the IValue to a complex double-precision float.

func (*IValue) ToDevice

func (ivalue *IValue) ToDevice() (device *Device)

Convert the IValue to a torch device.

func (*IValue) ToDouble

func (ivalue *IValue) ToDouble() float64

Convert the IValue to a double-precision float.

func (*IValue) ToGenericDict

func (ivalue *IValue) ToGenericDict() map[interface{}]*IValue

Convert the IValue to a generic dictionary of IValues (as a map of interfaces to IValues.)

func (*IValue) ToInt

func (ivalue *IValue) ToInt() int

Convert the IValue to an integer.

func (*IValue) ToList

func (ivalue *IValue) ToList() []*IValue

Convert the IValue to a generic list of IValues (as a slice.)

func (*IValue) ToNone

func (ivalue *IValue) ToNone() string

Convert the IValue to a null pointer (i.e., the string "None".)

func (*IValue) ToString

func (ivalue *IValue) ToString() string

Convert the IValue to a string.

func (*IValue) ToTensor

func (ivalue *IValue) ToTensor() *Tensor

Convert the IValue to a tensor.

func (*IValue) ToTensorList

func (ivalue *IValue) ToTensorList() []*Tensor

Convert the IValue to a list of tensors.

func (*IValue) ToTuple

func (ivalue *IValue) ToTuple() []*IValue

Convert the IValue to a generic tuple of IValues (as a slice.)

type Tensor

type Tensor struct {
	Pointer C.Tensor
}

Tensor wraps a pointer to a C.Tensor as an unsafe Pointer.

func Abs

func Abs(tensor *Tensor) *Tensor

Take the absolute value of input.

func Abs_

func Abs_(tensor *Tensor) *Tensor

In-place version of Abs().

func Add

func Add(tensor, other *Tensor, alpha float32) *Tensor

Adds other, scaled by alpha, to input.

func All

func All(tensor *Tensor) *Tensor

Check if all values in the tensor evaluate to true.

func AllByDim

func AllByDim(tensor *Tensor, dim int, keep_dims bool) *Tensor

Check if all values in the tensor evaluate to true along the given dimension.

func Any

func Any(tensor *Tensor) *Tensor

Check if any values in the tensor evaluate to true.

func AnyByDim

func AnyByDim(tensor *Tensor, dim int, keep_dims bool) *Tensor

Check if any values in the tensor evaluate to true along the given dimension.

func Arange

func Arange(begin, end, step float32, options *TensorOptions) *Tensor

Create an inclusive range tensor from begin position to end position along integer step size.

func Argmax

func Argmax(tensor *Tensor) *Tensor

Reduce a tensor to its maximum index.

func ArgmaxByDim

func ArgmaxByDim(tensor *Tensor, dim int, keep_dims bool) *Tensor

Reduce a tensor to its maximum index along the given dimension.

func Argmin

func Argmin(tensor *Tensor) *Tensor

Reduce a tensor to its minimum index.

func ArgminByDim

func ArgminByDim(tensor *Tensor, dim int, keep_dims bool) *Tensor

Reduce a tensor to its minimum index along the given dimension.

func Cat

func Cat(tensors []*Tensor, dim int64) *Tensor

Concatenate the given sequence of tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.

func Clamp

func Clamp(tensor, minimum, maximum *Tensor) *Tensor

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

func ClampMax

func ClampMax(tensor, maximum *Tensor) *Tensor

Clamps all elements in input to have maximum value of input tensor.

func ClampMax_

func ClampMax_(tensor, maximum *Tensor) *Tensor

In-place version of torch.ClampMax()

func ClampMin

func ClampMin(tensor, minimum *Tensor) *Tensor

Clamps all elements in input to have minimum value of input tensor.

func ClampMin_

func ClampMin_(tensor, minimum *Tensor) *Tensor

In-place version of torch.ClampMin()

func Clamp_

func Clamp_(tensor, minimum, maximum *Tensor) *Tensor

In-place version of torch.Clamp()

func Concat

func Concat(tensors []*Tensor, dim int64) *Tensor

Alias of torch.Cat.

func Concatenate

func Concatenate(tensors []*Tensor, dim int64) *Tensor

Alias of torch.Cat.

func Decode

func Decode(buffer []byte) (*Tensor, error)

Decode a pickled tensor back into a structured numerical format.

func Div

func Div(tensor, other *Tensor) *Tensor

Divides input by other.

func Empty

func Empty(size []int64, options *TensorOptions) *Tensor

Create a new tensor of given size filled with empty values.

func EmptyLike

func EmptyLike(reference *Tensor) *Tensor

Create a tensor filled with empty values in the shape of a reference.

func Eq

func Eq(tensor, other *Tensor) *Tensor

Create a new tensor comparing the element-wise equality of two tensors.

func Eye

func Eye(n, m int64, options *TensorOptions) *Tensor

Create an NxM identity matrix.

func Flatten

func Flatten(tensor *Tensor, startDim, endDim int64) *Tensor

Flattens input dimensions by reshaping them into a one-dimensional tensor. Only dimensions starting with start_dim and ending with end_dim are flattened. The order of elements in input is unchanged.

Unlike NumPy’s flatten, which always copies input’s data, this function may return the original object, a view, or copy. If no dimensions are flattened, then the original object input is returned. Otherwise, if input can be viewed as the flattened shape, then that view is returned. Finally, only if the input cannot be viewed as the flattened shape is input’s data copied. See (torch.Tensor).View() for details on when a view will be returned.

func Full

func Full(size []int64, value float32, options *TensorOptions) *Tensor

Create a new tensor of given size filled with static values.

func FullLike

func FullLike(reference *Tensor, value float32) *Tensor

Create a tensor filled with static values in the shape of a reference.

func Greater

func Greater(tensor, other *Tensor) *Tensor

Compute tensor > other element-wise.

func GreaterEqual

func GreaterEqual(tensor, other *Tensor) *Tensor

Compute tensor >= other element-wise.

func IndexSelect

func IndexSelect(tensor *Tensor, dim int64, index *Tensor) *Tensor

Return a new tensor which indexes the input tensor along dimension dim using the entries in index which is a LongTensor. The returned tensor has the same number of dimensions as the original tensor (input). The dimth dimension has the same size as the length of index; other dimensions have the same size as in the original tensor.

func IsClose

func IsClose(tensor, other *Tensor, rtol, atol float64) *Tensor

Create a new tensor describing the element-wise approximate equality of two tensors with given relative and absolute tolerances.

This function checks if all `tensor` and `other` satisfy the condition:

|tensor − other| <= atol + rtol * |other|

func IsFinite

func IsFinite(tensor *Tensor) *Tensor

Test if each element of the tensor is finite or not.

func IsIn

func IsIn(tensor, other *Tensor) *Tensor

Test if each element of tensor is in other. Returns a boolean tensor of the same shape as tensor that is True for elements in other and False otherwise.

func IsInf

func IsInf(tensor *Tensor) *Tensor

Test if each element of the tensor is infinite (positive or negative) or not.

func IsNaN

func IsNaN(tensor *Tensor) *Tensor

Returns a new tensor with boolean elements representing if each element of input is NaN or not. Complex values are considered NaN when either their real and/or imaginary part is NaN.

func IsNegInf

func IsNegInf(tensor *Tensor) *Tensor

Test if each element of the tensor is negative infinity or not.

func IsPosInf

func IsPosInf(tensor *Tensor) *Tensor

Test if each element of the tensor is positive infinity or not.

func IsReal

func IsReal(tensor *Tensor) *Tensor

Return a new tensor with boolean elements representing if each element of input is real-valued or not. All real-valued types are considered real. Complex values are considered real when their imaginary part is 0.

func Less

func Less(tensor, other *Tensor) *Tensor

Compute tensor < other element-wise.

func LessEqual

func LessEqual(tensor, other *Tensor) *Tensor

Compute tensor <= other element-wise.

func Linspace

func Linspace(begin, end float32, steps int64, options *TensorOptions) *Tensor

Create an linear space tensor from begin to end with given number of total steps.

func Load

func Load(path string) (*Tensor, error)

Load a tensor from the given path.

func LogSoftmax

func LogSoftmax(tensor *Tensor, dim int64) *Tensor

Apply a softmax followed by a logarithm.

While mathematically equivalent to log(softmax(x)), doing these two operations separately is slower and numerically unstable. This function uses an alternative formulation to compute the output and gradient correctly.

func LogicalAnd

func LogicalAnd(tensor, other *Tensor) *Tensor

Compute the element-wise logical AND of the given input tensors. Zeros are treated as False and non-zeros are treated as True.

func LogicalNot

func LogicalNot(tensor *Tensor) *Tensor

Computes the element-wise logical NOT of the given input tensor. If the input tensor is not a bool tensor, zeros are treated as False and non-zeros are treated as True.

func LogicalOr

func LogicalOr(tensor, other *Tensor) *Tensor

Compute the element-wise logical OR of the given input tensors. Zeros are treated as False and non-zeros are treated as True.

func LogicalXor

func LogicalXor(tensor, other *Tensor) *Tensor

Compute the element-wise logical XOR of the given input tensors. Zeros are treated as False and non-zeros are treated as True.

func Logspace

func Logspace(begin, end float32, steps int64, base float64, options *TensorOptions) *Tensor

Create an logarithmic space tensor from begin to end with given number of total steps.

func MM

func MM(a, b *Tensor) *Tensor

Perform a matrix multiplication of the matrices A and B. If A is an (n×m) tensor, B is an (m×p) tensor, the output will be an (n×p) tensor.

func Max

func Max(tensor *Tensor) *Tensor

Reduce a tensor to its maximum value.

func Maximum

func Maximum(tensor, other *Tensor) *Tensor

Computes the element-wise maximum of input and other.

func Mean

func Mean(tensor *Tensor) *Tensor

Reduce a tensor to its mean value.

func MeanByDim

func MeanByDim(tensor *Tensor, dim int, keep_dims bool) *Tensor

Reduce a tensor to its mean value along the given dimension.

func Median

func Median(tensor *Tensor) *Tensor

Reduce a tensor to its median value.

func Min

func Min(tensor *Tensor) *Tensor

Reduce a tensor to its minimum value.

func Minimum

func Minimum(tensor, other *Tensor) *Tensor

Computes the element-wise minimum of input and other.

func Mul

func Mul(tensor, other *Tensor) *Tensor

Multiplies input by other.

func NewTensor

func NewTensor(data interface{}) *Tensor

Create a new tensor from a Go slice.

func NewTensorFromBlob

func NewTensorFromBlob(data unsafe.Pointer, dtype Dtype, sizes []int64) (output *Tensor)

Create a new tensor that clones existing contiguous memory pointed to by data, of given data-type, and with given size. This function copies the input data, so subsequent in-place operations performed on the tensor will not mutate the input data.

func NotEqual

func NotEqual(tensor, other *Tensor) *Tensor

Compute tensor != other element-wise.

func Ones

func Ones(size []int64, options *TensorOptions) *Tensor

Create a new tensor of given size filled with ones.

func OnesLike

func OnesLike(reference *Tensor) *Tensor

Create a tensor filled with ones in the shape of a reference.

func Permute

func Permute(tensor *Tensor, dims ...int64) *Tensor

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

func Pow

func Pow(tensor *Tensor, exponent float64) *Tensor

Take the power of each element in input with exponent and returns a tensor with the result.

func Rand

func Rand(size []int64, options *TensorOptions) *Tensor

Create a new tensor of given size filled with uniform random values.

func RandInt

func RandInt(size []int64, low int64, high int64, options *TensorOptions) *Tensor

Create a new tensor of given size filled with random integers in [low, high).

func RandIntLike

func RandIntLike(reference *Tensor, low int64, high int64) *Tensor

Create a tensor filled with random integers in [low, high) in the shape of a reference.

func RandLike

func RandLike(reference *Tensor) *Tensor

Create a tensor filled with uniform random values in the shape of a reference.

func RandN

func RandN(size []int64, options *TensorOptions) *Tensor

Create a new tensor of given size filled with Gaussian random values.

func RandNLike

func RandNLike(reference *Tensor) *Tensor

Create a tensor filled with Gaussian random values in the shape of a reference.

func Range

func Range(begin, end, step float32, options *TensorOptions) *Tensor

Create an exclusive range tensor from begin position to end position along integer step size.

func Reshape

func Reshape(tensor *Tensor, shape ...int64) *Tensor

Create a new tensor with an updated shape.

func Sigmoid

func Sigmoid(tensor *Tensor) *Tensor

Computes the expit (also known as the logistic sigmoid function) of the elements of input.

func Slice

func Slice(tensor *Tensor, dim, start, stop, step int64) *Tensor

Perform NumPy-like tensor slicing where dim is the dimension to slice along, start and stop determine the [start, stop) bounds of the index, and step describes the spacing between elements in the slice.

func Sqrt

func Sqrt(tensor *Tensor) *Tensor

Take the square-root of input.

func Sqrt_

func Sqrt_(tensor *Tensor) *Tensor

In-place version of Sqrt().

func Square

func Square(tensor *Tensor) *Tensor

Take the square of input.

func Square_

func Square_(tensor *Tensor) *Tensor

In-place version of Square().

func Squeeze

func Squeeze(tensor *Tensor, dim ...int64) *Tensor

Return a tensor with all the dimensions of input of size 1 removed. For example, if input is of shape: (A×1×B×C×1×D) then the out tensor will be of shape: (A×B×C×D). When dim is given, a squeeze operation is done only in the given dimension. If input is of shape: (A×1×B), squeeze(input, 0) leaves the tensor unchanged, but squeeze(input, 1) will squeeze the tensor to the shape (A×B).

func Stack

func Stack(tensors []*Tensor, dim int64) *Tensor

Concatenate a sequence of tensors along a new dimension. All tensors need to be of the same size.

func Std

func Std(tensor *Tensor) *Tensor

Reduce a tensor to its standard deviation.

func StdByDim

func StdByDim(tensor *Tensor, dim int, unbiased bool, keep_dims bool) *Tensor

Reduce a tensor to its standard deviation along the given dimension.

func Sub

func Sub(tensor, other *Tensor, alpha float32) *Tensor

Subtracts other, scaled by alpha, from input.

func Sum

func Sum(tensor *Tensor) *Tensor

Reduce a tensor to its sum.

func SumByDim

func SumByDim(tensor *Tensor, dim int, keep_dims bool) *Tensor

Reduce a tensor to its sum along the given dimension.

func Tanh

func Tanh(tensor *Tensor) *Tensor

Return a new tensor with the hyperbolic tangent of the elements of input.

func TensorFromBlob

func TensorFromBlob(data unsafe.Pointer, dtype Dtype, sizes []int64) (output *Tensor)

Create a tensor view that wraps around existing contiguous memory pointed to by data, of given data-type, and with given size. This function does not copy the data buffer so in-place operations performed on the tensor will mutate the input buffer.

func Transpose

func Transpose(tensor *Tensor, dim0, dim1 int64) *Tensor

Return a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.

If input is a strided tensor then the resulting out tensor shares its underlying storage with the input tensor, so changing the content of one would change the content of the other.

If input is a sparse tensor then the resulting out tensor does not share the underlying storage with the input tensor.

If input is a sparse tensor with compressed layout (SparseCSR, SparseBSR, SparseCSC or SparseBSC) the arguments dim0 and dim1 must be both batch dimensions, or must both be sparse dimensions. The batch dimensions of a sparse tensor are the dimensions preceding the sparse dimensions.

func Unsqueeze

func Unsqueeze(tensor *Tensor, dim int64) *Tensor

Return a new tensor with a dimension of size one inserted at the specified position. The returned tensor shares the same underlying data with this tensor. A dim value within the range [-input.dim() - 1, input.dim() + 1) can be used. Negative dim will correspond to unsqueeze() applied at dim = dim + input.dim() + 1.

func Var

func Var(tensor *Tensor) *Tensor

Reduce a tensor to its variance.

func VarByDim

func VarByDim(tensor *Tensor, dim int, unbiased, keep_dims bool) *Tensor

Reduce a tensor to its variance along the given dimension.

func Zeros

func Zeros(size []int64, options *TensorOptions) *Tensor

Create a new tensor of given size filled with zeros.

func ZerosLike

func ZerosLike(reference *Tensor) *Tensor

Create a tensor filled with zeros in the shape of a reference.

func (*Tensor) Abs

func (tensor *Tensor) Abs() *Tensor

Take the absolute value of input.

func (*Tensor) Abs_

func (tensor *Tensor) Abs_() *Tensor

In-place version of Abs().

func (*Tensor) Add

func (tensor *Tensor) Add(other *Tensor, alpha float32) *Tensor

Adds other, scaled by alpha, to input.

func (*Tensor) Add_

func (tensor *Tensor) Add_(other *Tensor, alpha float32) *Tensor

In-place version of Add().

func (*Tensor) All

func (tensor *Tensor) All() *Tensor

Check if all values in the tensor evaluate to true.

func (*Tensor) AllByDim

func (tensor *Tensor) AllByDim(dim int, keep_dims bool) *Tensor

Check if all values in the tensor evaluate to true along the given dimension.

func (*Tensor) AllClose

func (tensor *Tensor) AllClose(other *Tensor, rtol, atol float64) bool

Return true if the tensor is approximately equal to another with given relative and absolute tolerances.

This function checks if all `tensor` and `other` satisfy the condition:

|tensor − other| <= atol + rtol * |other|

func (*Tensor) Any

func (tensor *Tensor) Any() *Tensor

Check if any values in the tensor evaluate to true.

func (*Tensor) AnyByDim

func (tensor *Tensor) AnyByDim(dim int, keep_dims bool) *Tensor

Check if any values in the tensor evaluate to true along the given dimension.

func (*Tensor) Argmax

func (tensor *Tensor) Argmax() *Tensor

Reduce a tensor to its maximum index.

func (*Tensor) ArgmaxByDim

func (tensor *Tensor) ArgmaxByDim(dim int, keep_dims bool) *Tensor

Reduce a tensor to its maximum index along the given dimension.

func (*Tensor) Argmin

func (tensor *Tensor) Argmin() *Tensor

Reduce a tensor to its minimum index.

func (*Tensor) ArgminByDim

func (tensor *Tensor) ArgminByDim(dim int, keep_dims bool) *Tensor

Reduce a tensor to its minimum index along the given dimension.

func (*Tensor) Backward

func (tensor *Tensor) Backward()

Compute gradients based on the results of a forward pass through the tensor.

func (*Tensor) CastTo

func (tensor *Tensor) CastTo(dtype Dtype) *Tensor

Create a new tensor with data cast to given type.

func (*Tensor) Clamp

func (tensor *Tensor) Clamp(minimum, maximum *Tensor) *Tensor

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

func (*Tensor) ClampMax

func (tensor *Tensor) ClampMax(maximum *Tensor) *Tensor

Clamps all elements in input to have maximum value of input tensor.

func (*Tensor) ClampMax_

func (tensor *Tensor) ClampMax_(maximum *Tensor) *Tensor

In-place version of torch.ClampMax()

func (*Tensor) ClampMin

func (tensor *Tensor) ClampMin(minimum *Tensor) *Tensor

Clamps all elements in input to have minimum value of input tensor.

func (*Tensor) ClampMin_

func (tensor *Tensor) ClampMin_(minimum *Tensor) *Tensor

In-place version of torch.ClampMin()

func (*Tensor) Clamp_

func (tensor *Tensor) Clamp_(minimum, maximum *Tensor) *Tensor

In-place version of torch.Clamp()

func (*Tensor) Clone

func (tensor *Tensor) Clone() (output *Tensor)

Create a clone of an existing tensor.

func (*Tensor) CopyTo

func (tensor *Tensor) CopyTo(device *Device) *Tensor

Create a new tensor with data copied to given device.

func (*Tensor) Copy_

func (tensor *Tensor) Copy_(other *Tensor)

SetData sets the tensor data held by b to a

func (*Tensor) Detach

func (tensor *Tensor) Detach() *Tensor

Create a new tensor with any taped gradients detached and grad disabled.

func (*Tensor) Dim

func (tensor *Tensor) Dim() int64

Return the number of dimensions the tensor occupies.

func (*Tensor) Div

func (tensor *Tensor) Div(other *Tensor) *Tensor

Divides input by other.

func (*Tensor) Div_

func (tensor *Tensor) Div_(other *Tensor) *Tensor

In-place version of Div().

func (*Tensor) Dtype

func (tensor *Tensor) Dtype() Dtype

Return the data-type of the tensor.

func (*Tensor) Encode

func (tensor *Tensor) Encode() ([]byte, error)

Encode a tensor into a pickled representation. Tensors are copied to the CPU before encoding.

func (*Tensor) Eq

func (tensor *Tensor) Eq(other *Tensor) *Tensor

Create a new tensor comparing element-wise equality of the tensor to another.

func (*Tensor) Equal

func (tensor *Tensor) Equal(other *Tensor) bool

Return true if the tensor is precisely equal to other element-wise.

func (*Tensor) Expand

func (tensor *Tensor) Expand(shape ...int64) *Tensor

Expand the dimensions of the tensor.

func (*Tensor) ExpandAs

func (tensor *Tensor) ExpandAs(other *Tensor) *Tensor

Expand the dimensions of the tensor to match that of a reference.

func (*Tensor) Flatten

func (tensor *Tensor) Flatten(startDim, endDim int64) *Tensor

Flattens input dimensions by reshaping them into a one-dimensional tensor. Only dimensions starting with start_dim and ending with end_dim are flattened. The order of elements in input is unchanged.

Unlike NumPy’s flatten, which always copies input’s data, this function may return the original object, a view, or copy. If no dimensions are flattened, then the original object input is returned. Otherwise, if input can be viewed as the flattened shape, then that view is returned. Finally, only if the input cannot be viewed as the flattened shape is input’s data copied. See (torch.Tensor).View() for details on when a view will be returned.

func (*Tensor) Grad

func (tensor *Tensor) Grad() *Tensor

Access the underlying gradients of the tensor.

func (*Tensor) Greater

func (tensor *Tensor) Greater(other *Tensor) *Tensor

Compute tensor > other element-wise.

func (*Tensor) GreaterEqual

func (tensor *Tensor) GreaterEqual(other *Tensor) *Tensor

Compute tensor >= other element-wise.

func (*Tensor) Index

func (tensor *Tensor) Index(index *Tensor) *Tensor

Access elements in a tensor using an index tensor for reference.

func (*Tensor) IndexSelect

func (tensor *Tensor) IndexSelect(dim int64, index *Tensor) *Tensor

Return a new tensor which indexes the input tensor along dimension dim using the entries in index which is a LongTensor. The returned tensor has the same number of dimensions as the original tensor (input). The dimth dimension has the same size as the length of index; other dimensions have the same size as in the original tensor.

func (*Tensor) IsClose

func (tensor *Tensor) IsClose(other *Tensor, rtol, atol float64) *Tensor

Create a new tensor describing the element-wise approximate equality of the tensor to another with given relative and absolute tolerances.

This function checks if all `tensor` and `other` satisfy the condition:

|tensor − other| <= atol + rtol * |other|

func (*Tensor) IsComplex

func (tensor *Tensor) IsComplex() bool

Return true if the tensor of a complex data-type.

func (*Tensor) IsConj

func (tensor *Tensor) IsConj() bool

Return true if the (complex) tensor is in conjugated form.

func (*Tensor) IsFinite

func (tensor *Tensor) IsFinite() *Tensor

Test if each element of the tensor is finite or not.

func (*Tensor) IsFloatingPoint

func (tensor *Tensor) IsFloatingPoint() bool

Return true if the tensor of a floating-point data-type.

func (*Tensor) IsIn

func (tensor *Tensor) IsIn(other *Tensor) *Tensor

Test if each element of tensor is in other. Returns a boolean tensor of the same shape as tensor that is True for elements in other and False otherwise.

func (*Tensor) IsInf

func (tensor *Tensor) IsInf() *Tensor

Test if each element of the tensor is infinite (positive or negative) or not.

func (*Tensor) IsNaN

func (tensor *Tensor) IsNaN() *Tensor

Returns a new tensor with boolean elements representing if each element of input is NaN or not. Complex values are considered NaN when either their real and/or imaginary part is NaN.

func (*Tensor) IsNegInf

func (tensor *Tensor) IsNegInf() *Tensor

Test if each element of the tensor is negative infinity or not.

func (*Tensor) IsNonzero

func (tensor *Tensor) IsNonzero() bool

Return true if the tensor is a single non-zero element (i.e., scalar.)

func (*Tensor) IsPosInf

func (tensor *Tensor) IsPosInf() *Tensor

Test if each element of the tensor is positive infinity or not.

func (*Tensor) IsReal

func (tensor *Tensor) IsReal() *Tensor

Return a new tensor with boolean elements representing if each element of input is real-valued or not. All real-valued types are considered real. Complex values are considered real when their imaginary part is 0.

func (*Tensor) Item

func (tensor *Tensor) Item() interface{}

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

Users can do type assertion and get the value like:

```

if value, ok := a.Item().(float64); ok {
    // process the value
}

```

This function currently only supports signed data types.

func (*Tensor) Less

func (tensor *Tensor) Less(other *Tensor) *Tensor

Compute tensor < other element-wise.

func (*Tensor) LessEqual

func (tensor *Tensor) LessEqual(other *Tensor) *Tensor

Compute tensor <= other element-wise.

func (*Tensor) LogSoftmax

func (tensor *Tensor) LogSoftmax(dim int64) *Tensor

Apply a softmax followed by a logarithm.

While mathematically equivalent to log(softmax(x)), doing these two operations separately is slower and numerically unstable. This function uses an alternative formulation to compute the output and gradient correctly.

func (*Tensor) LogicalAnd

func (tensor *Tensor) LogicalAnd(other *Tensor) *Tensor

Compute the element-wise logical AND of the given input tensors. Zeros are treated as False and non-zeros are treated as True.

func (*Tensor) LogicalNot

func (tensor *Tensor) LogicalNot() *Tensor

Computes the element-wise logical NOT of the given input tensor. If the input tensor is not a bool tensor, zeros are treated as False and non-zeros are treated as True.

func (*Tensor) LogicalOr

func (tensor *Tensor) LogicalOr(other *Tensor) *Tensor

Compute the element-wise logical OR of the given input tensors. Zeros are treated as False and non-zeros are treated as True.

func (*Tensor) LogicalXor

func (tensor *Tensor) LogicalXor(other *Tensor) *Tensor

Compute the element-wise logical XOR of the given input tensors. Zeros are treated as False and non-zeros are treated as True.

func (*Tensor) MM

func (tensor *Tensor) MM(other *Tensor) *Tensor

Perform a matrix multiplication of the matrices A and B. If A is an (n×m) tensor, B is an (m×p) tensor, the output will be an (n×p) tensor.

func (*Tensor) Max

func (tensor *Tensor) Max() *Tensor

Reduce a tensor to its maximum value.

func (*Tensor) MaxByDim

func (tensor *Tensor) MaxByDim(dim int, keep_dims bool) ValueIndexPair

Reduce a tensor to its maximum value along the given dimension.

func (*Tensor) Maximum

func (tensor *Tensor) Maximum(other *Tensor) *Tensor

Computes the element-wise maximum of input and other.

func (*Tensor) Mean

func (tensor *Tensor) Mean() *Tensor

Reduce a tensor to its mean value.

func (*Tensor) MeanByDim

func (tensor *Tensor) MeanByDim(dim int, keep_dims bool) *Tensor

Reduce a tensor to its mean value along the given dimension.

func (*Tensor) Median

func (tensor *Tensor) Median() *Tensor

Reduce a tensor to its median value.

func (*Tensor) MedianByDim

func (tensor *Tensor) MedianByDim(dim int, keep_dims bool) ValueIndexPair

Reduce a tensor to its median value along the given dimension.

func (*Tensor) Min

func (tensor *Tensor) Min() *Tensor

Reduce a tensor to its minimum value.

func (*Tensor) MinByDim

func (tensor *Tensor) MinByDim(dim int, keep_dims bool) ValueIndexPair

Reduce a tensor to its minimum value along the given dimension.

func (*Tensor) Minimum

func (tensor *Tensor) Minimum(other *Tensor) *Tensor

Computes the element-wise minimum of input and other.

func (*Tensor) Mul

func (tensor *Tensor) Mul(other *Tensor) *Tensor

Multiplies input by other.

func (*Tensor) Mul_

func (tensor *Tensor) Mul_(other *Tensor) *Tensor

In-place version of Mul().

func (*Tensor) NotEqual

func (tensor *Tensor) NotEqual(other *Tensor) *Tensor

Compute tensor != other element-wise.

func (*Tensor) Numel

func (tensor *Tensor) Numel() int64

Return the number of elements in the tensor.

func (*Tensor) Permute

func (tensor *Tensor) Permute(dims ...int64) *Tensor

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

func (*Tensor) Pow

func (tensor *Tensor) Pow(exponent float64) *Tensor

Take the power of each element in input with exponent and returns a tensor with the result.

func (*Tensor) RequiresGrad

func (tensor *Tensor) RequiresGrad() bool

Return true if the tensor is taping gradients.

func (*Tensor) Reshape

func (tensor *Tensor) Reshape(shape ...int64) *Tensor

Create a new tensor with an updated shape.

func (*Tensor) ReshapeAs

func (tensor *Tensor) ReshapeAs(other *Tensor) *Tensor

Create a new tensor with an updated shape according to a reference tensor.

func (*Tensor) Save

func (tensor *Tensor) Save(path string) error

Save the tensor to the given path.

func (*Tensor) SetData

func (tensor *Tensor) SetData(other *Tensor)

Sets the tensor data to that of a separate reference tensor.

func (*Tensor) SetRequiresGrad

func (tensor *Tensor) SetRequiresGrad(requiresGrad bool)

Set the gradient taping state of the tensor to a new value.

func (*Tensor) Shape

func (tensor *Tensor) Shape() []int64

Return the shape of the tensor data.

func (*Tensor) Sigmoid

func (tensor *Tensor) Sigmoid() *Tensor

Computes the expit (also known as the logistic sigmoid function) of the elements of input.

func (*Tensor) Slice

func (tensor *Tensor) Slice(dim, start, stop, step int64) *Tensor

Perform NumPy-like tensor slicing where dim is the dimension to slice along, start and stop determine the [start, stop) bounds of the index, and step describes the spacing between elements in the slice.

func (*Tensor) Sort

func (tensor *Tensor) Sort(dim int64, descending bool) ValueIndexPair

Sort the elements of the input tensor along a given dimension by value. If descending is True then the elements are sorted in descending order by value. A struct of (Values, Indices) is returned, where the values are the sorted values and indices are the indices of the elements in the original input tensor.

func (*Tensor) Sqrt

func (tensor *Tensor) Sqrt() *Tensor

Take the square-root of input.

func (*Tensor) Sqrt_

func (tensor *Tensor) Sqrt_() *Tensor

In-place version of Sqrt().

func (*Tensor) Square

func (tensor *Tensor) Square() *Tensor

Take the square of input.

func (*Tensor) Square_

func (tensor *Tensor) Square_() *Tensor

In-place version of Square().

func (*Tensor) Squeeze

func (tensor *Tensor) Squeeze(dim ...int64) *Tensor

Return a tensor with all the dimensions of input of size 1 removed. For example, if input is of shape: (A×1×B×C×1×D) then the out tensor will be of shape: (A×B×C×D). When dim is given, a squeeze operation is done only in the given dimension. If input is of shape: (A×1×B), squeeze(input, 0) leaves the tensor unchanged, but squeeze(input, 1) will squeeze the tensor to the shape (A×B).

func (*Tensor) Std

func (tensor *Tensor) Std() *Tensor

Reduce a tensor to its standard deviation.

func (*Tensor) StdByDim

func (tensor *Tensor) StdByDim(dim int, unbiased bool, keep_dims bool) *Tensor

Reduce a tensor to its standard deviation along the given dimension.

func (*Tensor) StdMean

func (tensor *Tensor) StdMean() (*Tensor, *Tensor)

Reduce a tensor to its mean value and standard deviation.

func (*Tensor) StdMeanByDim

func (tensor *Tensor) StdMeanByDim(dim int, unbiased, keep_dims bool) (*Tensor, *Tensor)

Reduce a tensor to its mean value and standard deviation along given dimension.

func (*Tensor) String

func (tensor *Tensor) String() string

Convert the tensor to a readable string representation.

func (*Tensor) Sub

func (tensor *Tensor) Sub(other *Tensor, alpha float32) *Tensor

Subtracts other, scaled by alpha, from input.

func (*Tensor) Sub_

func (tensor *Tensor) Sub_(other *Tensor, alpha float32) *Tensor

In-place version of Sub().

func (*Tensor) Sum

func (tensor *Tensor) Sum() *Tensor

Reduce a tensor to its sum.

func (*Tensor) SumByDim

func (tensor *Tensor) SumByDim(dim int, keep_dims bool) *Tensor

Reduce a tensor to its sum along the given dimension.

func (*Tensor) Tanh

func (tensor *Tensor) Tanh() *Tensor

Return a new tensor with the hyperbolic tangent of the elements of input.

func (*Tensor) To

func (tensor *Tensor) To(device *Device, dtype Dtype) *Tensor

Create a new tensor with data copied to given device and cast to given type.

func (*Tensor) ToBytes

func (tensor *Tensor) ToBytes() []byte

Convert a tensor to a raw binary representation as a byte slice. Note that this operation implies a safe copy of the underlying data. There are no garbage collector side-effects from usage of this function.

func (*Tensor) ToBytesUnsafe

func (tensor *Tensor) ToBytesUnsafe() []byte

Convert a tensor to a raw binary representation as a byte slice. Note that this is a copy-free operation and simply returns a slice with its header updated to point to the underlying tensor data. If the tensor is garbage collected, the slice will be invalidated. Usage of this function typically requires calls to `runtime.KeepAlive(tensor)` for the input tensor to prevent accidentally freeing the tensor before the byte slice is consumed.

func (*Tensor) ToSlice

func (tensor *Tensor) ToSlice() interface{}

Convert the Tensor to a Go slice. This function implies a flattening of the tensor to return a 1-dimensional vector.

func (*Tensor) TopK

func (tensor *Tensor) TopK(k, dim int64, largest, sorted bool) ValueIndexPair

Return the k largest elements of the given input tensor along a given dimension. If largest is False then the k smallest elements are returned. A struct of (Values, Indices) is returned with the values and indices of the largest k elements of each row of the input tensor in the given dimension dim. The boolean option sorted if True, will make sure that the returned k elements are themselves sorted.

func (*Tensor) Transpose

func (tensor *Tensor) Transpose(dim0, dim1 int64) *Tensor

Return a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.

If input is a strided tensor then the resulting out tensor shares its underlying storage with the input tensor, so changing the content of one would change the content of the other.

If input is a sparse tensor then the resulting out tensor does not share the underlying storage with the input tensor.

If input is a sparse tensor with compressed layout (SparseCSR, SparseBSR, SparseCSC or SparseBSC) the arguments dim0 and dim1 must be both batch dimensions, or must both be sparse dimensions. The batch dimensions of a sparse tensor are the dimensions preceding the sparse dimensions.

func (*Tensor) Unsqueeze

func (tensor *Tensor) Unsqueeze(dim int64) *Tensor

Return a new tensor with a dimension of size one inserted at the specified position. The returned tensor shares the same underlying data with this tensor. A dim value within the range [-input.dim() - 1, input.dim() + 1) can be used. Negative dim will correspond to unsqueeze() applied at dim = dim + input.dim() + 1.

func (*Tensor) Var

func (tensor *Tensor) Var() *Tensor

Reduce a tensor to its variance.

func (*Tensor) VarByDim

func (tensor *Tensor) VarByDim(dim int, unbiased, keep_dims bool) *Tensor

Reduce a tensor to its variance along the given dimension.

func (*Tensor) VarMean

func (tensor *Tensor) VarMean() (*Tensor, *Tensor)

Reduce a tensor to its mean value and variance.

func (*Tensor) VarMeanByDim

func (tensor *Tensor) VarMeanByDim(dim int, unbiased, keep_dims bool) (*Tensor, *Tensor)

Reduce a tensor to its mean value and variance along given dimension.

func (*Tensor) View

func (tensor *Tensor) View(shape ...int64) *Tensor

Create a new tensor with an updated view of the underlying data.

func (*Tensor) ViewAs

func (tensor *Tensor) ViewAs(other *Tensor) *Tensor

Create a new tensor with an updated view of the underlying data that matches that of a reference tensor.

type TensorOptions

type TensorOptions struct {
	Pointer C.TensorOptions
}

TensorOptions wraps a C.TensorOptions.

func NewTensorOptions

func NewTensorOptions() (options *TensorOptions)

Create a new TensorOptions.

func (*TensorOptions) Device

func (options *TensorOptions) Device(device Device) *TensorOptions

Create a new TensorOptions with the given compute device.

func (*TensorOptions) Dtype

func (options *TensorOptions) Dtype(value Dtype) *TensorOptions

Create a new TensorOptions with the given data type.

func (*TensorOptions) PinnedMemory

func (options *TensorOptions) PinnedMemory(pinnedMemory bool) *TensorOptions

Create a new TensorOptions with the given memory pinning state.

func (*TensorOptions) RequiresGrad

func (options *TensorOptions) RequiresGrad(requiresGrad bool) *TensorOptions

Create a new TensorOptions with the given gradient taping state.

type ValueIndexPair

type ValueIndexPair struct {
	Values, Indices *Tensor
}

A representation of the return type for a paired value/index selection call.

func MaxByDim

func MaxByDim(tensor *Tensor, dim int, keep_dims bool) ValueIndexPair

Reduce a tensor to its maximum value along the given dimension.

func MedianByDim

func MedianByDim(tensor *Tensor, dim int, keep_dims bool) ValueIndexPair

Reduce a tensor to its median value along the given dimension.

func MinByDim

func MinByDim(tensor *Tensor, dim int, keep_dims bool) ValueIndexPair

Reduce a tensor to its minimum value along the given dimension.

func Sort

func Sort(tensor *Tensor, dim int64, descending bool) ValueIndexPair

Sort the elements of the input tensor along a given dimension by value. If descending is True then the elements are sorted in descending order by value. A struct of (Values, Indices) is returned, where the values are the sorted values and indices are the indices of the elements in the original input tensor.

func TopK

func TopK(tensor *Tensor, k, dim int64, largest, sorted bool) ValueIndexPair

Return the k largest elements of the given input tensor along a given dimension. If largest is False then the k smallest elements are returned. A struct of (Values, Indices) is returned with the values and indices of the largest k elements of each row of the input tensor in the given dimension dim. The boolean option sorted if True, will make sure that the returned k elements are themselves sorted.

Directories

Path Synopsis
cmd
nn
vision
ops

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL