| 18 | } |
| 19 | |
| 20 | func testMulForwardMatrixMatrix[T float.DType](t *testing.T) { |
| 21 | x1 := mat.NewDense[T](mat.WithShape(3, 4), mat.WithBacking([]T{ |
| 22 | 0.1, 0.2, 0.3, 0.0, |
| 23 | 0.4, 0.5, -0.6, 0.7, |
| 24 | -0.5, 0.8, -0.8, -0.1, |
| 25 | }), mat.WithGrad(true)) |
| 26 | |
| 27 | x2 := mat.NewDense[T](mat.WithShape(4, 3), mat.WithBacking([]T{ |
| 28 | 0.2, 0.7, 0.5, |
| 29 | 0.0, 0.4, 0.5, |
| 30 | -0.8, 0.7, -0.3, |
| 31 | 0.2, 0.0, -0.9, |
| 32 | }), mat.WithGrad(true)) |
| 33 | |
| 34 | f := NewMul(x1, x2) |
| 35 | assert.Equal(t, []mat.Tensor{x1, x2}, f.Operands()) |
| 36 | |
| 37 | y, err := f.Forward() |
| 38 | assert.Nil(t, err) |
| 39 | |
| 40 | assert.InDeltaSlice(t, []T{ |
| 41 | -0.22, 0.36, 0.06, |
| 42 | 0.7, 0.06, 0.0, |
| 43 | 0.52, -0.59, 0.48, |
| 44 | }, y.Data(), 1.0e-6) |
| 45 | |
| 46 | err = f.Backward(mat.NewDense[T](mat.WithShape(3, 3), mat.WithBacking([]T{ |
| 47 | 0.2, 0.7, 0.5, |
| 48 | 0.0, 0.4, 0.5, |
| 49 | -0.6, 0.7, -0.5, |
| 50 | }))) |
| 51 | assert.Nil(t, err) |
| 52 | |
| 53 | assert.InDeltaSlice(t, []T{ |
| 54 | 0.78, 0.53, 0.18, -0.41, |
| 55 | 0.53, 0.41, 0.13, -0.45, |
| 56 | 0.12, 0.03, 1.12, 0.33, |
| 57 | }, x1.Grad().Data(), 1.0e-6) |
| 58 | |
| 59 | assert.InDeltaSlice(t, []T{ |
| 60 | 0.32, -0.12, 0.5, |
| 61 | -0.44, 0.9, -0.05, |
| 62 | 0.54, -0.59, 0.25, |
| 63 | 0.06, 0.21, 0.4, |
| 64 | }, x2.Grad().Data(), 1.0e-2) |
| 65 | } |
| 66 | |
| 67 | func TestMul_ForwardMatrixVector(t *testing.T) { |
| 68 | t.Run("float32", testMulForwardMatrixVector[float32]) |