Tensorflow学习笔记(1)

来源:互联网 发布:淘宝女装自拍教程 编辑:程序博客网 时间:2024/05/29 14:37

刚刚接触,记记笔记


1.安装

• Virtualenv 安装 : 在一个独立的路径下安装 TensorFlow ,不会影响到你机器当前运行的 Python 程序

按照文档所说,选择Virtualenv安装Tensorflow

# Ubuntu/Linux 64-bit
$ sudo apt-get install python-pip python-dev python-virtualenv

# 在目录~/tensorflow下建立一个virtualenv环境
$ virtualenv --system-site-packages ~/tensorflow

# a.If using bash
$ source ~/tensorflow/bin/activate
# b.If using csh
$ source ~/tensorflow/bin/activate.csh

# 以上a和b任选一个可以激活Virtualenv环境
(tensorflow)$

#在激活环境下,安装tensorflow
# Ubuntu/Linux 64 − bit, CPU only:
(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64 − bit, CPU only (for python3):
(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl

#当不需要TensorFlow时,取消激活该环境
(tensorflow)$ deactivate



2.运行一下文档上的"Hellow,World",测试一下tensorflow

#激活环境
abc@abc:~/tensorflow$ source bin/activate

#进入python
(tensorflow) abc@abc:~/tensorflow$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf //所有用tensorflow的都需要先import
>>> hello = tf.constant('Hello, TensorFlow!') //将常亮字串赋给'hello'
>>> sess = tf.Session() //新建会话,可能会有以下的错误,貌似不影响
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 8
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 8
>>> print(sess.run(hello)) //运行输出hello

Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b)) //...一个加法
42


3.运行第一个TensorFlow模型(自带的...)
所有版本的TensorFlow的Python库中包都附带了一些演示模型
#查看模型位置
abc@abc:~$ python -c 'import site; print("\n".join(site.getsitepackages()))'
/usr/local/lib/python2.7/dist-packages
#激活环境
abc@abc:~$ source tensorflowbin/activate
#运行模型
(tensorflow) abc@abc:~$ python /usr/local/lib/python2.7/dist-packages/tensorflow/models/image/mnist/convolutional.py
Succesfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Succesfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Succesfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Succesfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 8
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 8
Initialized!
Epoch 0.00
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Epoch 0.12
Minibatch loss: 3.285, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 7.0%
...
Epoch 9.89
Minibatch loss: 1.602, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.9%
Test error: 0.8%

先下载,解压成图片资源。单个的图片可以看成是28x28的像素集合,根据颜色的深浅给出0-1的值。


因此,在 MNIST 训练数据集中, mnist.train.images 是一个形状为 [55000, 784] 的张量,第一个维度数字用来索引图片,第二个维度数字用来索引每张图片中的像素点.
在此张量里的每一个元素,都表示某张图片里的某个像素的强度值,值介于 0 和 1 之间.

模型做的就是根据图形,给出它是具体数字的概率。


4.关于TensorFlow的以下基础知识 :
• 使用图(graphs)来表示计算.
TensorFlow是一个以图(graphs)来表示计算的编程系统,图中的节点被称之为op(operation的缩写).一个op获得零或多个张量(tensors)执行计算,产生零或多个张量。
张量是一个按类型划分的多维数组。例如,你可以将一小组图像集表示为一个四维浮点数数组,这四个维度分别是[batch,height,width,channels]。
• 在会话(Session)中执行图.
TensorFlow的图是一种对计算的抽象描述。在计算开始前,图必须在会话(Session())中被启动.会话将图的op分发到如CPU或GPU之类的设备(Devices())上,
同时提供执行op的方法。这些方法执行后,将产生的张量(tensor)返回。
• 使用张量(tensors)来代表数据.
• 通过变量(Variables)维护状态.
• 使用供给(feeds)和取回(fetches)将数据传入或传出任何操作.



0 0