tensor package for golang
go get -u github.com/gotensor/gotensor
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
MIT