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

Function TestAddGradientsWithInitialValues

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

Source from the content-addressed store, hash-verified

133}
134
135func TestAddGradientsWithInitialValues(t *testing.T) {
136 var (
137 s = NewScope()
138 x = Placeholder(s.SubScope("x1"), tf.Float)
139 y0 = Square(s.SubScope("y0"), x)
140 y1 = Square(s.SubScope("y1"), y0)
141 )
142
143 grads0 := Gradients(s, []tf.Output{y1}, []tf.Output{y0})
144 if err := s.Err(); err != nil {
145 t.Fatal(err)
146 }
147 if len(grads0) != 1 {
148 t.Fatal(len(grads0))
149 }
150 if grads0[0].DataType() != tf.Float {
151 t.Fatalf("Got DataType %v, wanted %v", grads0[0].DataType(), tf.Float)
152 }
153
154 sub := s.SubScope("sub")
155 grads1 := Gradients(sub, []tf.Output{y0}, []tf.Output{x}, grads0[0])
156 if err := sub.Err(); err != nil {
157 t.Fatal(err)
158 }
159 if len(grads1) != 1 {
160 t.Fatal(len(grads1))
161 }
162 if grads1[0].DataType() != tf.Float {
163 t.Fatalf("Got DataType %v, wanted %v", grads1[0].DataType(), tf.Float)
164 }
165
166 graph, err := sub.Finalize()
167 if err != nil {
168 t.Fatal(err)
169 }
170 sess, err := tf.NewSession(graph, nil)
171 if err != nil {
172 t.Fatal(err)
173 }
174
175 c, _ := tf.NewTensor(float32(3.0))
176 outputs, err := sess.Run(
177 map[tf.Output]*tf.Tensor{x: c},
178 []tf.Output{grads1[0]},
179 nil)
180 if err != nil {
181 t.Fatal(err)
182 }
183 if outputs[0].Value().(float32) != 108.0 {
184 t.Fatalf("Got %v, wanted float 108.0", outputs[0].Value())
185 }
186}
187
188func TestValidateGradientsNames(t *testing.T) {
189 var (

Callers

nothing calls this directly

Calls 11

RunMethod · 0.95
NewScopeFunction · 0.85
SubScopeMethod · 0.80
PlaceholderFunction · 0.70
SquareFunction · 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