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

Function TestGraphAddGradients

tensorflow/go/graph_test.go:85–173  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

83}
84
85func TestGraphAddGradients(t *testing.T) {
86 g := NewGraph()
87 x1, err := Placeholder(g, "x1", Float)
88 if err != nil {
89 t.Fatal(err)
90 }
91 x2, err := Placeholder(g, "x2", Float)
92 if err != nil {
93 t.Fatal(err)
94 }
95 op0, err := g.AddOperation(OpSpec{
96 Type: "Square",
97 Name: "y0",
98 Input: []Input{x1},
99 })
100 if err != nil {
101 t.Fatal(err)
102 }
103 y0 := op0.Output(0)
104 op1, err := g.AddOperation(OpSpec{
105 Type: "Square",
106 Name: "y1",
107 Input: []Input{y0},
108 })
109 if err != nil {
110 t.Fatal(err)
111 }
112 y1 := op1.Output(0)
113 op2, err := g.AddOperation(OpSpec{
114 Type: "AddN",
115 Input: []Input{OutputList([]Output{y0, x2})},
116 })
117 if err != nil {
118 t.Fatal(err)
119 }
120 y2 := op2.Output(0)
121
122 grads0, err := g.AddGradients("", []Output{y1}, []Output{x1}, nil)
123 if err != nil {
124 t.Fatal(err)
125 }
126 if len(grads0) != 1 {
127 t.Fatal(len(grads0))
128 }
129 if grads0[0].DataType() != Float {
130 t.Fatalf("Got DataType %v, wanted %v", grads0[0].DataType(), Float)
131 }
132
133 grads1, err := g.AddGradients("", []Output{y2}, []Output{x1, x2}, nil)
134 if err != nil {
135 t.Fatal(err)
136 }
137 if len(grads1) != 2 {
138 t.Fatal(len(grads1))
139 }
140 if grads1[0].DataType() != Float {
141 t.Fatalf("Got DataType %v, wanted %v", grads1[0].DataType(), Float)
142 }

Callers

nothing calls this directly

Calls 11

AddOperationMethod · 0.95
AddGradientsMethod · 0.95
RunMethod · 0.95
NewGraphFunction · 0.85
OutputListTypeAlias · 0.85
NewTensorFunction · 0.85
PlaceholderFunction · 0.70
NewSessionFunction · 0.70
OutputMethod · 0.45
DataTypeMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected