Tape Gradient C++


Would like to use GradientTape in c++ but cannot find any code sample that demonstrates how to do so.

  • In python:
    with GradientTape as tape:
    y = x * x

  • In C++: ?

Does anyone have some sample to share?


You can find something at:


I’m able to create simple operation but seems like some operators are not available in C. Below the Ops I would like to use with Tape

// logprobabilities
    auto softmax = LogSoftmax(root, logitsBufPlc);
    auto onehot = OneHot(root, actionsBufPlc, numActions, 1, 0);
    auto multi = Multiply(root, onehot, softmax);
    auto logprobs = ReduceSum(root, multi, 1);

    // ratio
    auto sub = Subtract(root, logprobs, logProbsBufPlc);
    auto ratio = Exp(root, sub);

    // min advantage
    auto greater = Greater(root, advgBufPlc, 1);
    auto clipmin = Multiply(root, (1 - clipRatio), advgBufPlc);
    auto clipmax = Multiply(root, (1 + clipRatio), advgBufPlc);
    auto minadvg = Where3(root, greater, clipmin, clipmax);
    // loss
    auto minv = Min(root, Multiply(root, ratio, advgBufPlc), minadvg);
    auto reducedMean = ReduceMean(root.WithOpName("reduced"), minv, {0, 1});

Here you can find the C++ ops (array, nn, etc…):


This operators aren’t compatible with GradientTape in which needs a context as input. Check example below.

Tape tape(/persistent=/false);
AbstractTensorHandle* neg_output;
TF_RETURN_IF_ERROR(ops::Neg(ctx, inputs[0], &neg_output, “Neg”));
tape.RecordOperation(inputs, {neg_output}, nullptr, “Neg”);

For that I think that you need to wait for: