# 1.认识下TensorBoard

TensorFlow不仅是一个软件库，而是一整套包括TensorFlow、TensorBoard、Tensor Serving在内的软件包。为了更大程度地利用TensorFlow，我们应该了解如何将它们串联起来应用。在和一部分，我们来探索下TensorBoard。 TensorBoard是一个图（graph）可视化软件，在（安装TensorFlow的时候会默认安装）。下面是谷歌的介绍: `The computations you'll use TensorFlow for - like training a massive deep neural network - can be complex and confusing. To make it easier to understand, debug, and optimize TensorFlow programs, we've included a suite of visualization tools called TensorBoard.`

```import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
with tf.Session() as sess:
print(sess.run(x))```

`writer = tf.summary.FileWriter([logdir], [graph])`

[graph] 是运行程序所在的图，可以通过`tf.get_default_graph()`返回程序默认图，也可以通过sess.graph返回当前会话中运行的图，后者需要你自己先创建一个session。无论哪种方式，都要你在定义graph之后创建一个writer，否则TensorBoard不能可视化程序。

[logdir]是存储日志文件的路径

```import tensorflow as tf

a = tf.constant(2)
b = tf.constant(3)
writer = tf.summary.FileWriter('./graphs', tf.get_default_graph())
with tf.Session() as sess:
# writer = tf.summary.FileWriter('./graphs', sess.graph) # if you prefer creating your writer using session's graph
print(sess.run(x))
writer.close()```

```\$ python3 [my_program.py]
\$ tensorboard --logdir="./graphs" --port 6006```

```a = tf.constant(2, name="a")
b = tf.constant(2, name="b")

# 2.常量op

op:图中的节点(operation 的缩写). 下面是创建constant的操作 `tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)`

• 常数 `num = tf.constant(2, name="num")`
• 向量 `# constant of 1d tensor (vector)` `a = tf.constant([2, 2], name="vector")`
• 矩阵 `# constant of 2x2 tensor (matrix)` `b = tf.constant([[0, 1], [2, 3]], name="matrix")` 可以通过填充创建tensor，类似于numpy中的操作
• tf.zeros(shape, dtype=tf.float32, name=None)
```# create a tensor of shape and all elements are zeros
tf.zeros([2, 3], tf.int32) ==> [[0, 0, 0], [0, 0, 0]]```
• tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)
```# create a tensor of shape and type (unless type is specified) as the input_tensor but all elements are zeros.
# input_tensor [[0, 1], [2, 3], [4, 5]]
tf.zeros_like(input_tensor) ==> [[0, 0], [0, 0], [0, 0]]```
• tf.ones(shape, dtype=tf.float32, name=None)
```# create a tensor of shape and all elements are ones
tf.ones([2, 3], tf.int32) ==> [[1, 1, 1], [1, 1, 1]]```
• tf.ones_like(input_tensor, dtype=None, name=None, optimize=True)
```# create a tensor of shape and type (unless type is specified) as the input_tensor but all elements are ones.
# input_tensor is [[0, 1], [2, 3], [4, 5]]
tf.ones_like(input_tensor) ==> [[1, 1], [1, 1], [1, 1]]```
• tf.fill(dims, value, name=None)
```# create a tensor filled with a scalar value.
tf.fill([2, 3], 8) ==> [[8, 8, 8], [8, 8, 8]]```
• tf.lin_space(start, stop, num, name=None)
```tf.lin_space(start, stop, num, name=None)
# create a sequence of num evenly-spaced values are generated beginning at start. If num > 1, the values in the sequence increase by (stop - start) / (num - 1), so that the last one is exactly stop.
# comparable to but slightly different from numpy.linspace

tf.lin_space(10.0, 13.0, 4, name="linspace") ==> [10.0 11.0 12.0 13.0]```
• tf.range([start], limit=None, delta=1, dtype=None, name='range')
```# create a sequence of numbers that begins at start and extends by increments of delta up to but not including limit
# slight different from range in Python

# 'start' is 3, 'limit' is 18, 'delta' is 3
tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]
# 'start' is 3, 'limit' is 1,  'delta' is -0.5
tf.range(start, limit, delta) ==> [3, 2.5, 2, 1.5]
# 'limit' is 5
tf.range(limit) ==> [0, 1, 2, 3, 4]```

```for _ in np.linspace(0, 10, 4): # OK
for _ in tf.linspace(0.0, 10.0, 4): # TypeError: 'Tensor' object is not iterable.

for _ in range(4): # OK
for _ in tf.range(4): # TypeError: 'Tensor' object is not iterable.```

```tf.random_normal
tf.truncated_normal
tf.random_uniform
tf.random_shuffle
tf.random_crop
tf.multinomial
tf.random_gamma
tf.set_random_seed```

# 3. 数学运算

• division相关操作 TensorFlow 运算相当完美与标准，全部内容在这 tf.div（a/b）是TensorFlow的风格，返回a除以b的商数,比如8/3,返回2；

tf.divide（a/b）才和Python的风格一样，a除以b

```a = tf.constant([2, 2], name='a')
b = tf.constant([[0, 1], [2, 3]], name='b')
with tf.Session() as sess:
print(sess.run(tf.div(b, a)))             ⇒ [[0 0] [1 1]]
print(sess.run(tf.divide(b, a)))          ⇒ [[0. 0.5] [1. 1.5]]
print(sess.run(tf.truediv(b, a)))         ⇒ [[0. 0.5] [1. 1.5]]
print(sess.run(tf.floordiv(b, a)))        ⇒ [[0 0] [1 1]]
print(sess.run(tf.realdiv(b, a)))         ⇒ # Error: only works for real values
print(sess.run(tf.truncatediv(b, a)))     ⇒ [[0 0] [1 1]]
print(sess.run(tf.floor_div(b, a)))       ⇒ [[0 0] [1 1]]```
• tf.add_n `tf.add_n([a, b, b]) => equivalent to a + b + b`
• 点积 Dot
```a = tf.constant([10, 20], name='a')
b = tf.constant([2, 3], name='b')
with tf.Session() as sess:
print(sess.run(tf.multiply(a, b)))           ⇒ [20 60] # element-wise multiplication
print(sess.run(tf.tensordot(a, b, 1)))       ⇒ 80 # 按列相乘然后相加```

# 数据类型

0 条评论

## 相关文章

1792

### 一致性hash算法 - consistent hashing

consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出，目前在 ...

921

### Python数据分析(二): Pandas技巧 (2)

Pandas的第一部分: http://www.cnblogs.com/cgzl/p/7681974.html github地址: https://github...

2846

3917

### geotrellis使用（十五）使用Bokeh进行栅格数据可视化统计

Geotrellis系列文章链接地址http://www.cnblogs.com/shoufengwei/p/5619419.html 目录 前言 实现方案 ...

3327

### 一致性hash算法清晰详解！

consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出，目前在 ...

801

### 一致性hash算法清晰详解！

consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出，目前在 ...

802

5356

### 一致性hash算法清晰详解！

consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出，目前在 ...

991

### R语言基础命令与安装

1. R的安装过程 1.1.首先附上清华线路的下载链接Windows版3.3.1 1.2. 选择安装路径 ? 1.3. 注意根据自己的计算机位数选择，如我的是6...

2185