[deeplearning-006] tensorflow安装和测试

本文介绍了TensorFlow的安装过程,并通过链接提供了针对高手的深度学习教程,该教程利用卷积网络、池化和全连接层将模型测试精度提高到99.2%。此外,还探讨了TensorFlow的底层运行机制。
摘要由CSDN通过智能技术生成
1. 官网
www.tensorflow.org,需要翻墙。


2.tensorflow的概念抽象
节点:表示数据运算。
边:在节点之间传递多维数据(张量)




3.安装
3.1 文档
https://www.tensorflow.org/install/




3.2 操作系统需要ubuntu 14.04以上


3.3 两种安装版本:cpu和gpu。如果工作机上没有nvida gpu,就安装cpu版本。cpu版本安装简单,因此,即使你有gpu,也建议先安装这个版本。


3.4 推荐使用virtualenv进行安装。推荐使用native pip安装。如果是anancode,推荐使用pip install,而不是conda insall。


3.5 用anacode进行安装


pyenv global anaconda3-4.2.0


conda create -n tensorflow python=3.4


source activate tensorflow #这一步会出错,没有出现期望的结果。
暂时先忽略掉这个安装方式。


3.6 用anacode3-4.2进行安装
mkdir tensorflow-site
cd tensorflow-site
pyenv local anancode3-4.2
pip3 install tensorflow
以后,进入该目录,都要用pyenv设置local的python,然后运行既可


3.7 验证安装
运行一小段代码验证
---------------------
import tensorflow as tf
hello = tf.constant('Hello Tensorflow')
sess = tf.Session()
print(sess.run(hello))
---------------------
运行后会输出b'Hello,Tensorflow'




4.给新手的tf教程
https://www.tensorflow.org/get_started/mnist/beginners
建议都从这里开始。因为这里会解释一些很重要的概念。


MNIST数据集,每幅图是28x28=784个像素,训练集有55000个数据,测试集有10000个数据。minist.train.images是一个tensor,shape是[55000,784]。训练集的标记,是一个[55000, 10]的tensor。


软最大算法,也就是softmax,以线性函数方式实现。公式简单。


用tensorflow的方式实现,如下:






placeholder,不是一个特定的值,而是,存放一些在计算是可能要用到的值。


用交叉熵评估做cost函数。

示例代码如下

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.  
#  
# Licensed under the Apache License, Version 2.0 (the "License");  
# you may not use this file except in compliance with the License.  
# You may obtain a copy of the License at  
#  
#     http://www.apache.org/licenses/LICENSE-2.0  
#  
# Unless required by applicable law or agreed to in writing, software  
# distributed under the License is distributed on an "AS IS" BASIS,  
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
# See the License for the specific language governing permissions and  
# limitations under the License.  
# ==============================================================================  
  
  
"""A very simple MNIST classifier. 
 
 
See extensive documentation at 
https://www.tensorflow.org/get_started/mnist/beginners 
"""  
from __future__ import absolute_import  
from __future__ import division  
from __future__ import print_function  
  
  
import argparse  
import sys  
  
  
from tensorflow.examples.tutorials.mnist import input_data  
  
  
import tensorflow as tf  
  
  
FLAGS = None  
  
  
  
  
def main(_):  
  # Import data  
  mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)  
  
  
  # Create the model  
  x = tf.placeholder(tf.float32, [None, 784])  
  W = tf.Variable(tf.zeros([784, 10]))  
  b = tf.Variable(tf.zeros([10]))  
  y = tf.matmul(x, W) + b  
  
  
  # Define loss and optimizer  
  y_ = tf.placeholder(tf.float32, [None, 10])  
  
  
  # The raw formulation of cross-entropy,  
  #  
  #   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),  
  #                                 reduction_indices=[1]))  
  #  
  # can be numerically unstable.  
  #  
  # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw  
  # outputs of 'y', and then average across the batch.  
  cross_entropy = tf.reduce_mean(  
      tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))  
  train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)  
  
  
  sess = tf.InteractiveSession()  
  tf.global_variables_initializer().run()  
  # Train  
  for _ in range(1000):  
    batch_xs, batch_ys = mnist.train.next_batch(100)  
    sess.run(train_step, feed_dict={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(sess.run(accuracy, feed_dict={x: mnist.test.images,  
                                      y_: mnist.test.labels}))  
  
  
if __name__ == '__main__':  
  parser = argparse.ArgumentParser()  
  parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',  
                      help='Directory for storing input data')  
  FLAGS, unparsed = parser.parse_known_args()  
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)  
-------------------------  
注意:下载数据集也需要翻墙。  
结果:  
------------  
Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz  
Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz  
Extracting /tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz  
Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz  
2017-11-06 15:40:53.056574: 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  
0.9153  


5.给高手的教程
https://www.tensorflow.org/get_started/mnist/pros
这个教程后半段,使用了卷积网络,卷积网,池化,密连接层,将测试精度提升到了99.2%。




6.tensorflow运行机制
以底层api方式解释tensorflow运行









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值