TensorFlow 不僅僅可以用于機(jī)器學(xué)習(xí)。在此教程中,我們所舉的例子(較為尋常)是使用 TensorFlow 模擬偏微分方程的行為(https://en.wikipedia.org/wiki/Partial_differential_equation)。我們將模擬幾個(gè)雨滴落在方形池塘水面的情形。
基本設(shè)置
需要導(dǎo)入一些庫(kù)。
#Import libraries for simulationimport tensorflow as tfimport numpy as np#Imports for visualizationimport PIL.Imagefrom io import BytesIOfrom IPython.display import clear_output, Image, display
將池塘水面的狀態(tài)顯示為圖像的函數(shù)。
def DisplayArray(a, fmt='jpeg', rng=[0,1]): """Display an array as a picture.""" a = (a - rng[0])/float(rng[1] - rng[0])*255 a = np.uint8(np.clip(a, 0, 255)) f = BytesIO() PIL.Image.fromarray(a).save(f, fmt) clear_output(wait = True) display(Image(data=f.getvalue()))
接下來(lái),我們發(fā)起一個(gè)互動(dòng)式 TensorFlow 會(huì)話,以方便練習(xí)。如果我們使用可執(zhí)行的 .py 文件進(jìn)行模擬,則常規(guī)會(huì)話一樣可行。
sess = tf.InteractiveSession()
計(jì)算便利函數(shù)
def make_kernel(a):
"""Transform a 2D array into a convolution kernel""" a = np.asarray(a) a = a.reshape(list(a.shape) + [1,1]) return tf.constant(a, dtype=1)def simple_conv(x, k): """A simplified 2D convolution operation""" x = tf.expand_dims(tf.expand_dims(x, 0), -1) y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding='SAME') return y[0, :, :, 0]def laplace(x): """Compute the 2D laplacian of an array""" laplace_k = make_kernel([[0.5, 1.0, 0.5], [1.0, -6., 1.0], [0.5, 1.0, 0.5]]) return simple_conv(x, laplace_k)
定義 PDE
我們的池塘是一個(gè)完美的 500 x 500 正方形,就像自然界中的大多數(shù)池塘一樣。
N = 500
接下來(lái),我們創(chuàng)建池塘,并在其表面落入一些雨滴。
# Initial Conditions -- some rain drops hit a pond
# Set everything to zerou_init = np.zeros([N, N], dtype=np.float32)ut_init = np.zeros([N, N], dtype=np.float32)# Some rain drops hit a pond at random pointsfor n in range(40): a,b = np.random.randint(0, N, 2) u_init[a,b] = np.random.uniform()DisplayArray(u_init, rng=[-0.1, 0.1])
現(xiàn)在,我們指定微分方程的詳細(xì)信息。
# eps -- time resolution# damping -- wave dampingeps = tf.placeholder(tf.float32, shape=())damping = tf.placeholder(tf.float32, shape=())# Create variables for simulation stateU = tf.Variable(u_init)Ut = tf.Variable(ut_init)# Discretized PDE update rulesU_ = U + eps * UtUt_ = Ut + eps * (laplace(U) - damping * Ut)# Operation to update the statestep = tf.group( U.assign(U_), Ut.assign(Ut_))
運(yùn)行模擬
情況變得有趣起來(lái) - 使用簡(jiǎn)單的 for 循環(huán)讓其持續(xù)運(yùn)行。
# Initialize state to initial conditions
tf.global_variables_initializer().run()# Run 1000 steps of PDEfor i in range(1000): # Step simulation step.run({eps: 0.03, damping: 0.04}) DisplayArray(U.eval(), rng=[-0.1, 0.1])
-
機(jī)器學(xué)習(xí)
+關(guān)注
關(guān)注
66文章
8488瀏覽量
134011 -
tensorflow
+關(guān)注
關(guān)注
13文章
330瀏覽量
61010
原文標(biāo)題:TensorFlow 不僅用于機(jī)器學(xué)習(xí),還能模擬偏微分方程
文章出處:【微信號(hào):tensorflowers,微信公眾號(hào):Tensorflowers】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
Matlab偏微分方程工具箱應(yīng)用說(shuō)明
Matlab求解微分方程(ODEs/PDEs)
計(jì)算機(jī)Fortran編程實(shí)現(xiàn)偏微分方程拉普拉斯變換
偏微分方程式數(shù)值解之計(jì)算機(jī)算法詳解
偏微分方程式數(shù)值解之計(jì)算機(jī)Fortran算法詳解附圖(續(xù)i)
偏微分方程式數(shù)值解之計(jì)算機(jī)Fortran算法詳解附圖(續(xù)ii)
常微分方程的MAtLAB解法
常微分方程復(fù)習(xí),常微分方程pdf
微分方程式的建立與求解
基于偏微分方程的閃光照相圖像修補(bǔ)算法
基于分?jǐn)?shù)階偏微分方程在圖像處理中應(yīng)用設(shè)計(jì)
異性四階偏微分方程耦合二階偏微分方程的圖像放大算法
模擬集成電路之頻率響應(yīng)分析零極點(diǎn)

谷歌AI:學(xué)習(xí)更好的偏微分方程仿真方法
Matlab/Simulink建模詳解:一階時(shí)變偏微分方程的求解

評(píng)論