Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate amazing Fractal patterns with TensorFlow

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to use TensorFlow to generate amazing fractal patterns, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Today, let's introduce a small project: generating fractal patterns in TensorFlow. Fractal itself is only a mathematical concept, which has nothing to do with machine learning, but through the generation of fractal, we can understand how to carry out mathematical calculation in TensorFlow and how to carry out basic process control. It is a very good training project for learning TensorFlow.

Before the start, it should be noted that TensorFlow officially provides a tutorial to generate fractal patterns (address: www.tensorflow.org/tutorials/mandelbrot). However, the images generated in the official tutorials are too ugly, and can only generate one pattern. I made some improvements to the official code, and added many types of fractals. In addition, you can not only generate images, but also produce gif animation.

Mandelbrot collection

Mandelbrot set is the most classic example of fractal. Consider the iterative formula z _ {n} ^ 2 + c (z and c are both plural). When z ^ 0 is 0, the resulting values can be composed of a sequence of c, c ^ 2 + c, (c ^ 2 + c) ^ 2 ^ c. When the sequence diverges to infinity, the corresponding points belong to the Mandelbrot set.

For example, when centering 0, it is obvious that the sequence is always 0 and does not diverge, so 0 does not belong to the Mandelbrot set.

For example, when centering 3i, the corresponding number is listed as 3i,-9cm 3i, 63-51i, 1431-647j. . The number is getting larger and larger, so 3i belongs to the Mandelbrot collection

On the two-dimensional plane, all the points that do not belong to the Mandelbrot set are marked as black, and all the points that belong to the Mandelbrot set are given different colors according to their divergence speed, and the classic image of Mandelbrot can be obtained:

The above figure is calculated entirely using TensorFlow. You should have seen a similar figure online. In TensorFlow, we define the following calculation steps:

Xs = tf.constant (Z.astype (np.complex64)) zs = tf.Variable (xs) ns = tf.Variable (tf.zeros_like (xs, tf.float32)) with tf.Session (): tf.global_variables_initializer (). Run () zs_ = tf.where (tf.abs (zs)

< R, zs**2 + xs, zs) not_diverged = tf.abs(zs_) < R step = tf.group( zs.assign(zs_), ns.assign_add(tf.cast(not_diverged, tf.float32)) for i in range(ITER_NUM): step.run() final_step = ns.eval() final_z = zs_.eval() zs就对应我们之前迭代公式的z,而xs就对应迭代公式中的c。为了方便起见,只要计算时数值的绝对值大于一个事先指定的值R,就认为其发散。每次计算使用tf.where只对还未发散的值进行计算。结合ns和zs_就可以计算颜色,得到经典的Mandelbrot图像。 Julia集合 Julia集合和Mandelbrot集合差不多,但这次我们固定c,转而计算发散的z的值。即c是固定的常数(可以任取),数列变成z,z^2+c,(z^2+c)^2 +c,…..。如果该数列发散,对应的z就属于Julia集合。对此,我们只要在原来的程序中修改两行内容,就可以生成Julia集合: xs = tf.constant(np.full(shape=Z.shape, fill_value=c, dtype=Z.dtype)) zs = tf.Variable(Z) 我们在fill_value=c处指定了Julia集合中的c值,只要使用不同的c值,就可以生成完全不同的Julia集合! 默认:c = -0.835 – 0.2321i : 将c值变为c = -0.8 * 1j ,并调整颜色(调整方法参考Github页面的说明): 选用c=0.285 + 0.01i ,图案又变得完全不同: 生成Julia集合的动画 在Julia集合中,每次都对c的值进行微小的改变,并将依次生成图片制作为gif,就可以生成如下所示的动画,对应的代码为julia_gif.py: 这里由于上传gif有大小限制的关系,只展示了一个小尺寸的动画图像。程序中提供了一个width参数,可以修改它以生成更大尺寸,质量更高的动画图像。 探索Mandelbrot集合 (注意:下面的图片可能对密集恐惧症患者不太友好。。。因此慎重翻页。。) 在前面生成的Mandelbrot集合中,我们可以将图像放大,选取某些区域进行生成,就可以得到格式各样造型迥异的分形图案,对应的程序为mandelbrot_area.py。 在Mandelbrot集合中,有很多地方图案比较奇特,如下图中的9个位置。

The place numbered 2 is called "Elephant Valley" because the pattern here is very similar to that of an elephant, and you can get an image of the area by running mandelbrot_area.py directly:

The place numbered 3 is called "Triple Spiral Valley" (triple helix). Change the coordinate position in mandelbrot_area.py to (ratio adjusts the color):

Start_x =-0.090 # x range end_x =-0.086 start_y = 0.654 # y range end_y = 0.657 width = 1000 ratio1, ratio2, ratio3 = 0.2,0.6,0.6

You can get the pattern of the place:

The place numbered 1 is called "Seahorse Valley" (seahorse valley), and the corresponding coordinates are:

Start_x =-0.750 # x range end_x =-0.747 start_y = 0.099 # y range end_y = 0.102 width = 1000 ratio1, ratio2, ratio3 = 0.1,0.1,0.3

The image is as follows, which is a little bit similar to the seahorse.

Generate more patterns

The project provides two jupyter notebook:Mandelbrot.ipynb and Julia.ipynb for more convenient exploration of Mandelbrot collection and Julia collection.

On how to use TensorFlow to generate amazing fractal patterns to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report