Can tensorflow's mod operator match python's modulo implementation?
<p>Python's <code>%</code> operator always returns a number with the same sign as the divisor (second argument), for example:</p>

<pre><code>-7.0 % 3.0 -&gt; 2.0
</code></pre>

<p>However, Tensorflow's mod operator seems to be implemented slightly differently:</p>

<pre><code>tf.mod(-7.0, 3.0).eval() -&gt; -1.0
</code></pre>

<p>How can I get Tensorflow to return the same value as the python implementation?</p>

<pre><code>import tensorflow as tf


def main():

    v_num = -7.0
    v_div = 3.0

    mod_tf = tf.mod(v_num, v_div)
    mod_py = v_num % v_div

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        print('TF: {} % {} = {}'.format(v_num, v_div, mod_tf.eval()))
        print('PY: {} % {} = {}'.format(v_num, v_div, mod_py))

if __name__ == "__main__":
    main()
</code></pre>

<p>Interesting finding. Maybe worth filing a github issue here: <a href="https://github.com/tensorflow/tensorflow/issues" rel="nofollow">https://github.com/tensorflow/tensorflow/issues</a></p>

<p>For a workaround, I think you can use this line:</p>

<pre><code>mod_tf = tf.cond(mod_tf &lt; 0, lambda: mod_tf+v_div, lambda: mod_tf) 
</code></pre>

python tensorflow