-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (93 loc) · 3 KB
/
main.go
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
package main
import (
"fmt"
"gonn/tensor"
"gonum.org/v1/gonum/mat"
)
func main() {
test1D()
test2D()
test3D()
testAdd1D()
testAdd2D()
testAdd3D()
}
func test1D() {
// Initialize 1D tensors
xData := mat.NewVecDense(3, []float64{1.0, 2.0, 3.0})
yData := mat.NewVecDense(3, []float64{4.0, 5.0, 6.0})
// Perform element-wise multiplication
ctx := &tensor.Context{}
mulOp := &tensor.Mul{}
result := mulOp.Forward(ctx, xData, yData).(*mat.VecDense) // Assuming Forward returns *mat.VecDense for 1D
fmt.Println("Result of 1D multiplication:", result.RawVector().Data)
}
func test2D() {
// Initialize 2D tensors
xData := mat.NewDense(2, 3, []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0})
yData := mat.NewDense(2, 3, []float64{7.0, 8.0, 9.0, 10.0, 11.0, 12.0})
// Perform element-wise multiplication
ctx := &tensor.Context{}
mulOp := &tensor.Mul{}
result := mulOp.Forward(ctx, xData, yData).(*mat.Dense) // Assuming Forward returns *mat.Dense for 2D
fmt.Printf("Result of 2D multiplication:\n%v\n", mat.Formatted(result))
}
func test3D() {
// Initialize 3D tensors as slices of 2D tensors
xData := []*mat.Dense{
mat.NewDense(2, 2, []float64{1.0, 2.0, 3.0, 4.0}),
mat.NewDense(2, 2, []float64{5.0, 6.0, 7.0, 8.0}),
}
yData := []*mat.Dense{
mat.NewDense(2, 2, []float64{9.0, 10.0, 11.0, 12.0}),
mat.NewDense(2, 2, []float64{13.0, 14.0, 15.0, 16.0}),
}
// Perform element-wise multiplication
ctx := &tensor.Context{}
mulOp := &tensor.Mul{}
result := mulOp.Forward(ctx, xData, yData).([]*mat.Dense) // Assuming Forward returns []*mat.Dense for 3D
fmt.Println("Result of 3D multiplication:")
for i, m := range result {
fmt.Printf("Layer %d:\n%v\n", i+1, mat.Formatted(m))
}
}
func testAdd1D() {
// Initialize 1D tensors
xData := mat.NewVecDense(3, []float64{1.0, 2.0, 3.0})
yData := mat.NewVecDense(3, []float64{4.0, 5.0, 6.0})
// Perform element-wise addition
ctx := &tensor.Context{}
addOp := &tensor.Add{}
result := addOp.Forward(ctx, xData, yData).(*mat.VecDense)
fmt.Println("Result of 1D addition:", result.RawVector().Data)
}
func testAdd2D() {
// Initialize 2D tensors
xData := mat.NewDense(2, 2, []float64{1.0, 2.0, 3.0, 4.0})
yData := mat.NewDense(2, 2, []float64{5.0, 6.0, 7.0, 8.0})
// Perform element-wise addition
ctx := &tensor.Context{}
addOp := &tensor.Add{}
result := addOp.Forward(ctx, xData, yData).(*mat.Dense)
fmt.Println("Result of 2D addition:")
fmt.Println(mat.Formatted(result))
}
func testAdd3D() {
// Initialize 3D tensors as slices of 2D tensors
xData := []*mat.Dense{
mat.NewDense(2, 2, []float64{1.0, 2.0, 3.0, 4.0}),
mat.NewDense(2, 2, []float64{9.0, 8.0, 7.0, 6.0}),
}
yData := []*mat.Dense{
mat.NewDense(2, 2, []float64{5.0, 6.0, 7.0, 8.0}),
mat.NewDense(2, 2, []float64{5.0, 4.0, 3.0, 2.0}),
}
// Perform element-wise addition
ctx := &tensor.Context{}
addOp := &tensor.Add{}
result := addOp.Forward(ctx, xData, yData).([]*mat.Dense)
fmt.Println("Result of 3D addition:")
for i, m := range result {
fmt.Printf("Layer %d:\n%v\n", i+1, mat.Formatted(m))
}
}