强化学习笔记2-Python/OpenAI/TensorFlow/ROS-程序指令
TensorFlow
TensorFlow 是 Google 推出的开源软件库,广泛应用于数值计算、机器学习与深度学习开发。它基于可在多种平台上共享和执行的数据流图机制,适合构建高效的计算模型。

TensorFlow 在深度学习模型构建中使用非常广泛,而深度学习本身又是机器学习的重要分支。张量本质上就是多维数组,因此所谓 TensorFlow,可以理解为在计算图中流动的多维数组(张量)。安装 Anaconda 之后,配置 TensorFlow 环境会更加方便;当然,直接安装 tensorflow 也同样简单。无论您使用 Windows、Linux 还是 macOS,都可以通过下面的命令快速完成 TensorFlow 安装。
conda install -c conda-forge tensorflow
或
pip install --user tensorflow
pip3 install --user tensorflow
如需启用 GPU 加速支持,则需要安装对应的 -gpu 版本。
运行下面的 hello world 示例程序,即可检查 TensorFlow 是否安装成功。
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
hello = tf.constant("Hello World")
sess = tf.Session()
print(sess.run(hello))
显示结果如下:
b'Hello World'
变量Variables、常量Constants、占位符Placeholders
变量、常量和占位符是 TensorFlow 的基础组成部分,但初学者在学习 TensorFlow 编程时,往往容易混淆这三者。下面分别介绍它们的含义以及彼此之间的区别。
变量
变量是用于保存数值的容器,通常会作为计算图中其他操作的输入。我们可以使用 tf.Variable()函数创建 TensorFlow 变量。在下面的示例中,变量使用随机正态分布中的值进行初始化,并命名为 weights。
weights = tf.Variable(tf.random_normal([8, 9], stddev=0.1), name="weights")
不过,在定义变量之后,还需要通过 tf.global_variables_initializer()方法显式创建初始化操作,该方法会为变量分配所需资源并完成初始化。
常量
常量与变量不同,它们的值一旦确定就不能再改变。
常量在整个计算过程中保持不变。我们可以通过 tf.constant()函数来创建常量。
x = tf.constant(666)
占位符
可以把占位符理解为一种特殊变量:只定义数据类型和维度,但不立即赋值。占位符本身没有具体数值,它的值会在程序运行时动态传入。占位符提供了一个名为 shape 的可选参数,用于指定数据维度。如果 shape 设置为 none,那么在运行时就可以传入任意大小的数据。占位符通常使用 tf.placeholder()函数进行定义。
x = tf.placeholder("float", shape=None)
简单来说,我们通常使用 tf.variable 来存储数据,使用 tf.placeholder 来接收外部输入数据。
计算图(ROS中也有这个概念)Computation Graph
在 TensorFlow 中,几乎所有内容都可以表示为由节点和边组成的计算图。其中,节点表示数学运算,例如加法、乘法等;边则表示张量。计算图在资源优化方面非常高效,同时也有利于实现分布式计算与并行执行。
假设我们有节点B,其输入依赖于节点A的输出,这种类型的依赖性称为直接依赖:
A = tf.multiply(8,5)
B = tf.multiply(A,1)
当节点B不依赖于节点A进行输入时,它被称为间接依赖:
A = tf.multiply(8,5)
B = tf.multiply(4,3)
因此,只要理解了这些节点之间的依赖关系,我们就能更合理地分配独立计算任务,在现有资源条件下减少整体计算时间。每当我们导入 tensorflow 时,系统都会自动生成一个默认图,之后创建的所有节点都会关联到这个默认计算图中。
会话Sessions
仅仅定义计算图并不会真正执行运算,为了运行计算图,我们需要使用 TensorFlow 会话。 sess = tf.Session()我们可以通过 tf.Session()方法为计算图创建会话,该会话会分配内存来保存变量当前值。创建会话之后,就可以使用 sess.run()方法执行图中的操作。也就是说,在 TensorFlow 中运行任何内容,通常都需要先启动一个会话,示例代码如下:
import tensorflow as tf
a = tf.multiply(2,3)
print(a)
输出:
Tensor("Mul_4:0", shape=(), dtype=int32)
这里打印出的并不是数值 6,而是一个 TensorFlow 对象。原因正如前面所说,导入 tensorflow 后会自动创建默认计算图,而我们新建的所有节点都会被附加到图中。若要真正执行图中的运算,还需要初始化 TensorFlow 会话,如下所示:
import tensorflow as tf
a = tf.multiply(2,3)
create tensorflow session for executing the session
with tf.Session() as sess:
#run the session
print(sess.run(a))
输出:
6
综合到一个示例中:
先把告警信息关掉,避免无关提示干扰输出:
warnings.filterwarnings('ignore')
接着导入 TensorFlow:
import tensorflow as tf
然后定义一个常量 hello,内容就是 "Hello World":
hello = tf.constant("Hello World")
再创建会话对象:
sess = tf.Session()
通过会话执行并打印 hello 的结果:
print(sess.run(hello))
下面再做一个简单的乘法计算,6 乘以 8:
a = tf.multiply(6,8)
最后输出 a:
print(a)
create tensorflow session for executing the session
with tf.Session() as sess:
run the session
print(sess.run(a))
b'Hello World'
Tensor("Mul:0", shape=(), dtype=int32)
48
TensorBoard
TensorBoard 是 TensorFlow 的可视化工具,可用于展示计算图结构,也可以用来绘制训练过程中的各种中间指标与结果。借助 TensorBoard,我们能够更直观地查看复杂模型,这对模型调试、分析和分享都非常有帮助。下面通过一个基础示例,说明如何构建计算图并在 TensorBoard 中进行可视化。
首先,让我们导入库:
import tensorflow as tf
接下来,我们初始化变量:
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
g = tf.add(c,f)
现在,我们将创建一个 tensorflow 会话,并使用 tf.summary.FileWriter()把图结构写入事件文件,随后就可以通过 TensorBoard 查看对应结果:
with tf.Session() as sess:
writer = tf.summary.FileWriter("logs", sess.graph)
print(sess.run(g))
writer.close()
输出:
26
要运行 tensorboard,请打开终端,进入当前工作目录后输入:
tensorboard --logdir=logs --port=6003
添加范围Adding Scope
范围的作用是降低计算图的复杂度,并通过对相关节点进行分组来帮助我们更清晰地理解模型结构。例如,在上面的示例中,可以将图拆分为两个不同的组,分别命名为计算和结果。如果查看前面的例子会发现,节点 a 到 e 主要用于执行计算,而节点 g 用于得到最终结果。因此,可以借助范围将它们分组显示,使结构更加直观。范围通常使用 tf.name_scope()函数创建。
with tf.name_scope("Computation"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
with tf.name_scope("Result"):
g = tf.add(c,f)
如果进一步观察计算范围,还可以继续细分为更小的部分,以增强可读性。比如可以把节点 a 到 c 划分为第 1 部分,把节点 d 到 e 划分为第 2 部分,因为第 1 部分和第 2 部分之间彼此独立。
with tf.name_scope("Computation"):
with tf.name_scope("Part1"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a,b)
with tf.name_scope("Part2"):
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d,e)
通过在 TensorBoard 中进行可视化展示,我们可以更直观地理解这些范围划分。完整代码如下所示:
以下是使用 TensorFlow 构建计算图的代码示例,展示了如何通过 name_scope 组织节点,并写入日志以便在 TensorBoard 中查看:with tf.name_scope("Computation"):
with tf.name_scope("Part1"):
a = tf.constant(5)
b = tf.constant(4)
c = tf.multiply(a, b)
with tf.name_scope("Part2"):
d = tf.constant(2)
e = tf.constant(3)
f = tf.multiply(d, e)
with tf.name_scope("Result"):
g = tf.add(c, f)
with tf.Session() as sess:
writer = tf.summary.FileWriter("logs", sess.graph)
print(sess.run(g))
writer.close()
全部示例如下:
import tensorflow as tf
with tf.name_scope("Computation"):
with tf.name_scope("Part1"):a = tf.constant(5)b = tf.constant(4)c = tf.multiply(a,b)with tf.name_scope("Part2"):d = tf.constant(2)e = tf.constant(3)f = tf.multiply(d,e) with tf.name_scope("Result"):
g = tf.add(c,f) with tf.Session() as sess:
writer = tf.summary.FileWriter("logs", sess.graph)print(sess.run(g))writer.close() 使用:tensorboard --logdir=logs --port=6003
在浏览器复制如下地址:TensorBoard 1.13.1 at https://TPS2:6003 (Press CTRL C to quit)
不同操作系统和运行环境中显示方式可能会略有差异。
扩展阅读:
OpenAI博客
TensorFlow官网
Github
作者:zhangrelay
来源:CSDN
原文:https://blog.csdn.net/ZhangRelay/article/details/91414600
版权声明:本文为博主原创文章,转载请附上博文链接!
