02_tensorflow2环境安装、CUDA 10.1 和CUDnn 7.6.5 版本安装、https://tensorflow.google.cn/overview中概述翻译
1、新手入門
1.1 安裝tensorflow環境
1、安裝了Anaconda,進入Anaconda Prompt:
關于安裝Anaconda的博文參考:python安裝、anaconda安裝、pycharm安裝
2、檢測anaconda環境是否安裝成功:conda --version
(base) C:\Users\toto>conda --version conda 4.7.123、檢測目前安裝了哪些環境變量:
(base) C:\Users\toto>conda info --envs # conda environments: # base * D:\installed\Anaconda34、執行以下命令(下面的可能不需要執行,下面是看網上博文資料得出的):
(base) C:\Users\toto>conda info --envs # conda environments: # base * D:\installed\Anaconda35、先看看有哪些tensorflow的版本可以安裝。
在Anaconda Prompt(Anaconda3)中輸入:
發現會出現如下錯誤:
(base) C:\Users\toto>pip install tensorflow==3.0 ERROR: Could not find a version that satisfies the requirement tensorflow==3.0 (from versions: 1.13.0rc1, 1.13.0rc2, 1.13.1, 1.13.2, 1.14.0rc0, 1.14.0rc1, 1.14.0, 1.15.0rc0, 1.15.0rc1, 1.15.0rc2, 1.15.0rc3, 1.15.0, 1.15.2, 1.15.3, 2.0.0a0, 2.0.0b0, 2.0.0b1, 2.0.0rc0, 2.0.0rc1, 2.0.0rc2, 2.0.0, 2.0.1, 2.0.2, 2.1.0rc0, 2.1.0rc1, 2.1.0rc2, 2.1.0, 2.1.1, 2.2.0rc0, 2.2.0rc1, 2.2.0rc2, 2.2.0rc3, 2.2.0rc4, 2.2.0) ERROR: No matching distribution found for tensorflow==3.0(base) C:\Users\toto>出現上面錯誤的原因是,筆者安裝的時候,壓根兒都沒有tensorflow3.0版本的,通過打印出的可選版本,可以知道最高版本是2.2.0
可以通過以下命令安裝tensorflow:
(base) C:\Users\toto>pip install tensorflow==2.2.0 --default-timeout=1000 -i https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: tensorflow==2.2.0 in d:\installed\anaconda3\lib\site-packages (2.2.0) Requirement already satisfied: absl-py>=0.7.0 in d:\installed\anaconda3\lib\site-packages (from tensorflow==2.2.0) (0.9.0) Requirement already satisfied: astunparse==1.6.3 in d:\installed\anaconda3\lib\site-packages (from xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Requirement already satisfied: pyasn1>=0.1.3 in d:\installed\anaconda3\lib\site-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow==2.2.0) (0.4.8) Requirement already satisfied: oauthlib>=3.0.0 in d:\installed\anaconda3\lib\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow==2.2.0) (3.1.0) Requirement already satisfied: more-itertools in d:\installed\anaconda3\lib\site-packages (from zipp>=0.5->importlib-metadata; python_version < "3.8"->markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow==2.2.0) (7.2.0)(base) C:\Users\toto>6、看tensorflow官網,安裝命令是:
pip install --upgrade pip pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple如果在上面過程中出現了如下錯誤:
Script file 'D:\Anaconda3\Scripts\pip-script.py' is not present解決辦法是:
easy_install pip要注意的是,上面的過程一定要保證網絡比較好,否則資料下載的很慢。
最后執行以下命令:
(base) C:\Users\toto>python Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import tensorflow as tf 2020-05-25 21:24:40.485797: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found 2020-05-25 21:24:40.495018: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. >>> tf.__version__ '2.2.0' >>>發現上面輸出的內容是:2.2.0,也就是tensorflow 2.2.0版本
如果上面的過程還是出現了:HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out 這種問題。解決辦法:
https://blog.csdn.net/chengyq116/article/details/93651619
原因是:
安裝超時
網絡原因導致的安裝超時,重新下載。
設置超時時間延長后重新下載。
下載命令后添加參數 --default-timeout=1000,時間可以自定義。
pip install 默認超時時間是 15s,在安裝的過程中可能會超時。我們在執行命令時,可顯式指定超時時間。
更換安裝源。
清華大學開源軟件鏡像 https://mirrors.tuna.tsinghua.edu.cn/
豆瓣開源鏡像
https://pypi.douban.com/simple sudo pip install -i https://pypi.douban.com/simple scipy sudo pip3 install -i https://pypi.douban.com/simple scikit-learn下載離線包,使用 pip install ******.whl 安裝。
然后再執行:
pip --default-timeout=1000 install tensorflow==2.2.0 -i https://pypi.tuna.tsinghua.edu.cn/simple (對于tensorflow2.x CPU的和GPU的包都在此中,1.x的cpu的和gpu的包是分開的)2 CUDA 10.1 和CUDnn 7.6.5 版本安裝
TensorFlow 2.2.0 版本依賴:
Python 3.7:官方下載頁面(3.7.7 版)
CUDA 10.1:官方下載頁面
CUDnn 7.6.5:官方頁面,下載需要注冊一個開發者賬戶
2.1 CUDA下載
(1)、選擇Windows > x86_64 > 10 > exe(local)
(2)、下載后是一個exe文件,讀者自行安裝即可,不要修改其中的路徑信息,完全使用默認路徑安裝即可。
安裝過程會出現(另外網友的安裝方式是:https://www.cnblogs.com/arxive/p/11198420.html):
(3)、接著是下載和安裝對應的cuDNN文件。下載地址為:(https://developer.nvidia.com/rdp/cudnn-archive)
在這個里面,選擇window10相關的。在這個過程中,提示注冊一個用戶,那就按照它的提示進行注冊即可。
最后,下載到的軟件包:
(4)解壓cuDNN,直接將其解壓到CUDA安裝目錄即可。如下圖所示:
即路徑:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
3 TensorFlow綜述
TensorFlow是一個端到端的開源的機器學習平臺。
TensorFlow可以讓初學者和專家容易的創建機器學習模塊,閱讀下面的章節可以開始學習。
Tutorials
這里面包含了端到端的示例來告訴您如何使用TensorFlow,具體:
guide
Guides中解釋了TensorFlow中的這些概念和組件。
2.1.1 For beginners(初學者)
最好的起點是用戶友好的順序(Sequential)API。您可以通過將構建塊連接在一起來創建模型。運行下面的"Hello World"例子。接著閱讀 tutorials以學習更多。
import tensorflow as tf mnist = tf.keras.datasets.mnist(x_train, y_train),(x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0model = tf.keras.models.Sequential([tf.keras.layers.Flatten(input_shape=(28, 28)),tf.keras.layers.Dense(128, activation='relu'),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(10, activation='softmax') ])model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])model.fit(x_train, y_train, epochs=5) model.evaluate(x_test, y_test)輸出:
2020-05-26 19:38:16.347823: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found 2020-05-26 19:38:16.360988: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. 2020-05-26 19:38:46.447461: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll 2020-05-26 19:38:48.037471: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: pciBusID: 0000:01:00.0 name: GeForce GTX 1060 computeCapability: 6.1 coreClock: 1.6705GHz coreCount: 10 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 178.99GiB/s 2020-05-26 19:38:48.044844: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found 2020-05-26 19:38:48.053003: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found 2020-05-26 19:38:48.060887: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found 2020-05-26 19:38:48.068320: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found 2020-05-26 19:38:48.076245: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found 2020-05-26 19:38:48.083698: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found 2020-05-26 19:38:48.091561: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found 2020-05-26 19:38:48.092104: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 2020-05-26 19:38:48.155358: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2020-05-26 19:38:48.791458: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x19c132f21d0 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2020-05-26 19:38:48.792138: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version 2020-05-26 19:38:48.831546: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix: 2020-05-26 19:38:48.831961: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] Epoch 1/5 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2933 - accuracy: 0.9151 Epoch 2/5 1875/1875 [==============================] - 3s 2ms/step - loss: 0.1411 - accuracy: 0.9582 Epoch 3/5 1875/1875 [==============================] - 3s 1ms/step - loss: 0.1038 - accuracy: 0.9685 Epoch 4/5 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0883 - accuracy: 0.9725 Epoch 5/5 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0744 - accuracy: 0.9758 313/313 [==============================] - 0s 1ms/step - loss: 0.0777 - accuracy: 0.97632.1.2 For experts (專家)
子類話的API為高級研究提供了一個按運行定義的接口,為您的模型創建一個類,然后命令式地寫向前傳。輕松創建自定義層、激活函數,和訓練循環(training loops)。運行下面的"Hello World"示例。可以通過訪問tutorials學習更多的內容
class MyModel(tf.keras.Model):def __init__(self):super(MyModel, self).__init__()self.conv1 = Conv2D(32, 3, activation='relu')self.flatten = Flatten()self.d1 = Dense(128, activation='relu')self.d2 = Dense(10, activation='softmax')def call(self, x):x = self.conv1(x)x = self.flatten(x)x = self.d1(x)return self.d2(x) model = MyModel()with tf.GradientTape() as tape:logits = model(images)loss_value = loss(logits, labels) grads = tape.gradient(loss_value, model.trainable_variables) optimizer.apply_gradients(zip(grads, model.trainable_variables))2.1.3 TensorFlow和Keras之間的關系
TensorFlow的高級API基于Keras API標準,用于定義和訓練神經網絡。Keras支持快速原型設計、最先進的研究和生產——所有這些都具有用戶友好的api
Read the Keras Guide for TensorFlow 2
3 常見問題解決
一步一步的探索,以幫助你的項目
初學者的第一個神經網絡程序
專家的生成對抗網絡
專家的神經網絡機器翻譯NMT
4 tensorflow社區
TensorFlow on GitHub
在Stack Overflow上問問題
參與社區討論
打個賞唄,您的支持是我堅持寫好博文的動力
總結
以上是生活随笔為你收集整理的02_tensorflow2环境安装、CUDA 10.1 和CUDnn 7.6.5 版本安装、https://tensorflow.google.cn/overview中概述翻译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 听党指挥、能打胜仗、作风优良的人民军队
- 下一篇: 在部队里监护人是指什么人