深度学习框架Tensorflow学习与应用(一Tensorflow简介,Anaconda安装,Tensorflow的CPU版本安装)

一 TensorFlow GPU环境
环境和版本信息: 操作系统:ubuntu14.04
GPU:NIVIAD GTX 1070
Python版本:3.5以上
CUDNN:7.0
CUDA:9.0
TensorFlow-GPU:1.10.1 注:大家不一定要严格按照以上的版本,主要是保证CUDA、CUDNN与显卡能够匹配即可。

二TensorFlow三个核心概念
2.1Graph:
在TensorFlow中,计算图时一个有向图,用来描述计算节点以及计算节点之间的关系,所以在TensorFlow中我们存储在一个值活着数组的时候,存的其实是这个值活着数组的计算图而不是其本身的数字。
#####关于计算图的操作

1,新建计算图: g=tf.Graph(),但是不同计算图赏的张量是不能共享的,这个是存在于变量
2,指定计算图的使用的deice: with g.device("/gpu:0")
3,  设置默认计算图: with g.as_default:
4, 在绘画中可以指定使用的计算图: with tf.Session(graph=g1):

#GPU版

root@c3c3d78304f9:~# cat gpu_example.py 
#!/usrbin/env python3.5
import tensorflow as tf
g = tf.Graph()
with g.device("/gpu:0"):
    d = tf.constant([10,9,8,7])
    e = tf.constant([1,2,3,4])
    f_1 = d+e
    print(f_1.graph)
    print(d.graph,e.graph)
    sess=tf.Session()
    print(sess.run(f_1))

root@c3c3d78304f9:~# python3.5 gpu_example.py 
<tensorflow.python.framework.ops.Graph object at 0x7f1ad4afe9e8>
<tensorflow.python.framework.ops.Graph object at 0x7f1ad4afe9e8> <tensorflow.python.framework.ops.Graph object at 0x7f1ad4afe9e8>
2018-11-11 12:51:27.575737: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-11-11 12:51:27.678706: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-11-11 12:51:27.680526: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1356] Found device 0 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.721
pciBusID: 0000:29:00.0
totalMemory: 10.91GiB freeMemory: 10.55GiB
2018-11-11 12:51:27.680544: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-11-11 12:51:27.843339: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-11-11 12:51:27.843381: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929]      0 
2018-11-11 12:51:27.843388: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0:   N 
2018-11-11 12:51:27.843594: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10210 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:29:00.0, compute capability: 6.1)
[11 11 11 11]

CPU 版本
import tensorflow as tf

a = tf.constant([1,2,3,4],name='a')
b = tf.constant([0,1,2,3],name='b')
c=a+b
print(a.graph, b.graph)
print(c.graph)
sess=tf.Session()
print(sess.run(c))

运行结果
[1 3 5 7]
import tensorflow as tf
g1=tf.Graph()     #定义一张图
with g1.as_default():
    a = tf.constant([1,2,3], name='a')#用常量试试看
    b = tf.get_variable('b',initializer=tf.constant_initializer()(shape=[1]))#用常量试试看
g2=tf.Graph()

with g2.as_default():
    a = tf.constant([2,3],name='a')#用常量试试看
    b = tf.get_variable('b',initializer=tf.constant_initializer()(shape=[3]))#用常量试试看

#在session里面指定图
with tf.Session(graph=g2) as sess:
    with g1.device("/cpu:0"):
        tf.global_variables_initializer().run()
        c=a+1
        print("常量的情况下 = ",sess.run(c))
        with tf.variable_scope("",reuse=True):
            print("变量情况下 = ",sess.run(tf.get_variable("b")))

#在session里面指定图
with tf.Session(graph=g2) as sess:
    with g2.device("/gpu:0"):
        tf.global_variables_initializer().run()
        c=a+1
        print("常量的情况下 = ", sess.run(c))
        with tf.variable_scope("",reuse=True):
            print("变量情况下 = ",sess.run(tf.get_variable("b")))

运行结果
root@c3c3d78304f9:~# python3.5 1.py
2018-11-11 13:28:40.767842: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-11-11 13:28:40.870770: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-11-11 13:28:40.871278: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1356] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.721
pciBusID: 0000:29:00.0
totalMemory: 10.91GiB freeMemory: 10.55GiB
2018-11-11 13:28:40.871295: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-11-11 13:28:41.063014: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-11-11 13:28:41.063058: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929]      0
2018-11-11 13:28:41.063069: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0:   N
2018-11-11 13:28:41.063274: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10210 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:29:00.0, compute capability: 6.1)
常量的情况下= [3 4]
变量情况下= [0. 0. 0.]
2018-11-11 13:28:41.148431: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-11-11 13:28:41.148473: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-11-11 13:28:41.148481: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929]      0
2018-11-11 13:28:41.148487: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0:   N
2018-11-11 13:28:41.148575: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10210 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:29:00.0, compute capability: 6.1)
常量的情况下 = [3 4]
变量情况下 =  [0. 0. 0.]


