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

Method AddGradients

tensorflow/go/graph.go:182–237  ·  view source on GitHub ↗

AddGradients adds operations to compute the partial derivatives of the sum of tensors in y with respect to tensors in x, i.e., d(y[0] + y[1] + ...) / d x[0], d(y[0] + y[1] + ... ) / d x[1] etc. prefix, if non-empty, is the name prefix used for all operations added to the graph to compute these grad

(prefix string, y []Output, x []Output, dx []Output)

Source from the content-addressed store, hash-verified

180// prefix, if non-empty, is the name prefix used for all operations added to the graph to compute
181// these gradients.
182func (g *Graph) AddGradients(prefix string, y []Output, x []Output, dx []Output) ([]Output, error) {
183 var (
184 cprefix *C.char
185
186 cy = make([]C.TF_Output, len(y))
187 cx = make([]C.TF_Output, len(x))
188 cdx = make([]C.TF_Output, len(dx))
189 cdy = make([]C.TF_Output, len(x))
190
191 pcy *C.TF_Output
192 pcx *C.TF_Output
193 pcdx *C.TF_Output
194 pcdy *C.TF_Output
195
196 status = newStatus()
197 )
198
199 if len(y) > 0 {
200 pcy = &cy[0]
201 for i, o := range y {
202 cy[i] = o.c()
203 }
204 }
205 if len(x) > 0 {
206 pcx = &cx[0]
207 for i, o := range x {
208 cx[i] = o.c()
209 }
210 pcdy = &cdy[0]
211 }
212 if len(dx) > 0 {
213 pcdx = &cdx[0]
214 for i, o := range dx {
215 cdx[i] = o.c()
216 }
217 }
218
219 // If prefix is "", the C.TF_AddGradientsWithPrefix need cprefix to be nil but not ""
220 if len(prefix) != 0 {
221 cprefix = C.CString(prefix)
222 defer C.free(unsafe.Pointer(cprefix))
223 }
224
225 C.TF_AddGradientsWithPrefix(g.c, cprefix, pcy, C.int(len(y)), pcx, C.int(len(x)), pcdx, status.c, pcdy)
226
227 if err := status.Err(); err != nil {
228 return nil, err
229 }
230 dy := make([]Output, len(x))
231 for i, co := range cdy {
232 op := &Operation{co.oper, g}
233 dy[i] = Output{op, int(co.index)}
234 }
235
236 return dy, nil
237}
238
239// OpSpec is the specification of an Operation to be added to a Graph

Callers 5

TestGraphAddGradientsFunction · 0.95
GradientsFunction · 0.45

Calls 5

makeFunction · 0.85
newStatusFunction · 0.85
freeMethod · 0.80
cMethod · 0.45
ErrMethod · 0.45