Tensorflow-forming-graphs

提供:Dev Guides
移動先:案内検索

TensorFlow-グラフの形成

偏微分方程式(PDE)は微分方程式であり、いくつかの独立変数の未知の関数を持つ偏微分を含みます。 偏微分方程式を参照して、新しいグラフの作成に焦点を当てます。

寸法500 * 500平方の池があると仮定します-

*N = 500*

次に、偏微分方程式を計算し、それを使用してそれぞれのグラフを作成します。 グラフを計算するための以下の手順を検討してください。

  • ステップ1 *-シミュレーション用のライブラリをインポートします。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
  • ステップ2 *-2D配列を畳み込みカーネルに変換し、2D畳み込み演算を簡素化するための関数を含めます。
def make_kernel(a):
   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([[return simple_conv(x, laplace_k)

sess = tf.InteractiveSession()
  • ステップ3 *-反復回数を含めてグラフを計算し、それに応じてレコードを表示します。
N = 500

# Initial Conditions -- some rain drops hit a pond

# Set everything to zero
u_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 points
for n in range(100):
   a,b = np.random.randint(0, N, 2)
   u_init[a,b] = np.random.uniform()

plt.imshow(u_init)
plt.show()

# Parameters:
# eps -- time resolution
# damping -- wave damping
eps = tf.placeholder(tf.float32, shape = ())
damping = tf.placeholder(tf.float32, shape = ())

# Create variables for simulation state
U = tf.Variable(u_init)
Ut = tf.Variable(ut_init)

# Discretized PDE update rules
U_ = U + eps *Ut
Ut_ = Ut + eps* (laplace(U) - damping * Ut)

# Operation to update the state
step = tf.group(U.assign(U_), Ut.assign(Ut_))

# Initialize state to initial conditions
tf.initialize_all_variables().run()

# Run 1000 steps of PDE
for i in range(1000):
   # Step simulation
   step.run({eps: 0.03, damping: 0.04})

   # Visualize every 50 steps
   if i % 500 == 0:
      plt.imshow(U.eval())
      plt.show()

グラフは以下のようにプロットされます-

グラフの形成

プロットされたグラフ