# 强化学习笔记-Python/OpenAI/TensorFlow/ROS-程序指令

## TensorFlow

conda install -c conda-forge tensorflow

pip install --user tensorflow

pip3 install --user 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

weights = tf.Variable(tf.random_normal([8, 9], stddev=0.1), name="weights")

x = tf.constant(666)

x = tf.placeholder("float", shape=None)

### 计算图（ROS中也有这个概念）Computation Graph

TensorFlow中的所有内容都将表示为由节点和边组成的计算图，其中节点是数学运算，例如加法，乘法等。边是张量。 计算图在优化资源方面非常有效，并且还促进了分布式计算。

A = tf.multiply(8,5) B = tf.multiply(A,1)

A = tf.multiply(8,5) B = tf.multiply(4,3)

```import tensorflow as tf
a = tf.multiply(2,3)
print(a)```

`Tensor("Mul_4:0", shape=(), dtype=int32)`

```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`

```import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
hello = tf.constant("Hello World")
sess = tf.Session()
print(sess.run(hello))
a = tf.multiply(6,8)
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)

with tf.Session() as sess:     writer = tf.summary.FileWriter("logs", sess.graph)     print(sess.run(g))     writer.close()

26

tensorboard --logdir=logs --port=6003

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)

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("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"):
with tf.Session() as sess:
writer = tf.summary.FileWriter("logs", sess.graph)
print(sess.run(g))
writer.close()```

• OpenAI博客
• TensorFlow官网
• Github

0 条评论