深度学习技术一直在发展,但是深度学习框架caffe的更新跟不上进度,很多新的技术在caffe里用不了。TensorFlow在Google的支持下如日中天,于是在学了caffe后还要学一下tensorflow。在训练神经网络模型时将两个深度学习框架结合使用。
一、 安装anaconda
1.在continuum官网 下载anaconda
现在的版本有python3.6版本和python2.7版本
2.anaconda官网下载,tensorflow中的有些技术不支持python3.6,推荐使用linux版的python 2.7版本。
sudo Anaconda2-2.5.0-Linux-x86_64.sh
二、 安装tensorflow
anaconda search -t conda tensorflow
conda install --channel https://conda.anaconda.org/jjhelmus tensorflow
三、 调试
进入python编译环境
import tensorflow as tf
四、数据结构
TensorFlow用张量来表示所有的数据
1.常量
(1)引入tensorflow语句
import tensorflow as tf
(2)定义数值为10的常量a
a=tf.constant(10)
2.变量
(1)使用Variable定义
m=tensorflow.Variable(tensorflow.zeros([3,3]))
(2)定义了一个3x3的全0矩阵m,0为初始化值
init=tensorflow.global_variables_initializer()
(3)变量定义完后,初始化操作显式的执行一下
3.占位符
在tensorflow中变量在定义时要初始化,用占位符来占无法确定初始化变量值的位置
x = tensorflow.placeholder(tensorflow.float32, [None, 784])
指定这个变量的类型和shape,再用feed的方式来输入值
4.图(graph)
(1)定义两个变量,并将两个数相加,输出结果
x=3
y=2
z=x+y
print(z)
(2)这是在python中的写法,在TensorFlow中这样的语法会出错。
(3)x,y,z分别是三个tensor对象,对象间的运算称之为操作(op), tf不会去一条条地执行各个操作,而是把所有的操作都放入到一个图(graph)中,图中的每一个结点就是一个操作。
(4)将整个graph 的计算过程交给一个 TensorFlow 的Session, 此 Session 可以运行整个计算过程,比起操作(operations)逐行执行的效率高。
x = tensorflow.Variable(3)
y = tensorflow.Variable(5)
z=x+y
init =tensorflow.global_variables_initializer()
with tensorflow.Session() as sess:
sess.run(init)
print(sess.run(z))
(5)其中sess.run()即是执行,注意要先执行变量初始化操作,再执行运算操作。
(6)Session需要先创建,使用完后还需要释放。因此使用with…as..语句,让系统自动释放。
五、第一个程序——HelloWorld
import tensorflow as tf
word=tf.constant('hello,world!')
with tf.Session() as sess:
print(sess.run(word))
六、深度学习的入门实例mnist
1.mnist数据
(1)tensorflow提供一个input_data.py文件,专门用于下载mnist数据,直接调用即可,代码如下:
import tensorflow.examples.tutorials.mnist.input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
(2)执行完成后,会在当前目录下新建一个文件夹MNIST_data, 下载的数据将放入这个文件夹内。下载的四个文件为:
文件 | 内容 |
---|---|
train-images-idx3-ubyte.gz | 训练集 -55000张训练图片,5000张验证图片 |
train-labelss-idx1-ubyte.gz | 训练集图片对应的数字标签 |
t10k-images-idx3-ubyte.gz | 测试集图片 - 10000张图片 |
t10k-labels-idx1-ubyte.gz | 测试集图盘对应的数字标签 |
input_data
文件会调用一个maybe_download
函数,确保数据下载成功。函数还会判断数据是否已经下载。
(3)下载下来的数据集被分三个子集:5.5W行的训练数据集(mnist.train),5千行的验证数据集(mnist.validation)和1W行的测试数据集(mnist.test)。因为每张图片为28x28的黑白图片,所以每行为784维的向量
(4)每个子集都由两部分组成:图片部分(images)和标签部分(labels), 用下面的代码来查看 :
print mnist.train.images.shape
print mnist.train.labels.shape
print mnist.validation.images.shape
print mnist.validation.labels.shape
print mnist.test.images.shape
print mnist.test.labels.shape
2.CSV数据
(1)tensorflow提供了除了mnist手写字体图片的数据,还有csv的数据练习,存放路径为:
/home/cow/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/data/text_train.csv
(2)读取数据
import tensorflow.contrib.learn.python.learn.datasets.base as base
iris_data,iris_label=base.load_iris()
house_data,house_label=base.load_boston()
数据集分别为iris鸢尾花卉数据集、波士顿房价数据集
3.cifar10数据
tensorflow提供了cifar10数据的下载和读取的函数,直接调用即可。代码如下:
import tensorflow.models.image.cifar10.cifar10 as cifar10
cifar10.maybe_download_and_extract()
images, labels = cifar10.distorted_inputs()
print images
print labels