tensorflow環境搭建


官網:https://www.tensorflow.org/

https://www.tensorflow.org/install/install_windows

You must choose one of the following types of TensorFlow to install:

  • TensorFlow with CPU support only. If your system does not have a NVIDIA® GPU, you must install this version. Note that this version of TensorFlow is typically much easier to install (typically, in 5 or 10 minutes), so even if you have an NVIDIA GPU, we recommend installing this version first. Prebuilt binaries will use AVX instructions.
  • TensorFlow with GPU support. TensorFlow programs typically run significantly faster on a GPU than on a CPU. Therefore, if your system has a NVIDIA® GPU meeting the prerequisites shown below and you need to run performance-critical applications, you should ultimately install this version.

 

Requirements to run TensorFlow with GPU support

If you are installing TensorFlow with GPU support using one of the mechanisms described in this guide, then the following NVIDIA software must be installed on your system:

  • CUDA® Toolkit 9.0. For details, see NVIDIA's documentation Ensure that you append the relevant Cuda pathnames to the %PATH% environment variable as described in the NVIDIA documentation.
  • The NVIDIA drivers associated with CUDA Toolkit 9.0.
  • cuDNN v7.0. For details, see NVIDIA's documentation. Note that cuDNN is typically installed in a different location from the other CUDA DLLs. Ensure that you add the directory where you installed the cuDNN DLL to your %PATH%environment variable.
  • GPU card with CUDA Compute Capability 3.0 or higher for building from source and 3.5 or higher for our binaries. See NVIDIA documentation for a list of supported GPU cards.

If you have a different version of one of the preceding packages, please change to the specified versions. In particular, the cuDNN version must match exactly: TensorFlow will not load if it cannot find cuDNN64_7.dll. To use a different version of cuDNN, you must build from source.

tensorflow有兩個版本,一個是CPU的,一個是GPU的,其中GPU對顯卡有要求,必須是NVIDIA

 

CUDA(Compute Unified Device Architecture),是顯卡廠商NVIDIA推出的運算平台。 CUDA™是一種由NVIDIA推出的通用並行計算架構,該架構使GPU能夠解決復雜的計算問題。

 

Determine how to install TensorFlow

You must pick the mechanism by which you install TensorFlow. The supported choices are as follows:

  • "native" pip
  • Anaconda

Native pip installs TensorFlow directly on your system without going through a virtual environment. Since a native pip installation is not walled-off in a separate container, the pip installation might interfere with other Python-based installations on your system. However, if you understand pip and your Python environment, a native pip installation often entails only a single command! Furthermore, if you install with native pip, users can run TensorFlow programs from any directory on the system.

In Anaconda, you may use conda to create a virtual environment. However, within Anaconda, we recommend installing TensorFlow with the pip install command, not with the conda install command.

NOTE: The conda package is community supported, not officially supported. That is, the TensorFlow team neither tests nor maintains this conda package. Use that package at your own risk.

anaconda2和anaconda3的區別也要注意。

 

使用anaconda:

接下來需要設置 Anaconda 倉庫鏡像,因為默認連接的是國外鏡像地址,下載速度比較慢,我們把鏡像地址改為清華大學開源軟件鏡像站,打開 Anaconda Prompt, 輸入:

 

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

The Anaconda installation is community supported, not officially supported.

Take the following steps to install TensorFlow in an Anaconda environment:

  1. Follow the instructions on the Anaconda download site to download and install Anaconda.

  2. Create a conda environment named tensorflow by invoking the following command:

     

     
    C:> conda create -n tensorflow pip python=3.5 

     

  3. Activate the conda environment by issuing the following command:

     

     
    C:> activate tensorflow
     (tensorflow)C:>  # Your prompt should change 

     

  4. Issue the appropriate command to install TensorFlow inside your conda environment. To install the CPU-only version of TensorFlow, enter the following command:

     

     
    (tensorflow)C:> pip install --ignore-installed --upgrade tensorflow 

     

    To install the GPU version of TensorFlow, enter the following command (on a single line):

     

     
    (tensorflow)C:> pip install --ignore-installed --upgrade tensorflow-gpu 

 

輸入activate tensorflow可以看到 命令行行前面有個tensorflow,這個就是當前運行環境。

 

遇到的一個問題:

powshell中無法active TensorFlow,我輸入命令:

PS C:\WINDOWS\system32> activate tensorflow
PS C:\WINDOWS\system32> python -V
Python 2.7.14 :: Anaconda, Inc.

如上圖還是沒有進入tensorflow環境。

看到一個回答在:另外在powershell中無法使用activate指令激活環境,需要進入到cmd中(https://www.zhihu.com/question/58573630 ,原因在於:

https://blog.csdn.net/yannanxiu/article/details/78703888

 )

我輸入cmd,然后:

PS C:\WINDOWS\system32> cmd
Microsoft Windows [版本 10.0.16299.371]
(c) 2017 Microsoft Corporation。保留所有權利。

C:\WINDOWS\system32>activate tensorflow

(tensorflow) C:\WINDOWS\system32>

可以看到進入tensorflow環境了。

 

運行 開始菜單 ->Anaconda3—>Anaconda Navigator,點擊左側的Environments,可以看到tensorflow的環境已經創建好了。

在Anaconda Prompt中啟動tensorflow環境:

activate tensorflow

注:當不使用tensorflow時,關閉tensorflow環境,命令為:deactivate(返回默認的python 3.6環境)

 

運行pip install --ignore-installed --upgrade tensorflow

報錯:

大量報:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\mingsoo\\AppData\\Local\\Temp\\pip-build-5gbn3vww\\gast\\setup.py'

----------------------------------------
Failed building wheel for gast
Running setup.py clean for gast

這種類似錯誤

 

運行命令:pip install --upgrade --ignore-installed setuptools  

然后接着安裝tensorflow就安裝成功了。

 

Validate your installation

Start a terminal.

If you installed through Anaconda, activate your Anaconda environment.

Invoke python from your shell as follows:

 
$ python

Enter the following short program inside the python interactive shell:

 
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

If the system outputs the following, then you are ready to begin writing TensorFlow programs:

 
Hello, TensorFlow!

If the system outputs an error message instead of a greeting, see Common installation problems.

If you are new to machine learning, we recommend the following:

If you are experienced with machine learning but new to TensorFlow, see Getting Started with TensorFlow.

 

no moduled named tensorflow無此模塊報錯問題解決

 

 在spyder里面和Ipython里面輸入測試代碼,會報錯沒有tensorflow這個模塊。但是在spyder和ipython里測試代碼,會提示沒有tensorflow模塊,

Import Error: No module named tensorflow.


解決方法就是在tensorflow里面也給其安裝屬於python3.5的spyder。

在開始菜單中搜索anaconda navigator,點擊運行;

點擊左側的home,選擇application on tensorflow(有一個下拉框)

會發現spyder屬於右側綠色install,這個時候點擊install就可以在tensorflow里Python3.5環境里安裝spyder,安裝好之后,就如圖所示,顯示launch(已安裝),

 
點擊launch,即可啟動在tensorflow里面的spyder,,可以看到右下框,那個Python console變成了3.5版本,
 
 
 

 若要在pycharm中使用,則需選擇 
 建工程后在  File-Setting–Project Interpreter  選擇  tensorflow  下的Python解釋器
 
 

參考:

windows7 下Anaconda2與Anaconda3並存

https://blog.csdn.net/y1535766478/article/details/75097984

https://blog.csdn.net/sb19931201/article/details/53648615

 https://www.cnblogs.com/W-Yentl/p/7783933.html

https://www.cnblogs.com/nosqlcoco/p/6923861.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM