native

package
v0.9.24 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2022 License: Apache-2.0 Imports: 4 Imported by: 33

Documentation

Overview

package native is a utility package for gorgonia.org/tensor.

Amongst other things, it provides iterators that use Go slice semantics, while keeping a reference to the underlying memory. This means you can update the slices and the changes will be reflected back into the original tensor.

There is of course a cost of using the native iterators and selectors - allocation costs. For best performance, don't use these in a tight loop.

Example (Clobber)

The iterators are iteratos in the truest sense. The data isn't copied, as this example shows

var T *Dense
T = New(WithShape(2, 3), WithBacking(Range(Float64, 0, 6)))
fmt.Printf("Before :\n%v", T)

xx, _ := MatrixF64(T)
xx[1][1] = 10000
fmt.Printf("After :\n%v", T)
Output:

Before :
⎡0  1  2⎤
⎣3  4  5⎦
After :
⎡    0      1      2⎤
⎣    3  10000      5⎦
Example (Iterator)

There are times where it is more effective to use native Go slice semantics to do work (for example, when performing batch work over kernels). Iterators are useful for this purpose. This package provides iterators for the standard types However, custom types are also available. See Vector, Matrix and Tensor3 examples.

var T *Dense
T = New(WithShape(2, 3), WithBacking(Range(Float64, 0, 6)))
x, err := MatrixF64(T)
if err != nil {
	fmt.Printf("ERR: %v", err)
}

for _, row := range x {
	fmt.Printf("%v\n", row)
}
Output:

[0 1 2]
[3 4 5]
Example (Matrix)
backing := []MyType{
	0, 1,
	2, 3,
	4, 5,
}
T := tensor.New(tensor.WithShape(3, 2), tensor.WithBacking(backing))
val, err := Matrix(T)
if err != nil {
	fmt.Printf("error: %v", err)
}

it := val.([][]MyType)
fmt.Println(it)
Output:

[[0 1] [2 3] [4 5]]
Example (Select)

The NativeSelect function squashes the dimensions, and returns an iterator in native Go slice semantics.

// Selection is a bit of an interesting use case. Sometimes you don't want to iterate through the layers.
//
// For example, in a number of use cases where you have a 4-Tensor, you'd typically reshape it to some
// 2D matrix which can then be plugged into BLAS algorithms directly. Sometimes you wouldn't need to reshape.
// All you have to do is squash the dimensions inwards. This function does that.
//
// The best way to explain the Select functions is through concrete examples.
// Imagine a tensor with (2,3,4,5) shape. Arbitrarily, we call them (NCHW) - Batch Size, Channel Count, Height, Width.
// If we want to select all the channels, across all batches, then `NativeSelectX(T, 1)` would yield all channels. The resulting matrix will be (6, 20)
// If we want to select all the heights, across all channels and batches, then `NativeSelectX(T, 2) will yield all heights. The resulting matrix will be (24, 5)
//
// If for some reason the format was in NHWC, then you would need to reshape. This wouldn't be useful.

var T *Dense
T = New(WithShape(2, 3, 4, 5), WithBacking(Range(Float64, 0, 2*3*4*5)))
x, err := SelectF64(T, 1)
if err != nil {
	fmt.Printf("ERR %v", err)
}
for _, row := range x {
	fmt.Printf("%3.0f\n", row)
}
Output:

[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19]
[ 20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39]
[ 40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59]
[ 60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79]
[ 80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99]
[100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119]
Example (Tensor3)
backing := []MyType{
	0, 1, 2, 3,
	4, 5, 6, 7,
	8, 9, 10, 11,

	12, 13, 14, 15,
	16, 17, 18, 19,
	20, 21, 22, 23,
}
T := tensor.New(tensor.WithShape(2, 3, 4), tensor.WithBacking(backing))
val, err := Tensor3(T)
if err != nil {
	fmt.Printf("error: %v", err)
}
it := val.([][][]MyType)
fmt.Println(it)
Output:

