tfutil

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Index

Constants

View Source
const (
	TypeScalar = "scalar"
	TypeTensor = "tensor"
)

Variables

This section is empty.

Functions

func Cast

func Cast[T, S PrimitiveTypes](input *Tensor[T], output *Tensor[S]) error

Cast casts input tensor into output tensor data type

Types

type PrimitiveTypes

type PrimitiveTypes interface {
	bool |
		int8 | int16 | int32 | int64 |
		uint8 | uint16 | uint32 | uint64 |
		float32 | float64 |
		complex64 | complex128 |
		string
}

PrimitiveTypes are type constraints for supported underlying types

type Scalar

type Scalar[T PrimitiveTypes] struct {
	// contains filtered or unexported fields
}

Scalar represents a singular value type parametrized by supported types

func NewScalar

func NewScalar[T PrimitiveTypes](value T) *Scalar[T]

NewScalar generates a new scalar parametrized by a data type

func (*Scalar[T]) Clone

func (g *Scalar[T]) Clone() *Scalar[T]

Clone creates a clone of receiver scalar

func (*Scalar[T]) Marshal

func (g *Scalar[T]) Marshal() (*tf.Tensor, error)

Marshal produces an instance of upstream tensor based on scalar value

func (*Scalar[T]) MarshalJSON

func (g *Scalar[T]) MarshalJSON() ([]byte, error)

MarshalJSON serializes scalar with additional metadata such as data types in tensorflow and go. Use scalar in json.Marshal for this method to be called indirectly.

func (*Scalar[T]) String

func (g *Scalar[T]) String() string

String prints numpy representation of scalar value

func (*Scalar[T]) Unmarshal

func (g *Scalar[T]) Unmarshal(tfTensor *tf.Tensor) error

Unmarshal populates receiver scalar using value from input upstream tensor

func (*Scalar[T]) UnmarshalJSON

