1. 版本
CPU/GPU、CUDA、python、tensorflow版本盡量使用以下配置,參考下面圖片或網址:
在 Windows 環境中從源代碼構建 | TensorFlow (google.cn)
查看顯卡支持的cuda版本:
查看顯卡GPU支持的CUDA版本,和對應版本tensorflow - 簡書 (jianshu.com)
如上圖就是支持CUDA11(及以下)
2. 安裝(通過conda)
安裝所需命令來自於MOOC北京大學tensorflow課程:人工智能實踐:Tensorflow筆記_中國大學MOOC(慕課) (icourse163.org)
2.1 安裝anaconda
官網:Anaconda | The World's Most Popular Data Science Platform,選擇不同版本的工具會自動安裝其對應版本的python,如果已經安裝過python環境選擇保留或卸載沒什么影響。
至於什么是anaconda和安裝細節,自己搜,或者移步至:Anaconda的安裝和詳細介紹(帶圖文) - 代碼幫 - 博客園 (cnblogs.com)
(推薦安裝過程選中添加環境變量)
以下過程中選擇的版本應結合自己需要以及上面的圖表。
conda安裝完成后,打開這個,開始后續操作
2.2 安裝cuda和cudnn
以下非必須,只是個人需要,所以做了記錄。如果不需要或者下面安裝語句報錯了,可能是需要換一個國內的鏡像(鏡像問題看后續)或者電腦硬件不支持,可以跳過直接安裝tensorflow。
(1)新建一個python環境
conda create -n TF2.4 python=3.8
其中TF2.4是自己起名,隨意;python的版本應與自己電腦上的保持一致(或者不一致讓它再下個?)。成功的話會讓你選擇是否執行,鍵入y即可
(2)進入新建的環境並安裝英偉達的SDK
conda activate TF2.4 conda install cudatoolkit=11.0
(3)安裝英偉達深度學習軟件包
conda install cudnn=8.0
2.3 安裝tensorflow
pip install tensorflow==2.4
如果看到下圖,那么恭喜你,基本成功了
2.4 驗證及查看安裝版本
依次鍵入以下語句:
python import tensorflow as tf tf.__version__
3. 安裝過程中遇到的錯誤
3.1 HTTP error occurred...
解決:更改鏡像或者再試一次(真的會有用)
3.2 The channel is not accessible or is invalid.
使用國內鏡像后報錯:The channel is not accessible or is invalid.
解決:
換別的鏡像或者恢復默認源(就是切回原來的境外url,多試幾次會成功的)
(1)換別的鏡像
anaconda | 鏡像站使用幫助 | 清華大學開源軟件鏡像站 | Tsinghua Open Source Mirror(這也是我報錯終結的地方)
(1條消息) Conda配置國內鏡像源_taoyu94的博客-CSDN博客_conda鏡像源
在“3.1”中的兩條命令會生成這么個文件,打開並編輯內容為:
channels: - defaults show_channel_urls: true default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
然后再次執行“install”命令
(2)恢復默認源
3.3 PackagesNotFoundError: The following packages are not available from current channels
解決:
conda config --add channels conda-forge
4. 完成
安裝完成,現在可以在pycharm中使用創建好的環境了!