ubuntu16.04 +tensorflow1.4安装 + 运行问题解决

来源:互联网 发布:云烟 淘宝客 编辑:程序博客网 时间:2024/05/16 18:16

ubuntu16.04 +tensorflow1.4安装

1、下载anaconda最新版,并安装:

bash Anaconda3-5.0.1-Linux-x86_64.sh
安装完成后输入:source ~/.bashrc.环境配置生效。

2、安装tensorflow环境以及tensorflow软件
conda create -n tensorflow python=3.5
source activate tensorflow
pip install --ignore-installed --upgrade \
 https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp35-cp35m-linux_x86_64.whl
注意:
@国内无法访问https://storage.googleapis.com/,去github上找对应的下载代替掉。但不知道为什么,安装完成后没有tensorboard功能。
@找国内清华镜像:https://mirrors.tuna.tsinghua.edu.cn/help/tensorflow/
@这里如果python版本和tensorflow-1.4.0-cp35-cp35m-linux_x86_64.whl的版本不一致,安装时会出现*.whl is not supported wheel on this platform的错误提升信息。

3、运行:
source activate tensorflow
否则import tensorflow时会提示找不到。

sudo ldconfig /usr/local/cuda/lib64
或者
export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
否则会提示libcublas.so.8.0:cannot open shared object file:No such file or directory

4、电脑重启后无法进入source
~/.bashrc中添加:

exprot PATH=/home/chenxi/Anaconda/bin:$PATH

tensorflow问题:

问题一、报错 failed call to cuDevicePrimaryCtxRetain: CUDA_ERROR_INVALID_DEVICE
tensorflow/stream_executor/cuda/cuda_driver.cc:523] A non-primary context 0x3f287f0 exists before initializing the StreamExecutor. We haven't verified StreamExecutor works with that.
原因是在创建session时没有使用我想让它用的gpu,所以需要设置CUDA_VISIBLE_DEVICES
方法1 在python程序中
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
方法2
运行时 
CUDA_VISIBLE_DEVICES=1 python **.py
方法3
直接在环境变量中 export 

问题二、CUDA_ERROE_OUT_OF_MEMORY
E tensorflow/stream_executor/cuda/cuda_driver.cc:924] failed to alloc 17179869184 bytes on host: CUDA_ERROR_OUT_OF_MEMORY
W ./tensorflow/core/common_runtime/gpu/pool_allocator.h:195] could not allocate pinned host memory of size: 17179869184
Killed
是 服务器的GPU大小为M,tensorflow只能申请N(N<M),也就是tensorflow告诉你 不能申请到GPU的全部资源 然后就不干了。

解决方法:
找到代码中Session
在session定义前 增加

config = tf.ConfigProto(allow_soft_placement=True)
#最多占gpu资源的70%
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
#开始不会给tensorflow全部gpu资源 而是按需增加
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
这样就没问题了


问题三、训练中,GPU占用率低。
目前无解,ubuntu下能全速运行。

原创粉丝点击