gorgonia.org/gorgonia@v0.9.17/known_issues_test.go (about) 1 package gorgonia 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 "gorgonia.org/tensor" 9 ) 10 11 func TestIssue182(t *testing.T) { 12 // This test revolves around repeated calls to run a VM. 13 // Formerly, upon running the VM once, the derivation of the constant is set. 14 // This derivation value would get Add()ed to upon subsequqent calls to run the VM. 15 // 16 // This behaviour was fixed to make sure constants do not have derivatives 17 assert := assert.New(t) 18 19 // Build the graph 20 g := NewGraph() 21 22 aback := []float64{2.0, 2.0, 2.0} 23 x := NewVector(g, tensor.Float64, WithName("x"), WithShape(3)) 24 a := NewConstant(tensor.New(tensor.WithBacking(aback), tensor.WithShape(3))) 25 26 b := NewScalar(g, tensor.Float64) 27 28 xT := tensor.New(tensor.WithBacking([]float64{1, 1, 1}), tensor.WithShape(3)) 29 y, err := Mul(x, a) 30 if err != nil { 31 t.Fatal(err) 32 } 33 z, err := Mul(y, b) 34 if err != nil { 35 t.Fatal(err) 36 } 37 dz, err := Grad(z, x) 38 if err != nil { 39 t.Fatal(err) 40 } 41 machine := NewTapeMachine(g) 42 defer machine.Close() 43 44 machine.Let(x, xT) 45 machine.Let(b, -0.5) 46 for turns := 0; turns < 4; turns++ { 47 if err := machine.RunAll(); err != nil { 48 t.Fatalf("Machine failed to run at turn %v", turns) 49 } 50 machine.Reset() 51 } 52 53 correct := []float64{-1, -1, -1} 54 assert.Equal(correct, dz[0].Value().Data().([]float64)) 55 if _, ok := a.boundTo.(*dualValue); ok { 56 t.Fatalf("Expected constants to not have derivatives") 57 } 58 } 59 60 // func TestIssue217(t *testing.T) { 61 // //it works, cost = 22 62 // if err := issue217(tensor.Shape{2, 2}, tensor.Shape{2, 2}); err != nil { 63 // t.Fatal(err) 64 // } 65 66 // //panic: Node Σ[0](%2) :: float32, has 0 dimensions(Shape: ()). Input shape is (1, 1)... 67 // if err := issue217(tensor.Shape{2, 2}, tensor.Shape{2, 1}); err != nil { 68 // t.Fatal(err) 69 // } 70 71 // //panic: Node Σ[1](%2) :: float32, has 0 dimensions(Shape: ()). Input shape is (1, 1)... 72 // if err := issue217(tensor.Shape{1, 2}, tensor.Shape{2, 2}); err != nil { 73 // t.Fatal(err) 74 // } 75 // } 76 77 // func issue217(xS, yS tensor.Shape) error { 78 79 // g := NewGraph() 80 // x := NewMatrix(g, Float32, WithName("x"), WithShape(xS...), WithInit(RangedFrom(0))) 81 // y := NewMatrix(g, Float32, WithName("y"), WithShape(yS...), WithInit(RangedFrom(0))) 82 83 // z := Must(Mul(x, y)) 84 // cost := Must(Sum(z)) 85 // //cost := Must(Mean(z)) 86 87 // _, err := Grad(cost, x, y) 88 // if err != nil { 89 // return errors.Wrap(err, "Grad") 90 // } 91 92 // m := NewTapeMachine(g) 93 // if err = m.RunAll(); err != nil { 94 // return errors.Wrap(err, "Run") 95 // } 96 // return nil 97 // } 98 99 func TestIssue268_im2col(t *testing.T) { 100 g := NewGraph() 101 x := NewTensor(g, tensor.Float32, 4, WithShape(1, 2, 5, 5), WithInit(RangedFrom(0))) 102 yT := tensor.New(tensor.WithShape(1, 5, 5, 18), tensor.WithBacking([]float32{ 103 0, 0, 0, 0, 0, 1, 0, 5, 6, 0, 0, 0, 0, 25, 26, 0, 30, 31, 0, 0, 0, 0, 1, 2, 5, 6, 7, 0, 0, 0, 25, 26, 27, 30, 104 31, 32, 0, 0, 0, 1, 2, 3, 6, 7, 8, 0, 0, 0, 26, 27, 28, 31, 32, 33, 0, 0, 0, 2, 3, 4, 7, 8, 9, 0, 0, 0, 27, 28, 105 29, 32, 33, 34, 0, 0, 0, 3, 4, 0, 8, 9, 0, 0, 0, 0, 28, 29, 0, 33, 34, 0, 0, 0, 1, 0, 5, 6, 0, 10, 11, 0, 25, 106 26, 0, 30, 31, 0, 35, 36, 0, 1, 2, 5, 6, 7, 10, 11, 12, 25, 26, 27, 30, 31, 32, 35, 36, 37, 1, 2, 3, 6, 7, 8, 107 11, 12, 13, 26, 27, 28, 31, 32, 33, 36, 37, 38, 2, 3, 4, 7, 8, 9, 12, 13, 14, 27, 28, 29, 32, 33, 34, 37, 38, 108 39, 3, 4, 0, 8, 9, 0, 13, 14, 0, 28, 29, 0, 33, 34, 0, 38, 39, 0, 0, 5, 6, 0, 10, 11, 0, 15, 16, 0, 30, 31, 0, 35, 109 36, 0, 40, 41, 5, 6, 7, 10, 11, 12, 15, 16, 17, 30, 31, 32, 35, 36, 37, 40, 41, 42, 6, 7, 8, 11, 12, 13, 16, 110 17, 18, 31, 32, 33, 36, 37, 38, 41, 42, 43, 7, 8, 9, 12, 13, 14, 17, 18, 19, 32, 33, 34, 37, 38, 39, 42, 43, 111 44, 8, 9, 0, 13, 14, 0, 18, 19, 0, 33, 34, 0, 38, 39, 0, 43, 44, 0, 0, 10, 11, 0, 15, 16, 0, 20, 21, 0, 35, 36, 112 0, 40, 41, 0, 45, 46, 10, 11, 12, 15, 16, 17, 20, 21, 22, 35, 36, 37, 40, 41, 42, 45, 46, 47, 11, 12, 13, 16, 113 17, 18, 21, 22, 23, 36, 37, 38, 41, 42, 43, 46, 47, 48, 12, 13, 14, 17, 18, 19, 22, 23, 24, 37, 38, 39, 42, 114 43, 44, 47, 48, 49, 13, 14, 0, 18, 19, 0, 23, 24, 0, 38, 39, 0, 43, 44, 0, 48, 49, 0, 0, 15, 16, 0, 20, 21, 0, 115 0, 0, 0, 40, 41, 0, 45, 46, 0, 0, 0, 15, 16, 17, 20, 21, 22, 0, 0, 0, 40, 41, 42, 45, 46, 47, 0, 0, 0, 16, 17, 116 18, 21, 22, 23, 0, 0, 0, 41, 42, 43, 46, 47, 48, 0, 0, 0, 17, 18, 19, 22, 23, 24, 0, 0, 0, 42, 43, 44, 47, 48, 117 49, 0, 0, 0, 18, 19, 0, 23, 24, 0, 0, 0, 0, 43, 44, 0, 48, 49, 0, 0, 0, 0, 118 })) 119 y, err := Im2Col(x, []int{3, 3}, []int{1, 1}, []int{1, 1}, []int{1, 1}) 120 if err != nil { 121 t.Fatal(err) 122 } 123 124 machine := NewTapeMachine(g) 125 if err = machine.RunAll(); err != nil { 126 t.Fatal(err) 127 } 128 assert.Equal(t, yT.Shape(), y.Shape(), "Tensors should be the same") 129 assert.InDeltaSlice(t, yT.Data(), y.Value().Data(), 1e-5, "Tensors should be the same") 130 } 131 132 func TestIssue273_maxpool_pads(t *testing.T) { 133 g := NewGraph() 134 x := NewTensor(g, tensor.Float32, 4, WithShape(1, 2, 5, 5), WithInit(RangedFrom(0))) 135 yT := tensor.New( 136 tensor.WithShape(1, 2, 7, 7), 137 tensor.WithBacking([]float32{ 138 0, 1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 9, 9, 9, 10, 11, 12, 13, 14, 14, 14, 15, 16, 139 17, 18, 19, 19, 19, 20, 21, 22, 23, 24, 24, 24, 20, 21, 22, 23, 24, 24, 24, 140 20, 21, 22, 23, 24, 24, 24, 25, 26, 27, 28, 29, 29, 29, 30, 31, 32, 33, 34, 141 34, 34, 35, 36, 37, 38, 39, 39, 39, 40, 41, 42, 43, 44, 44, 44, 45, 46, 47, 142 48, 49, 49, 49, 45, 46, 47, 48, 49, 49, 49, 45, 46, 47, 48, 49, 49, 49, 143 })) 144 145 y, err := MaxPool2D(x, []int{3, 3}, []int{2, 2}, []int{1, 1}) 146 if err != nil { 147 t.Fatal(err) 148 } 149 150 machine := NewTapeMachine(g) 151 if err = machine.RunAll(); err != nil { 152 t.Fatal(err) 153 } 154 155 assert.Equal(t, yT.Shape(), y.Shape(), "Tensors should be the same") 156 assert.InDeltaSlice(t, yT.Data(), y.Value().Data(), 1e-5, "Tensors should be the same") 157 158 } 159 160 func TestIssue233_F32(t *testing.T) { 161 g := NewGraph() 162 xV := tensor.New(tensor.WithShape(1, 1, 5, 5), tensor.WithBacking([]float32{ 163 0, 0, 0, 0, 0, 164 1, 1, 1, 1, 1, 165 2, 2, 2, 2, 2, 166 3, 3, 3, 3, 3, 167 4, 4, 4, 4, 4, 168 })) 169 kernelV := tensor.New(tensor.WithShape(1, 1, 3, 3), tensor.WithBacking([]float32{ 170 1, 1, 1, 171 1, 1, 1, 172 1, 1, 1, 173 })) 174 175 x := NewTensor(g, Float32, 4, WithShape(1, 1, 5, 5), WithValue(xV), WithName("x")) 176 w := NewTensor(g, Float32, 4, WithShape(1, 1, 3, 3), WithValue(kernelV), WithName("w")) 177 178 y, err := Conv2d(x, w, tensor.Shape{3, 3}, []int{1, 1}, []int{1, 1}, []int{1, 1}) 179 if err != nil { 180 t.Fatal(err) 181 } 182 // logger := log.New(os.Stderr, "", 0) 183 // vm := NewTapeMachine(g, WithLogger(logger), WithWatchlist(), WithValueFmt("%#v")) 184 vm := NewTapeMachine(g) 185 if err := vm.RunAll(); err != nil { 186 t.Fatal(err) 187 } 188 189 correct := []float32{ 190 2, 3, 3, 3, 2, 191 6, 9, 9, 9, 6, 192 12, 18, 18, 18, 12, 193 18, 27, 27, 27, 18, 194 14, 21, 21, 21, 14, 195 } 196 t.Logf("%v", y.Value()) 197 198 assert.Equal(t, correct, y.Value().Data()) 199 } 200 201 func TestIssue233_F64(t *testing.T) { 202 g := NewGraph() 203 xV := tensor.New(tensor.WithShape(1, 1, 5, 5), tensor.WithBacking([]float64{ 204 0, 0, 0, 0, 0, 205 1, 1, 1, 1, 1, 206 2, 2, 2, 2, 2, 207 3, 3, 3, 3, 3, 208 4, 4, 4, 4, 4, 209 })) 210 kernelV := tensor.New(tensor.WithShape(1, 1, 3, 3), tensor.WithBacking([]float64{ 211 1, 1, 1, 212 1, 1, 1, 213 1, 1, 1, 214 })) 215 216 x := NewTensor(g, Float64, 4, WithShape(1, 1, 5, 5), WithValue(xV), WithName("x")) 217 w := NewTensor(g, Float64, 4, WithShape(1, 1, 3, 3), WithValue(kernelV), WithName("w")) 218 219 y, err := Conv2d(x, w, tensor.Shape{3, 3}, []int{1, 1}, []int{1, 1}, []int{1, 1}) 220 if err != nil { 221 t.Fatal(err) 222 } 223 // logger := log.New(os.Stderr, "", 0) 224 // vm := NewTapeMachine(g, WithLogger(logger), WithWatchlist(), WithValueFmt("%#v")) 225 vm := NewTapeMachine(g) 226 if err := vm.RunAll(); err != nil { 227 t.Fatal(err) 228 } 229 230 correct := []float64{ 231 2, 3, 3, 3, 2, 232 6, 9, 9, 9, 6, 233 12, 18, 18, 18, 12, 234 18, 27, 27, 27, 18, 235 14, 21, 21, 21, 14, 236 } 237 238 assert.Equal(t, correct, y.Value().Data()) 239 } 240 241 func TestIssue363(t *testing.T) { 242 g := NewGraph() 243 244 x := NewScalar(g, Float64, WithName("x")) 245 y := NewScalar(g, Float64, WithName("y")) 246 247 z, err := Add(x, y) 248 249 if err != nil { 250 t.Fatal("error adding:", err) 251 } 252 253 z, err = Neg(z) //last node is Neg operator. So gradients are zero 254 255 // z, err = gorgonia.Mul(gorgonia.NewConstant(-1.0), z) //This results in non zero gradients 256 257 if err != nil { 258 t.Fatal("error in Multiply with -1:", err) 259 } 260 261 Let(x, 2.5) 262 Let(y, 2.0) 263 264 m := NewLispMachine(g) 265 266 defer m.Close() 267 268 err = m.RunAll() 269 270 if err != nil { 271 t.Fatal("error in running the lisp machine:", err) 272 } 273 274 t.Log("value of z:", z.Value()) 275 276 xgrad, err := x.Grad() 277 278 if err != nil { 279 t.Fatal("error in getting the xgrad:", err) 280 } 281 282 ygrad, err := y.Grad() 283 284 if err != nil { 285 t.Fatal("error in getting the ygrad:", err) 286 } 287 288 actualxgrad := xgrad.Data().(float64) 289 290 actualygrad := ygrad.Data().(float64) 291 292 if actualxgrad == 0.0 { 293 t.Log("xgrad=", actualxgrad, "ygrad=", actualygrad) 294 t.Fatal("zero xgrad") 295 } 296 297 if actualygrad == 0.0 { 298 t.Fatal("zero ygrad") 299 } 300 301 t.Log("xgrad=", actualxgrad, "ygrad=", actualygrad) 302 303 } 304 305 func TestIssue368(t *testing.T) { 306 c := require.New(t) 307 308 g := NewGraph() 309 x := NewTensor(g, Float32, 2, WithShape(2, 5), WithInit(GlorotU(1.0))) 310 311 sm, err := SoftMax(x, 1) 312 c.NoError(err) 313 c.NotNil(sm) 314 }