TensorFlow study notes 0- installation TensorFlow environment

TensorFlow study notes 0- installation TensorFlow environment

Author: YunYuan

EDITORIAL

  • System: Windows Enterprise 10 x64
  • CPU:Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
  • GPU: NVIDIA GeForce 1050 GTX Ti

    Therefore, the present note-Win10 64-bit system, the GPU to set up TensorFlow development environment.

TensorFlow-GPU environment installation

  1. First, download and install Anaconda, unrestricted version, the latest version can be, I am here Anaconda3-5.2.0-Windows-x86_64; pay attention to check the setting environment variables

  2. Go https://github.com/fo40225/tensorflow-windows-wheel download a version tensorflow you want to download, here are select tensorflow-windows-wheel/1.10.0/py36/GPU/,
    download cuda92cudnn72avx2folder *.whlfile I downloaded tensorflow_gpu-1.10.0-cp36-cp36m-win_amd64.whl, you can see here 文件名given the configuration requirements, cuda 9.2 version, cudnn requires version 7.2 , note the two version numbers.

  3. To https://developer.nvidia.com/cuda-toolkit-archive and https://developer.nvidia.com/rdp/cudnn-archive respectively corresponding version downloaded and CUDA CUDNN: cuda_9.2.148_win10and patchpackets; and cudnn7.2. Installation and patch package CUDA; Cudnn then obtained after extracting three small folders (include, bin, lib) CUDA copied to the installation directory (the default directory installation C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2) can;

  4. Open Anaconda promt (open to the best administrator), enter in the Start menu conda --versionto verify that the installation was successful conda;

  5. Enter the Anaconda promt in conda info --envsorder to check what are the current environment;

  6. Create a new environment conda create --name tf python=3.6, which environment name is called tf, the middle will enter y / n, you can press y; if the latter need to remove the environmentconda remove -n tf --all

  7. Entering the environment activate tf

  8. Check the version of Python python --version

  9. Enter the directory where you are .whl file, input pip install tensorflow_gpu-1.10.0-cp36-cp36m-win_amd64.whlcan be;

    If it is determined later need to use OpenCV, can also be installed pip install opencv-pythonOpenCV package (if you have not heard what OpenCV, please automatically ignore the statement)

  10. In the current environment type python into the python editing environment, enter the following code to test
import tensorflow as tf
hello = tf.constant("Hello, Tensorflow!")
sess = tf.Session()
print(sess.run(hello))

a=tf.constant(10)
b = tf.constant(20)
print(sess.run(a+b))

test environment

  1. Install and test the Spyder:

Open Anaconda Navigator (Start Menu -> Anaconda 3-> Anaconda Navigator, Application on >> Select tf), engage in a spyder play,
click on the following spyder "install", installed becomes "Launch", and click on it to go a.

In spyder says Hello to tensorflow!
import tensorflow as tf hello = tf.constant("Hello, Tensorflow!") sess = tf.Session() print(sess.run(hello))

  1. Further testing:

First look tensorflow is not installed properly and can be imported:

A test code:

import tensorflow as tf
a = tf.test.is_built_with_cuda()  # 判断CUDA是否可以用
b = tf.test.is_gpu_available(
cuda_only=False,
min_cuda_compute_capability=None
)  # 判断GPU是否可以用
print(a)
print(b)

The output is:

True
True

On behalf of CUDA and GPU available

Test Code II:

import tensorflow as tf
#Creates a graph.

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)

#Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

#Runs the op.
print(sess.run(c))

The output is:

[[22. 28.] [49. 64.]]
  1. GPU test run using the case of TensorFlow

The following is not directly see the code is used in the GPU

Test code three:

import tensorflow as tf
with tf.device('/cpu:0'):
  a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
  b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
with tf.device('/gpu:1'):
 c = a + b
sess =tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(tf.global_variables_initializer())
print(sess.run(c))

Note :

  1. Three in the above test code may be an error, you need to change the code

    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))

among them

  • allow_soft_placement=TrueShow: computing device can choose, if this parameter is not being given. Because not all operations can be placed on the GPU, if forced operation will not be put on the GPU assigned to the GPU, it will be thrown.

  • log_device_placement=True Show: log log file computing device will be displayed in real time (using the Python command line will be displayed not displayed when using spyder, spyder can use performance analysis tool for viewing).

In no hurry to run to open the Task Manager, click properties, find your own graphics card NVIDIA GPU the column, clicking, you can display the utilization of the GPU, under normal circumstances, you do not have to run any program, what are the GPU utilization is 0, no waves, then you run the code three, you will find GPU utilization began to change.

Further

  • The above has been fully tested tensorflow-gpu development environment windows10 system. If you want to know more about it: you can try to learn about this code:
import tensorflow as tf
import numpy as np

# 使用 NumPy 生成假数据(phony data), 总共 100 个点.
x_data = np.float32(np.random.rand(2, 100)) # 建立一个2*100的随机数的矩阵
y_data = np.dot([0.100, 0.200], x_data) + 0.300  # (2*1)点乘(2*100)

# 构造一个线性模型,这里创建了一个"计算图"
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b

# 最小化方差,这里给出你要优化的指标以及优化的方式
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 启动图 (graph),并进行初始化
init = tf.initialize_all_variables()
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
sess.run(init)

# 拟合平面
for step in range(0, 201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
# 得到最佳拟合结果 W: [[0.100  0.200]], b: [0.300]

#关闭会话
sess.close()

Written in the last

In order to ensure the future of the environment not unitary moths,

Do not change the GPU driver

Do not updated GPU driver

Do not updated GPU driver

If you accidentally updated the graphics driver, press notes reinstall the configuration environment.

Guess you like

Origin www.cnblogs.com/charleechan/p/11008500.html