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

Function TestGraphAddGradientsSums

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

Source from the content-addressed store, hash-verified

173}
174
175func TestGraphAddGradientsSums(t *testing.T) {
176 g := NewGraph()
177 x, err := Placeholder(g, "x", Float)
178 if err != nil {
179 t.Fatal(err)
180 }
181 op0, err := g.AddOperation(OpSpec{
182 Type: "Square",
183 Name: "y0",
184 Input: []Input{x},
185 })
186 if err != nil {
187 t.Fatal(err)
188 }
189 y0 := op0.Output(0)
190 op1, err := g.AddOperation(OpSpec{
191 Type: "Square",
192 Name: "y1",
193 Input: []Input{y0},
194 })
195 y1 := op1.Output(0)
196
197 grad, err := g.AddGradients("", []Output{y0, y1}, []Output{x}, nil)
198 if err != nil {
199 t.Fatal(err)
200 }
201 if len(grad) != 1 {
202 t.Fatal(len(grad))
203 }
204 if grad[0].DataType() != Float {
205 t.Fatalf("Got DataType %v, wanted %v", grad[0].DataType(), Float)
206 }
207
208 sess, err := NewSession(g, nil)
209 if err != nil {
210 t.Fatal(err)
211 }
212
213 c, _ := NewTensor(float32(3.0))
214 outputs, err := sess.Run(
215 map[Output]*Tensor{x: c},
216 []Output{grad[0]},
217 nil)
218 if err != nil {
219 t.Fatal(err)
220 }
221 if outputs[0].Value().(float32) != 114.0 {
222 t.Fatalf("Got %v, wanted float 114.0", outputs[0].Value())
223 }
224}
225
226func TestGraphAddGradientsWithInitialValues(t *testing.T) {
227 g := NewGraph()

Callers

nothing calls this directly

Calls 10

AddOperationMethod · 0.95
AddGradientsMethod · 0.95
RunMethod · 0.95
NewGraphFunction · 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