TensorFlow实现Mandelbrot Set(曼德布洛特集合)

import tensorflow as tf

import numpy as np

import PIL.Image

from io import BytesIO

from IPython.display import Image, display


def DisplayFractal(a, fmt = 'jpeg'):

    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 = BytesIO()

    PIL.Image.fromarray(a).save(f, fmt)

    display(Image(data=f.getvalue()))


sess = tf.InteractiveSession()


# Use NumPy to create a 2D array of complex numbers

Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]

Z = X+1j*Y


xs = tf.constant(Z.astype(np.complex64))

zs = tf.Variable(xs)

ns = tf.Variable(tf.zeros_like(xs, tf.float32))



tf.initialize_all_variables().run()

# Compute the new values of z: z^2 + x

zs_ = zs*zs + xs



# Have we diverged with this new value?
not_diverged = tf.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, tf.float32)))



for i in range(200):

    step.run()


DisplayFractal(ns.eval())

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值