[[[0 1 2 3] [4 5 6 7] [8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]]
Example (Vector)
backing := []MyType{
	0, 1, 2, 3,
}
T := tensor.New(tensor.WithShape(4), tensor.WithBacking(backing))
val, err := Vector(T)
if err != nil {
	fmt.Printf("error: %v", err)
}
it := val.([]MyType)
fmt.Println(it)
Output:

[0 1 2 3]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Matrix

func Matrix(t *Dense) (interface{}, error)

func MatrixB

func MatrixB(t *Dense) (retVal [][]bool, err error)

MatrixB converts a *Dense into a [][]bool If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixC128

func MatrixC128(t *Dense) (retVal [][]complex128, err error)

MatrixC128 converts a *Dense into a [][]complex128 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixC64

func MatrixC64(t *Dense) (retVal [][]complex64, err error)

MatrixC64 converts a *Dense into a [][]complex64 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixF32

func MatrixF32(t *Dense) (retVal [][]float32, err error)

MatrixF32 converts a *Dense into a [][]float32 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixF64

func MatrixF64(t *Dense) (retVal [][]float64, err error)

MatrixF64 converts a *Dense into a [][]float64 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixI

func MatrixI(t *Dense) (retVal [][]int, err error)

MatrixI converts a *Dense into a [][]int If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixI16

func MatrixI16(t *Dense) (retVal [][]int16, err error)

MatrixI16 converts a *Dense into a [][]int16 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixI32

func MatrixI32(t *Dense) (retVal [][]int32, err error)

MatrixI32 converts a *Dense into a [][]int32 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixI64

func MatrixI64(t *Dense) (retVal [][]int64, err error)

MatrixI64 converts a *Dense into a [][]int64 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixI8

func MatrixI8(t *Dense) (retVal [][]int8, err error)

MatrixI8 converts a *Dense into a [][]int8 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixStr

func MatrixStr(t *Dense) (retVal [][]string, err error)

MatrixStr converts a *Dense into a [][]string If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixU

func MatrixU(t *Dense) (retVal [][]uint, err error)

MatrixU converts a *Dense into a [][]uint If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixU16

func MatrixU16(t *Dense) (retVal [][]uint16, err error)

MatrixU16 converts a *Dense into a [][]uint16 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixU32

func MatrixU32(t *Dense) (retVal [][]uint32, err error)

MatrixU32 converts a *Dense into a [][]uint32 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixU64

func MatrixU64(t *Dense) (retVal [][]uint64, err error)

MatrixU64 converts a *Dense into a [][]uint64 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func MatrixU8

func MatrixU8(t *Dense) (retVal [][]uint8, err error)

MatrixU8 converts a *Dense into a [][]uint8 If the *Dense does not represent a matrix of the wanted type, it will return an error.

func SelectB

func SelectB(t *Dense, axis int) (retVal [][]bool, err error)

SelectB creates a slice of flat data types. See Example of NativeSelectF64.

func SelectC128

func SelectC128(t *Dense, axis int) (retVal [][]complex128, err error)

SelectC128 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectC64

func SelectC64(t *Dense, axis int) (retVal [][]complex64, err error)

SelectC64 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectF32

func SelectF32(t *Dense, axis int) (retVal [][]float32, err error)

SelectF32 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectF64

func SelectF64(t *Dense, axis int) (retVal [][]float64, err error)

SelectF64 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectI

func SelectI(t *Dense, axis int) (retVal [][]int, err error)

SelectI creates a slice of flat data types. See Example of NativeSelectF64.

func SelectI16

func SelectI16(t *Dense, axis int) (retVal [][]int16, err error)

SelectI16 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectI32

func SelectI32(t *Dense, axis int) (retVal [][]int32, err error)

SelectI32 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectI64

func SelectI64(t *Dense, axis int) (retVal [][]int64, err error)

SelectI64 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectI8

func SelectI8(t *Dense, axis int) (retVal [][]int8, err error)

SelectI8 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectStr

func SelectStr(t *Dense, axis int) (retVal [][]string, err error)

SelectStr creates a slice of flat data types. See Example of NativeSelectF64.

func SelectU

func SelectU(t *Dense, axis int) (retVal [][]uint, err error)

SelectU creates a slice of flat data types. See Example of NativeSelectF64.

func SelectU16

func SelectU16(t *Dense, axis int) (retVal [][]uint16, err error)

SelectU16 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectU32

func SelectU32(t *Dense, axis int) (retVal [][]uint32, err error)

SelectU32 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectU64

func SelectU64(t *Dense, axis int) (retVal [][]uint64, err error)

SelectU64 creates a slice of flat data types. See Example of NativeSelectF64.

func SelectU8

func SelectU8(t *Dense, axis int) (retVal [][]uint8, err error)

SelectU8 creates a slice of flat data types. See Example of NativeSelectF64.

func Tensor3

func Tensor3(t *Dense) (interface{}, error)

func Tensor3B

func Tensor3B(t *Dense) (retVal [][][]bool, err error)

Tensor3B converts a *Dense into a [][][]bool. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3C128

func Tensor3C128(t *Dense) (retVal [][][]complex128, err error)

Tensor3C128 converts a *Dense into a [][][]complex128. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3C64

func Tensor3C64(t *Dense) (retVal [][][]complex64, err error)

Tensor3C64 converts a *Dense into a [][][]complex64. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3F32

func Tensor3F32(t *Dense) (retVal [][][]float32, err error)

Tensor3F32 converts a *Dense into a [][][]float32. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3F64

func Tensor3F64(t *Dense) (retVal [][][]float64, err error)

Tensor3F64 converts a *Dense into a [][][]float64. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3I

func Tensor3I(t *Dense) (retVal [][][]int, err error)

Tensor3I converts a *Dense into a [][][]int. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3I16

func Tensor3I16(t *Dense) (retVal [][][]int16, err error)

Tensor3I16 converts a *Dense into a [][][]int16. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3I32

func Tensor3I32(t *Dense) (retVal [][][]int32, err error)

Tensor3I32 converts a *Dense into a [][][]int32. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3I64

func Tensor3I64(t *Dense) (retVal [][][]int64, err error)

Tensor3I64 converts a *Dense into a [][][]int64. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3I8

func Tensor3I8(t *Dense) (retVal [][][]int8, err error)

Tensor3I8 converts a *Dense into a [][][]int8. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3Str

func Tensor3Str(t *Dense) (retVal [][][]string, err error)

Tensor3Str converts a *Dense into a [][][]string. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3U

func Tensor3U(t *Dense) (retVal [][][]uint, err error)

Tensor3U converts a *Dense into a [][][]uint. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3U16

func Tensor3U16(t *Dense) (retVal [][][]uint16, err error)

Tensor3U16 converts a *Dense into a [][][]uint16. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3U32

func Tensor3U32(t *Dense) (retVal [][][]uint32, err error)

Tensor3U32 converts a *Dense into a [][][]uint32. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3U64

func Tensor3U64(t *Dense) (retVal [][][]uint64, err error)

Tensor3U64 converts a *Dense into a [][][]uint64. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Tensor3U8

func Tensor3U8(t *Dense) (retVal [][][]uint8, err error)

Tensor3U8 converts a *Dense into a [][][]uint8. If the *Dense does not represent a 3-tensor of the wanted type, it will return an error.

func Vector

func Vector(t *Dense) (interface{}, error)

func VectorB

func VectorB(t *Dense) (retVal []bool, err error)

VectorB converts a *Dense into a []bool If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorC128

func VectorC128(t *Dense) (retVal []complex128, err error)

VectorC128 converts a *Dense into a []complex128 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorC64

func VectorC64(t *Dense) (retVal []complex64, err error)

VectorC64 converts a *Dense into a []complex64 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorF32

func VectorF32(t *Dense) (retVal []float32, err error)

VectorF32 converts a *Dense into a []float32 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorF64

func VectorF64(t *Dense) (retVal []float64, err error)

VectorF64 converts a *Dense into a []float64 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorI

func VectorI(t *Dense) (retVal []int, err error)

VectorI converts a *Dense into a []int If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorI16

func VectorI16(t *Dense) (retVal []int16, err error)

VectorI16 converts a *Dense into a []int16 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorI32

func VectorI32(t *Dense) (retVal []int32, err error)

VectorI32 converts a *Dense into a []int32 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorI64

func VectorI64(t *Dense) (retVal []int64, err error)

VectorI64 converts a *Dense into a []int64 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorI8

func VectorI8(t *Dense) (retVal []int8, err error)

VectorI8 converts a *Dense into a []int8 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorStr

func VectorStr(t *Dense) (retVal []string, err error)

VectorStr converts a *Dense into a []string If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorU

func VectorU(t *Dense) (retVal []uint, err error)

VectorU converts a *Dense into a []uint If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorU16

func VectorU16(t *Dense) (retVal []uint16, err error)

VectorU16 converts a *Dense into a []uint16 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorU32

func VectorU32(t *Dense) (retVal []uint32, err error)

VectorU32 converts a *Dense into a []uint32 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorU64

func VectorU64(t *Dense) (retVal []uint64, err error)

VectorU64 converts a *Dense into a []uint64 If the *Dense does not represent a vector of the wanted type, it will return an error.

func VectorU8

func VectorU8(t *Dense) (retVal []uint8, err error)

VectorU8 converts a *Dense into a []uint8 If the *Dense does not represent a vector of the wanted type, it will return an error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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