Documentation ¶
Index ¶
- func Argmax(m Matrix) []int
- func Broadcast(m, n Matrix) (Matrix, Matrix)
- func Dim(m Matrix) (int, int)
- func Flatten(m Matrix) []float64
- func Max(m Matrix) float64
- func Mean(m Matrix) float64
- func Min(m Matrix) float64
- func Shape(m Matrix) []int
- func Size(m Matrix) int
- func Sum(m Matrix) float64
- type Matrix
- func Add(m, n Matrix) Matrix
- func AddC(c float64, m Matrix) Matrix
- func BroadcastTo(shape []int, m Matrix) Matrix
- func Clip(m Matrix, min, max float64) Matrix
- func Cos(m Matrix) Matrix
- func Div(m, n Matrix) Matrix
- func Dot(m, n Matrix) Matrix
- func Exp(m Matrix) Matrix
- func F(m Matrix, f func(a float64) float64) Matrix
- func F2(m, n Matrix, f func(a, b float64) float64) Matrix
- func From(x [][]int) Matrix
- func Log(m Matrix) Matrix
- func Mask(m Matrix, f func(x float64) bool) Matrix
- func MaxAxis1(m Matrix) Matrix
- func Mul(m, n Matrix) Matrix
- func MulC(c float64, m Matrix) Matrix
- func New(v ...[]float64) Matrix
- func OneLike(m Matrix) Matrix
- func Pow(c float64, m Matrix) Matrix
- func Rand(m, n int, s ...randv2.Source) Matrix
- func Randn(m, n int, s ...randv2.Source) Matrix
- func Reshape(shape []int, m Matrix) Matrix
- func Sin(m Matrix) Matrix
- func Sub(m, n Matrix) Matrix
- func SubC(c float64, m Matrix) Matrix
- func SumAxis0(m Matrix) Matrix
- func SumAxis1(m Matrix) Matrix
- func SumTo(shape []int, m Matrix) Matrix
- func Tanh(m Matrix) Matrix
- func Transpose(m Matrix) Matrix
- func Zero(m, n int) Matrix
- func ZeroLike(m Matrix) Matrix
Examples ¶
- Add
- AddC
- Argmax
- Broadcast
- BroadcastTo
- BroadcastTo (Column)
- BroadcastTo (NoEffect)
- BroadcastTo (Row)
- Clip
- Cos
- Dim
- Div
- Dot
- Exp
- From
- Log
- Mask
- Max
- MaxAxis1
- Mean
- Min
- Mul
- MulC
- OneLike
- Pow
- Rand
- Rand (Nil)
- Rand (Seed)
- Randn
- Randn (Seed)
- Reshape
- Shape
- Sin
- Size
- Sub
- SubC
- Sum
- SumAxis0
- SumAxis1
- SumTo (Axis0)
- SumTo (Axis1)
- SumTo (Noeffect)
- SumTo (Sum)
- Tanh
- Zero
- ZeroLike
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Argmax ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2, 3}, []float64{4, 6, 5}, []float64{9, 8, 7}, ) fmt.Println(matrix.Argmax(A)) }
Output: [2 1 0]
func Broadcast ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) B := matrix.New( []float64{1, 2}, ) AA, BB := matrix.Broadcast(A, B) for _, r := range AA { fmt.Println(r) } for _, r := range BB { fmt.Println(r) } }
Output: [1 2] [3 4] [1 2] [1 2]
func Dim ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.Zero(2, 3) fmt.Println(matrix.Dim(A)) }
Output: 2 3
func Max ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 5}, []float64{3, 4}, ) fmt.Println(matrix.Max(A)) }
Output: 5
func Mean ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2, 3, 4, 5}, []float64{6, 7, 8, 9, 10}, ) fmt.Println(matrix.Mean(A)) }
Output: 5.5
func Min ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, -5}, []float64{3, 4}, ) fmt.Println(matrix.Min(A)) }
Output: -5
func Shape ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.Zero(2, 3) fmt.Println(matrix.Shape(A)) }
Output: [2 3]
Types ¶
type Matrix ¶
type Matrix [][]float64
func Add ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) B := matrix.New( []float64{5, 6}, []float64{7, 8}, ) for _, r := range matrix.Add(A, B) { fmt.Println(r) } }
Output: [6 8] [10 12]
func AddC ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.AddC(1, A) { fmt.Println(r) } }
Output: [2 3] [4 5]
func BroadcastTo ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New([]float64{1}) for _, r := range matrix.BroadcastTo([]int{3, 5}, A) { fmt.Println(r) } }
Output: [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]
Example (Column) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1}, []float64{2}, ) for _, r := range matrix.BroadcastTo([]int{-1, 5}, A) { fmt.Println(r) } }
Output: [1 1 1 1 1] [2 2 2 2 2]
Example (NoEffect) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.BroadcastTo([]int{2, 2}, A) { fmt.Println(r) } }
Output: [1 2] [3 4]
Example (Row) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New([]float64{1, 2}) for _, r := range matrix.BroadcastTo([]int{5, -1}, A) { fmt.Println(r) } }
Output: [1 2] [1 2] [1 2] [1 2] [1 2]
func Clip ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{-3, -2, -1, 0, 1, 2, 3, 4, 5, 6}, []float64{7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, ) for _, r := range matrix.Clip(A, 0, 10) { fmt.Println(r) } }
Output: [0 0 0 0 1 2 3 4 5 6] [7 8 9 10 10 10 10 10 10 10]
func Cos ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.Cos(A) { fmt.Println(r) } }
Output: [0.5403023058681398 -0.4161468365471424] [-0.9899924966004454 -0.6536436208636119]
func Div ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) B := matrix.New( []float64{5, 6}, []float64{7, 8}, ) for _, r := range matrix.Div(A, B) { fmt.Println(r) } }
Output: [0.2 0.3333333333333333] [0.42857142857142855 0.5]
func Dot ¶
Dot returns the dot product of m and n.
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) B := matrix.New( []float64{5, 6}, []float64{7, 8}, ) for _, r := range matrix.Dot(A, B) { fmt.Println(r) } }
Output: [19 22] [43 50]
func Exp ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.Exp(A) { fmt.Println(r) } }
Output: [2.718281828459045 7.38905609893065] [20.085536923187668 54.598150033144236]
func From ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { for _, r := range matrix.From([][]int{{1, 2}, {3, 4}}) { fmt.Printf("%.2f\n", r) } }
Output: [1.00 2.00] [3.00 4.00]
func Log ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.Log(A) { fmt.Println(r) } }
Output: [0 0.6931471805599453] [1.0986122886681096 1.3862943611198906]
func Mask ¶
Mask returns a matrix with elements that 1 if f() is true and 0 otherwise.
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{-1, 2}, []float64{3, -4}, ) for _, r := range matrix.Mask(A, func(v float64) bool { return v > 0 }) { fmt.Println(r) } }
Output: [0 1] [1 0]
func MaxAxis1 ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, []float64{5, 2}, ) for _, r := range matrix.MaxAxis1(A) { fmt.Println(r) } }
Output: [2] [4] [5]
func Mul ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) B := matrix.New( []float64{5, 6}, []float64{7, 8}, ) for _, r := range matrix.Mul(A, B) { fmt.Println(r) } }
Output: [5 12] [21 32]
func MulC ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.MulC(2, A) { fmt.Println(r) } }
Output: [2 4] [6 8]
func OneLike ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.Zero(2, 3) for _, r := range matrix.OneLike(A) { fmt.Println(r) } }
Output: [1 1 1] [1 1 1]
func Pow ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.Pow(2, A) { fmt.Println(r) } }
Output: [1 4] [9 16]
func Rand ¶
Rand returns a matrix with elements that pseudo-random number in the half-open interval [0.0,1.0). m, n is the dimension of the matrix. s is the source of the pseudo-random number.
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { shape := matrix.Shape(matrix.Rand(2, 3)) fmt.Println(shape) }
Output: [2 3]
Example (Nil) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { shape := matrix.Shape(matrix.Rand(2, 3, nil)) fmt.Println(shape) }
Output: [2 3]
Example (Seed) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" "github.com/itsubaki/autograd/rand" ) func main() { s := rand.Const() for _, r := range matrix.Rand(2, 3, s) { fmt.Println(r) } }
Output: [0.9999275824802834 0.8856419373528862 0.38147752771154886] [0.4812673234167829 0.44417259544314847 0.5210016660132573]
func Randn ¶
Randn returns a matrix with elements that normally distributed float64 in the range [-math.MaxFloat64, +math.MaxFloat64] with standard normal distribution. m, n is the dimension of the matrix. s is the source of the pseudo-random number.
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { shape := matrix.Shape(matrix.Randn(2, 3)) fmt.Println(shape) }
Output: [2 3]
Example (Seed) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" "github.com/itsubaki/autograd/rand" ) func main() { s := rand.Const() for _, r := range matrix.Randn(2, 3, s) { fmt.Println(r) } }
Output: [0.5665360716030388 -0.6123972949371448 0.5898947122637695] [-0.3678242340302933 1.0919575041640825 -0.4438344619606553]
func Reshape ¶
Reshape returns the matrix with the given shape.
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) fmt.Println(matrix.Reshape([]int{1, 4}, A)) fmt.Println(matrix.Reshape([]int{4, 1}, A)) fmt.Println(matrix.Reshape([]int{2, 2}, A)) fmt.Println() fmt.Println(matrix.Reshape([]int{1, -1}, A)) fmt.Println(matrix.Reshape([]int{4, -1}, A)) fmt.Println(matrix.Reshape([]int{2, -1}, A)) fmt.Println() fmt.Println(matrix.Reshape([]int{-1, 1}, A)) fmt.Println(matrix.Reshape([]int{-1, 4}, A)) fmt.Println(matrix.Reshape([]int{-1, 2}, A)) fmt.Println() }
Output: [[1 2 3 4]] [[1] [2] [3] [4]] [[1 2] [3 4]] [[1 2 3 4]] [[1] [2] [3] [4]] [[1 2] [3 4]] [[1] [2] [3] [4]] [[1 2 3 4]] [[1 2] [3 4]]
func Sin ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.Sin(A) { fmt.Println(r) } }
Output: [0.8414709848078965 0.9092974268256816] [0.1411200080598672 -0.7568024953079282]
func Sub ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) B := matrix.New( []float64{10, 20}, []float64{30, 40}, ) for _, r := range matrix.Sub(A, B) { fmt.Println(r) } }
Output: [-9 -18] [-27 -36]
func SubC ¶
SubC returns c - m
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.SubC(1, A) { fmt.Println(r) } }
Output: [0 -1] [-2 -3]
func SumAxis0 ¶
SumAxis0 returns the sum of each column.
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.SumAxis0(A) { fmt.Println(r) } }
Output: [4 6]
func SumAxis1 ¶
SumAxis1 returns the sum of each row.
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.SumAxis1(A) { fmt.Println(r) } }
Output: [3] [7]
func SumTo ¶
Example (Axis0) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) fmt.Println(matrix.SumTo([]int{1, 2}, A)) }
Output: [[4 6]]
Example (Axis1) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) fmt.Println(matrix.SumTo([]int{2, 1}, A)) }
Output: [[3] [7]]
Example (Noeffect) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) fmt.Println(matrix.SumTo([]int{2, 3}, A)) }
Output: [[1 2] [3 4]]
Example (Sum) ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) fmt.Println(matrix.SumTo([]int{1, 1}, A)) }
Output: [[10]]
func Tanh ¶
Example ¶
package main import ( "fmt" "github.com/itsubaki/autograd/matrix" ) func main() { A := matrix.New( []float64{1, 2}, []float64{3, 4}, ) for _, r := range matrix.Tanh(A) { fmt.Println(r) } }
Output: [0.7615941559557649 0.9640275800758169] [0.9950547536867305 0.999329299739067]