tensor

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package tensor defines the tensor interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CPUOperator

type CPUOperator struct {
}

CPUOperator can process CPU tensors.

func (CPUOperator) Adam

func (to CPUOperator) Adam(
	params, gradients Tensor,
	vHistory, sHistory Tensor,
	smoothFactor1, smoothFactor2, learningRate float64,
)

Adam runs the adam gradient descent algorithm.

func (CPUOperator) AvgPoolingBackward

func (to CPUOperator) AvgPoolingBackward(forwardIn, backwardIn Tensor,
	kernelSize, padding, stride []int) Tensor

AvgPoolingBackward calculates the backward propagation results for AvgPooling layers.

func (CPUOperator) AvgPoolingForward

func (to CPUOperator) AvgPoolingForward(
	t Tensor,
	kernelSize, padding, stride []int,
) Tensor

AvgPoolingForward calculates the forward propagation results for AvgPooling layers.

func (CPUOperator) Clear

func (to CPUOperator) Clear(t Tensor)

Clear sets all the element to 0.

func (CPUOperator) Clone

func (to CPUOperator) Clone(t Tensor) Tensor

Clone duplicates the tensor.

func (CPUOperator) Copy

func (to CPUOperator) Copy(dst, src Tensor)

Copy moves the data, size, and descriptor from the src tensor to the destination tensor.

func (CPUOperator) Create

func (to CPUOperator) Create(size []int) Tensor

Create creates a new CPU tensor.

func (CPUOperator) CreateWithData

func (to CPUOperator) CreateWithData(
	data []float64,
	size []int,
	descriptor string,
) Tensor

CreateWithData creates a new CPU tensor and fill it with the given data.

func (CPUOperator) CrossEntropy

func (to CPUOperator) CrossEntropy(t Tensor, label []int) float64

CrossEntropy calculates the cross entropy.

func (CPUOperator) CrossEntropyDerivative

func (to CPUOperator) CrossEntropyDerivative(t Tensor, label []int) Tensor

CrossEntropyDerivative generators the final derivative of the forward pass.

func (CPUOperator) Dilate

func (to CPUOperator) Dilate(t Tensor, dilate []int) Tensor

Dilate add 0s in the rows and columns.

func (CPUOperator) Dump

func (to CPUOperator) Dump(t Tensor) string

Dump converts the content of the tensor to a string.

func (CPUOperator) ElementWiseMul

func (to CPUOperator) ElementWiseMul(t1, t2 Tensor) Tensor

ElementWiseMul performs element-wise multiplication operation.

func (CPUOperator) Free

func (to CPUOperator) Free(t Tensor)

Free does nothing as Go's gabage collector will take care of it.

func (CPUOperator) Gemm

func (to CPUOperator) Gemm(
	transA, transB bool,
	alpha, beta float64,
	a, b, c Tensor,
) Tensor

Gemm performs alpha x A x B + beta x C operation.

func (CPUOperator) Im2Col

func (to CPUOperator) Im2Col(
	t Tensor,
	kernelSize, padding, stride, dilation []int,
) Tensor

Im2Col performs the im2col operation.

func (CPUOperator) Init

func (to CPUOperator) Init(t Tensor, data []float64)

Init initialize tensor with data and size.

func (CPUOperator) MaxPoolingBackward

func (to CPUOperator) MaxPoolingBackward(
	forwardIn, backwardIn, mask Tensor,
	kernelSize, padding, stride []int,
) Tensor

MaxPoolingBackward calculates the back propagation results for MaxPooling layers.

func (CPUOperator) MaxPoolingForward

func (to CPUOperator) MaxPoolingForward(
	t Tensor,
	kernelSize, padding, stride []int,
) (out, mask Tensor)

MaxPoolingForward calculates the forward propagation results for MaxPooling layers.

func (CPUOperator) RMSProp

func (to CPUOperator) RMSProp(
	params, gradients Tensor,
	sHistory Tensor,
	smoothFactor, learningRate float64,
)

RMSProp runs the rmsProp gradient descent algorithm.

func (CPUOperator) ReluBackward

func (to CPUOperator) ReluBackward(forwardIn, backwardIn Tensor) Tensor

ReluBackward runs the relu backward propagation algorithm.

func (CPUOperator) ReluForward

func (to CPUOperator) ReluForward(in Tensor) Tensor

ReluForward runs the ReLU forward propagation algorithm.

func (CPUOperator) Repeat

func (to CPUOperator) Repeat(t Tensor, times int) Tensor

Repeat will create a new tensor that duplicates the input tensor by n times.

func (CPUOperator) Reshape

func (to CPUOperator) Reshape(t Tensor, newSize []int) Tensor

Reshape creates a new tensor wit the same element but a different shape.

func (CPUOperator) Rotate180

func (to CPUOperator) Rotate180(t Tensor) Tensor

Rotate180 rotates all the lowest level matrics by 180 degree.

func (CPUOperator) ScaleAdd

func (to CPUOperator) ScaleAdd(alpha, beta float64, a, b Tensor) Tensor

ScaleAdd performs the alpht*A + beta*B operation

func (CPUOperator) Slice

