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

Function SoftmaxGrad

tensorflow/cc/gradients/nn_grad.cc:27–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25namespace {
26
27Status SoftmaxGrad(const Scope& scope, const Operation& op,
28 const std::vector<Output>& grad_inputs,
29 std::vector<Output>* grad_outputs) {
30 // Softmax gradient function.
31 // p = softmax(x) maps from [batch, n] to [batch, m]
32 // dp/dx = [dp0/dx0 ... dp0/dxn-1 ]
33 // [ ... ... ]
34 // [dpm-1/dx0 ... dpm-1/dxn-1]
35 // dL/dx = dp/dx * dL/dy
36 //
37 // Using alternative formula:
38 // dL/dx = dL/dy * y - sum(dL/dy * y) * y
39 // = (dL/dy - sum(dL/dy * y)) * y
40 auto y = op.output(0);
41 auto dyy = Mul(scope, grad_inputs[0], y);
42 auto sum = Reshape(scope, Sum(scope, dyy, {1}), {-1, 1});
43 auto sub = Sub(scope, grad_inputs[0], sum);
44 auto dx = Mul(scope, sub, y);
45 grad_outputs->push_back(dx);
46 return scope.status();
47}
48REGISTER_GRADIENT_OP("Softmax", SoftmaxGrad);
49
50bool IsZero(const Scope& scope, const Output& grad) {

Callers

nothing calls this directly

Calls 7

outputMethod · 0.65
MulFunction · 0.50
ReshapeFunction · 0.50
SumClass · 0.50
SubFunction · 0.50
push_backMethod · 0.45
statusMethod · 0.45

Tested by

no test coverage detected