Tensorflow - Installation on Windows

  • Install Python

下载 Python 3.6.4,然后安装,如果安装失败并提示权限问题,则尝试用Administrator运行安装包。

  • Install Tensorflow
$ cd D:\Tools\python-3.6.4
$ Scripts\pip3 install --upgrade tensorflow -v

......
    Removing file or directory d:\tools\python-3.6.4\lib\site-packages\setuptools\windows_support.py
    Removing file or directory d:\tools\python-3.6.4\scripts\easy_install-3.6.exe
    Removing file or directory d:\tools\python-3.6.4\scripts\easy_install.exe
    Successfully uninstalled setuptools-28.8.0

Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.10 numpy-1.13.3 protobuf-3.5.1 setuptools-38.2.5 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.13 wheel-0.30.0
Cleaning up...

如果安装GPU版的执行下面的命令:

$ cd D:\Tools\python-3.6.4
$ Scripts\pip3 install --upgrade tensorflow-gpu
  • Test Tensorflow
$ python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-12-25 14:27:41.518584: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
>>>
  • 示例 1
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> a = tf.placeholder(tf.float32)
>>> b = tf.placeholder(tf.float32)
>>> c = a + b
>>> print(sess.run(c, {a:5, b:7}))
12.0
>>> d = c * 3
>>> print(sess.run(d, {a:5, b:7}))
36.0
  • 示例 2
import tensorflow as tf
sess = tf.Session()
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W*x + b
 
init = tf.global_variables_initializer()
sess.run(init)
 
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas)
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

输出结果

23.66

计算步骤

x y’ y dy dy^2
1 0 0 0 0
2 0.3 -1 -1.3 1.69
3 0.6 -2 -2.6 6.76
4 0.9 -3 -3.9 15.21
23.66
  • 测试学习性能
$ git clone https://github.com/tensorflow/models.git
$ cd models/tutorials/image/cifar10
$ python cifar10_train.py

在配置GTX 1060的显卡机器上有如下输出:

$ python .\cifar10_train.py
>> Downloading cifar-10-binary.tar.gz 100.0%
Successfully downloaded cifar-10-binary.tar.gz 170052171 bytes.
Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
2018-06-08 22:02:28.088067: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-06-08 22:02:29.218715: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1356] Found device 0 with properties:
name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.6705
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 4.97GiB
2018-06-08 22:02:29.255508: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1435] Adding visible gpu devices: 0
2018-06-08 22:02:29.979030: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-06-08 22:02:29.999034: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:929]      0
2018-06-08 22:02:30.014485: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:942] 0:   N
2018-06-08 22:02:30.029338: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4740 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)
2018-06-08 22:02:35.241648: step 0, loss = 4.68 (176.6 examples/sec; 0.725 sec/batch)
2018-06-08 22:02:35.557645: step 10, loss = 4.62 (4050.7 examples/sec; 0.032 sec/batch)
2018-06-08 22:02:35.740644: step 20, loss = 4.44 (6994.6 examples/sec; 0.018 sec/batch)
2018-06-08 22:02:35.923668: step 30, loss = 4.42 (6993.6 examples/sec; 0.018 sec/batch)
2018-06-08 22:02:36.096645: step 40, loss = 4.37 (7399.8 examples/sec; 0.017 sec/batch)
2018-06-08 22:02:36.270653: step 50, loss = 4.42 (7356.0 examples/sec; 0.017 sec/batch)
...
2018-06-08 22:10:56.495653: step 26230, loss = 0.75 (7231.4 examples/sec; 0.018 sec/batch)  
2018-06-08 22:10:56.669646: step 26240, loss = 0.81 (7356.6 examples/sec; 0.017 sec/batch)  
2018-06-08 22:10:56.848646: step 26250, loss = 0.72 (7150.8 examples/sec; 0.018 sec/batch)  
2018-06-08 22:10:57.028645: step 26260, loss = 0.84 (7111.2 examples/sec; 0.018 sec/batch)  
2018-06-08 22:10:57.208645: step 26270, loss = 0.75 (7111.1 examples/sec; 0.018 sec/batch)  

再试试用CPU学习的性能,设置环境变量CUDA_VISIBLE_DEVICES为-1,然后重新学习:

$ $env:CUDA_VISIBLE_DEVICES = "-1"
$ python cifar10_train.py

输出如下内容:

Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
2018-06-08 22:57:16.501575: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-06-08 22:57:17.573366: E T:\src\github\tensorflow\tensorflow\stream_executor\cuda\cuda_driver.cc:406] failed call to cuInit: CUDA_ERROR_NO_DEVICE
2018-06-08 22:57:17.596320: I T:\src\github\tensorflow\tensorflow\stream_executor\cuda\cuda_diagnostics.cc:158] retrieving CUDA diagnostic information for host: mBook
2018-06-08 22:57:17.617780: I T:\src\github\tensorflow\tensorflow\stream_executor\cuda\cuda_diagnostics.cc:165] hostname: mBook
2018-06-08 22:57:19.719951: step 0, loss = 4.68 (386.7 examples/sec; 0.331 sec/batch)
2018-06-08 22:57:23.671922: step 10, loss = 4.62 (323.9 examples/sec; 0.395 sec/batch)
2018-06-08 22:57:27.478931: step 20, loss = 4.57 (336.2 examples/sec; 0.381 sec/batch)
2018-06-08 22:57:31.280924: step 30, loss = 4.61 (336.7 examples/sec; 0.380 sec/batch)
2018-06-08 22:57:35.059926: step 40, loss = 4.34 (338.7 examples/sec; 0.378 sec/batch)
2018-06-08 22:57:38.858922: step 50, loss = 4.32 (336.9 examples/sec; 0.380 sec/batch)
2018-06-08 22:57:42.650932: step 60, loss = 4.20 (337.6 examples/sec; 0.379 sec/batch)
2018-06-08 22:57:46.435932: step 70, loss = 4.24 (338.2 examples/sec; 0.378 sec/batch)
2018-06-08 22:57:50.228921: step 80, loss = 4.23 (337.5 examples/sec; 0.379 sec/batch)
2018-06-08 22:57:54.000922: step 90, loss = 4.04 (339.3 examples/sec; 0.377 sec/batch)
2018-06-08 22:57:57.859921: step 100, loss = 4.14 (331.7 examples/sec; 0.386 sec/batch)
...

对比可见GPU(GTX 1060)相比CPU(i7-7700HQ)在学习速度上快大约20倍,优势很明显。