I'm trying to learn tensorflow from working examples online but came across the example where i'm literally wondered how it works. Can any explain the maths behind this particular function of tensorflow and how [ns] get its value out of boolean data type.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
Y, X = np.mgrid[-2.3:2.3:0.005, -5:5:0.005]
Z = X+1j*Y
c = tf.constant(Z, np.complex64)#.astype(np.complex64))
zs = tf.Variable(c)
ns = tf.Variable(tf.zeros_like(c, tf.float32))
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
zs_ = zs*zs + c
not_diverged = tf.abs(zs_) > 4
step = tf.group(zs.assign(zs_),
ns.assign_add(tf.cast(not_diverged, tf.float32)))
nx = tf.reduce_sum(ns)
zx = tf.reduce_sum(zs_)
cx = tf.reduce_sum(c)
zf = tf.reduce_all(not_diverged)
for i in range(200):
step.run()
print(sess.run([nx,zx,cx,zf]))
plt.imshow(ns.eval())
plt.show()