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.12

3、检测目前安装了哪些环境变量:

(base) C:\Users\toto>conda info --envs
# conda environments:
#
base                  *  D:\installed\Anaconda3

4、执行以下命令(下面的可能不需要执行,下面是看网上博文资料得出的):

(base) C:\Users\toto>conda info --envs
# conda environments:
#
base                  *  D:\installed\Anaconda3

5、先看看有哪些tensorflow的版本可以安装。
在Anaconda Prompt(Anaconda3)中输入:

pip install tensorflow==3.0

发现会出现如下错误:

(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

解决办法是:

扫描二维码关注公众号,回复: 11666572 查看本文章
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,在安装的过程中可能会超时。我们在执行命令时,可显式指定超时时间。

--default-timeout=1000

更换安装源。
清华大学开源软件镜像 https://mirrors.tuna.tsinghua.edu.cn/

https://pypi.tuna.tsinghua.edu.cn/simple
sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scipy
sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scikit-learn

豆瓣开源镜像

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.0

model = 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.9763

2.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上问问题

参与社区讨论

打个赏呗,您的支持是我坚持写好博文的动力
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/toto1297488504/article/details/106330806