func (g *Scalar[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON parses serialized scalar value

func (*Scalar[T]) Value

func (g *Scalar[T]) Value() T

Value retrieves underlying value of the scalar

type Tensor

type Tensor[T PrimitiveTypes] struct {
	// contains filtered or unexported fields
}

Tensor is a generic non-scalar data structures that includes vectors, matrices and higher dimensional structures. Tensor's representation is a slice because it is easier to work with compared to multidimensional slices. Shape stores the underlying dimensionality.

func Abs

func Abs(complexT *Tensor[complex128]) *Tensor[float64]

Abs returns absolute valued tensor such that each element of output is absolute value of each of the complex values of input

func ApplyOperatorXY added in v0.6.0

func ApplyOperatorXY[T PrimitiveTypes](x, y *Tensor[T],
	operator func(*op.Scope, tf.Output, tf.Output) tf.Output) (*Tensor[T], error)

ApplyOperatorXY applies any operator accepting two inputs tensors and outputting single tensor, hence the name XY

func ApplyOperators added in v0.6.0

func ApplyOperators[T PrimitiveTypes](input *Tensor[T],
	operators ...func(*op.Scope, tf.Output) tf.Output) (*Tensor[T], error)

ApplyOperators successively applies operators (last one first) For instance if the operator list is [op1, op2, op3], then it will be executed as op1(op2(op3(input)))

func Complex128

func Complex128(realT, imagT *Tensor[float64]) (*Tensor[complex128], error)

Complex128 packs input real and imaginary parts to a complex128 valued tensor

func Complex64

func Complex64(realT, imagT *Tensor[float32]) (*Tensor[complex64], error)

Complex64 packs input real and imaginary parts to a complex64 valued tensor

func DotApply

func DotApply[T PrimitiveTypes](f func(values ...T) T, tensors ...*Tensor[T]) (*Tensor[T], error)

DotApply applies input function f over each corresponding elements of input tensors and returns a new tensor using output of that function. For instance, if input function f sums up all values of its input, then this will have a result of performing element wise sum over input tensors

func Imag32

func Imag32(complexT *Tensor[complex64]) *Tensor[float32]

Imag32 pulls imaginary elements from input tensor and packs them into a new tensor of float32 type

func Imag64

func Imag64(complexT *Tensor[complex128]) *Tensor[float64]

Imag64 pulls imaginary elements from input tensor and packs them into a new tensor of float64 type

func MatrixInverse

func MatrixInverse[T PrimitiveTypes](input *Tensor[T]) (*Tensor[T], error)

MatrixInverse inverts the tensor assuming it is a square matrix, otherwise it will throw error

func MatrixMultiply

func MatrixMultiply[T PrimitiveTypes](x, y *Tensor[T]) (*Tensor[T], error)

MatrixMultiply performs matrix multiplication

func Mul

func Mul[T PrimitiveTypes](x, y *Tensor[T]) (*Tensor[T], error)

Mul performs element wise multiplication of two tensors

func NewTensor

func NewTensor[T PrimitiveTypes](value []T, shape ...int) (*Tensor[T], error)

NewTensor creates a new tensor with specified dimensions. If no dimension argument is specified, it is assumed that a vector is being created and the shape assumes value equal to the length of the input slice

func NewTensorFromAny

func NewTensorFromAny[T PrimitiveTypes](value any) (*Tensor[T], error)

NewTensorFromAny does not take shape inputs and infers shape of tensor from the dimensions of the value, as in value being a multidimensional slice of data. The type parametrization must be provided at the time of instantiating this function.

func NewTensorFromFunc

func NewTensorFromFunc[T PrimitiveTypes](f func(int) T, shape ...int) (*Tensor[T], error)

NewTensorFromFunc generates a new tensor using an input function that is called for each element

func Real32

func Real32(complexT *Tensor[complex64]) *Tensor[float32]

Real32 pulls real elements from input tensor and packs them into a new tensor of float32 type

func Real64

func Real64(complexT *Tensor[complex128]) *Tensor[float64]

Real64 pulls real elements from input tensor and packs them into a new tensor of float64 type

func Transpose

func Transpose[T PrimitiveTypes](input *Tensor[T], perm ...int) (*Tensor[T], error)

Transpose transposes a tensor. perm refers to the new order of dimensions. For instance, if input tensor is 2x3 and perm for a standard transpose should be [1, 0] referring to a shape of 3x2. If perm values are not provides it defaults to such reversal of input shape.

func (*Tensor[T]) ApplyFunc

func (g *Tensor[T]) ApplyFunc(f func(T) T)

ApplyFunc applies input function f over each element of tensor transforming it in place

func (*Tensor[T]) Clone

func (g *Tensor[T]) Clone() (*Tensor[T], error)

Clone creates a clone of receiver tensor

func (*Tensor[T]) ExpandDims

func (g *Tensor[T]) ExpandDims(dim int) error

ExpandDims adds a new dimension

func (*Tensor[T]) GetElement

func (g *Tensor[T]) GetElement(indices ...int) (T, error)

GetElement retrieves an element indexed by indices. This is a slow method, for faster access it is recommended to obtain a multidimensional slice and index off of that.

func (*Tensor[T]) GetMultiDimSlice

func (g *Tensor[T]) GetMultiDimSlice() (any, error)

GetMultiDimSlice fetches a multidimensional slice corresponding to underlying slice. For instance a float64 tensor with shape [2, 3, 4] will result in a [][][]float64 slice as output of this method since there are three dimensions. Similarly, a bool tensor with shape [2, 3, 3, 4] will result in [][][][]bool as output. Please note that it is users responsibility to perform type assertion correctly on returned value

func (*Tensor[T]) Marshal

func (g *Tensor[T]) Marshal() (*tf.Tensor, error)

Marshal returns an instance of upstream tensorflow tensor object. string tensor reshaping is currently not supported natively in go. it is, however, possible to reshape it via a tf session running over a graphdef that was generated using python code for reshape function

func (*Tensor[T]) MarshalJSON

func (g *Tensor[T]) MarshalJSON() ([]byte, error)

MarshalJSON serializes tensor with additional metadata such as tensorflow data type and go data type. Use tensor in json.Marshal for this method to be called indirectly.

func (*Tensor[T]) NumElements

func (g *Tensor[T]) NumElements() int

NumElements is the total number of elements in the tensor

func (*Tensor[T]) Reshape

func (g *Tensor[T]) Reshape(shape ...int) error

Reshape reshapes to new shape

func (*Tensor[T]) SetElement

func (g *Tensor[T]) SetElement(value T, indices ...int) error

func (*Tensor[T]) Shape

func (g *Tensor[T]) Shape() []int

Shape returns tensor shape

func (*Tensor[T]) String

func (g *Tensor[T]) String() string

String prints matrices in human-readable format

func (*Tensor[T]) Sub

func (g *Tensor[T]) Sub(start, end, stride []int) (*Tensor[T], error)

Sub fetches sub a new tensor without altering original. startIndices if nil is set to a slice of zeros indicating starting from the beginning of the tensor. Length of such slice is always equal to the receiver dimension indicating start value for each dimension. Similarly, endLengths indicate end value similar to how a sub slicing end index works. if endLengths is nil, it is set to the shape slice of the receiver tensor. strides are jumps and if nil is set to slice of ones. The lengths of each of these inputs is, therefore, either nil or equal to the length of the shape of the receiver tensor

func (*Tensor[T]) Unmarshal

func (g *Tensor[T]) Unmarshal(tfTensor *tf.Tensor) error

Unmarshal populates receiver based on input upstream tensor object. string tensor reshaping is currently not supported natively in go. it is, however, possible to reshape it via a tf session running over a graphdef that was generated using python code for reshape function

func (*Tensor[T]) UnmarshalJSON

func (g *Tensor[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON parses serialized tensor

func (*Tensor[T]) Value

func (g *Tensor[T]) Value() []T

Value returns underlying slice representation of the tensor

Jump to

Keyboard shortcuts

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