1. Anaconda 安装
官方网站下载安装包,直接执行即可。
bash Anaconda3-4.3.1-Linux-x86_64.sh #Python 3.5 版本
查询安装信息
conda info
查询当前已经安装的库
conda list
安装库
conda install pkg_name
更新库
conda update pkg_name
conda update -- all #更新所有包
更新Anaconda
conda update conda
2. Tensorflow 安装
官方下载更新工具包的速度很慢,所以继续添加清华大学 TUNA提供的Anaconda仓库镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
conda install numpy #测试是否添加成功
Tensorflow安装
新建Anaconda 环境:
conda create -n tensorflow python=3.6
激活tensorflow虚拟环境(之后的使用每次也都要先激活虚拟环境才可用):
source activate tensorflow
关闭tensorflow虚拟环境:
source deactivate
命令搜索当前可用的tensorflow版本
anaconda search -t conda tensorflow
选择需要的版本:
anaconda show anaconda/tensorflow-gpu
从打印出的命令安装:
conda install --channel https://conda.anaconda.org/anaconda tensorflow-gpu
测试安装
source activate tensorflow
python
import tensorflow as tf
tf.__version__
输:'1.8.0'
遇到问题:
session = tf.session()
E tensorflow/core/common_runtime/direct_session.cc:154] Internal: CUDA runtime implicit initialization on GPU:0 failed. Status: unknown error
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1" #设置使用的GPU,并没用解决使用GPU的问题。
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
session = tf.Session()
print(sess.run(hello))
输出:
2018-06-24 20:35:02.462192: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-06-24 20:35:02.470414: E tensorflow/stream_executor/cuda/cuda_driver.cc:406] failed call to cuInit: CUDA_ERROR_NO_DEVICE
2018-06-24 20:35:02.470590: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:158] retrieving CUDA diagnostic information for host: zc-GE62-2QD
2018-06-24 20:35:02.470633: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:165] hostname: zc-GE62-2QD
2018-06-24 20:35:02.470778: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:189] libcuda reported version is: 384.130.0
2018-06-24 20:35:02.470887: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:193] kernel reported version is: 384.130.0
2018-06-24 20:35:02.470925: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:300] kernel version seems to match DSO: 384.130.0
这是由于TensorFlow 使用的是Cuda-9.2的驱动版本,可参照tensorflow官网的介绍:tf安装指导
并参照其中的CUDA安装链接:cuda 安装链接
本人下载了.run的安装文件:cuda_9.2.88_396.26_linux.run 和 cuda_9.2.88.1_linux.run, 先安装前者。
安装命令:
sudo ./cuda_9.2.88_396.26_linux.run
sudo ./cuda_9.2.88.1_linux.run
注意:安装CUDA驱动需要ctrl+alt+f1, 进入tty命令行安装,并关闭x-config:sudo service lightdm stop
另外,安装的时候需要注意不要安装open-gl选项,不选择nvidia-xconfig选项,选择上述选项会导致重启后
进入登录界面的无限循环,如果不幸进入了循环,请参照恢复链接:登录循环恢复链接
测试:
(tensorflow) zc@zc-GE62-2QD:~$ python
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('hello, Tensorflow')
>>> session=tf.Session()
2018-07-28 00:02:35.801458: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-07-28 00:02:35.986928: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-07-28 00:02:35.987282: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1356] Found device 0 with properties:
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.176
pciBusID: 0000:01:00.0
totalMemory: 1.96GiB freeMemory: 1.92GiB
2018-07-28 00:02:35.987302: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-07-28 00:05:14.965212: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-07-28 00:05:14.965244: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929] 0
2018-07-28 00:05:14.965253: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0: N
2018-07-28 00:05:14.965397: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1687 MB memory) -> physical GPU (device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0)
>>> print(session.run(hello))
b'hello, Tensorflow'
>>>
终于配置GPU 成功,接下来的一个博客将实现CNN 和 RNN的Mnist识别实现代码。