Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conv2DGrad & MaxPoolGradHelper #12665

Merged
merged 2 commits into from
Sep 7, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tensorflow/cc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ cc_library(
":cc_ops",
":cc_ops_internal",
":grad_op_registry",
":gradients",
],
alwayslink = 1,
)
Expand Down
81 changes: 81 additions & 0 deletions tensorflow/cc/gradients/nn_grad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
#include "tensorflow/cc/ops/standard_ops.h"

#include "tensorflow/cc/framework/grad_op_registry.h"
#include "tensorflow/cc/framework/gradients.h"

namespace tensorflow {
namespace ops {
Expand Down Expand Up @@ -118,6 +119,86 @@ Status BiasAddGradHelper(const Scope& scope, const Operation& op,
}
REGISTER_GRADIENT_OP("BiasAdd", BiasAddGradHelper);

Status Conv2DGrad(const Scope& scope, const Operation& op,
const std::vector<Output>& grad_inputs,
std::vector<Output>* grad_outputs) {
string data_format;
string padding;
std::vector<int32> strides;
bool use_cudnn_on_gpu;
auto attrs = op.output(0).node()->attrs();
GetNodeAttr(attrs, "data_format", &data_format);
GetNodeAttr(attrs, "padding", &padding);
GetNodeAttr(attrs, "strides", &strides);
GetNodeAttr(attrs, "use_cudnn_on_gpu", &use_cudnn_on_gpu);
Conv2DBackpropInput::Attrs input_attrs;
input_attrs.DataFormat(data_format);
input_attrs.UseCudnnOnGpu(use_cudnn_on_gpu);
auto dx_1 = Conv2DBackpropInput(scope, Shape(scope, op.input(0)),
op.input(1), grad_inputs[0],
strides, padding, input_attrs);
grad_outputs->push_back(dx_1);
Conv2DBackpropFilter::Attrs filter_attrs;
filter_attrs.DataFormat(data_format);
filter_attrs.UseCudnnOnGpu(use_cudnn_on_gpu);
auto dx_2 = Conv2DBackpropFilter(scope, op.input(0),
Shape(scope, op.input(1)), grad_inputs[0],
strides, padding, filter_attrs);
grad_outputs->push_back(dx_2);
return scope.status();
}
REGISTER_GRADIENT_OP("Conv2D", Conv2DGrad);

Status MaxPoolGradHelper(const Scope& scope, const Operation& op,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what the status of MaxPool and MaxPoolV2 is, but my guess is that MaxPoolV2 is the one more commonly used now. You may want to also add that gradient

@ops.RegisterGradient("MaxPoolV2")

If you feel like it you can add MaxPoolGradGrad and MaxPoolGradGradV2, but I doubt these are blocking your models.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, didn't know about MaxPoolV2. Added it.

const std::vector<Output>& grad_inputs,
std::vector<Output>* grad_outputs) {
string data_format;
string padding;
std::vector<int32> strides;
std::vector<int32> ksize;
auto attrs = op.output(0).node()->attrs();
GetNodeAttr(attrs, "data_format", &data_format);
GetNodeAttr(attrs, "ksize", &ksize);
GetNodeAttr(attrs, "padding", &padding);
GetNodeAttr(attrs, "strides", &strides);
internal::MaxPoolGrad::Attrs grad_attrs;
grad_attrs.DataFormat(data_format);
auto dx = internal::MaxPoolGrad(scope, op.input(0),
op.output(0),
grad_inputs[0],
ksize, strides,
padding, grad_attrs);
grad_outputs->push_back(dx);
return scope.status();
}
REGISTER_GRADIENT_OP("MaxPool", MaxPoolGradHelper);

Status MaxPoolGradV2Helper(const Scope& scope, const Operation& op,
const std::vector<Output>& grad_inputs,
std::vector<Output>* grad_outputs) {
string data_format;
string padding;
auto attrs = op.output(0).node()->attrs();
GetNodeAttr(attrs, "data_format", &data_format);
GetNodeAttr(attrs, "padding", &padding);
MaxPoolGradV2::Attrs grad_attrs;
grad_attrs.DataFormat(data_format);
auto dx = MaxPoolGradV2(scope, op.input(0),
op.output(0),
grad_inputs[0],
op.input(1),
op.input(2),
padding,
grad_attrs);
grad_outputs->push_back(dx);
grad_outputs->push_back(NoGradient());
grad_outputs->push_back(NoGradient());
return scope.status();
}
REGISTER_GRADIENT_OP("MaxPoolV2", MaxPoolGradV2Helper);



} // anonymous namespace
} // namespace ops
} // namespace tensorflow
27 changes: 27 additions & 0 deletions tensorflow/cc/gradients/nn_grad_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,32 @@ TEST_F(NNGradTest, BiasAddGradHelper) {
RunTest({x,bias}, {shape, bias_shape}, {y}, {shape});
}

TEST_F(NNGradTest, Conv2DGrad) {
TensorShape shape({1, 2, 2, 1});
auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
Tensor filter = test::AsTensor<float>({0.5f}, {1, 1, 1, 1});
const std::vector<int> strides{1, 1, 1, 1};
auto y = Conv2D(scope_, x, filter, strides, "SAME");
RunTest(x, shape, y, shape);
}

TEST_F(NNGradTest, MaxPoolGradHelper) {
TensorShape shape({1, 2, 2, 1});
auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
const std::vector<int> ksize{1, 2, 2, 1};
const std::vector<int> strides{1, 1, 1, 1};
auto y = MaxPool(scope_, x, ksize, strides, "SAME");
RunTest(x, shape, y, shape);
}

TEST_F(NNGradTest, MaxPoolGradV2Helper) {
TensorShape shape({1, 2, 2, 1});
auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(shape));
Tensor ksize = test::AsTensor<int>({1, 2, 2, 1}, {4});
Tensor strides = test::AsTensor<int>({1, 1, 1, 1}, {4});
auto y = MaxPoolV2(scope_, x, ksize, strides, "SAME");
RunTest(x, shape, y, shape);
}

} // namespace
} // namespace tensorflow