TensorFlow Tutorial-Mandelbrot Set (Translation)

TensorFlow Tutorial (Mandelbrot Set) https://www.tensorflow.org/versions/master/tutorials/mandelbrot/index.html#mandelbrot-set It is a translation of. We look forward to pointing out any translation errors.


Visualization of the Mandelbrot set is not machine learning, but it is a fun example of how to use TensorFlow for general math. This is actually a fairly simple implementation of visualization, but it keeps the point. (Later, we may provide a more elaborate implementation below to produce a more really beautiful image.)

Note: This tutorial was originally prepared for the IPython notebook.

basic configuration

You need some imports to get started.

# Import libraries for simulation
import tensorflow as tf
import numpy as np

# Imports for visualization
import PIL.Image
from cStringIO import StringIO
from IPython.display import clear_output, Image, display
import scipy.ndimage as nd

Defines a function that receives an iteration count and actually displays the image.

def DisplayFractal(a, fmt='jpeg'):
  """Display an array of iteration counts as a
     colorful picture of a fractal."""
  a_cyclic = (6.28*a/20.0).reshape(list(a.shape)+[1])
  img = np.concatenate([10+20*np.cos(a_cyclic),
                        30+50*np.sin(a_cyclic),
                        155-80*np.cos(a_cyclic)], 2)
  img[a==a.max()] = 0
  a = img
  a = np.uint8(np.clip(a, 0, 255))
  f = StringIO()
  PIL.Image.fromarray(a).save(f, fmt)
  display(Image(data=f.getvalue()))

Session and variable initialization

I often use interactive sessions to tinker, but it works the same for regular sessions.

   sess = tf.InteractiveSession()

NumPy and TensorFlow can be mixed freely, which is convenient.

# Use NumPy to create a 2D array of complex numbers on [-2,2]x[-2,2]

Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]
Z = X+1j*Y

Defines and initializes a TensorFlow tensor.

xs = tf.constant(Z.astype("complex64"))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, "float32"))

In TensorFlow, variables must be explicitly initialized before they can be used.

tf.initialize_all_variables().run()

Definition and execution of calculations

Specify multiple calculations ...

# Compute the new values of z: z^2 + x
zs_ = zs*zs + xs

# Have we diverged with this new value?
not_diverged = tf.complex_abs(zs_) < 4

# Operation to update the zs and the iteration count.
#
# Note: We keep computing zs after they diverge! This
#       is very wasteful! There are better, if a little
#       less simple, ways to do this.
#
step = tf.group(
  zs.assign(zs_),
  ns.assign_add(tf.cast(not_diverged, "float32"))
  )

... and do it 200 steps

for i in range(200): step.run()

Let's take a look at what we got.

DisplayFractal(ns.eval())

図

Not bad!

Recommended Posts

TensorFlow Tutorial-Mandelbrot Set (Translation)
TensorFlow Tutorial-TensorFlow Mechanics 101 (Translation)
TensorFlow Tutorial-Image Recognition (Translation)
TensorFlow Tutorial-MNIST Data Download (Translation)
TensorFlow Tutorial-Sequence Transformation Model (Translation)
TensorFlow Tutorial-Partial Differential Equations (Translation)
TensorFlow Tutorial-Convolutional Neural Network (Translation)
TensorFlow MNIST For ML Beginners Translation
TensorFlow Tutorial-Vector Representation of Words (Translation)
TensorFlow Deep MNIST for Experts Translation