MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / TestAddGradients

Function TestAddGradients

tensorflow/go/op/gradients_test.go:26–92  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

24)
25
26func TestAddGradients(t *testing.T) {
27 var (
28 s = NewScope()
29 x1 = Placeholder(s.SubScope("x1"), tf.Float)
30 x2 = Placeholder(s.SubScope("x2"), tf.Float)
31 y0 = Square(s.SubScope("y0"), x1)
32 y1 = Square(s.SubScope("y1"), y0)
33 y2 = AddN(s.SubScope("y2"), []tf.Output{y0, x2})
34 )
35
36 grads0 := Gradients(s, []tf.Output{y1}, []tf.Output{x1})
37 if err := s.Err(); err != nil {
38 t.Fatal(err)
39 }
40 if len(grads0) != 1 {
41 t.Fatal(len(grads0))
42 }
43 if grads0[0].DataType() != tf.Float {
44 t.Fatalf("Got DataType %v, wanted %v", grads0[0].DataType(), tf.Float)
45 }
46
47 sub := s.SubScope("sub")
48 grads1 := Gradients(sub, []tf.Output{y2}, []tf.Output{x1, x2})
49 if err := sub.Err(); err != nil {
50 t.Fatal(err)
51 }
52 if len(grads1) != 2 {
53 t.Fatal(len(grads1))
54 }
55 if grads1[0].DataType() != tf.Float {
56 t.Fatalf("Got DataType %v, wanted %v", grads1[0].DataType(), tf.Float)
57 }
58 if grads1[1].DataType() != tf.Float {
59 t.Fatalf("Got DataType %v, wanted %v", grads1[1].DataType(), tf.Float)
60 }
61
62 graph, err := sub.Finalize()
63 if err != nil {
64 t.Fatal(err)
65 }
66 sess, err := tf.NewSession(graph, nil)
67 if err != nil {
68 t.Fatal(err)
69 }
70
71 c1, _ := tf.NewTensor(float32(3.0))
72 c2, _ := tf.NewTensor(float32(3.0))
73 outputs, err := sess.Run(
74 map[tf.Output]*tf.Tensor{x1: c1, x2: c2},
75 []tf.Output{grads0[0], grads1[0], grads1[1]},
76 nil)
77 if err != nil {
78 t.Fatal(err)
79 }
80 if len(outputs) != 3 {
81 t.Fatal(len(outputs))
82 }
83 if outputs[0].Value().(float32) != 108.0 {

Callers

nothing calls this directly

Calls 12

RunMethod · 0.95
NewScopeFunction · 0.85
SubScopeMethod · 0.80
PlaceholderFunction · 0.70
SquareFunction · 0.70
AddNFunction · 0.70
GradientsFunction · 0.70
ErrMethod · 0.45
DataTypeMethod · 0.45
FinalizeMethod · 0.45
NewSessionMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected