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 ¶
- func Matrix(t *Dense) (interface{}, error)
- func MatrixB(t *Dense) (retVal [][]bool, err error)
- func MatrixC128(t *Dense) (retVal [][]complex128, err error)
- func MatrixC64(t *Dense) (retVal [][]complex64, err error)
- func MatrixF32(t *Dense) (retVal [][]float32, err error)
- func MatrixF64(t *Dense) (retVal [][]float64, err error)
- func MatrixI(t *Dense) (retVal [][]int, err error)
- func MatrixI16(t *Dense) (retVal [][]int16, err error)
- func MatrixI32(t *Dense) (retVal [][]int32, err error)
- func MatrixI64(t *Dense) (retVal [][]int64, err error)
- func MatrixI8(t *Dense) (retVal [][]int8, err error)
- func MatrixStr(t *Dense) (retVal [][]string, err error)
- func MatrixU(t *Dense) (retVal [][]uint, err error)
- func MatrixU16(t *Dense) (retVal [][]uint16, err error)
- func MatrixU32(t *Dense) (retVal [][]uint32, err error)
- func MatrixU64(t *Dense) (retVal [][]uint64, err error)
- func MatrixU8(t *Dense) (retVal [][]uint8, err error)
- func SelectB(t *Dense, axis int) (retVal [][]bool, err error)
- func SelectC128(t *Dense, axis int) (retVal [][]complex128, err error)
- func SelectC64(t *Dense, axis int) (retVal [][]complex64, err error)
- func SelectF32(t *Dense, axis int) (retVal [][]float32, err error)
- func SelectF64(t *Dense, axis int) (retVal [][]float64, err error)
- func SelectI(t *Dense, axis int) (retVal [][]int, err error)
- func SelectI16(t *Dense, axis int) (retVal [][]int16, err error)
- func SelectI32(t *Dense, axis int) (retVal [][]int32, err error)
- func SelectI64(t *Dense, axis int) (retVal [][]int64, err error)
- func SelectI8(t *Dense, axis int) (retVal [][]int8, err error)
- func SelectStr(t *Dense, axis int) (retVal [][]string, err error)
- func SelectU(t *Dense, axis int) (retVal [][]uint, err error)
- func SelectU16(t *Dense, axis int) (retVal [][]uint16, err error)
- func SelectU32(t *Dense, axis int) (retVal [][]uint32, err error)
- func SelectU64(t *Dense, axis int) (retVal [][]uint64, err error)
- func SelectU8(t *Dense, axis int) (retVal [][]uint8, err error)
- func Tensor3(t *Dense) (interface{}, error)
- func Tensor3B(t *Dense) (retVal [][][]bool, err error)
- func Tensor3C128(t *Dense) (retVal [][][]complex128, err error)
- func Tensor3C64(t *Dense) (retVal [][][]complex64, err error)
- func Tensor3F32(t *Dense) (retVal [][][]float32, err error)
- func Tensor3F64(t *Dense) (retVal [][][]float64, err error)
- func Tensor3I(t *Dense) (retVal [][][]int, err error)
- func Tensor3I16(t *Dense) (retVal [][][]int16, err error)
- func Tensor3I32(t *Dense) (retVal [][][]int32, err error)
- func Tensor3I64(t *Dense) (retVal [][][]int64, err error)
- func Tensor3I8(t *Dense) (retVal [][][]int8, err error)
- func Tensor3Str(t *Dense) (retVal [][][]string, err error)
- func Tensor3U(t *Dense) (retVal [][][]uint, err error)
- func Tensor3U16(t *Dense) (retVal [][][]uint16, err error)
- func Tensor3U32(t *Dense) (retVal [][][]uint32, err error)
- func Tensor3U64(t *Dense) (retVal [][][]uint64, err error)
- func Tensor3U8(t *Dense) (retVal [][][]uint8, err error)
- func Vector(t *Dense) (interface{}, error)
- func VectorB(t *Dense) (retVal []bool, err error)
- func VectorC128(t *Dense) (retVal []complex128, err error)
- func VectorC64(t *Dense) (retVal []complex64, err error)
- func VectorF32(t *Dense) (retVal []float32, err error)
- func VectorF64(t *Dense) (retVal []float64, err error)
- func VectorI(t *Dense) (retVal []int, err error)
- func VectorI16(t *Dense) (retVal []int16, err error)
- func VectorI32(t *Dense) (retVal []int32, err error)
- func VectorI64(t *Dense) (retVal []int64, err error)
- func VectorI8(t *Dense) (retVal []int8, err error)
- func VectorStr(t *Dense) (retVal []string, err error)
- func VectorU(t *Dense) (retVal []uint, err error)
- func VectorU16(t *Dense) (retVal []uint16, err error)
- func VectorU32(t *Dense) (retVal []uint32, err error)
- func VectorU64(t *Dense) (retVal []uint64, err error)
- func VectorU8(t *Dense) (retVal []uint8, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MatrixB ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 SelectC128 ¶
func SelectC128(t *Dense, axis int) (retVal [][]complex128, err error)
SelectC128 creates a slice of flat data types. See Example of NativeSelectF64.
func Tensor3B ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 VectorB ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
VectorU32 converts a *Dense into a []uint32 If the *Dense does not represent a vector of the wanted type, it will return an error.
Types ¶
This section is empty.