The original text is transferred from: https://blog.csdn.net/u012052268/article/details/74202439
Recently, due to the need to use TensorFlow for work, I can only supplement relevant knowledge. Originally, the blogger planned to play on Ubantu, but for some reasons, he gave up the idea and moved to Pycharm to play. The following is a good installation tutorial I saw in the process of collecting information, and I want to share it.
1. Install Anaconda
Select the corresponding Anaconda to install, click here to download the corresponding system version of Anaconda, the current version of the official website is Anaconda 4.3.1 for python3.6. The author installed version 4.3.0.
Just like installing ordinary software, you can select all defaults, and pay attention to adding python3.6 to the environment variable.
So that Anaconda is installed, we can use the following command to check which packages Anaconda has installed.
Run the start menu -> Anaconda3 -> Anaconda Prompt:
conda list
You can see that common packages such as numpy and sympy have been installed.
2. Install Tensorflow
TensorFlow currently only supports python 3.5 under Windows.
(1) Open Anaconda Prompt and enter the Tsinghua warehouse image, so the update will be faster:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
(2) Also use Anaconda to create a python3.5 environment in Anaconda Prompt, the environment name is tensorflow, enter the following command:
conda create -n tensorflow python=3.5
Run the start menu -> Anaconda3 -> Anaconda Navigator, click Environments on the left, and you can see that the tensorflow environment has been created.
(3) Start the tensorflow environment in Anaconda Prompt:
activate tensorflow
Note: When not using tensorflow, close the tensorflow environment, the command is: deactivate
(4) Install the cpu version of TensorFlow
pip install --upgrade --ignore-installed tensorflow
Note: The installation method of the GPU version is not introduced here. The GPU version needs to install cuda8+cudnn5. If you need it, please search other blog posts.
Note: Be sure to install it in the environment of the tensorflow you just created! ! ! !
This way the tensorflow cpu version is installed.
(5) Test tensorflow
Start the tensorflow environment in Anaconda Prompt and enter the python environment.
The test code is as follows:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
operation result:
3. Other issues
Maybe we are not satisfied here. When we import tensorflow in ipython and Spyder that comes with Anaconda, we have been failing, prompting No module named 'tensorflow' , as shown in the figure below, because we did not open them in the environment of tensorflow.
In order to use tensorflow in ipython and Spyder, we need to install these two plugins in the tensorflow environment.
Open Anaconda Navigator, select Not installed, find ipython and Spyder and install them. The author has already installed them here, so it is not displayed on this page.
Switch to installed, you can see that both have been installed, in fact, you can install it according to your needs. The image below shows Spyder already installed: With
the plugin installed, we need to test it.
Start the tensorflow environment in Anaconda Prompt, run ipython, import tensorflow and find success:
Similarly, start the tensorflow environment in Anaconda Prompt, and run Spyder, after a while it will start the Spyder IDE, and import tensorflow is also successful:
Note: Be sure to start tensorflow Only Spyder in the environment can import tensorflow, do not go to the start menu to run Spyder, it cannot be run there, such as:
4. Using tensorflow in pycharm
I am accustomed to using PyCharm for development. The configuration is as follows:
After creating a new project,
select the Python interpreter under tensorflow in File-Setting-Project Interpreter,
such as my interpreter location:
After deployment, you can run a HelloWorld
import tensorflow as tf
hello = tf.constant("Hello!TensorFlow")
sess = tf.Session()
print(sess.run(hello))
run it
take off!
The advantage of this method: you don't have to open and close the environment every time.
(activate
tensorflow, deactivate tensorflow)