forked from gorgonia/gorgonia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast_test.go
83 lines (66 loc) · 2.29 KB
/
broadcast_test.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
package gorgonia
import (
"io/ioutil"
"testing"
"github.com/chewxy/gorgonia/tensor"
"github.com/stretchr/testify/assert"
)
func TestBroadcastPattern(t *testing.T) {
assert := assert.New(t)
var bcpat BroadcastPattern
// make sure that the basics work
bcpat = NewBroadcastPattern(nil, []byte{1})
assert.Equal(BroadcastPattern(0x02), bcpat)
bcpat = NewBroadcastPattern(nil, []byte{0})
assert.Equal(BroadcastPattern(0x01), bcpat)
bcpat = NewBroadcastPattern([]byte{1, 0}, nil)
assert.Equal(BroadcastPattern(0x30), bcpat)
bcpat = NewBroadcastPattern([]byte{0}, nil)
assert.Equal(BroadcastPattern(0x10), bcpat)
// checks
bcpat = NewBroadcastPattern(nil, []byte{1})
assert.True(bcpat.bc(false, 1))
assert.False(bcpat.bc(true, 1))
// ons
bcpat = NewBroadcastPattern(nil, []byte{1})
assert.Equal([]int{1}, bcpat.on()[1])
assert.Nil(bcpat.on()[0])
bcpat = NewBroadcastPattern([]byte{2, 1}, []byte{1})
assert.Equal([]int{1, 2}, bcpat.on()[0])
assert.Equal([]int{1}, bcpat.on()[1])
}
func TestBroadcast2(t *testing.T) {
assert := assert.New(t)
var g *ExprGraph
var x, y, z *Node
var m *lispMachine
var err error
xT := tensor.New(tensor.WithShape(2, 3), tensor.WithBacking(tensor.Range(tensor.Float64, 0, 6)))
yT := tensor.New(tensor.WithShape(2), tensor.WithBacking([]float64{100, 200}))
g = NewGraph()
x = NewMatrix(g, Float64, WithShape(2, 3), WithValue(xT), WithName("x"))
y = NewVector(g, Float64, WithShape(2), WithValue(yT), WithName("y"))
z, err = Broadcast(addOpType, x, y, NewBroadcastPattern(nil, []byte{1}))
if err != nil {
ioutil.WriteFile("Broadcast.dot", []byte(g.ToDot()), 0644)
t.Fatal(err)
}
m = NewLispMachine(g, ExecuteFwdOnly())
if err := m.RunAll(); err != nil {
t.Fatal(err)
}
assert.Equal([]float64{100, 101, 102, 203, 204, 205}, extractF64s(z.Value()))
g = NewGraph()
x = NewMatrix(g, Float64, WithShape(2, 3), WithValue(xT), WithName("x"))
y = NewVector(g, Float64, WithShape(2), WithValue(yT), WithName("y"))
z, err = Broadcast(addOpType, y, x, NewBroadcastPattern([]byte{1}, nil))
if err != nil {
ioutil.WriteFile("Broadcast.dot", []byte(g.ToDot()), 0644)
t.Fatalf("%+v", err)
}
m = NewLispMachine(g, ExecuteFwdOnly())
if err := m.RunAll(); err != nil {
t.Fatal(err)
}
assert.Equal([]float64{100, 101, 102, 203, 204, 205}, extractF64s(z.Value()))
}