美文网首页我爱编程
虚拟环境下用国内镜像安装TensorFlow,指定GPU

虚拟环境下用国内镜像安装TensorFlow,指定GPU

作者: 嫩芽33 | 来源:发表于2017-07-14 14:23 被阅读0次

1. 卸载

pip uninstall tensorflow

2.设置虚拟环境

(http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html)

【虚拟环境是一个将不同项目所需求的依赖分别放在独立的地方的一个工具,它给这些工程创建虚拟的Python环境。在安装TensorFlow之前可以先创建虚拟的Python环境用于安装。优点是:它解决了“项目X依赖于版本1.x,而项目Y需要项目4.x”的两难问题,而且使你的全局site-packages目录保持干净和可管理】  

运行

virtualenv ~/VENVs/tf

3.安装TensorFlow

可直接运行

pip install tensorflow_gpu

4.更换pip源到国内镜像,安装TensorFlow

在网速很差的情况下,可更换pip源到国内镜像,运行

pip install tensorflow_gpu -i http://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn


关于国内镜像,看到了这篇博客 http://blog.csdn.net/chenghuikai/article/details/55258957

(1)pip国内的一些镜像

阿里云 http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

豆瓣(douban) http://pypi.douban.com/simple/

清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

(2)修改源方法:

a.临时使用:

可以在使用pip的时候在后面加上-i参数,指定pip源

eg: pip install *** -i https://pypi.tuna.tsinghua.edu.cn/simple

b.永久修改:

Linux:

修改 ~/.pip/pip.conf (没有就创建一个), 内容如下:

[global]

index-url = https://pypi.tuna.tsinghua.edu.cn/simple

windows:

直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini,内容如下

[global]

index-url = https://pypi.tuna.tsinghua.edu.cn/simple

5. 在虚拟环境下运行TensorFlow

先进入虚拟环境

. ~/VENVs/tf/bin/activate

就会进入到运行TensorFlow的环境下,显示为:(tf) [****]$

之后就正常运行

python *.py

退出虚拟环境,运行

deactivate

6. 安装其他包,比如scikit-image

pip install scikit-image pillow

同样,如果网速慢,可以运行

pip install scikit-image pillow -i http://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn

7. 看服务器gpu使用情况:

nvidia-smi

8. 指定gpu:

http://www.cnblogs.com/darkknightzh/p/6591923.html

当有多个GPU,tensorflow默认全部使用。如果想只使用部分GPU,可以设置CUDA_VISIBLE_DEVICES。在调用python程序时,可以使用

(1)终端执行程序时设置使用的GPU

CUDA_VISIBLE_DEVICES=1,2 python my_script.py            (Devices 1, 2 will be visible; device 0,3 is masked)

CUDA_VISIBLE_DEVICES=""            (No GPU will be visible)

(2)python代码中设置使用的GPU

import os

os.environ["CUDA_VISIBLE_DEVICES"] = "2"


作为新手,要学习的还很多~~记录下来方便以后查找

相关文章

网友评论

    本文标题:虚拟环境下用国内镜像安装TensorFlow,指定GPU

    本文链接:https://www.haomeiwen.com/subject/ucnnhxtx.html