TensorFlow Installation and Example-(Ubuntu16.04.1 & Anaconda3)

Source: Internet
Author: User
Tags deprecated virtualenv git clone

TensorFlow Installation and Example-(Ubuntu16.04.1 & Anaconda3)

    1. Python-pip and Python-dev

PIP is the default package manager for Python, install TensorFlow directly with PIP, install both packages

Command: Apt-get install PYTHON-PIP Python-dev python-virtualenv

You can virtualenv create an isolated container to install TensorFlow. This is optional, which makes it easier to troubleshoot installation problems.

    1. Installing Anaconda3

Command: Bash anaconda3-4.3.1-linux-x86_64.sh

Anaconda3 is a scientific computing release of Python, built with hundreds of of libraries that Python often uses, as well as many machine learning and data mining libraries, easy to Scikit-learn, NumPy, scipy and Pandas, There are also some tensorflow dependent libraries that are recommended to install this version of Python.

    1. Ubuntu (32-bit) installation TensorFlow error

Export Tf_binary_url = HTTPS://STORAGE.GOOGLEAPIS.COM/TENSORFLOW/LINUX/CPU/TENSORFLOW-1.0.0RC0-CP35-CP35M-LINUX_X86_64.WHL

Pip Install--upgrade $ tf_binary_url

The TensorFlow does not support 32-bit and supports only 64-bit . This is too pit Dad, my computer performance is not, has been using VirtualBox or VMware running 32-bit Ubuntu (12.04), just started to think that Ubuntu version is too low, has been upgraded to UBUNTU16, found or not, Finally, we found that TensorFlow only supports 64-bit.

This is definitely a big pit, careful not to enter!

The following is an error:

TENSORFLOW-1.0.0RC0-CP35-CP35M-LINUX_X86_64.WHL is not a supported wheel on this platform

When this error occurs, check to see if the Ubuntu system is 32-bit or 64-bit:

Command: Uname–m

Tip i686 indicates that the system is a 32-bit version.

    1. VirtualBox installing 64-bit Ubuntu

Win7 on the Vitualbox virtual machine is not supported by default to install 64 for the virtual machine system, as shown below, you can select only 32-bit installation:

This is because the motherboard bios of the PC is not enabled by default to enable motherboard virtualization Technology (virtualization). To run some operating systems, virtualization software and virtual machines, hardware virtualization needs to be enabled. In most cases, operating systems that do not require virtualization technology can function properly in systems with virtualization technology enabled, but some operating systems that require this technology must be enabled for virtualization to run.
All the latest processors and motherboards support virtualization technology, check if your motherboard vendor is supported and know how to enable or disable VT in the BIOS. When virtualization technology is enabled on the motherboard, the operating system can detect it immediately.

Enable motherboard virtualization technology, Lenovo Computer (my) Restart the system press F2 into the BIOS, after entering the BIOS:

Advanced->CPU menu, Inter (CR) virtualization

Select Enable, then press F10 to save, restart the computer can be VirtualBox on the 64-bit system options, and then download the latest 64-bit operating system image on the Ubuntu website Ubuntu-16.04.2-desktop-amd64.iso

http://cn.ubuntu.com/download/

Installing an Ubuntu virtual machine is simple, so here's not a detailed word.

    1. VirtualBox Shared Folders

The importance of sharing files between hosts (Windows) and virtual machines (Ubuntu) is not much to say, but here's a quick introduction to how to set up shared folders.

Create a shared folder under any of the Windows drive characters, such as E:\Shared, then set mount point in Ubuntu system , mount point directory to add "shared" directory, then execute "mount-t vboxsf shared/mnt/share/" , you can complete the settings for the shared folder.

Restart the Ubuntu system into the/media directory to see the sf_shared directory, which is the shared share directory of Windows, as follows:

This makes it easy to share files between windows and Ubuntu virtual machines.

To set up automatic mounts:

That is, add an item to the Etc/fstab

Shared/mnt/share vboxsf Rw,gid=110,uid=1100,auto 0 0

    1. Install the correct ANACONDA3 version

On 64-bit Ubuntu Anaconda3 must install 64-bit Anaconda3 namely Anaconda3-4.3.1-linux-x86_64.sh, If you accidentally downloaded the 32-bit version of anaconda3-4.3.1-linux-x86.sh, install it in Ubuntu (64-bit):

Command: Bash anaconda3-4.3.1-linux-x86.sh

The following error is reported: cannot execute native linux-32 binary, output from ' Uname–a ' is

The correct is to install the 64-bit ANACONDA3, namely:

Command: Bash anaconda3-4.3.1-linux-x86_64. Sh

    1. Error TENSORFLOW-1.0.0RC0-CP35-CP35M-LINUX_X86_64.WHL is not a supported wheel on this platform

Ubuntu version Yes, when installing TensorFlow still error:

said that the PIP version is low, so follow the prompts: Pip Install–upgrade pip, and then install TensorFlow again, still reported the following error,

The Python version of the currently installed Anaconda3 is then viewed by command python–version, which is Python 3.6.0

And the tensorflow that we're going to install is based on Python 3.5, which can be installed by command: Pip Install–upgrade https://storage.googleapis.com/tensorflow/linux/ Cpu/tensorflow-1.0.0rc0-cp35-cp35m-linux_x86_x64.whl

As can be seen, CP3, cp35m is the representative of the Python version is 3.5, and we previously installed Anaconda3 Python version 3.6.0 does not match, so decisively modify the TensorFlow installation command:

Pip Install–upgrade HTTPS://STORAGE.GOOGLEAPIS.COM/TENSORFLOW/LINUX/CPU/TENSORFLOW-1.0.0RC0-CP36-CP36M-LINUX_X86_X64.WHL

You can see that the TensorFlow is installed correctly!

Look at the following tips to indicate that TENSORFLOW-1.0.0RC0 has been properly installed:

    1. TensorFlow instances

The following is an example of how tensorflow is used and how it is trained to get optimized parameters. In this example, Y_data is the actual output, where X_data is the raw training input data, its weight is 0.1, the bias is 0.3, that is, a linear line, Y is the predictive value of the training model, TensorFlow training to achieve the goal is to make Y and Y_ Data is the mean variance of the predicted value and the actual value loss the smallest, after the series of training, if the training obtained weights weight, and biases bias approximation of 0.1 and 0.3 that the training achieved good results.

"""

Please note that this code was only for Python. If you is using Python, please modify the code accordingly.

"""

Import TensorFlow as TF

Import NumPy as NP #科学计算的模块

# Create Data

X_data = Np.random.rand (+). Astype (Np.float32) #生成100个随机数列, the data type is FLOAT32,TF most of them are float32

Y_data = x_data*0.1 + 0.3 #0.1 is a weight, 0.3 is biased

# # Create TensorFlow structure start # # #

Weights = tf. Variable (Tf.random_uniform ([1], -1.0, 1.0)) #用Variable初始化Weights权重变量-Random series Generation-1 to 1 of 1-dimensional data,

biases = tf. Variable (Tf.zeros ([1])) #偏置初始化为0,

y = weights*x_data + biases #所要预测的y, by training the Y to approximate the real y_data, the optimized parameter variables are Weights and biases

Loss = Tf.reduce_mean (Tf.square (y-y_data)) #预测的y和实际的y_data的差别, i.e. mean variance

Optimizer = Tf.train.GradientDescentOptimizer (0.5) #用tf的梯度下降优化器减少误差loss, improve the accuracy of the parameters, 0.5 for learning efficiency (generally less than 1)

Train = optimizer.minimize (loss) #训练以减少误差

init = Tf.initialize_all_variables () #初始化tf结构图中的所有变量, this is weights and biases

# # CREATE TensorFlow structure End # # #

Sess = tf. Session () #激活创建的结构图, sesssion is the dialogue control of the execution command of the neural network

Sess.run (init) #激活init, i.e. all structures

For step in range (201): #让神经网络一步一步的训练 201 Steps

Sess.run (train) # Start training

If step% = = 0: #每间隔20步打印以下训练得到的变量值

Print (step, Sess.run (Weights), Sess.run (biases))

Training effect:

We see that each of the 20 steps prints the weights and biases of the training, and with the weight and bias of the training gradually approaching 0.1 and 0.3, the model passed the training to achieve good results.

    1. Instance warning handling

We also see some warnings during the run:

WARNING:tensorflow:From tf_exam1.py:23:initialize_all_variables (from Tensorflow.python.ops.variables) is Deprecated and'll is removed after 2017-03-02.

Instructions for updating:

Use ' Tf.global_variables_initializer ' instead.

This means that initialize_all_variables is going to be deprecated, and it is recommended to replace it with the Global_variables_initializer () function. So follow the prompts with a new function instead.

W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn ' t compiled to use SSE4.2 instructions, BU T these is available on your machine and could speed up CPU computations.

W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn ' t compiled to use AVX instructions, but th ESE is available on your machine and could speed up CPU computations

These warings mean that you have these instruction sets available on your machine and use them to speed up your CPU, but your tensorflow does not use these instruction sets when compiling.

This is a problem with the PIP install directive that my TensorFlow uses when installing. There are two main solutions, one is to modify the display level of warning information, so that this information no longer appear, and the other is to recompile the installation TensorFlow, use these instruction sets at compile time. Here I try a second solution. And because I don't have an efficient GPU on my machine, I'm trying to install a CPU version.

This warning is not processed because it does not affect the experiment, so the program after the first warning has changed to:

"""

Please note that this code was only for Python. If you is using Python, please modify the code accordingly.

"""

Import TensorFlow as TF

Import NumPy as NP #科学计算的模块

# Create Data

X_data = Np.random.rand (+). Astype (Np.float32) #生成100个随机数列, the data type is FLOAT32,TF most of them are float32

Y_data = x_data*0.1 + 0.3 #0.1 is a weight, 0.3 is biased

# # Create TensorFlow structure start # # #

Weights = tf. Variable (Tf.random_uniform ([1], -1.0, 1.0)) #用Variable初始化Weights权重变量-Random series Generation-1 to 1 of 1-dimensional data,

biases = tf. Variable (Tf.zeros ([1])) #偏置初始化为0,

y = weights*x_data + biases #所要预测的y, by training the Y to approximate the real y_data, the optimized parameter variables are Weights and biases

Loss = Tf.reduce_mean (Tf.square (y-y_data)) #预测的y和实际的y_data的差别, i.e. mean variance

Optimizer = Tf.train.GradientDescentOptimizer (0.5) #用tf的梯度下降优化器减少误差loss, improve the accuracy of the parameters, 0.5 for learning efficiency (generally less than 1)

Train = optimizer.minimize (loss) #训练以减少误差

init = Tf.global_variables_initializer () #初始化tf结构图中的所有变量, this is weights and biases

# # CREATE TensorFlow structure End # # #

Sess = tf. Session () #激活创建的结构图, sesssion is the dialogue control of the execution command of the neural network

Sess.run (init) #激活init, i.e. all structures

For step in range (201): #让神经网络一步一步的训练 201 Steps

Sess.run (train) # Start training

If step% = = 0: #每间隔20步打印以下训练得到的变量值

Print (step, Sess.run (Weights), Sess.run (biases))

The effect of running:

This time without the first warning.

The example program is stored on the Oschina:

Http://git.oschina.net/wjiang/tensorflow_python3

git clone Http://git.oschina.net/wjiang/tensorflow_python3

TensorFlow Installation and instances-(Ubuntu16.04.1 & Anaconda3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.