gotensor

package module
v0.0.0-...-d3aba7b Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: MIT Imports: 3 Imported by: 0

README

gotensor

Build status go.dev reference Go Report Card

tensor package for golang

Install

go get -u github.com/gotensor/gotensor

How to use

Creating a new tensor:

data := []interface{}{1, 2, 3, 4, 5, 6}
shape := []int{2, 3}
tx := tensor.NewTensor(data, shape)

fmt.Println(tx)
// Shape: [2 3]
// 1 2 3
// 4 5 6

Creating a tensor with all zeros:

shape := []int{3, 3}
tx := tensor.Zeros(shape)

fmt.Println(tx)
// Shape: [3 3]
// 0 0 0
// 0 0 0
// 0 0 0

Creating a tensor with all ones:

shape := []int{2, 2}
tx := tensor.Ones(shape)

fmt.Println(tx)
// Shape: [2 2]
// 1 1
// 1 1

Getting a single value from the tensor:

data := []interface{}{1, 2, 3, 4, 5, 6}
shape := []int{2, 3}
tx := tensor.NewTensor(data, shape)
value, err := tx.Get(0, 1)
if err != nil {
    fmt.Println(err)
}
fmt.Println(value) // output: 2

Setting a single value in the tensor:

data := []interface{}{1, 2, 3, 4, 5, 6}
shape := []int{2, 3}
tx := tensor.NewTensor(data, shape)
err := tx.Set(7, 1, 2)
if err != nil {
    fmt.Println(err)
}
fmt.Println(tx)
// Shape: [2 3]
// 1 2 3
// 4 5 7

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tensor

type Tensor struct {
	// Data holds the tensor values.
	Data []interface{}

	// Shape holds the tensor dimensions.
	Shape []int
}

Tensor represents a multidimensional array of values.

func NewTensor

func NewTensor(data []interface{}, shape []int) Tensor

NewTensor creates a new tensor with the given data and shape.

func Ones

func Ones(shape []int) Tensor

Ones creates a new tensor initialized with ones of the given shape.

func Zeros

func Zeros(shape []int) Tensor

Zeros creates a new tensor initialized with zeros of the given shape.

func (*Tensor) Get

func (t *Tensor) Get(indices ...int) (interface{}, error)

Get retrieves a single value from the tensor given the indices specified. It returns an error if the indices provided are out of bounds.

func (*Tensor) Len

func (t *Tensor) Len() int

Len returns the total number of elements in the tensor.

func (*Tensor) Set

func (t *Tensor) Set(value interface{}, indices ...int) error

Set sets a single value in the tensor given the value to set and the indices specified. It returns an error if the indices provided are out of bounds.

func (Tensor) String

func (t Tensor) String() string

String returns a string representation of the tensor.

Jump to

Keyboard shortcuts

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