Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tensorflow/cc/gradients/nn_grad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ Status SeluGradHelper(const Scope& scope, const Operation& op,
}
REGISTER_GRADIENT_OP("Selu", SeluGradHelper);

Status BiasAddGradHelper(const Scope& scope, const Operation& op,
const std::vector<Output>& grad_inputs,
std::vector<Output>* grad_outputs) {
string data_format;
BiasAddGrad::Attrs input_attrs;
GetNodeAttr(op.output(0).node()->attrs(), "data_format", &data_format);
input_attrs.DataFormat(data_format);
auto dx_1 = BiasAddGrad(scope, grad_inputs[0], input_attrs);
grad_outputs->push_back(Identity(scope, grad_inputs[0]));
grad_outputs->push_back(dx_1);
return scope.status();
}
REGISTER_GRADIENT_OP("BiasAdd", BiasAddGradHelper);

} // anonymous namespace
} // namespace ops
} // namespace tensorflow
18 changes: 18 additions & 0 deletions tensorflow/cc/gradients/nn_grad_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ class NNGradTest : public ::testing::Test {
EXPECT_LT(max_error, 1e-4);
}

void RunTest(const OutputList& xs, const std::vector<TensorShape>& x_shapes,
const OutputList& ys, const std::vector<TensorShape>& y_shapes) {
TF_ASSERT_OK(scope_.status());
float max_error;
TF_ASSERT_OK(
ComputeGradientError(scope_, xs, x_shapes, ys, y_shapes, &max_error));
EXPECT_LT(max_error, 1e-4);
}

Scope scope_;
};

Expand Down Expand Up @@ -113,5 +122,14 @@ TEST_F(NNGradTest, SeluGrad) {
RunTest(x, x_init_value, y, shape);
}

TEST_F(NNGradTest, BiasAddGradHelper) {
TensorShape shape({4, 5});
TensorShape bias_shape({5});
auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
auto bias = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(bias_shape));
auto y = BiasAdd(scope_, x, bias);
RunTest({x,bias}, {shape, bias_shape}, {y}, {shape});
}

} // namespace
} // namespace tensorflow