2016-10-08 陈伟才 人工智能学堂
一、Tensorflow简介
Tensorflow ( https://www.tensorflow.org/ ) 是google大脑团队打造的一款开源软件库,用于人工智能、机器学习以及深度学习等领域。Tensorflow主要包括Tersor和Flow两个主要的概念,tensor表示多维数组multi-dimensional arrays,flow表示数据流图data flow graph。Tensorflow (https://github.com/tensorflow/tensorflow )从star数、fork数来看,可以说是目前最火热的机器学习开源框架,值得我们深入学习。
二、Ubuntu/Linux和MAC OS X简单安装过程
Tensorflow可以通过二进制或者源码来进行安装。本文通过tensorflow的二进制包进行Ubuntu和MAC平台下安装演示。
1. Ubuntu安装
以Ubuntu 14.04.4 LTS (GNU/Linux 3.13.0-86-generic x86_64)为例,安装过程如下蓝色字体:
#apt-get update//获取ubuntu最新的软件包
#sudo apt-get install python-pip python-dev python-virtualenv
#virtualenv --system-site-packages ~/tensorflow
#source ~/tensorflow/bin/activate
#pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl//我们安装CPU版本为例,如果需要支持GPU Card,则安装对应的tensorflow GPU版本
#deactivate
至此tensorflow已经安装完毕,我们检测一下是否安装成功了,
#~/tensorflow/bin/python2.7
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import tensorflow as tf
>>>hello = tf.constant('Hello, TensorFlow!')
>>>sess = tf.Session()
>>>print sess.run(hello)
Hello, TensorFlow!
>>>
2. MAC OS X安装
MAC OS X需要OSX 10.11 EL Capitan版本才能支持Tensorflow,所以安装Tensorflow之前,请自行升级MAX OS系统。升级完MAX OS X系统成功后,MAX默认是开启rootless,所以还需要关系rootless。
MAC只支持CPU版本,目前还不支持GPU Card,MAC安装过程同样也很简单,如下:
#sudo easy_install pip
#sudo pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl
三、简单入门示例MNIST
TensorFlow是一个非常强大的用来做大规模数值计算的库。其所擅长的任务之一就是实现以及训练深度神经网络。Tensorflow官网入门示例MNIST,https://www.tensorflow.org/versions/r0.11/tutorials/mnist/beginners/index.html#softmax-regressions,是采用softmax regression进行机器学习的入门例子。
MNIST是一个入门级的计算机视觉数据集,它包含各种手写数字图:
Softmax回归就是推广版本的逻辑回归。 只不过逻辑回归是个2分类问题,而Softmax是多分类问题,仅此而已。有关softmax regression算法,请参考http://deeplearning.stanford.edu/wiki/index.php/Softmax_Regression,本文不重点描述和推导。
程序代码github地址是https://github.com/chenweicai/tensorflow-study/blob/master/tf_softmax_mnist.py,内容如下:
# Softmax Regression using tensorflow.
import tensorflow as tf
# Download the mnist data.
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/MNIST_data", one_hot=True)
# Input placeholder, 2-D tensor of floating-point nunbers.
# here None means that a dimension can be of any length.
x = tf.placeholder(tf.float32, [None, 784])
# Initialize both W and b as tensors full of zeros.
# Since we are going to learn W and b, it doesn't
# matter very much what they initial are.
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
# Maichine Learning Model.
y = tf.nn.softmax(tf.matmul(x, W) + b)
# New placeholder to input the correct answers.
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), \
reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
# Training 1000 times, 100 for each loop.
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_:batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Testing accuracy using test images.
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
执行,输出如下:
# ~/tensorflow/bin/python2.7 tf_softmax_mnist.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting /tmp/MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting /tmp/MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /tmp/MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting /tmp/MNIST_data/t10k-labels-idx1-ubyte.gz
0.9168
上述红色0.9168,即为softmax学习mnist的准确率。
长按二维码关注公众号人工智能学堂
网友评论