首頁 > 軟體

Ubuntu上以virtualenv方式安裝TensorFlow

2020-06-16 17:38:08

本文介紹了如何在Ubuntu上以virtualenv方式安裝tensorflow。  

安裝pip和virtualenv:

# Ubuntu/Linux 64-bit

sudo apt-get install Python-pip python-dev python-virtualenv

# Mac OS X

sudo easy_install pip

sudo pip install --upgrade virtualenv

建立 Virtualenv 虛擬環境:

  進入你想安裝tensorflow的父目錄下,然後執行下面命令建立虛擬環境:

virtualenv --system-site-packages tensorflow

啟用虛擬環境並安裝tensorflow:

  對於python27,則執行如下命令:

source ./tensorflow/bin/activate  # If using bash

source ./tensorflow/bin/activate.csh  # If using csh

(tensorflow)$  # Your prompt should change

# Ubuntu/Linux 64-bit, CPU only:

pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled:

pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only:

pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py2-none-any.whl

  對於python3則執行如下命令:

source ./tensorflow/bin/activate  # If using bash

source ./tensorflow/bin/activate.csh  # If using csh

(tensorflow)$  # Your prompt should change

# Ubuntu/Linux 64-bit, CPU only:

pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled:

pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl

# Mac OS X, CPU only:

pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py3-none-any.whl

測試安裝:

  在終端執行如下命令進入python shell環境:

python

  在python shell環境中測試:

>>> import tensorflow as tf

>>> hello = tf.constant('Hello, TensorFlow!')

>>> sess = tf.Session()

>>> print(sess.run(hello))

Hello, TensorFlow!

>>> a = tf.constant(10)

>>> b = tf.constant(32)

>>> print(sess.run(a + b))

42

>>>

• 如果遇到如下錯誤:

1

2

  _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)

ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory

  那是你的CUDA安裝設定不對:

    安裝CUDA和CUDNN可以參考 這篇文章 。

  且新增如下兩行到你的 ~/.bashrc 檔案

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"

export CUDA_HOME=/usr/local/cuda

• 如果遇到如下錯誤:

Python 2.7.9 (default, Apr  2 2015, 15:33:21)

[GCC 4.9.2] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import tensorflow

I tensorflow/stream_executor/dso_loader.cc:93] Couldn't open CUDA library libcublas.so.7.0. LD_LIBRARY_PATH: :/usr/local/cuda/lib64

I tensorflow/stream_executor/cuda/cuda_blas.cc:2188] Unable to load cuBLAS DSO.

I tensorflow/stream_executor/dso_loader.cc:93] Couldn't open CUDA library libcudnn.so.6.5. LD_LIBRARY_PATH: :/usr/local/cuda/lib64

I tensorflow/stream_executor/cuda/cuda_dnn.cc:1382] Unable to load cuDNN DSO

I tensorflow/stream_executor/dso_loader.cc:93] Couldn't open CUDA library libcufft.so.7.0. LD_LIBRARY_PATH: :/usr/local/cuda/lib64

I tensorflow/stream_executor/cuda/cuda_fft.cc:343] Unable to load cuFFT DSO.

I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcuda.so locally

I tensorflow/stream_executor/dso_loader.cc:93] Couldn't open CUDA library libcurand.so.7.0. LD_LIBRARY_PATH: :/usr/local/cuda/lib64

I tensorflow/stream_executor/cuda/cuda_rng.cc:333] Unable to load cuRAND DSO.

  由安裝報錯可知,它使用的是7.0版本,故找不到,而如果你安裝的是7.5版本,則可以執行如下命令新增相應連結:

sudo ln -s /usr/local/cuda/lib64/libcudart.so.7.5 /usr/local/cuda/lib64/libcudart.so.7.0

sudo ln -s libcublas.so.7.5 libcublas.so.7.0

sudo ln -s libcudnn.so.4.0.4 libcudnn.so.6.5

sudo ln -s libcufft.so libcufft.so.7.0<br>sudo ln -s libcurand.so libcurand.so.7.0

Ubuntu 15.04下TensorFlow原始碼方式安裝 http://www.linuxidc.com/Linux/2016-07/133223.htm

如何評價Tensorflow和其它深度學習系統 http://www.linuxidc.com/Linux/2016-07/133221.htm


IT145.com E-mail:sddin#qq.com