MCPcopy
hub / github.com/nlpodyssey/spago / testAppendRowsForward

Function testAppendRowsForward

mat/gradfn/appendrows_test.go:20–58  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18}
19
20func testAppendRowsForward[T float.DType](t *testing.T) {
21 x := mat.Tensor(mat.NewDense[T](mat.WithShape(2, 3), mat.WithBacking([]T{
22 11, 12, 13,
23 21, 22, 23,
24 }), mat.WithGrad(true)))
25
26 vs := []mat.Tensor{
27 mat.NewDense[T](mat.WithShape(1, 3), mat.WithBacking([]T{31, 32, 33}), mat.WithGrad(true)),
28 mat.NewDense[T](mat.WithShape(3, 1), mat.WithBacking([]T{41, 42, 43}), mat.WithGrad(true)),
29 }
30 f := NewAppendRows(x, vs...)
31
32 assert.Equal(t, []mat.Tensor{x, vs[0], vs[1]}, f.Operands())
33
34 y, err := f.Forward()
35 assert.Nil(t, err)
36
37 mat.AssertMatrixEquals(t, mat.NewDense[T](mat.WithShape(4, 3), mat.WithBacking([]T{
38 11, 12, 13,
39 21, 22, 23,
40 31, 32, 33,
41 41, 42, 43,
42 })), y.(mat.Matrix))
43
44 err = f.Backward(mat.NewDense[T](mat.WithShape(4, 3), mat.WithBacking([]T{
45 0, 1, 2,
46 3, 4, 5,
47 6, 7, 8,
48 9, 0, 1,
49 })))
50 assert.NoError(t, err)
51
52 mat.AssertMatrixEquals(t, mat.NewDense[T](mat.WithShape(2, 3), mat.WithBacking([]T{
53 0, 1, 2,
54 3, 4, 5,
55 })), x.Grad().(mat.Matrix))
56 mat.AssertMatrixEquals(t, mat.NewDense[T](mat.WithBacking([]T{6, 7, 8})).T(), vs[0].Grad().(mat.Matrix))
57 mat.AssertMatrixEquals(t, mat.NewDense[T](mat.WithBacking([]T{9, 0, 1})).T(), vs[1].Grad().(mat.Matrix))
58}

Callers

nothing calls this directly

Calls 11

TensorInterface · 0.92
WithShapeFunction · 0.92
WithBackingFunction · 0.92
WithGradFunction · 0.92
AssertMatrixEqualsFunction · 0.92
NewAppendRowsFunction · 0.85
OperandsMethod · 0.65
ForwardMethod · 0.65
BackwardMethod · 0.65
GradMethod · 0.65
TMethod · 0.65

Tested by

no test coverage detected