Tensorflow
简介
TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理。Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow为张量从流图的一端流动到另一端计算过程。TensorFlow是将复杂的数据结构传输至人工智能神经网中进行分析和处理过程的系统。
TensorFlow可被用于语音识别或图像识别等多项机器深度学习领域,对2011年开发的深度学习基础架构DistBelief进行了各方面的改进,它可在小到一部智能手机、大到数千台数据中心服务器的各种设备上运行。TensorFlow将完全开源,任何人都可以用。
MNIST手写数据集入门
数据下载数据http://yann.lecun.com/exdb/mnist/
其中mnist_softmax.py脚本: MNIST机器学习入门
Tensorflow依赖于一个高效的C++后端来进行计算。与后端的这个连接叫做session。一般而言,使用TensorFlow程序的流程是先创建一个图,然后在session中启动它。
# Import data
#from tensorflow.examples.tutorials.mnist import input_data
import input_data
import tensorflow as tf
mnist = input_data.read_data_sets(“Mnist_data/”, one_hot=True)
sess = tf.InteractiveSession()
# Create the model
W = tf.Variable(tf.zeros([784, 10])) # placeholder是个占位符
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# Train
tf.initialize_all_variables().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_: batch_ys})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
accuracy.eval()时,会触发Tensorflow执行计算图,从而获取output计算结点的结果。
TensorBoard
TensorBoard可以将训练过程中的各种绘制数据展示出来,包括标量(scalars),图片(images),音频(Audio),计算图(graph),数据分布,直方图(histograms)和嵌入式向量。
使用TensorBoard展示数据,需要在执行Tensorflow就算图的过程中,将各种类型的数据汇总并记录到日志文件中。然后使用TensorBoard读取这些日志文件,解析数据并生产数据可视化的Web页面,让我们可以在浏览器中观察各种汇总数据。
mnist_with_summaries.py主要提供了一种在Tensorboard可视化方法,首先,编译运行代码:
运行完毕后,打开终端Terminal,使用sh窗口输入
tensorboard
–logdir=~/tensorflow_demo/mnist_run/mnist_logs(与文件中输出路径一径)
终端中就会运行显示:Starting TensorBoard on port 6006 (You can navigate to
http://localhost:6006),如果
如果使用sh窗口的话,那么就需要在vnc中打开浏览器,输入 http://127.0.1.1:6006
其中,有一些选项,例如菜单栏里包括EVENTS, IMAGES, GRAPH,
HISTOGRAMS,都可以一一点开查看~