xref: /aosp_15_r20/external/tensorflow/tensorflow/go/op/gradients.go (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1/*
2Copyright 2016 The TensorFlow Authors. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package op
18
19import (
20	"fmt"
21
22	tf "github.com/tensorflow/tensorflow/tensorflow/go"
23)
24
25// Gradients adds gradients computation ops to the graph according to scope.
26//
27// Arguments:
28//  y: output of the function to derive
29//  x: inputs of the function for which partial derivatives are computed
30//  dx: if not null, the partial derivatives of some loss function L w.r.t. y
31//
32//  return the partial derivatives
33func Gradients(scope *Scope, y []tf.Output, x []tf.Output, dx ...tf.Output) (output []tf.Output) {
34	if len(scope.controlDependencies) > 0 {
35		scope.UpdateErr("Gradients", fmt.Errorf("Gradients does not currently support control dependencies (via Scope.WithControlDependencies)."))
36		return
37	}
38	if scope.device != "" {
39		scope.UpdateErr("Gradients", fmt.Errorf("Gradients does not currently support device annotations (via Scope.WithDevice)."))
40		return
41	}
42
43	var err error
44	if output, err = scope.graph.AddGradients(scope.opName("Gradients"), y, x, dx); err != nil {
45		scope.UpdateErr("Gradients", err)
46		return
47	}
48	return output
49}
50