介绍
我想接触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 环境变量是自动创建的,所以添加导入。
(我被困在这里)
将以下内容添加到代码的开头
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 似乎可以安全地工作。
- 安装 CUDA 工具包
- 包括CUDA8系列的DLL
- 导入 CUDA 路径(在 Windows 上)
设计一点
该教程有效。 .
光看准确率就不是很有趣,所以我稍微修改了一下。
- 选择测试数据并显示图像(pyplot)
- 显示最高概率预测
我只是想检查图像中的结果,因此该方法合适是一种耻辱。 .
最终代码如下
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教程】的文章