tensorflow学习笔记

1. 目的

了解tensorflow框架

1.1 推荐

  • 官方QA : 常见疑问经典解答
  • 官方博客: 膜拜牛人的job

2. tensor与placeholder

# tensor 方式
a = tf.constant(3.0, dtype=tf.float32)
b = tf.constant(4.0) # also tf.float32 implicitly
total = a + b
print(a)
print(b)
print(total)

# placeholder方式
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y
print(sess.run(z, feed_dict={x: 3, y: 4.5}))
print(sess.run(z, feed_dict={x: [1, 3], y: [2, 4]}))

# 关于placeholder与tensor的不同, 官网说法: 
The only difference between placeholders and other tf.Tensors is that placeholders throw an error if no value is fed to them.

3. tensorboard

3.1 官方教程

# 第1步, 首先进入一个临时目录, 如: ~/data/tmp 
# 输入ipython 进入环境后, 输入如下代码:
In [3]: a = tf.constant(3.0, dtype=tf.float32)
In [4]: b = tf.constant(4.0) # also tf.float32 implicitly
In [5]: total = a + b
In [6]: writer = tf.summary.FileWriter('.')
In [7]: writer.add_graph(tf.get_default_graph())

# 第2步, 然后, 新打开terminal, 进入目录: ~/data/tmp , 查看当前目录, 已经有一个event文件
ls
events.out.tfevents.1518424941.pcname
# 第3步, 在terminal 命令行输入如下命令, 点击输出的链接:
tensorboard --logdir .
TensorBoard 0.4.0rc3 at http://pcname:6006 (Press CTRL+C to quit)
W0212 16:43:46.173834 Reloader tf_logging.py:86] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.

3.2 官方Example

  • code下载
# 第一步: 下载官方Example代码: mnist_with_summaries.py, 放到本地任意目录, 如: ~/data/tmp
# 第二步: 运行该代码
$ python mnist_with_summaries.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz
2018-02-12 16:57:06.788351: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Accuracy at step 0: 0.0515
Accuracy at step 10: 0.7009
...
Adding run metadata for 999

# 第三步: 该代码会把可视化的event 文件, 放在如下目录:
# Merge all the summaries and write them out to
# /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default)
$ ls /tmp/tensorflow/mnist/logs/mnist_with_summaries/test/
events.out.tfevents.1518425827.pcname
# terminal命令行, 直接运行如下命令, 然后点击输出的URL: 
$ tensorboard --logdir /tmp/tensorflow/mnist/logs/mnist_with_summaries/test/
TensorBoard 0.4.0rc3 at http://pcname:6006 (Press CTRL+C to quit)
  • 输出图片如下:


    tensorboard.png

你可能感兴趣的:(tensorflow学习笔记)