分享好友 人工智能首页 频道列表

在 GPU 上运行 TensorFlow 教程 (MNIST)

tensorflow教程  2023-03-08 11:056270

介绍

我想接触TensorFlow,暂时想运行教程。
有GPU!
这就是我的想法,但是我很难让它发挥作用,所以我将把信息留在这里。

它是为谁准备的?

  • 想要在 GPU 上运行 TensorFlow 的人

概述

基本遵循TensorFlow官方教程
它是作为初学者编写的,所以如果它顺利,它很容易!
https://www.tensorflow.org/tutorials/quickstart/beginner

我在以下环境中运行它。

  • 操作系统:Windows 10
  • Python:Python 3.10.6
  • TensorFlow:张量流 GPU 2.9.1
  • 显卡:RTX2070Super

创建 Python 虚拟环境

似乎建议创建一个虚拟环境,因为用库和各种版本污染环境很麻烦。
这里也是官方的方法,所以放上链接。
https://www.tensorflow.org/install/pip?hl=ja

我输入了教程源代码并尝试运行它。 .

即使键入并执行以下教程的源代码,也会出现错误
https://www.tensorflow.org/tutorials/quickstart/beginner

错误信息
- 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. alled properly if you would like to use GPU. Follow the guide 
- Skipping registering GPU devices...
- 2022-07-25 17:44:34.995926: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2 wing CPU instructions in performance-critical operations:  AVX
- To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
- <TensorSliceDataset element_spec=TensorSpec(shape=(), dtype=tf.int32, name=None)>

安装 CUDA 工具包

就我而言,我试图在 GPU 上运行,所以我需要为 GPU 安装库。
从 nvidia 页面下载并安装!
https://developer.nvidia.com/cuda-downloads

安装后检查时的另一个错误。 .

错误信息
- W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found

添加了 DLL 加载

Windows 版本 Python 3.8 或更高版本似乎需要单独加载 DLL。
CUDA 环境变量是自动创建的,所以添加导入。
(我被困在这里)
将以下内容添加到代码的开头

米到圣。 py
import os; os.add_dll_directory(os.path.join(os.environ['CUDA_PATH'], 'bin'));

我认为这是解决方案,但我得到了一个错误

错误信息
- W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found

安装CUDA8系列DLL

我希望你从一开始就把它放进去。 .欢迎在下方下载。
https://developer.nvidia.com/cuda-80-ga2-download-archive

解压缩 ZIP 并将错误消息 DLL 复制到以下内容。
C:\Program Files\NVIDIA GPU 计算工具包\CUDA\v11.7\bin

终于搬家了!

做完以上并执行后,学习进度就显示在终端上了!
TensorFlowのチュートリアルをGPUで動かす(MNIST)
所以,如果你注意以下几点,TensorFlow 似乎可以安全地工作。

  • 安装 CUDA 工具包
  • 包括CUDA8系列的DLL
  • 导入 CUDA 路径(在 Windows 上)

设计一点

该教程有效。 .
光看准确率就不是很有趣,所以我稍微修改了一下。

  • 选择测试数据并显示图像(pyplot)
  • 显示最高概率预测

我只是想检查图像中的结果,因此该方法合适是一种耻辱。 .
最终代码如下

米到圣。 py
import os; os.add_dll_directory(os.path.join(os.environ['CUDA_PATH'], 'bin')); # GPUを使用するためのDLL読み込み設定
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test_org, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test_org / 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)
])

predictions = model(x_train[:1]).numpy()
tf.nn.softmax(predictions).numpy()
print(predictions)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
# print(loss_fn(y_train[:1], predictions).numpy())

model.compile(optimizer='adam',
                loss=loss_fn,
                metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)

probability_model = tf.keras.Sequential([
    model,
    tf.keras.layers.Softmax()
])
target = 0
while target >= 0:
    target = int(input("テスト画像のindexを入力してね"))
    if target < 0:
        next
    plt.imshow(x_test_org[target],
                cmap=plt.cm.binary)
    plt.show()
    testdata = x_test[target:target+1]
    ans = probability_model(testdata)
    ans_idx = np.argmax(ans[0])
    print("この文字は" + str(ans_idx) + "だと思う")
    print(y_test[target:target+1])

原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308622953.html

查看更多关于【tensorflow教程】的文章

展开全文
相关推荐
反对 0
举报 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
tensorflow2.0——LSTM,GRU(Sequential层版)
前面都是写的cell版本的GRU和LSTM,比较底层,便于理解原理。下面的Sequential版不用自定义state参数的形状,使用更简便: import tensorflow as tfimport osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'assert tf.__version__.startswith('2.')# 设置相关底层

0评论2023-02-10907

TensorFlow基础笔记(7) 图像风格化效果与性能优化进展
参考 http://hacker.duanshishi.com/?p=1693http://blog.csdn.net/hungryof/article/details/53981959http://blog.csdn.net/hungryof/article/details/61195783http://blog.csdn.net/wyl1987527/article/details/70245214https://www.ctolib.com/AdaIN-style.

0评论2023-02-09559

Tensorflow报错总结 TensorFlow文档
输入不对应报错内容:WARNING:tensorflow:Model was constructed with shape (None, 79) for input Tensor("genres:0", shape=(None, 79), dtype=float32), but it was called on an input with incompatible shape (128, 5).定义模型的输入和训练时候传入的in

0评论2023-02-09636

深度学习框架之TensorFlow的概念及安装(ubuntu下基于pip的安装,IDE为Pycharm)
2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源。TensorFlow 是使用数据流图进行数值计算的开源软件库。也就是说,TensorFlow 使用图(graph)来表示计算任务。图中的节点表示数学运算,边表示运算之间用来交流的多维数组(也就是tensor,张量)

0评论2023-02-09857

TensorFlow源码分析——Tensor与Eigen tensorflow 开源
TensorFlow底层操作的数据结构是Tensor(张量),可以表示多维的数据,其实现在core/framework/tensor.h中,对于tensor的理解主要分两大块:1.Tensor的组成成分2.Tensor是如何进行数学运算的(TensorFlow本质就是处理大量训练数据集,在底层要实现深度学习常

0评论2023-02-09693

tensorflow scope的作用
  我们在使用tensorflow的时候,当你想复用一个函数的模块,调试时候回提示你变量已经出现,提示你是否重用。那我们当然是不重用的,因为每一个变量都是我们需要的。  要体现不同,就在不同的变量中使用name scope限定,那么其中的重复名字就不会出现问题

0评论2023-02-09697

TensorFlow——LinearRegression简单模型代码
代码函数详解tf.random.truncated_normal()函数tf.truncated_normal函数随机生成正态分布的数据,生成的数据是截断的正态分布,截断的标准是2倍的stddev。zip()函数zip() 函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些

0评论2023-02-09381

tensorFlow2.1下的tf.data.Dataset.from_tensor_slices()的用法
一、总结一句话总结:将输入的张量的第一个维度看做样本的个数,沿其第一个维度将tensor切片,得到的每个切片是一个样本数据。实现了输入张量的自动切片。# from_tensor_slices 为输入张量的每一行创建一个带有单独元素的数据集ts = tf.constant([[1, 2], [3,

0评论2023-02-09789

TensorFlow基础笔记(14) 网络模型的保存与恢复_mnist数据实例
http://blog.csdn.net/huachao1001/article/details/78502910http://blog.csdn.net/u014432647/article/details/75276718https://zhuanlan.zhihu.com/p/32887066#coding:utf-8#http://blog.csdn.net/zhuiqiuk/article/details/53376283#http://blog.csdn.net/

0评论2023-02-091030

更多推荐