func (to CPUOperator) Slice(
	t Tensor,
	start, end int,
) Tensor

Slice creates another tensor that shares part of the underlying buffer bounded by [start, end).

func (CPUOperator) Softmax

func (to CPUOperator) Softmax(t Tensor) Tensor

Softmax caculates the softmax vector.

func (CPUOperator) SoftmaxCrossEntropyDerivative

func (to CPUOperator) SoftmaxCrossEntropyDerivative(
	t Tensor,
	label []int,
) Tensor

SoftmaxCrossEntropyDerivative generators the final derivative of the forward pass.

func (CPUOperator) Sum

func (to CPUOperator) Sum(t Tensor, axis []int) Tensor

Sum calculate sums over given axis.

func (CPUOperator) Transpose

func (to CPUOperator) Transpose(t Tensor, order []int) Tensor

Transpose can reorder the axes.

func (CPUOperator) Zeros

func (to CPUOperator) Zeros(size []int) Tensor

Zeros creates a tensor with given size and fill the tensor with 0s.

type Operator

type Operator interface {
	Create(size []int) Tensor
	CreateWithData(data []float64, size []int, descriptor string) Tensor
	Free(t Tensor)
	Copy(dst, src Tensor)
	Clone(t Tensor) Tensor
	Dump(t Tensor) string

	Init(t Tensor, data []float64)
	Slice(t Tensor, start, end int) Tensor
	Repeat(t Tensor, times int) Tensor
	Clear(t Tensor)
	Zeros(size []int) Tensor

	Reshape(t Tensor, newSize []int) Tensor
	Transpose(t Tensor, order []int) Tensor
	Rotate180(t Tensor) Tensor
	Dilate(t Tensor, dilate []int) Tensor

	Sum(t Tensor, axis []int) Tensor
	Gemm(transA, transB bool, alpha, beta float64, a, b, c Tensor) Tensor
	Im2Col(t Tensor, kernelSize, padding, stride, dilation []int) Tensor
	MaxPoolingForward(t Tensor,
		kernelSize, padding, stride []int,
	) (out, mask Tensor)
	MaxPoolingBackward(
		forwardIn, backwardIn, mask Tensor,
		kernelSize, padding, stride []int) Tensor
	AvgPoolingForward(t Tensor,
		kernelSize, padding, stride []int) Tensor
	AvgPoolingBackward(forwardIn, backwardIn Tensor,
		kernelSize, padding, stride []int) Tensor
	Softmax(t Tensor) Tensor
	CrossEntropy(t Tensor, label []int) float64
	CrossEntropyDerivative(t Tensor, label []int) Tensor
	SoftmaxCrossEntropyDerivative(t Tensor, label []int) Tensor

	ElementWiseMul(t1, t2 Tensor) Tensor
	ScaleAdd(alpha, beta float64, a, b Tensor) Tensor

	RMSProp(params, gradient, sHistory Tensor,
		smoothFactor, learningRate float64)
	Adam(params, gradients, vHistory, sHistory Tensor,
		smoothFactor1, smoothFactor2, learningRate float64)

	ReluForward(in Tensor) Tensor
	ReluBackward(forwardIn, backwardIn Tensor) Tensor
}

Operator can process tensors

type SimpleTensor

type SimpleTensor struct {
	// contains filtered or unexported fields
}

A SimpleTensor is a multi-dimensional matrix.

func (SimpleTensor) Descriptor

func (t SimpleTensor) Descriptor() string

Descriptor returns the descriptor of the tensor.

func (SimpleTensor) Dim

func (t SimpleTensor) Dim() int

Dim returns the number of dimensions that the tensor has.

func (SimpleTensor) NumElement

func (t SimpleTensor) NumElement() int

NumElement returns the total number of elements in the tensor.

func (*SimpleTensor) SetDescriptor

func (t *SimpleTensor) SetDescriptor(d string)

SetDescriptor sets the descriptor of the tensor

func (*SimpleTensor) SetSize

func (t *SimpleTensor) SetSize(newSize []int)

SetSize sets the size of the tensor

func (SimpleTensor) Size

func (t SimpleTensor) Size() []int

Size returns the size of the tensor.

func (SimpleTensor) Vector

func (t SimpleTensor) Vector() []float64

Vector returns the raw data of the tensor.

type Tensor

type Tensor interface {
	// Dim returns the number of dimension of the tensor
	Dim() int

	// NumElement returns the total number of elements in the tensor
	NumElement() int

	// Size returns the length of the tensor in each dimension, from the
	// outermost dimension to the innermost dimension.
	Size() []int

	// SetSize sets the size of the tensor.
	SetSize([]int)

	// Vector returns the data of the tensor represented in a pure vector.
	// Here, we use float64. However, the concrete tensor implementation can
	// use lower-precision numbers.
	Vector() []float64

	// Descriptor represents what does each dimension of the tensor represents.
	//
	// Tokens that represent the meaning of the dimensions include N, C, H, W.
	Descriptor() string

	// SetDescriptor sets the descriptor of the tensor.
	SetDescriptor(d string)
}

A Tensor is a multi-dimension matrix.

Jump to

Keyboard shortcuts

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