<h3 dir="auto">System information</h3>
<ul dir="auto">
<li><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>: Y</li>
<li><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>: Linux Centos 7</li>
<li><strong>TensorFlow installed from (source or binary)</strong>: Binary</li>
<li><strong>TensorFlow version (use command below)</strong>: v1.8.0-3463-g39ea5a7044 1.10.0-dev20180620</li>
<li><strong>Python version</strong>: Python 3.6.5</li>
<li><strong>Bazel version (if compiling from source)</strong>:</li>
<li><strong>GCC/Compiler version (if compiling from source)</strong>:</li>
<li><strong>CUDA/cuDNN version</strong>: Cuda 9.0</li>
<li><strong>GPU model and memory</strong>: NA</li>
<li><strong>Exact command to reproduce</strong>:</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from __future__ import absolute_import, division, print_function
import tensorflow as tf

transfer = lambda x: tf.where(x &lt; 0, 1./(1.-x), tf.log(x+1)) 

def f(x):
    xt = tf.placeholder(tf.float32, shape=(1,))
    yt  = transfer(xt)
    dydx = tf.gradients(ys=yt, xs=xt)[0]
    with tf.Session() as sess:
        rval = sess.run([xt, yt, dydx], feed_dict={xt:[x]})
    return rval

print(f(1.))  # [1, 2, nan]
print(f(-1.)) # [-1, 0.5, nan]
print(f(0.)) # [0, 0, 1] (no nan here, interestingly)"><pre class="notranslate"><code class="notranslate">from __future__ import absolute_import, division, print_function
import tensorflow as tf

transfer = lambda x: tf.where(x &lt; 0, 1./(1.-x), tf.log(x+1)) 

def f(x):
    xt = tf.placeholder(tf.float32, shape=(1,))
    yt  = transfer(xt)
    dydx = tf.gradients(ys=yt, xs=xt)[0]
    with tf.Session() as sess:
        rval = sess.run([xt, yt, dydx], feed_dict={xt:[x]})
    return rval

print(f(1.))  # [1, 2, nan]
print(f(-1.)) # [-1, 0.5, nan]
print(f(0.)) # [0, 0, 1] (no nan here, interestingly)
</code></pre></div>
<h3 dir="auto">Problem</h3>
<p dir="auto">Gradients computed through the tf.where command erroneously returns nans. Presumably this is because the gradient computation computes the gradient through both conditions, which may result in a division by zero or log of zero.</p>
<p dir="auto">When running tensorboard on Mac OS X currently, you have to specify the absolute directory to the logs path in order to get it to run.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="tensorboard --logdir /Users/myusername/Code/tensor-play/tf-play/linear_regression/logs/events"><pre class="notranslate">tensorboard --logdir /Users/myusername/Code/tensor-play/tf-play/linear_regression/logs/events</pre></div>
<p dir="auto">It should accept relative paths (like most unix commands) for convenience.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="tensorboard --logdir logs/events"><pre class="notranslate">tensorboard --logdir logs/events</pre></div>