ENH: add Relu6GradGrad (#13268)
* ENH: add Relu6GradGrad * TST: add test case * CLN: import nn_grad * TST: add init value
This commit is contained in:
parent
b3d5ec90bc
commit
c0ca50a477
@ -4201,6 +4201,19 @@ cuda_py_test(
|
|||||||
main = "client/session_benchmark.py",
|
main = "client/session_benchmark.py",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cuda_py_test(
|
||||||
|
name = "nn_grad_test",
|
||||||
|
size = "small",
|
||||||
|
srcs = ["ops/nn_grad_test.py"],
|
||||||
|
additional_deps = [
|
||||||
|
":client_testlib",
|
||||||
|
":framework_for_generated_wrappers",
|
||||||
|
":nn_grad",
|
||||||
|
":nn_ops",
|
||||||
|
"//third_party/py/numpy",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
py_library(
|
py_library(
|
||||||
name = "tf_item",
|
name = "tf_item",
|
||||||
srcs = [
|
srcs = [
|
||||||
|
@ -352,6 +352,13 @@ def _Relu6Grad(op, grad):
|
|||||||
return gen_nn_ops._relu6_grad(grad, op.outputs[0]) # pylint: disable=protected-access
|
return gen_nn_ops._relu6_grad(grad, op.outputs[0]) # pylint: disable=protected-access
|
||||||
|
|
||||||
|
|
||||||
|
@ops.RegisterGradient("Relu6Grad")
|
||||||
|
def _Relu6GradGrad(op, grad):
|
||||||
|
x = op.inputs[1]
|
||||||
|
return (gen_nn_ops._relu6_grad(grad, x), array_ops.zeros(
|
||||||
|
shape=array_ops.shape(x), dtype=x.dtype))
|
||||||
|
|
||||||
|
|
||||||
@ops.RegisterGradient("Elu")
|
@ops.RegisterGradient("Elu")
|
||||||
def _EluGrad(op, grad):
|
def _EluGrad(op, grad):
|
||||||
return gen_nn_ops._elu_grad(grad, op.outputs[0])
|
return gen_nn_ops._elu_grad(grad, op.outputs[0])
|
||||||
|
48
tensorflow/python/ops/nn_grad_test.py
Normal file
48
tensorflow/python/ops/nn_grad_test.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
# ==============================================================================
|
||||||
|
"""Tests for Python ops defined in nn_grad.py."""
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import division
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from tensorflow.python.framework import constant_op
|
||||||
|
from tensorflow.python.framework import dtypes
|
||||||
|
from tensorflow.python.ops import gradient_checker
|
||||||
|
from tensorflow.python.ops import gradients_impl
|
||||||
|
from tensorflow.python.ops import nn_grad
|
||||||
|
from tensorflow.python.ops import nn_ops
|
||||||
|
from tensorflow.python.platform import test
|
||||||
|
|
||||||
|
|
||||||
|
class Relu6OpTest(test.TestCase):
|
||||||
|
def testRelu6GradGrad(self):
|
||||||
|
inputs = constant_op.constant([[-2, -1, 1, 3], [5, 7, 8, 9]],
|
||||||
|
dtype=dtypes.float32)
|
||||||
|
x_init_value = np.array([[-3.5, -1.5, 2, 4], [4.5, 7.5, 8.5, 11]])
|
||||||
|
r = nn_ops.relu6(inputs)
|
||||||
|
r_g = gradients_impl.gradients(r, inputs)[0]
|
||||||
|
with self.test_session():
|
||||||
|
error = gradient_checker.compute_gradient_error(
|
||||||
|
inputs, inputs.get_shape().as_list(),
|
||||||
|
r_g, r_g.get_shape().as_list(),
|
||||||
|
x_init_value=x_init_value)
|
||||||
|
self.assertLess(error, 1e-4)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test.main()
|
Loading…
x
Reference in New Issue
Block a user