Ubuntu系统---以virtualenv方式安装Tensorflow-CPU
Ubuntu系統---以virtualenv方式安裝Tensorflow-CPU
一、安裝環境
環境:Ubuntu18.04+CPU+python2.7
本文介紹:如何在ubuntu上以virtualenv方式安裝tensorflow。
采用virtualenv方式安裝的原因?系統中的多個python混用會導致$PYTHONPATH混亂,或者各個工程對于package的版本要求不同等等情況。有一個簡單的解決方案就是用virtualenv來隔離多個python,其本質只是實現隔離不同python中$PYTHONPATH的路徑,當然也可以衍生到隔離多個$PATH。
二、安裝步驟
2.1 準備
如果你有兩個項目,一個需要python2.7開發,一個需要python3.5開發,那么virtualenv是一個很好的選擇。如果你想創建一個python2.7和python3.5的虛擬環境的話。首先你的電腦上得裝有python2.7和python3.5,而且需要裝好pip和pip3。
2.2 安裝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 virtualenv2.3 創建 Virtualenv 虛擬環境
進入你想安裝tensorflow的父目錄下,然后執行下面命令建立虛擬環境:
$ cd TF-virtualenv
$? virtualenv --system-site-packages tensorflow
2.4 激活虛擬環境并安裝tensorflow
對于python2.7,則執行如下命令:
$ 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: $ pip3 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: $ pip3 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 ?2.4 驗證Tensorflow CPU版本
在終端執行如下命令進入python shell環境: $ cd TF-virtualenv $ cd tensorflow $ python >>> 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)) ? 總結使用過程: 2.4.1 virtualenv中激活tensorflow 當環境創建好了,我們必須在每次使用tensorflow時激活它。 $ source ~/tensorflow/bin/activate 在同一個環境中,這意味著你會在你的shell中看到它。 (tensorflow)$ 2.4.2 使用tensorflow 鍵入python和下面的python代碼。 python # Python import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello)) 這是我的結果。雖然它與官方驗證結果不同,但是應該沒問題。 >>> print(sess.run(hello)) b'Hello, TensorFlow!' >>>退出Python環境
>>> exit()
也可以, 使用類似命令運行 TensorFlow 程序: (tensorflow)$ cd tensorflow/models/image/mnist (tensorflow)$ python convolutional.py
2.4.3 停用virtualenv
# 當使用完 TensorFlow (tensorflow)$ deactivate # 停用 virtualenv $ # 你的命令提示符會恢復原樣
附,可以參考官網: http://www.tensorfly.cn/tfdoc/get_started/os_setup.html
再說明,
ubuntu下python import tensorflow顯示非法指令(核心已轉儲)
1、版本
ubuntu版本為18.04
python為2.7
tensorflow為pip安裝的1.10.0版本
2、解決
刪除原先的tensorflow:sudo pip uninstall tensorflow
虛擬機Vitualenv下不用sudo
下載低版本的tensorflow版本安裝就可以解決問題了。
===========================================
從Tensorflow1.2開始,不再支持GPU在Mac OS X上的安裝。
tensorFlow安裝命令
$ pip install tensorflow # Python 2.7; CPU support $ pip3 install tensorflow # Python 3.n; CPU support如果上面的安裝出錯,可以通過以下命令安裝最新的TensorFlow:
$ sudo pip install --upgrade tfBinaryURL # Python 2.7 $ sudo pip3 install --upgrade tfBinaryURL # Python 3.nwhere tfBinaryURL identifies the URL of the TensorFlow Python package. The appropriate value of tfBinaryURL depends on the operating system and Python version. Find the appropriate value for tfBinaryURL here. For example, if you are installing TensorFlow for Mac OS and Python 2.7 issue the following command:
$ sudo pip3 install --upgrade \https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0-py2-none-any.whl上面以安裝TensorFlow 1.3.0為例,若輸入的是pip,則為python2安裝TensorFlow 1.3.0
更新TensorFlow
(tensorflow)$ pip install --upgrade tensorflow # for Python 2.7 (tensorflow)$ pip3 install --upgrade tensorflow # for Python 3.n=====================================================================================
ubuntu下virtualenvwrapper的安裝與卸載
1. virtualenvwrapper的安裝 $ sudo pip install virtualenvwrapper 2. virtualenvwrapper的卸載 $ sudo pip uninstall virtualenvwrapper ?轉載于:https://www.cnblogs.com/carle-09/p/9579149.html
總結
以上是生活随笔為你收集整理的Ubuntu系统---以virtualenv方式安装Tensorflow-CPU的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中常见功能包描述
- 下一篇: 泛型算法(二十三)之排列算法