曼德布洛特(Mandelbrot)集合
来源:互联网 发布:淘宝上的yizi是正品吗 编辑:程序博客网 时间:2024/06/05 18:59
说明:使用python3.6、tensorflow1.3,windows环境。
# 导入仿真库
import tensorflow as tfimport numpy as np
# 导入可视化库
import PIL.Image #python3.6安装pillow库
# from cStringIO import StringIO #改成下面的
from io import BytesIO
from IPython.display import clear_output, Image, display
import scipy.ndimage as nd
#显示图形
import matplotlib.pyplot as plt
# 现在我们将定义一个函数来显示迭代计算出的图像
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 = StringIO() py3改写
f = BytesIO()
PIL.Image.fromarray(a).save(f, fmt)
display(Image(data=f.getvalue()))
# 会话(session)和变量(variable)初始化
sess = tf.InteractiveSession()
# 使用NumPy创建一个在[-2,2]x[-2,2]范围内的2维复数数组
Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]
Z = X+1j*Y
# 现在我们定义并初始化一组TensorFlow的张量 (tensors)
xs = tf.constant(Z.astype("complex64"))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, "float32"))
# TensorFlow在使用之前需要你明确给定变量的初始值
#tf.initialize_all_variables().run()
tf.global_variables_initializer().run()
# 定义并运行计算
# 计算一个新值z: z^2 + x
zs_ = zs*zs + xs
# 这个新值会发散吗?
#not_diverged = tf.complex_abs(zs_) < 4
not_diverged = tf.abs(zs_) < 4
# 更新zs并且迭代计算。
# 说明:在这些值发散之后,我们仍然在计算zs,这个计算消耗特别大!
# 如果稍微简单点,这里有更好的方法来处理。
step = tf.group(
zs.assign(zs_),
ns.assign_add(tf.cast(not_diverged, "float32"))
)
# 继续执行几百个步骤
for i in range(200): step.run()
# 让我们看看我们得到了什么
# DisplayFractal(ns.eval()) 这一行改成下面的将图形显示出来
plt.imshow(ns.eval())
plt.show()
阅读全文
0 0
- 曼德布洛特(Mandelbrot)集合
- Tensorflow教程-曼德布洛特(Mandelbrot)集合
- TensorFlow实现Mandelbrot Set(曼德布洛特集合)
- 曼德勃罗(Mandelbrot)集合与其编程实现
- 【Python】科学计算(一)——Mandelbrot集合
- OpenMP并行化实例----Mandelbrot集合并行化计算
- Mandelbrot集
- Mandelbrot图像
- Mandelbrot集
- Mandelbrot set
- Mandelbrot集
- 悼念 Benoit MandelBrot
- Mandelbrot 并行实现
- Mandelbrot命令行实现
- [原创+分享]Mandelbrot Explorer
- C#---Mandelbrot---ShinePans
- 曼德勃罗集(Mandelbrot Set)
- Qt示例[Mandelbrot Example]
- SpringCloud: 服务链路追踪(Spring Cloud Sleuth)
- FFmpeg源代码分析:libswscale的sws_getContext()
- ArrayList
- sklearn中高斯混合模型
- unity VRTK拿起物体设置成碰撞物体的子物体
- 曼德布洛特(Mandelbrot)集合
- boost之pool
- 软件公司该怎么接项目
- Java程序执行超时——Future接口介绍
- 欢迎使用CSDN-markdown编辑器
- 初识Hibernate:Hibernate初步应用与基本配置
- 如何控制线程并发数,如何使用信号池和线程池?
- Python BeautifulSoup4 我碰到的一些不常用的用法
- 使用dom4j解析xml标签,将标签属性与文本内容添加进集合