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
|
package tensor
import "fmt"
func Example_sparse_basics() {
xs := []int{1, 2, 6, 8}
ys := []int{1, 2, 1, 6}
vals := []float32{3, 1, 4, 1}
S := CSCFromCoord(Shape{9, 7}, xs, ys, vals)
T := New(WithShape(9, 7), Of(Float32)) // dense
Result, _ := Add(S, T)
fmt.Printf("When adding a sparse tensor to a dense tensor, the result is of %T:\n=============================================================================\n%+#s\n", Result, Result)
Result, _ = Add(T, S)
fmt.Printf("And vice versa - %T\n=========================\n%+#s\n", Result, Result)
// Output:
// When adding a sparse tensor to a dense tensor, the result is of *tensor.Dense:
// =============================================================================
// Matrix (9, 7) [7 1]
// ⎡0 0 0 0 0 0 0⎤
// ⎢0 3 0 0 0 0 0⎥
// ⎢0 0 1 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 4 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎣0 0 0 0 0 0 1⎦
//
// And vice versa - *tensor.Dense
// =========================
// Matrix (9, 7) [7 1]
// ⎡0 0 0 0 0 0 0⎤
// ⎢0 3 0 0 0 0 0⎥
// ⎢0 0 1 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 4 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎣0 0 0 0 0 0 1⎦
}
func Example_sparse_advanced() {
xs := []int{1, 2, 6, 8}
ys := []int{1, 2, 1, 6}
vals := []int16{3, 1, 4, 1}
S := CSCFromCoord(Shape{9, 7}, xs, ys, vals)
T := New(WithShape(9, 7), Of(Int16)) // dense
Reuse := New(WithShape(9, 7), Of(Int16)) // reuse must be a *Dense because the result will always be a dense
Result, _ := Add(S, T, WithReuse(Reuse))
fmt.Printf("Operations involving sparse tensors also do take the usual function options like Reuse:\n%+#s\nResult == Reuse: %t", Result, Result == Reuse)
// Output:
// Operations involving sparse tensors also do take the usual function options like Reuse:
// Matrix (9, 7) [7 1]
// ⎡0 0 0 0 0 0 0⎤
// ⎢0 3 0 0 0 0 0⎥
// ⎢0 0 1 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎢0 4 0 0 0 0 0⎥
// ⎢0 0 0 0 0 0 0⎥
// ⎣0 0 0 0 0 0 1⎦
//
// Result == Reuse: true
}
|