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

Function TestGraphAddGradientsWithInitialValues

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

Source from the content-addressed store, hash-verified

224}
225
226func TestGraphAddGradientsWithInitialValues(t *testing.T) {
227 g := NewGraph()
228 x, err := Placeholder(g, "x", Float)
229 op0, err := g.AddOperation(OpSpec{
230 Type: "Square",
231 Name: "y0",
232 Input: []Input{x},
233 })
234 if err != nil {
235 t.Fatal(err)
236 }
237 y0 := op0.Output(0)
238 op1, err := g.AddOperation(OpSpec{
239 Type: "Square",
240 Name: "y1",
241 Input: []Input{y0},
242 })
243 if err != nil {
244 t.Fatal(err)
245 }
246 y1 := op1.Output(0)
247
248 grads0, err := g.AddGradients("", []Output{y1}, []Output{y0}, nil)
249 if err != nil {
250 t.Fatal(err)
251 }
252 if len(grads0) != 1 {
253 t.Fatal(len(grads0))
254 }
255 if grads0[0].DataType() != Float {
256 t.Fatalf("Got DataType %v, wanted %v", grads0[0].DataType(), Float)
257 }
258
259 grads1, err := g.AddGradients("", []Output{y0}, []Output{x}, []Output{grads0[0]})
260 if err != nil {
261 t.Fatal(err)
262 }
263 if len(grads1) != 1 {
264 t.Fatal(len(grads1))
265 }
266 if grads1[0].DataType() != Float {
267 t.Fatalf("Got DataType %v, wanted %v", grads1[0].DataType(), Float)
268 }
269
270 sess, err := NewSession(g, nil)
271 if err != nil {
272 t.Fatal(err)
273 }
274
275 c, _ := NewTensor(float32(3.0))
276 outputs, err := sess.Run(
277 map[Output]*Tensor{x: c},
278 []Output{grads1[0]},
279 nil)
280 if err != nil {
281 t.Fatal(err)
282 }
283 if outputs[0].Value().(float32) != 108.0 {

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