Skip to content

Commit fb01ebb

Browse files
martinwicketensorflower-gardener
authored andcommitted
Deprecate tf.inv in favor of tf.reciprocal.
Change: 139240711
1 parent 7632193 commit fb01ebb

File tree

17 files changed

+80
-49
lines changed

17 files changed

+80
-49
lines changed

tensorflow/cc/gradients/math_grad.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Status InvGrad(const Scope& scope, const Operation& op,
5050
return scope.status();
5151
}
5252
REGISTER_GRADIENT_OP("Inv", InvGrad);
53+
REGISTER_GRADIENT_OP("Reciprocal", InvGrad);
5354

5455
Status SquareGrad(const Scope& scope, const Operation& op,
5556
const std::vector<Output>& grad_inputs,
@@ -68,7 +69,7 @@ Status SqrtGrad(const Scope& scope, const Operation& op,
6869
// y = sqrt(x)
6970
// dy/dx = 0.5 * (1 / sqrt(x)) = 0.5 * (1 / y)
7071
// dx = dy * (0.5 * (1 / y))
71-
auto y_inv = Inv(scope, op.output(0));
72+
auto y_inv = Reciprocal(scope, op.output(0));
7273
auto half = Cast(scope, Const(scope, 0.5), op.input(0).type());
7374
auto dx = Mul(scope, grad_inputs[0], Mul(scope, half, y_inv));
7475
grad_outputs->push_back(dx);
@@ -82,7 +83,7 @@ Status RsqrtGrad(const Scope& scope, const Operation& op,
8283
// y = 1/x^1/2 = x^-1/2
8384
// dy/dx = -1/2 * x^-3/2 = -1/2 * x^-1/2 * x^-1 = -1/2 * y * x^-1
8485
// dx = dy * (-1/2 * y * x^-1)
85-
auto x_inv = Inv(scope, op.input(0));
86+
auto x_inv = Reciprocal(scope, op.input(0));
8687
auto y = op.output(0);
8788
auto neghalf = Cast(scope, Const(scope, -0.5), op.input(0).type());
8889
auto a = Mul(scope, neghalf, x_inv);
@@ -110,7 +111,8 @@ Status LogGrad(const Scope& scope, const Operation& op,
110111
// f(x) = log(x) = y
111112
// df/dx = 1 / x
112113
// dx = dy * (1 / x)
113-
grad_outputs->push_back(Mul(scope, grad_inputs[0], Inv(scope, op.input(0))));
114+
grad_outputs->push_back(
115+
Mul(scope, grad_inputs[0], Reciprocal(scope, op.input(0))));
114116
return scope.status();
115117
}
116118
REGISTER_GRADIENT_OP("Log", LogGrad);
@@ -186,7 +188,7 @@ Status AsinGrad(const Scope& scope, const Operation& op,
186188
// dx = dy * (1 / (1 - x * x)^1/2)
187189
auto x2 = Square(scope, op.input(0));
188190
auto one = Cast(scope, Const(scope, 1.0), op.input(0).type());
189-
auto dydx = Inv(scope, Sqrt(scope, Sub(scope, one, x2)));
191+
auto dydx = Reciprocal(scope, Sqrt(scope, Sub(scope, one, x2)));
190192
auto dx = Mul(scope, grad_inputs[0], dydx);
191193
grad_outputs->push_back(dx);
192194
return scope.status();
@@ -201,7 +203,7 @@ Status AcosGrad(const Scope& scope, const Operation& op,
201203
// dx = dy * (- 1 / (1 - x * x)^1/2)
202204
auto x2 = Square(scope, op.input(0));
203205
auto one = Cast(scope, Const(scope, 1.0), op.input(0).type());
204-
auto dydx = Neg(scope, Inv(scope, Sqrt(scope, Sub(scope, one, x2))));
206+
auto dydx = Neg(scope, Reciprocal(scope, Sqrt(scope, Sub(scope, one, x2))));
205207
auto dx = Mul(scope, grad_inputs[0], dydx);
206208
grad_outputs->push_back(dx);
207209
return scope.status();
@@ -214,7 +216,7 @@ Status TanGrad(const Scope& scope, const Operation& op,
214216
// y = tan(x)
215217
// dy/dx = sec(x)^2 = 1 / cos(x)^2
216218
// dx = dy * (1 / cos(x)^2)
217-
auto dydx = Square(scope, Inv(scope, Cos(scope, op.input(0))));
219+
auto dydx = Square(scope, Reciprocal(scope, Cos(scope, op.input(0))));
218220
auto dx = Mul(scope, grad_inputs[0], dydx);
219221
grad_outputs->push_back(dx);
220222
return scope.status();
@@ -228,7 +230,7 @@ Status AtanGrad(const Scope& scope, const Operation& op,
228230
// dy/dx = 1 / (1 + x^2)
229231
// dx = dy * (1 / (1 + x^2)
230232
auto one = Cast(scope, Const(scope, 1.0), op.input(0).type());
231-
auto dydx = Inv(scope, Add(scope, one, Square(scope, op.input(0))));
233+
auto dydx = Reciprocal(scope, Add(scope, one, Square(scope, op.input(0))));
232234
auto dx = Mul(scope, grad_inputs[0], dydx);
233235
grad_outputs->push_back(dx);
234236
return scope.status();

tensorflow/cc/gradients/math_grad_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class CWiseUnaryGradTest : public ::testing::Test {
8484
y = Neg(scope_, x);
8585
break;
8686
case INV:
87-
y = Inv(scope_, x);
87+
y = Reciprocal(scope_, x);
8888
break;
8989
case SQUARE:
9090
y = Square(scope_, x);
@@ -157,7 +157,7 @@ TEST_F(CWiseUnaryGradTest, Neg) {
157157
TestCWiseGrad(NEG, x_fn, dy_fn, dx_fn);
158158
}
159159

160-
TEST_F(CWiseUnaryGradTest, Inv) {
160+
TEST_F(CWiseUnaryGradTest, Reciprocal) {
161161
auto x_fn = [this](const int i) { return RV({-1, 1, -2, 2, -3, 3, -4, 4}); };
162162
auto dy_fn = [this](const float x) { return RV({0, -2, 2, -3, 3, -4, 4}); };
163163
auto dx_fn = [this](const float x, const float dy) {

tensorflow/contrib/factorization/python/ops/clustering_ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ def _mini_batch_training_op(self, inputs, cluster_idx_list,
358358
cluster_center_updates -= tf.cast(
359359
tf.reshape(count_updates, broadcast_shape),
360360
inp.dtype) * old_cluster_centers
361-
learning_rate = tf.inv(tf.cast(old_counts + count_updates, inp.dtype))
361+
learning_rate = tf.reciprocal(tf.cast(old_counts + count_updates,
362+
inp.dtype))
362363
learning_rate = tf.reshape(learning_rate, broadcast_shape)
363364
# scale by 1 / (n + k), see comment above.
364365
cluster_center_updates *= learning_rate

tensorflow/contrib/labeled_tensor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
abs = _core.abs_function # pylint: disable=redefined-builtin
5454
neg = _core.neg
5555
sign = _core.sign
56-
inv = _core.inv
56+
reciprocal = _core.reciprocal
5757
square = _core.square
5858
round = _core.round_function # pylint: disable=redefined-builtin
5959
sqrt = _core.sqrt

tensorflow/contrib/labeled_tensor/python/ops/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ def op(labeled_tensor, name=None):
10981098
abs_function = define_unary_op('abs', math_ops.abs)
10991099
neg = define_unary_op('neg', math_ops.neg)
11001100
sign = define_unary_op('sign', math_ops.sign)
1101-
inv = define_unary_op('inv', math_ops.inv)
1101+
reciprocal = define_unary_op('reciprocal', math_ops.reciprocal)
11021102
square = define_unary_op('square', math_ops.square)
11031103
round_function = define_unary_op('round', math_ops.round)
11041104
sqrt = define_unary_op('sqrt', math_ops.sqrt)

tensorflow/contrib/labeled_tensor/python/ops/core_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def setUp(self):
687687
# TODO(shoyer): add unary + to core TensorFlow
688688
('pos', None, None, None),
689689
('sign', None, tf.sign, core.sign),
690-
('inv', None, tf.inv, core.inv),
690+
('reciprocal', None, tf.reciprocal, core.reciprocal),
691691
('square', None, tf.square, core.square),
692692
('round', None, tf.round, core.round_function),
693693
('sqrt', None, tf.sqrt, core.sqrt),

tensorflow/contrib/learn/python/learn/dataframe/transforms/unary_transforms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# `Series`.registered_name().
3030
UNARY_TRANSFORMS = [("__neg__", math_ops.neg),
3131
("sign", math_ops.sign),
32-
("inv", math_ops.inv),
32+
("reciprocal", math_ops.reciprocal),
3333
("square", math_ops.square),
3434
("round", math_ops.round),
3535
("sqrt", math_ops.sqrt),

tensorflow/core/ops/math_grad.cc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ REGISTER_OP_GRADIENT("Neg", NegGrad);
6262
Status InvGrad(const AttrSlice& attrs, FunctionDef* g) {
6363
// clang-format off
6464
return GradForUnaryCwise(g, {
65-
{{"y"}, "Inv", {"x"}},
65+
{{"y"}, "Reciprocal", {"x"}},
6666
{{"y2"}, "Square", {"y"}, {}, {"dy"}},
6767
{{"y2_neg"}, "Neg", {"y2"}},
6868
{{"dx"}, "Mul", {"dy", "y2_neg"}}
6969
});
7070
// clang-format on
7171
}
7272
REGISTER_OP_GRADIENT("Inv", InvGrad);
73+
REGISTER_OP_GRADIENT("Reciprocal", InvGrad);
7374

7475
Status SquareGrad(const AttrSlice& attrs, FunctionDef* g) {
7576
// clang-format off
@@ -87,7 +88,7 @@ Status SqrtGrad(const AttrSlice& attrs, FunctionDef* g) {
8788
// clang-format off
8889
return GradForUnaryCwise(g, {
8990
{{"y"}, "Sqrt", {"x"}},
90-
{{"y_inv"}, "Inv", {"y"}, {}, {"dy"}},
91+
{{"y_inv"}, "Reciprocal", {"y"}, {}, {"dy"}},
9192
FDH::Const("const", 0.5f),
9293
{{"half"}, "Cast", {"const"}, {{"SrcT", DT_FLOAT}, {"DstT", "$T"}}},
9394
{{"a"}, "Mul", {"half", "y_inv"}}, // .5 * 1/y
@@ -100,7 +101,7 @@ REGISTER_OP_GRADIENT("Sqrt", SqrtGrad);
100101
Status RsqrtGrad(const AttrSlice& attrs, FunctionDef* g) {
101102
// clang-format off
102103
return GradForUnaryCwise(g, {
103-
{{"x_inv"}, "Inv", {"x"}, {}, {"dy"}},
104+
{{"x_inv"}, "Reciprocal", {"x"}, {}, {"dy"}},
104105
{{"y"}, "Rsqrt", {"x"}},
105106
FDH::Const("const", -.5f),
106107
{{"neghalf"}, "Cast", {"const"}, {{"SrcT", DT_FLOAT}, {"DstT", "$T"}}},
@@ -125,7 +126,7 @@ REGISTER_OP_GRADIENT("Exp", ExpGrad);
125126
Status LogGrad(const AttrSlice& attrs, FunctionDef* g) {
126127
// clang-format off
127128
return GradForUnaryCwise(g, {
128-
{{"x_inv"}, "Inv", {"x"}, {}, {"dy"}},
129+
{{"x_inv"}, "Reciprocal", {"x"}, {}, {"dy"}},
129130
{{"dx"}, "Mul", {"dy", "x_inv"}}, // dy * 1/x
130131
});
131132
// clang-format on
@@ -201,7 +202,7 @@ Status AcosGrad(const AttrSlice& attrs, FunctionDef* g) {
201202
{{"one"}, "Cast", {"const"}, {{"SrcT", DT_FLOAT}, {"DstT", "$T"}}},
202203
{{"a"}, "Sub", {"one", "x2"}}, // 1 - x^2
203204
{{"b"}, "Sqrt", {"a"}},
204-
{{"inv"}, "Inv", {"b"}},
205+
{{"inv"}, "Reciprocal", {"b"}},
205206
{{"neg"}, "Neg", {"inv"}},
206207
{{"dx"}, "Mul", {"dy", "neg"}}
207208
});
@@ -217,7 +218,7 @@ Status AsinGrad(const AttrSlice& attrs, FunctionDef* g) {
217218
{{"one"}, "Cast", {"const"}, {{"SrcT", DT_FLOAT}, {"DstT", "$T"}}},
218219
{{"a"}, "Sub", {"one", "x2"}}, // 1 - x^2
219220
{{"b"}, "Sqrt", {"a"}},
220-
{{"inv"}, "Inv", {"b"}},
221+
{{"inv"}, "Reciprocal", {"b"}},
221222
{{"dx"}, "Mul", {"dy", "inv"}}
222223
});
223224
// clang-format on
@@ -231,7 +232,7 @@ Status AtanGrad(const AttrSlice& attrs, FunctionDef* g) {
231232
FDH::Const("const", 1.0f),
232233
{{"one"}, "Cast", {"const"}, {{"SrcT", DT_FLOAT}, {"DstT", "$T"}}},
233234
{{"a"}, "Add", {"one", "x2"}}, // 1 + x^2
234-
{{"inv"}, "Inv", {"a"}},
235+
{{"inv"}, "Reciprocal", {"a"}},
235236
{{"dx"}, "Mul", {"dy", "inv"}}
236237
});
237238
// clang-format on
@@ -242,7 +243,7 @@ Status TanGrad(const AttrSlice& attrs, FunctionDef* g) {
242243
// clang-format off
243244
return GradForUnaryCwise(g, {
244245
{{"cosx"}, "Cos", {"x"}},
245-
{{"secx"}, "Inv", {"cosx"}},
246+
{{"secx"}, "Reciprocal", {"cosx"}},
246247
{{"secx2"}, "Square", {"secx"}},
247248
{{"dx"}, "Mul", {"dy", "secx2"}}
248249
});

tensorflow/core/ops/math_grad_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,13 @@ TEST_F(MathGradTest, Neg) {
417417
test::ExpectClose(ans, dx);
418418
}
419419

420-
TEST_F(MathGradTest, Inv) {
420+
TEST_F(MathGradTest, Reciprocal) {
421421
auto x = test::AsTensor<float>({-3.f, -2.f, -1.f, 1.f, 2.f, 3.f},
422422
TensorShape({2, 3}));
423423
auto g = [](float x) { return -1.f / (x * x); };
424424
auto dx = test::AsTensor<float>(
425425
{g(-3.f), g(-2.f), g(-1.f), g(1.f), g(2.f), g(3.f)}, TensorShape({2, 3}));
426-
auto ans = SymGrad("Inv", x);
426+
auto ans = SymGrad("Reciprocal", x);
427427
test::ExpectClose(ans, dx);
428428
}
429429

tensorflow/core/ops/math_ops.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,23 @@ Computes numerical negative value element-wise.
217217
I.e., \\(y = -x\\).
218218
)doc");
219219

220-
REGISTER_OP("Inv").UNARY().Doc(R"doc(
220+
REGISTER_OP("Inv")
221+
.UNARY()
222+
.Doc(R"doc(
221223
Computes the reciprocal of x element-wise.
222224
I.e., \\(y = 1 / x\\).
223-
)doc");
225+
)doc")
226+
.Deprecated(17, "Use Reciprocal");
224227

225-
REGISTER_OP("InvGrad").UNARY_GRADIENT_COMPLEX().Doc(R"doc(
228+
REGISTER_OP("InvGrad")
229+
.UNARY_GRADIENT_COMPLEX()
230+
.Doc(R"doc(
226231
Computes the gradient for the inverse of `x` wrt its input.
227232
228233
Specifically, `grad = -dy * y*y`, where `y = 1/x`, and `dy`
229234
is the corresponding input gradient.
230-
)doc");
235+
)doc")
236+
.Deprecated(17, "Use ReciprocalGrad");
231237

232238
REGISTER_OP("Reciprocal").UNARY().Doc(R"doc(
233239
Computes the reciprocal of x element-wise.

0 commit comments

Comments
 (0)