array_grad.cc revision 2db9a1edc1695877b3f90181e74cfbd8b1a8cdc7
1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#include <vector> 17 18#include "tensorflow/cc/ops/standard_ops.h" 19 20#include "tensorflow/cc/framework/grad_op_registry.h" 21 22namespace tensorflow { 23namespace ops { 24namespace { 25 26Status PackGrad(const Scope& scope, const Operation& op, 27 const std::vector<Output>& grad_inputs, 28 std::vector<Output>* grad_outputs) { 29 int N; 30 TF_RETURN_IF_ERROR(GetNodeAttr(op.node()->def(), "N", &N)); 31 int axis; 32 TF_RETURN_IF_ERROR(GetNodeAttr(op.node()->def(), "axis", &axis)); 33 34 grad_outputs->reserve(N); 35 auto grad_op = Unpack(scope, grad_inputs[0], N, Unpack::Axis(axis)); 36 for (const Output& o : grad_op.output) { 37 grad_outputs->emplace_back(o); 38 } 39 return Status::OK(); 40} 41REGISTER_GRADIENT_OP("Pack", PackGrad); 42 43Status UnpackGrad(const Scope& scope, const Operation& op, 44 const std::vector<Output>& grad_inputs, 45 std::vector<Output>* grad_outputs) { 46 int axis; 47 TF_RETURN_IF_ERROR(GetNodeAttr(op.node()->def(), "axis", &axis)); 48 grad_outputs->push_back(Pack(scope, grad_inputs, Pack::Axis(axis))); 49 return Status::OK(); 50} 51REGISTER_GRADIENT_OP("Unpack", UnpackGrad); 52 53} // anonymous namespace 54} // namespace ops 55} // namespace tensorflow 56