使用Anaconda安装TensorFlow

来源:互联网 发布:如何裁剪linux内核 编辑:程序博客网 时间:2024/05/01 17:21

操作系统: Ubuntu 14.04

1. 安装Anaconda

从anaconda官网(https://www.continuum.io/downloads)上下载Linux版本的安装文件(推荐Python 2.7版本),运行sh完成安装。

2. 建立一个tensorflow的运行环境

[plain] view plain copy
  1. # Python 2.7  
  2. $ conda create -n tensorflow python=2.7  
  3.   
  4. # Python 3.4  
  5. $ conda create -n tensorflow python=3.4  
  6.   
  7. # Python 3.5  
  8. $ conda create -n tensorflow python=3.5  

3.在conda环境中安装tensorflow

在conda环境中安装tensorflow的好处是可以便捷的管理tensorflow的依赖包。分为两个步骤:激活上一步建立的名为tensorflow的conda环境;用conda或者pip工具安装Tensorflow,作者选择的是pip方式。

3.1 pip方式

pip方式需要首先激活conda环境

[plain] view plain copy
  1. $ source activate tensorflow  

然后根据要安装的不同tensorflow版本选择对应的一条环境变量设置export语句(操作系统,Python版本,CPU版本还是CPU+GPU版本)

[plain] view plain copy
  1. # Ubuntu/Linux 64-bit, CPU only, Python 2.7  
  2. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl  
  3.   
  4. # Ubuntu/Linux 64-bit, GPU enabled, Python 2.7  
  5. # Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.  
  6. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl  
  7.   
  8. # Mac OS X, CPU only, Python 2.7:  
  9. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py2-none-any.whl  
  10.   
  11. # Mac OS X, GPU enabled, Python 2.7:  
  12. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.10.0-py2-none-any.whl  
  13.   
  14. # Ubuntu/Linux 64-bit, CPU only, Python 3.4  
  15. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl  
  16.   
  17. # Ubuntu/Linux 64-bit, GPU enabled, Python 3.4  
  18. # Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.  
  19. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl  
  20.   
  21. # Ubuntu/Linux 64-bit, CPU only, Python 3.5  
  22. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp35-cp35m-linux_x86_64.whl  
  23.   
  24. # Ubuntu/Linux 64-bit, GPU enabled, Python 3.5  
  25. # Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.  
  26. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp35-cp35m-linux_x86_64.whl  
  27.   
  28. # Mac OS X, CPU only, Python 3.4 or 3.5:  
  29. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py3-none-any.whl  
  30.   
  31. # Mac OS X, GPU enabled, Python 3.4 or 3.5:  
  32. (tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.10.0-py3-none-any.whl  

最后根据是python 2还是3版本选择一句进行安装。

[plain] view plain copy
  1. # Python 2  
  2. (tensorflow)$ pip install --ignore-installed --upgrade $TF_BINARY_URL  
  3.   
  4. # Python 3  
  5. (tensorflow)$ pip3 install --ignore-installed --upgrade $TF_BINARY_URL  


3.2 conda方式

conda上面目前有人已经做好了tensorflow的pkg,但是版本不一定最新,且只有CPU版本,不支持GPU。

步骤也是首先激活conda环境,然后调用conda install 语句安装.

[plain] view plain copy
  1. $ source activate tensorflow  
  2. (tensorflow)$  # Your prompt should change  
  3.   
  4. # Linux/Mac OS X, Python 2.7/3.4/3.5, CPU only:  
  5. (tensorflow)$ conda install -c conda-forge tensorflow  

上面的步骤完成后,从conda环境中退出:

[plain] view plain copy
  1. (tensorflow)$ source deactivate  



4. 测试安装

[plain] view plain copy
  1. $ source activate tensorflow  
  2. (tensorflow)$  # Your prompt should change.  
  3. # Run Python programs that use TensorFlow.  
  4. ...  
  5. # When you are done using TensorFlow, deactivate the environment.  
  6. (tensorflow)$ source deactivate  
0 0