@@ -22,8 +22,6 @@ limitations under the License.
2222#include " tensorflow/core/lib/core/errors.h"
2323
2424namespace tensorflow {
25- using namespace ops ; // NOLINT(build/namespaces)
26-
2725namespace {
2826
2927// TODO(andydavis) Support returning relative error (as opposed to max error)
@@ -39,15 +37,16 @@ Status ComputeTheoreticalJacobianTranspose(
3937 const std::vector<TensorShape>& x_shapes,
4038 const std::vector<Tensor>& x_datas, const OutputList& ys,
4139 const std::vector<TensorShape>& y_shapes,
42- std::vector<Tensor>& jacobian_ts) {
40+ std::vector<Tensor>* jacobian_ts) {
4341 size_t y_num = y_shapes.size ();
4442 size_t x_num = x_shapes.size ();
4543 // Call AddSymbolicGradients to get 'dxs' (we will feed 'dys').
4644 OutputList dys;
4745 dys.reserve (y_shapes.size ());
4846 for (const auto & y_shape : y_shapes) {
4947 // TODO(suharshs): This currently assumes that all x's are the same type.
50- dys.push_back (Cast (scope, Const (scope, 1.0 , y_shape), xs[0 ].type ()));
48+ dys.push_back (
49+ ops::Cast (scope, ops::Const (scope, 1.0 , y_shape), xs[0 ].type ()));
5150 }
5251 OutputList dxs;
5352 TF_RETURN_IF_ERROR (AddSymbolicGradients (scope, ys, xs, dys, &dxs));
@@ -85,7 +84,7 @@ Status ComputeTheoreticalJacobianTranspose(
8584
8685 for (int x_idx = 0 ; x_idx < x_num; x_idx++) {
8786 const int64 x_size = x_shapes[x_idx].num_elements ();
88- auto jacobian = jacobian_ts[x_idx * y_num + y_idx].matrix <T>();
87+ auto jacobian = (* jacobian_ts) [x_idx * y_num + y_idx].matrix <T>();
8988 auto dx_flat = dxout[x_idx].flat <T>();
9089 for (int r = 0 ; r < x_size; ++r) {
9190 jacobian (r, c) = dx_flat (r);
@@ -98,20 +97,20 @@ Status ComputeTheoreticalJacobianTranspose(
9897 return Status::OK ();
9998}
10099
101- Status EvaluateGraph (ClientSession& session, const OutputList& xs,
102- const OutputList& ys, std::vector<Tensor>& x_datas,
100+ Status EvaluateGraph (ClientSession* session, const OutputList& xs,
101+ const OutputList& ys, std::vector<Tensor>* x_datas,
103102 std::vector<Tensor>* y_datas) {
104103 // Create the feed list.
105104 ClientSession::FeedType feed_list;
106- for (int i = 0 ; i < x_datas. size (); i++) {
107- feed_list.insert ({xs[i], x_datas[i]});
105+ for (int i = 0 ; i < x_datas-> size (); i++) {
106+ feed_list.insert ({xs[i], (* x_datas) [i]});
108107 }
109108
110- TF_RETURN_IF_ERROR (session. Run (feed_list, ys, y_datas));
109+ TF_RETURN_IF_ERROR (session-> Run (feed_list, ys, y_datas));
111110 for (int y_idx = 0 ; y_idx < y_datas->size (); y_idx++) {
112- for (int x_idx = 0 ; x_idx < x_datas. size (); x_idx++) {
111+ for (int x_idx = 0 ; x_idx < x_datas-> size (); x_idx++) {
113112 Tensor y_data = (*y_datas)[y_idx];
114- if (y_data.SharesBufferWith (x_datas[x_idx])) {
113+ if (y_data.SharesBufferWith ((* x_datas) [x_idx])) {
115114 // Create copies of outputs that share a buffer with any inputs since
116115 // the underlying buffer of the input Tensors are not copied for some
117116 // operations (i.e. Identity), which can lead to incorrect results for
@@ -129,14 +128,14 @@ Status ComputeNumericJacobianTranspose(const Scope& scope, const OutputList& xs,
129128 const OutputList& ys,
130129 const std::vector<TensorShape>& y_shapes,
131130 const T delta,
132- std::vector<Tensor>& x_datas,
133- std::vector<Tensor>& jacobian_ts) {
131+ std::vector<Tensor>* x_datas,
132+ std::vector<Tensor>* jacobian_ts) {
134133 size_t y_num = y_shapes.size ();
135134 size_t x_num = x_shapes.size ();
136135
137136 ClientSession session (scope);
138137 for (int x_idx = 0 ; x_idx < x_num; x_idx++) {
139- auto x_data_flat = x_datas[x_idx].flat <T>();
138+ auto x_data_flat = (* x_datas) [x_idx].flat <T>();
140139 const int64 x_size = x_shapes[x_idx].num_elements ();
141140
142141 // Compute the numeric Jacobian one column at a time by perturbing each
@@ -148,19 +147,19 @@ Status ComputeNumericJacobianTranspose(const Scope& scope, const OutputList& xs,
148147 // Evaluate at positive delta.
149148 x_data_flat (r) = v + delta;
150149 std::vector<Tensor> y_pos;
151- TF_RETURN_IF_ERROR (EvaluateGraph (session, xs, ys, x_datas, &y_pos));
150+ TF_RETURN_IF_ERROR (EvaluateGraph (& session, xs, ys, x_datas, &y_pos));
152151 // Evaluate at negative delta.
153152 x_data_flat (r) = v - delta;
154153 std::vector<Tensor> y_neg;
155- TF_RETURN_IF_ERROR (EvaluateGraph (session, xs, ys, x_datas, &y_neg));
154+ TF_RETURN_IF_ERROR (EvaluateGraph (& session, xs, ys, x_datas, &y_neg));
156155
157156 for (int y_idx = 0 ; y_idx < y_num; y_idx++) {
158157 // Compute element-wise centered difference and store in each Jacobian.
159158 auto y_pos_flat = y_pos[y_idx].flat <T>();
160159 auto y_neg_flat = y_neg[y_idx].flat <T>();
161160 const int64 y_size = y_shapes[y_idx].num_elements ();
162161 const T scale = 2 * delta;
163- auto jacobian = jacobian_ts[x_idx * y_num + y_idx].matrix <T>();
162+ auto jacobian = (* jacobian_ts) [x_idx * y_num + y_idx].matrix <T>();
164163 for (int c = 0 ; c < y_size; ++c) {
165164 jacobian (r, c) = (y_pos_flat (c) - y_neg_flat (c)) / scale;
166165 }
@@ -176,19 +175,19 @@ template <typename T>
176175void InitJacobians (const OutputList& xs,
177176 const std::vector<TensorShape>& x_shapes,
178177 const std::vector<TensorShape>& y_shapes,
179- std::vector<Tensor>& jacobians) {
178+ std::vector<Tensor>* jacobians) {
180179 size_t y_num = y_shapes.size ();
181180 size_t x_num = x_shapes.size ();
182181
183- jacobians. resize (y_num * x_num);
182+ jacobians-> resize (y_num * x_num);
184183 for (int x_idx = 0 ; x_idx < x_num; x_idx++) {
185184 const int64 x_size = x_shapes[x_idx].num_elements ();
186185 for (int y_idx = 0 ; y_idx < y_num; y_idx++) {
187186 const int64 y_size = y_shapes[y_idx].num_elements ();
188187 Tensor jacobian_t (xs[x_idx].type (), {x_size, y_size});
189188 auto jacobian_t_flat = jacobian_t .flat <T>();
190189 jacobian_t_flat.setZero ();
191- jacobians[x_idx * y_num + y_idx] = std::move (jacobian_t );
190+ (* jacobians) [x_idx * y_num + y_idx] = std::move (jacobian_t );
192191 }
193192 }
194193}
@@ -198,23 +197,23 @@ Status ComputeGradientErrorInternal(const Scope& scope, const OutputList& xs,
198197 const std::vector<TensorShape>& x_shapes,
199198 const OutputList& ys,
200199 const std::vector<TensorShape>& y_shapes,
201- std::vector<Tensor>& x_datas,
200+ std::vector<Tensor>* x_datas,
202201 T* max_error) {
203202 // Initialize theoretical Jacobians to zeros.
204203 std::vector<Tensor> jacobian_ts;
205- InitJacobians<T>(xs, x_shapes, y_shapes, jacobian_ts);
204+ InitJacobians<T>(xs, x_shapes, y_shapes, & jacobian_ts);
206205
207206 // Compute theoretical Jacobian.
208207 TF_RETURN_IF_ERROR (ComputeTheoreticalJacobianTranspose<T>(
209- scope, xs, x_shapes, x_datas, ys, y_shapes, jacobian_ts));
208+ scope, xs, x_shapes, * x_datas, ys, y_shapes, & jacobian_ts));
210209
211210 // Initialize numeric Jacobian to zeros.
212211 std::vector<Tensor> jacobian_ns;
213- InitJacobians<T>(xs, x_shapes, y_shapes, jacobian_ns);
212+ InitJacobians<T>(xs, x_shapes, y_shapes, & jacobian_ns);
214213
215214 // Compute numeric Jacobian.
216215 TF_RETURN_IF_ERROR (ComputeNumericJacobianTranspose<T>(
217- scope, xs, x_shapes, ys, y_shapes, 1e-3 , x_datas, jacobian_ns));
216+ scope, xs, x_shapes, ys, y_shapes, 1e-3 , x_datas, & jacobian_ns));
218217
219218 for (int i = 0 ; i < jacobian_ts.size (); i++) {
220219 // Compute the maximum error between theoretical and numeric Jacobians.
@@ -257,7 +256,7 @@ Status ComputeGradientError(const Scope& scope, const OutputList& xs,
257256 }
258257 // Compute gradient error.
259258 return ComputeGradientErrorInternal (scope, xs, x_shapes, ys, y_shapes,
260- x_datas, max_error);
259+ & x_datas, max_error);
261260}
262261
263262template <typename T>
@@ -268,7 +267,7 @@ Status ComputeGradientError(const Scope& scope, const Output& x,
268267 std::vector<Tensor> x_datas (1 , Tensor (x_init_value));
269268 // Compute gradient error.
270269 return ComputeGradientErrorInternal (scope, {x}, {x_datas[0 ].shape ()}, {y},
271- {y_shape}, x_datas, max_error);
270+ {y_shape}, & x_datas, max_error);
272271}
273272
274273#define INSTANTIATE_GRAD_ERR_TYPE (T ) \
0 commit comments