Using pip installed TensorFlow

Installation TensorFlow (pip way)

Installation and pip3 python3

First, you need to install python and pip:

      
      
1
2
3
      
      
$ sudo apt-get install python-pip python-dev
$ sudo apt-get install python3-pip

Then there will be coexistence python 2.7 and 3.5, the default version is 2.7, in order to use python 3.5 and pip3 default, you can add the following sentence to rename in the .bash_profile:

      
      
1
2
      
      
alias python=/usr/bin/python3
alias pip=/usr/bin/pip3

Verify the version with the following command:

      
      
1
2
3
4
      
      
$ python --version
Python 3.5.2
$ pip --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

Canceled renaming, only you need to perform unalias:

      
      
1
2
      
      
$ unalias python
$ unalias pip

Then start the installation tensorflow CPU version, then install version 0.12.1:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
      
      
$ pip install tensorflow
Collecting tensorflow
Downloading tensorflow-0.12.1-cp35-cp35m-manylinux1_x86_64.whl (43.1MB)
100% |████████████████████████████████| 43.1MB 23kB/s
Collecting wheel>=0.26 (from tensorflow)
Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
100% |████████████████████████████████| 71kB 1.3MB/s
Collecting six>=1.10.0 (from tensorflow)
Downloading six-1.10.0-py2.py3-none-any.whl
Collecting numpy>=1.11.0 (from tensorflow)
Downloading numpy-1.12.0-cp35-cp35m-manylinux1_x86_64.whl (16.8MB)
100% |████████████████████████████████| 16.8MB 60kB/s
Collecting protobuf>=3.1.0 (from tensorflow)
Downloading protobuf-3.2.0-cp35-cp35m-manylinux1_x86_64.whl (5.6MB)
100% |████████████████████████████████| 5.6MB 136kB/s
Collecting setuptools (from protobuf>=3.1.0->tensorflow)
Downloading setuptools-34.1.1-py2.py3-none-any.whl (389kB)
100% |████████████████████████████████| 399kB 308kB/s
Collecting packaging>=16.8 (from setuptools->protobuf>=3.1.0->tensorflow)
Downloading packaging-16.8-py2.py3-none-any.whl
Collecting appdirs>=1.4.0 (from setuptools->protobuf>=3.1.0->tensorflow)
Downloading appdirs-1.4.0-py2.py3-none-any.whl
Collecting pyparsing (from packaging>=16.8->setuptools->protobuf>=3.1.0->tensorflow)
Downloading pyparsing-2.1.10-py2.py3-none-any.whl (56kB)
100% |████████████████████████████████| 61kB 702kB/s
Installing collected packages: wheel, six, numpy, pyparsing, packaging, appdirs, setuptools, protobuf, tensorflow
Successfully installed appdirs numpy packaging protobuf pyparsing setuptools-20.7.0 six-1.10.0 tensorflow wheel-0.29.0
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

“2.20 更新”Google 于2017年2月16日(北京时间)凌晨2点在美国加利福尼亚州山景城举办了首届 TensorFlow 开发者峰会。Google 现场宣布全球领先的深度学习开源框架 TensorFlow 正式对外发布V1.0版本,并保证 Google 的本次发布版本的 API 接口满足生产环境稳定性要求。升级到1.0很简单,执行以下命令即可:

      
      
1
      
      
$ pip install --upgrade tensorflow

确认安装路径:

      
      
1
2
      
      
$ python -c 'import os; import inspect; import tensorflow; print(os.path.dirname(inspect.getfile(tensorflow)))'
/home/{username}/.local/lib/python3.5/site-packages/tensorflow

查看目录结构:

      
      
1
      
      
$ tree -d -L 3 ~/.local/lib/python3.5/site-packages/tensorflow

运行 TensorFlow

从命令行运行 TensorFlow

执行命令如下:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
      
      
$ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
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))
b 'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

运行 TensorFlow demo

这个 Demo 是用 MNIST 数据集进行手写字符的训练和测试:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
      
      
$ python -m tensorflow.models.image.mnist.convolutional
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Successfully 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
Initialized!
Step 0 (epoch 0.00), 7.1 ms
Minibatch loss: 8.334, learning rate: 0.010000
Minibatch error: 85.9%
Validation error: 84.6%
Step 100 (epoch 0.12), 205.8 ms
Minibatch loss: 3.250, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 7.6%
...
Step 8500 (epoch 9.89), 204.7 ms
Minibatch loss: 1.618, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.8%
Test error: 0.8%

主机是分配了4个核的 E7- 4830 @ 2.13GHz 虚机,每步耗时205 ms左右。

“2.20 更新”在 r1.0 下运行可能会遇到参数错误的问题,如下:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
      
      
$ python -m tensorflow.models.image.mnist.convolutional
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
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/{username}/.local/lib/python3.5/site-packages/tensorflow/models/image/mnist/convolutional.py", line 339, in <module>
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
File "/home/{username}/.local/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "/home/{username}/.local/lib/python3.5/site-packages/tensorflow/models/image/mnist/convolutional.py", line 231, in main
logits, train_labels_node))
File "/home/{username}/.local/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 1684, in sparse_softmax_cross_entropy_with_logits
labels, logits)
File "/home/{username}/.local/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 1533, in _ensure_xent_args
"named arguments (labels=..., logits=..., ...)" % name)
ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)

可参考 Fix *_cross_entropy_with_logits calls #864, 将

      
      
1
      
      
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits, train_labels_node))

Changed

      
      
1
      
      
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=train_labels_node, logits=logits))

Reference

TensorFlow Download and Setup

TensorFlow Developer Summit heavyweight -Google release TensorFlowV1.0

Isuue with models/tutorials/image/mnist #857

Original link large column  https://www.dazhuanlan.com/2019/08/16/5d55fe046f530/

Guess you like

Origin www.cnblogs.com/chinatrump/p/11415083.html