【问题标题】:Tensorflow PDE Tutorial QuestionsTensorFlow PDE 教程问题
【发布时间】:2016-10-26 23:17:57
【问题描述】:

我想知道如何让 Tensorflow 只更新特定的矩阵元素?以下代码来自 Tensorflow 教程 (https://www.tensorflow.org/versions/r0.11/tutorials/pdes/index.html#partial-differential-equations)。

  #Import libraries for simulation
  import tensorflow as tf
  import numpy as np

  #Imports for visualization
  import PIL.Image

  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))
    with open("fig/image.jpg","w") as f:
        PIL.Image.fromarray(a).save(f, "jpeg")

  #sess = tf.Session()
  sess = tf.InteractiveSession()

  # Computational Convenience Functions

  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)

  # Define the PDE

  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(40):
    a,b = np.random.randint(0, N, 2)
    u_init[a,b] = np.random.uniform()

  DisplayArray(u_init, rng=[-0.1, 0.1])

  # 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})
    DisplayArray(U.eval(), rng=[-0.1, 0.1])

step = tf.group(U.assign(U_),Ut.assign(Ut_)),我想知道是否可以只更新 U_[1:-1, 1:-1] 和 Ut_[1:-1, 1:-1] 内的值,以及将其余值保持为常数。

非常感谢!

【问题讨论】:

  • 该错误似乎与您的问题无关。您需要在第一次使用 TensorFlow 变量之前对其进行初始化。假设您有一个名为 session 的 Tensorflow 会话,请尝试在您第一次真正调用 session.run() 之前添加一些类似于 session.run(tf.initialize_all_variables()) 的代码。
  • @Peter Hawkins 感谢您的评论。更具体地说,我更新了我的问题。我实际上想更新U_[1:-1, 1:-1] 中的值,并保持其余矩阵值不变。

标签: tensorflow


【解决方案1】:

您可以在 Tensorflow 中执行切片赋值。试试这样的:

assign_op = U[1:-1,1:-1].assign(U_[1:-1, 1:-1])

(具体指标由您决定。)

【讨论】:

  • 我试过了:step = tf.group(U[1:-1,1:-1].assign(U_[1:-1,1:-1]),Ut[1:-1,1:-1].assign(Ut_[1:-1,1:-1])),但它不起作用。它返回一些错误,例如Caused by op u'Variable_1/read', defined at: File "01_TF_Tutorial_PDE_test01.py", line 64, in <module> Ut = tf.Variable(ut_init)
  • 你能显示完整的错误信息吗?你使用的是什么版本的 Tensorflow?
  • 这里是完整输出错误消息link的链接。非常感谢。
  • GPU 设备上似乎还不支持切片分配。我收集它正在被添加,应该会在接下来的几天内出现在 github 上。同时,您可以在 CPU 上运行您的代码(使用 with tf.device("CPU"):,或者通过切出您未更改的原始部分(使用 U_[0:1, 0:1] 或类似方法)来解决问题,然后将其与你正在改变的位(使用tf.concat
猜你喜欢
  • 2019-09-06
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 2011-03-17
相关资源
最近更新 更多