README.md
1 # C++ gradients
2
3 Gradients are currently being ported from
4 [python](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/ops)
5 to C++ (in this directory).
6
7 Contributions are welcome and much appreciated; please follow the instructions
8 below.
9
10 1. Create the op gradient function in `foo_grad.cc` corresponding to the
11 `foo_grad.py` file where the op originated (i.e. `array_grad.py` op
12 gradients should be written in `array_grad.cc`).
13
14 2. Write the op gradient with the following naming scheme:
15
16 Status OpNameGrad(const Scope& scope, const Operation& op,
17 const std::vector<Output>& grad_inputs,
18 std::vector<Output>* grad_outputs) {
19 ...
20 return scope.status();
21 }
22 REGISTER_GRADIENT_OP("OpName", OpNameGrad);
23
24 3. Ops gradients are implemented by using the [C++
25 API](https://www.tensorflow.org/api_docs/cc/).
26
27 4. Tests should be included in `foo_grad_test.cc`. Please see
28 [`array_grad_test.cc`](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/cc/gradients/array_grad_test.cc)
29 for an many examples. Tests are as simple as, creating a placeholder input
30 for the op's inputs and calling `RunTest` (`RunTest` uses a [gradient
31 checker](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/cc/framework/gradient_checker.cc)
32 to verify that the theoretical gradient matches the numeric gradient). For
33 example:
34
35 TEST_F(ArrayGradTest, IdentityGrad) {
36 TensorShape shape({5, 2});
37 auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
38 auto y = Identity(scope_, x);
39 RunTest(x, shape, y, shape);
40 }
41
42 NOTE: There are some ops that require features from the C++ API that are not yet
43 implemented.
44
45 * Ops that require PartialTensorShape information cannot yet be implemented.
46
47 * Ops that require SparseTensor or IndexSlices (currently only in python)
48 cannot yet be implemented.
49
50 * Maybe more.
51
52 For questions: Please create an issue assigned to suharshs.
53