
- TensorFlow 教程
- TensorFlow - 首页
- TensorFlow - 简介
- TensorFlow - 安装指南
- 了解人工智能
- 数学基础
- 机器学习与深度学习
- TensorFlow - 基础知识
- 卷积神经网络
- 循环神经网络
- TensorBoard 可视化
- TensorFlow - 词嵌入
- 单层感知器
- TensorFlow - 线性回归
- TFLearn 及其安装指南
- CNN 和 RNN 的区别
- TensorFlow - Keras
- TensorFlow - 分布式计算
- TensorFlow - 导出
- 多层感知器学习
- 感知器隐含层
- TensorFlow - 优化器
- TensorFlow - XOR 实现
- 梯度下降优化
- TensorFlow - 绘制图
- 使用 TensorFlow 进行图像识别
- 神经网络训练建议
- TensorFlow 有用资源
- TensorFlow - 快速指南
- TensorFlow - 有用资源
- TensorFlow - 讨论
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([[0.5, 1.0, 0.5], [1.0, -6., 1.0], [0.5, 1.0, 0.5]]) 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()
绘制的图如下所示 −


广告