Repository: tensorflow
Issue: Numerical instability of gradient calculation of tf.norm (nan at 0, inf for small values) 
Labels: stat:contribution welcome

### System information
- **Have I written custom code (as opposed to using a stock example script provided in TensorFlow)**: Yes see below
- **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**: Mac OS X 10.11.6
- **TensorFlow installed from (source or binary)**: binary
- **TensorFlow version (use command below)**: #v1.2.0-5-g435cdfc    1.2.1
- **Python version**: 3.6
- **Bazel version (if compiling from source)**:
- **CUDA/cuDNN version**: On CPU
- **GPU model and memory**:
- **Exact command to reproduce**: tf.norm at [0,0] see below for code

```
import numpy as np
import tensorflow as tf
print(tf.GIT_VERSION, "  ", tf.VERSION) #v1.2.0-5-g435cdfc    1.2.1

X = tf.placeholder(tf.float32, shape=(4,None))
Z = tf.norm(X, ord='euclidean', axis=1, name='logit')
var_grad = tf.gradients(Z, [X])

with tf.Session() as sess:
    X_ = np.array([
        [1],  # Grad OK
        [0],  # Grad NaN
        [1e-16],  # Grad OK
        [1e-19] #Grad Inf
    ], dtype=np.float32)
    sess.run(tf.global_variables_initializer())
    print(sess.run((Z, var_grad), feed_dict={X: X_}))
    # Result:
    #(array([9.99999940e-01, 0.00000000e+00, 9.99999951e-17,
    #        0.00000000e+00], dtype=float32), [array([[1.00000012],
    #                                                 [nan],
    #                                                 [1.],
    #                                                 [inf]], dtype=float32)])
```

### Describe the problem
`nan` is calculated for the gradient of `tf.norm` at zero values. For extremely small values `inf` is calculated. Note that the exact result should be 1 in all cases above. 

Above is a minimal example to reproduce it. The problem occurred in a real world scenario, when implementing a custom loss function (the entropy in https://arxiv.org/abs/1611.01449) and two embeddings where too close to each other (distance practically 0).

### Source code / logs
See above 

#### Output of logfile
```
== cat /etc/issue ===============================================
Darwin Olivers-MBP-5.fritz.box 15.6.0 Darwin Kernel Version 15.6.0: Tue Apr 11 16:00:51 PDT 2017; root:xnu-3248.60.11.5.3~1/RELEASE_X86_64 x86_64
Mac OS X 10.11.6

== are we in docker =========================================  echo == are we in docker ====================================num echo == are we in docker =========================================  ec==  echo == are we in docker =======================================c++ --version

== uname -a =====================================================
Darwin Olivers-MBP-5.fritz.box 15.6.0 Darwin Kernel Version 15.6.0: Tue Apr 11 16:00:51 PDT 2017; root:xnu-3248.60.11.5.3~1/RELEASE_X86_64 x86_64

== check pips ===================================================
numpy (1.13.0)
protobuf (3.3.0)
tensorflow (1.2.1)

== check for virtualenv ==============  echo == check for virtualenv =====on_b echo == check fo sys  echo == check for virtualenv ============== echo == check for virtualenv ============================================

== cat /etc/issue ===============================================
Darwin Olivers-MBP-5.fritz.box 15.6.0 Darwin Kernel Version 15.6.0: Tue Apr 11 16:00:51 PDT 2017; root:xnu-3248.60.11.5.3~1/RELEASE_X86_64 x86_64
Mac OS X 10.11.6

== are we in docker =============================================
No

== compiler =====================================================
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

== uname -a =====================================================
Darwin Olivers-MBP-5.fritz.box 15.6.0 Darwin Kernel Version 15.6.0: Tue Apr 11 16:00:51 PDT 2017; root:xnu-3248.60.11.5.3~1/RELEASE_X86_64 x86_64

== check pips ===================================================
numpy (1.13.0)
protobuf (3.3.0)
tensorflow (1.2.1)

== check for virtualenv =========================================
True

== tensorflow import ============================================
tf.VERSION = 1.2.1
tf.GIT_VERSION = v1.2.0-5-g435cdfc
tf.COMPILER_VERSION = v1.2.0-5-g435cdfc
Sanity check: array([1], dtype=int32)

== env ==========================================================
LD_LIBRARY_PATH is unset
DYLD_LIBRARY_PATH is unset

== nvidia-smi ===================================================
tf_env_collect.sh.txt: line 105: nvidia-smi: command not found

== cuda libs  ===================================================
```