Last active
September 4, 2018 12:56
-
-
Save riccardomurri/eff3b3eb4219455bffa52cbeaca34a16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
""" | |
Non-working example implementation of the Mandelbort fractal | |
using the TensorFlow API. | |
""" | |
import numpy as np | |
import tensorflow as tf | |
from tensorflow import ( | |
constant as C_, | |
Variable as V_, | |
placeholder as X_, | |
) | |
def mandelbrot(x, y): | |
""" | |
Compute number of iterations of the Mandelbrot function at (x,y). | |
""" | |
g, in_, out_ = mandelbrot_() | |
x_in, y_in = in_ | |
n_out, x_out, y_out = out_ | |
with tf.Session(graph=g).as_default() as session: | |
# initialize vars with null values | |
feed0 = { x_in:0.0, y_in:0.0 } | |
session.run(n_out.initializer, feed0) | |
session.run(x_out.initializer, feed0) | |
session.run(y_out.initializer, feed0) | |
# run the graph at the chosen point | |
feed = { x_in:x, y_in:y } | |
n_out, x_out, y_out = session.run(out_, feed) | |
print("({0},{1}): {2}".format(x, y, [n_out, x_out, y_out])) | |
def mandelbrot_(maxiter=255): | |
""" | |
Return graph computing the Mandelbrot set at (x,y). | |
""" | |
graph = tf.Graph() | |
with graph.as_default(): | |
# input placeholders | |
x = X_(tf.float32, shape=[], name='x_in') | |
y = X_(tf.float32, shape=[], name='y_in') | |
# output variables | |
n_ = V_(0, tf.int32, name='n') | |
x_ = V_(x, tf.float32, name='x') | |
y_ = V_(y, tf.float32, name='y') | |
# main loop | |
i_ = tf.constant(0) | |
def cond(i_, z_re_, z_im_): | |
return tf.logical_and( | |
tf.less(i_, maxiter), | |
(z_re_*z_re_ + z_im_*z_im_) < 4) | |
def body(i_, z_re_, z_im_): | |
return [ | |
i_+1, # iteration count | |
z_re_*z_re_ - z_im_*z_im_ + x, # real part of z | |
2*z_re_*z_im_ + y, # imag part of z | |
] | |
l = tf.while_loop(cond, body, [i_, x, y], | |
parallel_iterations=1) | |
with tf.control_dependencies(l): | |
n_.assign(l[0]) | |
x_.assign(l[1]) | |
y_.assign(l[2]) | |
return ( | |
graph, # graph | |
(x, y), # inputs | |
(n_, x_, y_) # outputs | |
) | |
# | |
# main | |
# | |
if __name__ == '__main__': | |
mandelbrot(0.25, -0.15) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
""" | |
Working example implementation of the Mandelbort fractal | |
using the TensorFlow API. | |
""" | |
import numpy as np | |
import tensorflow as tf | |
from tensorflow import ( | |
constant as C_, | |
Variable as V_, | |
placeholder as X_, | |
) | |
def mandelbrot(x, y): | |
""" | |
Compute number of iterations of the Mandelbrot function at (x,y). | |
""" | |
g, in_, out_ = mandelbrot_() | |
x_in, y_in = in_ | |
n_out, x_out, y_out = out_ | |
with tf.Session(graph=g).as_default() as session: | |
# (a) | |
# run the graph at the chosen point | |
feed = { x_in:x, y_in:y } | |
n_out, x_out, y_out = session.run(out_, feed) | |
print("({0},{1}): {2}".format(x, y, [n_out, x_out, y_out])) | |
def mandelbrot_(maxiter=255): | |
""" | |
Return graph computing the Mandelbrot set at (x,y). | |
""" | |
graph = tf.Graph() | |
with graph.as_default(): | |
# input placeholders | |
x = X_(tf.float32, shape=[], name='x_in') | |
y = X_(tf.float32, shape=[], name='y_in') | |
# output variables | |
n_ = V_(0, tf.int32, name='n') | |
x_ = V_(x, tf.float32, name='x') | |
y_ = V_(y, tf.float32, name='y') | |
# main loop | |
i_ = tf.constant(0) | |
def cond(i_, z_re_, z_im_): | |
return tf.logical_and( | |
tf.less(i_, maxiter), | |
(z_re_*z_re_ + z_im_*z_im_) < 4) | |
def body(i_, z_re_, z_im_): | |
return [ | |
i_+1, # iteration count | |
z_re_*z_re_ - z_im_*z_im_ + x, # real part of z | |
2*z_re_*z_im_ + y, # imag part of z | |
] | |
l = tf.while_loop(cond, body, [i_, x, y], | |
parallel_iterations=1) | |
# (b) | |
n_, x_, y_ = l | |
return ( | |
graph, # graph | |
(x, y), # inputs | |
(n_, x_, y_) # outputs | |
) | |
# | |
# main | |
# | |
if __name__ == '__main__': | |
mandelbrot(0.25, -0.15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment