Teach you how to install Tensorflow (both Windows and Linux versions)

     Now more and more artificial intelligence, machine learning, deep learning, and reinforcement learning have appeared, and then I have become interested in this, and I have done a little learning, so I will briefly introduce through this article, how to Building Tensorflow and how to use it. As a suggestion, it is best to learn a little Python basics and Linux knowledge!

Version: Windows7

One: Install Anaconda and Tensorflow

step:

1: Download Anaconda from the official website

https://www.anaconda.com/download/


2: Perform software installation (this is no special difference from ordinary)

Note that:

3: After installing Anaconda, test the environment variables

Enter command mode in Windows:

(1) Check whether the anaconda environment is successfully installed: conda --version


(2) Detect which environment variables are currently installed: conda info --envs


(3) For Anaconda, install a built-in python version parser (in fact, the python version)

        See what python versions are currently available: conda search --full -name python

        Install the python version (I installed the 3.5 version here, this is based on demand): conda create --name tensorflow python=3.5

(4) Activate the environment of tensflow: activate tensorflow (note: this can only be done after the subsequent installation is successful, otherwise an error will be prompted)


(5) The environment for detecting tensflow is added to Anaconda: conda info --envs (Note: it is only performed after the successful installation of the post-sequence, otherwise an error will be prompted)


(6) Detect the version of python in the current environment: python --version


(7) Exit the tensorflow environment: deactivate


(8) Switch to the tensorflow environment: activate tensorflow      

The above basics can give you a relatively simple understanding of Anaconda. In fact, it is similar to some operations of JDK. For example, if we check the version of jdk, we can also use java --version, so it is relatively simple to install tensorflow for Anaconda. The reason is exactly this, that is, it provides us with a basic dependency environment, which facilitates our subsequent installation operations;

Anaconda's official development documentation, you can take a look, or the official website is better:

https://docs.anaconda.com/anaconda/user-guide/getting-started

https://anaconda.org/

4: Perform a formal installation of Tensorflow

Note: According to the official documentation of Tensorflow, a command to install tensorflow is as follows:

pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.0.0-cp35-cp35m-win_x86_64.whl

However, if we do this directly in cmd, it may not be successful, and I don't know why at the beginning, but later found that it has something to do with the computer's cpu and graphics card, so the latter method is used to install;

5: Through the command: pip install --upgrade --ignore-installed tensorflow

The rest is to slowly wait for the installation process.

Tips: (1) If after this command, there is a prompt that you need to upgrade your pip version, then you can install it according to the above prompt.

6: After waiting for the completion, confirm whether the installation is successful

(1) Open the previously installed Anaconda


Both of these are fine. I will talk about the way to use Anaconda Prompt here;

Method 1 : Steps: ①Click directly to enter, the following content will be displayed:

②Switch to the environment of tensorflow

③ Enter the python editing environment

④ Then write a code that uses:


Method 2: By using the editor of spyder in Anaconda

In this way, it is simpler, just write the above code directly, and then run it, I won't introduce it here. . .

7: OK, if you get here, basically it has been realized from installation to success~~~~

Reminder: If you find that your conda and tensorflow environments are successfully installed, but there is a problem when you run the test code, then please note that for this reason, when you install tensorflow, you directly Under cmd, not in an environment you activated with conda, so tensorflow is not directly embedded in the conda environment, so it leads to an error that the module cannot be imported;

Solution: (1) Only need to activate tensorflow ------------ Note: This environment is created in point 3 of the third step;

(2) Then use the command in the fifth step.  

Two: Embed the Tensorflow environment into the editor

Environment: Tensorflow and Pycharm editor

step:

1: Download the Pycharm software, the download and installation of this is very simple, so I won't say more

2: Create a project with Pycharm


3: Set the relevant content of the project


Reminder: Pay attention to the choice of Interpreter above, because what we want to test now is to embed tensorflow into our IDE, which is convenient for us to develop, so this python parser is to choose the parser under the tensorflow directory we installed before, otherwise, we After that, the content of the tensorflow module cannot be used. . . Pay special attention. . . Of course, if you don't choose here, you can still modify it after creating the project. I will say later;

4: Create a py file for writing test code


5: Run the program code


OKOK,,, this shows that our environment has been integrated. . . . you're done

Reminder: Sometimes we will find that after we introduce the tensorflow module, an error will be reported. The reasons for this may be as follows:

(1) tensorflow was not installed successfully, in this case, you need to follow my steps again!

(2) The python parser in the IDE does not use the one installed in tensorflow, so it cannot be recognized

There are two solutions for this:

The first one is to choose the correct parser when creating the project, which is the method I used above

The second is to modify the configuration in the project project:

Step: 1: Select File---->setting

2:

3: Add a new parser


4: Find python.exe in tensorflow in the env in Anadonda we installed


5: Click apply, and then restart our IDE, so that the error that the module of tensorflow cannot be found will not be reported.

Printed book: Linux (Ubuntu14.0.1)

Three: Install Tensorflow in Linux environment (via Anaconda)

Steps: (1) Download the Linux version of Anaconda    https://www.anaconda.com/download/#linux

Downloading from the path of the official website is generally very slow, so you can go to this address to download (or leave a message): https://download.csdn.net/download/cs_hnu_scw/10389323

(2) Run the downloaded Anaconda, find the downloaded directory, and then execute the command: bash XXXXXXXXX (that is, the name of the Anaconda file)

(3) Just wait for the installation to complete;

When the following occurs:


Strong attention: during installation, you will be prompted whether to add this to the environment variable. It is best to choose Yes, otherwise you will have to add it manually every time, which is very inconvenient, so it is strongly recommended to add it directly to in environment variables;

(4) After completing the above steps, test the Anaconda environment;

Execute the command: conda --version (function: view the current version of Anaconda)

If the corresponding installation version appears, it means that the installation is successful, and you can continue with the subsequent installation steps.

(5) Add the tensorflow environment. Execute the command: conda create -n tensorflow python=3.5 (in terms of version, I personally prefer version 3.X+). When the execution is completed, just enter yes according to the prompt, and wait slowly.

(6) Activate the environment and execute the command: source activate tensorflow (function: enter the environment of tensorflow)

(7) Activate the tensorflow environment and execute the command:

pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

Be sure to pay attention to one place: if the version of python you installed is 2.7. Then just use the above address. If you use version 3.5, you need to modify it to the following link for a long time: (other versions are similar to modifications)

pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.1-cp35-cp35m-linux_x86_64.whl

(8) After the execution, the only thing left is to wait. For the prompts that appear, all are "yes".

(9) After the installation is complete, perform the test.

Specific steps:

1: In the environment of tensorflow, execute the command: python (function: to enter the python environment)

2: Then enter the code (this is actually the same as when testing when Windows was installed):

import tensorflow as tf
hello = tf.constant('first tensorflow')
sex = tf.Session ()
print sess.run (hello)
If: output first tensorflow, then the installation is successful.

to add on:

1: When you need to exit the python environment, that is, execute Ctrl+D or enter quit.

2: Exit the tensorflow environment, source deactivate

3: Activate the tensorflow environment, source activate tensorflow

Four: Pycharm integrates the tensorflow environment

(1) Download Pycharm, just go to the official website to download the Linux community version, and then perform the corresponding decompression command processing on the downloaded file. In addition, pay attention to the fact that running Pycharm in Linux is not a direct click. To run, you need to find the corresponding directory (bin directory), and then execute the command: sh pycharm.sh to run Pycharm.

(2) This is actually the same as the integration method of windows, but the path of tensorflow is different. Therefore, you can refer to the detailed configuration process for the Windows version above, and I won't say more here.

---------------------------------------------------------------------------------------------------------------------------------

Five: Tensorflow case practice

(1) Case practice: Just download the content I share through Baidu cloud disk, the content inside is packaged, so it should be understandable

Project link: https://pan.baidu.com/s/1-TelzkLHodDNsdX6G82ZOg        Password: b05p

Tips: (1) When running this code, ImportError: No module named 'matplotlib' will appear. This is because you are missing this package in python, so you need to add extra; or enter the tensorflow environment, and then pass pip install matplotlib

Solution: Enter cmd, then conda install matplotlib, and then wait for the installation to be successful, then you will find it to solve this problem;

(2) Recognition case of handwritten digits:

Data: https://pan.baidu.com/s/1UC6uBPPOBzZhYvNV93RgNw

Code:

#!/usr/bin/python
# -*- coding:utf-8 -*-
# @Time   : 2018/3/30 0030 15:20
# @Author : scw
# @File   : writenumbercompute.py
# Description: An example analysis of the recognition of handwritten digits
import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

# retrieve data
mnist = input_data.read_data_sets("E:/tensorflowdata/MNIST_data/", one_hot=True)

print('Training set information:')
print(mnist.train.images.shape,mnist.train.labels.shape)
print('Test set information:')
print(mnist.test.images.shape,mnist.test.labels.shape)
print('Validation set information:')
print(mnist.validation.images.shape,mnist.validation.labels.shape)

# build graph
sex = tf.InteractiveSession ()
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x,W) + b)

y_ = tf.placeholder(tf.float32, [None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

# do training
tf.global_variables_initializer().run()

for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  train_step.run({x: batch_xs, y_: batch_ys})

# model evaluation
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print('MNIST handwritten picture accuracy:')
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))

(3) Identification of dial keys and short message icons

Function description: It is mainly to realize an identification of the dial key icon and the short message key icon, as a simple classification Demo.


Baidu Cloud Address: Link: https://pan.baidu.com/s/1MC7Recml5laTsrHBQ94NcA Password: nwxj

(4) Face capture and recognition

Function description: Capture a specific character and identify whether it is a face in training, as a face recognition Demo.

Since there are too many data sets, Baidu Cloud does not allow them to be uploaded. Therefore, if you need students, you can leave a message, and I will check the news every day.

(5) Simple web crawler

Function description: A very simple and easy-to-use web crawler Demo.

Baidu Cloud Address: Link: https://pan.baidu.com/s/1FzIzmfYON9pUpms3GyVQqQ Password: 5di1

Six: the method of installing some additional libraries

(1) Install cv2: pip install opencv-python

(2) Install the face recognition library: pip install dlib == 18.17.100

(3) Install the machine learning library: pip install sklearn

(4) Install the scipy library: pip install scipy

(5) Install the numpy library: pip install numpy

(6) Install the Pillow image library: pip install Pillow

(7) Install the matplotlib drawing library: pip install matplotlib

Seven: Summary of some common problems

(1) Problem: In cmd, enter the conda command, and it prompts that conda is not an internal command

Solution: Add to the environment variable (Path of the system): the Scripts directory under your Anaconda installation directory, such as mine,

D:\anacondadownload\Scripts

(2)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325583001&siteId=291194637