**Note content: Install TensorFlow (python2.7 version) on Ubuntu **Note date: 2018-01-31
**My system environment: **
**Two versions of TensorFlow: **
The main installation forms of TensorFlow are as follows:
pip is the Python package management system: Pip Install Packages recursive abbreviation
The format of the pip installation package command is as follows:
pip install package name
The format of the pip uninstall software package command is as follows:
pip uninstall package name
I will use the virtualenv virtual environment for installation first, and then I will introduce the use of pip to install to the local system. The following is the official installation document of TensorFlow:
$ sudo apt-get install python-pip python-dev python-virtualenv
[ zero@zero-virtual-machine:~]$ virtualenv --system-site-packages tensorflow
Running virtualenv with interpreter /usr/bin/python2
New python executable in/home/zero/tensorflow/bin/python2
Also creating executable in/home/zero/tensorflow/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
[ zero@zero-virtual-machine:~]$ source ~/tensorflow/bin/activate(tensorflow)[zero@zero-virtual-machine:~]$
( tensorflow)[zero@zero-virtual-machine ~]$ easy_install -U pip
( tensorflow)[zero@zero-virtual-machine ~]$ pip install --upgrade tensorflow
Tip: If your pip installation is very slow, it is because the default is to use a foreign source, we can replace it with a domestic source:
[ zero@zero-virtual-machine ~]$ vim .pip/pip.conf #Edit to the following
[ global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com #Without this sentence, there will be a warning
disable-pip-version-check =true #Version not checked
timeout =120 #Timeout setting
Note: If there is no .pip/pip.conf, just create it.
( tensorflow)[zero@zero-virtual-machine ~]$ python
Python 2.7.12(default, Dec 42017,14:50:18)[GCC 5.4.020160609] on linux2
Type "help","copyright","credits" or "license"for more information.>>>import tensorflow
>>>
Some other operations:
The deactivate command can exit the virtual environment:
( tensorflow)[zero@zero-virtual-machine ~]$ deactivate
[ zero@zero-virtual-machine ~]$
To delete a virtual environment, you only need to delete the generated directory:
[ zero@zero-virtual-machine ~]$ rm -rf tensorflow/[zero@zero-virtual-machine ~]$
Above we demonstrated the installation of tensorflow through virtualenv, and then we will demonstrate the installation of tensorflow through local pip:
[ zero@zero-virtual-machine ~]$ sudo apt-get install python-pip python-dev
[ zero@zero-virtual-machine ~]$ pip install tensorflow
[ zero@zero-virtual-machine ~]$ python
Python 2.7.12(default, Dec 42017,14:50:18)[GCC 5.4.020160609] on linux2
Type "help","copyright","credits" or "license"for more information.>>>import tensorflow
>>>
This method is installed on the local system, while the previous installation method is installed in a virtual environment. You need to enter the virtual environment every time to use tensorflow, and it is not necessary to install it on the local system.
Uninstall tensorflow using the following command:
sudo pip uninstall tensorflow
We can use pip to install some basic Python libraries:
pip install numpy
pip install pandas
pip install matplotlib
Above we have installed TensorFlow, then we will write the first TensorFlow program: Hello World
[ zero@zero-virtual-machine ~]$ mkdir TensorFlow
[ zero@zero-virtual-machine ~]$ cd !$
cd TensorFlow
[ zero@zero-virtual-machine ~/TensorFlow]$ mkdir HelloWorld
[ zero@zero-virtual-machine ~/TensorFlow]$ cd !$
cd HelloWorld
[ zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$
[ zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$ vi helloworld.py #The content is as follows
# - *- coding: UTF-8-*-
# Introduce Tensorflow library
import tensorflow as tf
# Create a constant Operation (operation)
hw = tf.constant("Hello Wolrd!")
# Start a Tensorflow Session (session)
sess = tf.Session()
# Run Graph (compute graph)
print sess.run(hw)
# Close Session
sess.close()
[ zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$ python helloworld.py
2018- 02- 0100:22:43.680173: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Hello Wolrd![zero@zero-virtual-machine ~/TensorFlow/HelloWorld]$
As above, you can see that Hello Wolrd! is output normally, and some warning messages are printed. So far, our first TensorFlow program has been written.
The general meaning of this warning is: tensorflow thinks your computer cpu is OK, supports AVX (Advanced Vector Extensions), and the calculation speed can be improved, so you can open a better and faster mode, but the mode you are using may not be relatively So fast, so this is not actually an error, so if the current mode is not too slow, just ignore this warning.
If you don't want the output of this warning message, you can add these two sentences to the code:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
Then no warning message will be output.
Recommended Posts