2.2张量

张量(tensor)可以简单理解为多维数组。其中零阶张量表示标量(scalar),也就是一个数;一阶张量为向量(vector),也就是一维数组;
第n阶张量可以理解为一个n维数组。但是张量在TensorFlow中的实现病不是直接采用数组的形式,它只是对TensorFlow中运算结果的引用。在张量中病没有真正保存数字,它保存的是如何得到这些数字的计算过程。

2.3定义张量的三种方式
import tensorflow as tf
2.3.1 用的比较多
with tf.Session as less:
    print(less.run(result))

2.3.2
sess = tf.Session()
with less.as_default():
    print(result.eval())

2.3.3交互式,不需要指定会话,默认
sess=tf.InteractiveSession()#会自动成为默认会话

会话结束后,所有的变量都消失
root@c3c3d78304f9:~# cat tensorflow_example.py 
#!/usr/bin/env python3.5
import tensorflow as tf

a = tf.constant(2,name='a')
b = tf.constant([0,1,2,3], name='b')
c=a*b
print(a,b)
print(c)

sess = tf.Session()

print(sess.run(c))
root@c3c3d78304f9:~# python3.5 tensorflow_example.py 
Tensor("a:0", shape=(), dtype=int32) Tensor("b:0", shape=(4,), dtype=int32)
Tensor("mul:0", shape=(4,), dtype=int32)
2018-11-11 09:57:47.148221: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-11-11 09:57:47.253357: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-11-11 09:57:47.254083: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1356] Found device 0 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.721
pciBusID: 0000:29:00.0
totalMemory: 10.91GiB freeMemory: 10.55GiB
2018-11-11 09:57:47.254112: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-11-11 09:57:47.423734: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-11-11 09:57:47.423776: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929]      0 
2018-11-11 09:57:47.423785: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0:   N 
2018-11-11 09:57:47.423987: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10210 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:29:00.0, compute capability: 6.1)
[0 2 4 6]
root@c3c3d78304f9:~# 

2.3Session:
在TensorFlow中,计算图的计算过程都是在会话下进行的,同一个会话内的数据是可以共享的,会话结束计算的中间量酒会小时。
在TensorFlow需要指定会话。
三TensorFlow常用基础API
根据TensorFlow官网以及在日常的编程中的使用情况,梳理了一下需要掌握的TensorFlow基础API:
tf.Graph()
tf.Graph.device()
tf.Graph.as_default()
tf.Session()
tf.Session.run()
tf.Session.as_default()
tf.InteractiveSession()
tf.constant()
tf.Variable()
tf.get_variable()
tf.placeholder()  #占位,行参
tf.train() #重点
tf.nn()#重点
tf.nn.conv2d
tf.nn.relu
tf.nn.max_pool
tf.nn.dropout
tf.nn.softmax

tf.train
tf.train.GradientDescentOptimizer
tf.train.Saver

四重点API详解
4.1 tf.nn
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
第一个参数input: 指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height,in_width,in_channels]这样的shape,具体含义是[训练时一个batch的图片数量,图片高度,图片宽度,图像通道数],注意这时一个4维的Tensor,要求类型为float32和float64其中之一
第二个参数filter:相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height,filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],
要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维
第三个参数strides:卷积时在图像每一维的补偿,这时一个一维的向量,长度4
第四个参数padding:string类型的量,只能是"SAME","VALID"其中之一
第五个参数: use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true

4.2 tf.nn.relu

tf.nn.relu(features, name=None)
这个函数的作用是计算激活函数 relu,即max(features,0).即将矩阵中每行的非最大值置为0

4.3 tf.nn.max_pool池化层

第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map, 依然是[batch,height,width,channels]这样的shape
第二个参数ksize:池化窗口的大小,取一个四维向量,一半时[1,height, width,1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1,stride,stride, 1]
第四个参数padding:和卷积类似,可以取'VALID' 或者'SAME'

4.4 tf.nn.dropout
tf.nn.dropout(x,keep_prob,noise_shape=None,seed=None,name=None)
x :输入tensor..
keep_prob: float类型,每个元素被保留下来的概率
noise_shape:一个1维的int32张量,代表了随机产生"保留/丢弃"标志的shape
seed       :整形变量,随机数种子

4.5 tf.nn.softmax
标签编程两个其实就是逻辑回归,一般多标签(多图像识别)


五tf.train

tf.train
梯度下降


tf.train.GradientDescentOptimizer
优化器

tf.train.Saver
保存我们的模型



六项目和代码设计思路

Flask_app :前端等
execute   :定义好model后,执行器负责预测,训练,调用model来做预测
cnnModel  :定义一个类,把公共方法和参数都放入里面
getConfig :所有参数都可以配置,所有key都在配置文件中配置,修改起来非常方便,修改一个值所有都改掉



数据集下载地址
  255  wget http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
  256  wget http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
  257  wget http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
  258  wget http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值