本人配置:window10+GTX 1650+tensorflow-gpu 1.14+keras-gpu 2.2.5+python 3.6,親測可行
一.Anaconda安裝
直接到清華鏡像網站下載(什么版本都可以):https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
這是我下載的版本,自帶python版本為3.6
下載后直接安裝即可,可參考:https://www.cnblogs.com/maxiaodoubao/p/9854595.html
二.建立開發環境
1.打開Prompt
點擊開始,選擇Anaconda Prompt(anaconda3)
2.更換conda源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ conda config --set show_channel_urls yes
按照這么寫的話,后續創建環境會報錯:
所以直接打開.condarc文件,改為如下(將https改為http,去掉了default,末尾添加了/win-64/):
ssl_verify: true show_channel_urls: true channels: - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/ - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64/ - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/
3.創建虛擬環境
創建一個名為tensorflow ,python版本為3.6的虛擬環境
conda create -n tensorflow python=3.6
查看虛擬環境
conda info -e
激活開發環境
activate tensorflow
三.安裝tensorflow-gpu和keras-gpu
首先,這里有兩種安裝方式,一種是conda,一種是pip,conda下載較慢,但會自動安裝適合的CUDA和CuDnn,pip安裝快,但是需要手動安裝CUDA和CuDnn,這里重點介紹pip安裝方式
1.conda安裝
輸入命令,需要下載一些包,直到done,自動下載了gpu,直接可以使用,比較方便和簡單
conda install tensorflow-gpu==xxx.xxx.xx你想要的版本號
本人一開始使用這種方法,結果在下載時經常卡住,中斷,主要還是因為網絡問題,需要多試幾次,可以安裝成功,因此需要使用國內鏡像,但是使用鏡像后,依然安裝不成功,所以放棄了這種方法。
2.pip安裝(有很多坑)
(1)打開計算機管理
點擊查看gpu算力:CUDA GPUs | NVIDIA Developer
算力高於3.1就行,就可以跑深度程序。
(2)打開NVIDIV控制面板
可以看到最大支持CUDA版本是11.4,只要下載的版本沒有超過最大值即可。
(3)安裝CUDA
CUDA下載地址:CUDA Toolkit Archive | NVIDIA Developer (親測,官網下載不慢)
注意:cuda和cudnn安裝要注意版本搭配,以及和python版本的搭配,然后根據自己的需要安裝
以下是我的下載
下載之后:按照步驟安裝
配置環境變量:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\extras\CUPTI\libx64 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\libnvvp
不要直接在path里面配置,會顯示太大
(4)安裝cuDNN:
cuDNN下載地址:cuDNN Archive | NVIDIA Developer(親測,官網下載不慢)
注意:cuDNN要跟CUDA版本搭配好,不能隨便下載
安裝時,可能需要注冊NVIDIA賬戶,花一點時間注冊一下即可下載。
下載完后,將文件解壓,將里面的文件全部導入到CUDA/v10.0路徑下。
(5)安裝tensorflow-gpu和keras-gpu
可以對照表格安裝對應版本tensorflow和keras
pip install tensorflow-gpu==1.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -i https://pypi.doubanio.com/simple/ keras==2.2.5
(6)安裝其他庫
pip install -i https://pypi.doubanio.com/simple/ opencv-python pip install -i https://pypi.doubanio.com/simple/ pillow pip install -i https://pypi.doubanio.com/simple/ matplotlib pip install -i https://pypi.doubanio.com/simple/ sklearn
四.測試是否使用了GPU
進入python編譯環境,輸入一下代碼,如果結果是True,表示GPU可用
import tensorflow as tf print(tf.test.is_gpu_available())
若為True,使用命令查看gpu是否在運行
nvidia-smi
五.jupyter使用虛擬環境
其實使用虛擬環境非常簡單,只需要安裝一個nb_conda包就可以直接使用了
conda install nb_conda
在你的新環境上安裝ipykernel,重啟jupyter之后就可以用了
conda install -n tensorflow ipykernel
正好可以測試tensorflow和keras是否在GPU上運行
來段代碼測試一下:
import numpy as np from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten import matplotlib.pyplot as plt from sklearn import datasets # 樣本數據集,兩個特征列,兩個分類二分類不需要onehot編碼,直接將類別轉換為0和1,分別代表正樣本的概率。 X,y=datasets.make_classification(n_samples=200, n_features=2, n_informative=2, n_redundant=0,n_repeated=0, n_classes=2, n_clusters_per_class=1) # 構建神經網絡模型 model = Sequential() model.add(Dense(input_dim=2, units=1)) model.add(Activation('sigmoid')) # 選定loss函數和優化器 model.compile(loss='binary_crossentropy', optimizer='sgd') # 訓練過程 print('Training -----------') for step in range(501): cost = model.train_on_batch(X, y) if step % 50 == 0: print("After %d trainings, the cost: %f" % (step, cost)) # 測試過程 print('\nTesting ------------') cost = model.evaluate(X, y, batch_size=40) print('test cost:', cost) W, b = model.layers[0].get_weights() print('Weights=', W, '\nbiases=', b) # 將訓練結果繪出 Y_pred = model.predict(X) Y_pred = (Y_pred*2).astype('int') # 將概率轉化為類標號,概率在0-0.5時,轉為0,概率在0.5-1時轉為1 # 繪制散點圖 參數:x橫軸 y縱軸 plt.subplot(2,1,1).scatter(X[:,0], X[:,1], c=Y_pred[:,0]) plt.subplot(2,1,2).scatter(X[:,0], X[:,1], c=y) plt.show()
結果:
到此說明已經徹底成功安裝上tensorflow和keras了
七.可能的問題
1.安裝1.14.0版本的tensorflow后,運行時出現了錯誤
Using TensorFlow backend. D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint8 = np.dtype([("quint8", np.uint8, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint16 = np.dtype([("qint16", np.int16, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint16 = np.dtype([("quint16", np.uint16, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint32 = np.dtype([("qint32", np.int32, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. np_resource = np.dtype([("resource", np.ubyte, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint8 = np.dtype([("quint8", np.uint8, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint16 = np.dtype([("qint16", np.int16, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint16 = np.dtype([("quint16", np.uint16, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint32 = np.dtype([("qint32", np.int32, 1)]) D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. np_resource = np.dtype([("resource", np.ubyte, 1)])
解決方法:
這個問題意思就是numpy的版本過低或者過高都會出現警告,只需要先卸載重新指定版本的numpy即可解決此問題
pip uninstall numpy pip install numpy==1.16.4
2.anaconda卸載不干凈:
解決辦法:
(1)執行命令
conda config --remove-key channels conda install anaconda-clean anaconda-clean --yes
(2)運行安裝目錄下的 Uninstall-Anaconda3.exe 程序即可,這樣就成功地將anaconda完全卸載干凈了
3.利用鏡像安裝tensorflow-gpu
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu
4.高版本可能出現錯誤:AttributeError: module ‘tensorflow_core._api.v2.config’ has no attribute ‘experimental_list_devices’
解決方法:解決module ‘tensorflow_core._api.v2.config‘ has no attribute ‘experimental_list_devices‘_sinysama的博客 (親測有效)
八.網盤下載
1.anaconda下載(5.2.0和5.3.1):
鏈接:https://pan.baidu.com/s/1-iw1hjfL2u4CumCW0b0Zvg
提取碼:hort
2.cuDNN和CUDA下載(10.0,10.1,11.4):
鏈接:https://pan.baidu.com/s/1Vy83Oq9QHCMRq8har9NeMg
提取碼:yxce
參考文章:
FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version
如何完全卸載Anaconda(如何下載Anaconda-Clean package)_托馬斯-酷濤的博客
怎么查看keras 或者 tensorflow 正在使用的GPU_Thinker_and_FKer的博客
Jupyter Notebook運行指定的conda虛擬環境_我是天才很好的博客
Anaconda鏡像安裝tensorflow-gpu1.14及Keras超詳細版_Xnion的博客
win10完整Tensorflow-GPU環境搭建教程-附CUDA+cuDNN安裝過程_尤利烏斯.X的博客