venv(虚拟环境)中 运行 tensorflow 时的问题

Problem when running tensorflow in venv (virtual enviromnemt)

(env - Windows10,使用 NVIDIA gpu,Powershell)

我想在虚拟环境中运行下面的示例代码

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()
model.add(Dense(100, activation='relu', 
                input_shape=(32,32,1)))
model.add(Dense(50, activation='relu'))
model.add(Dense(5, activation='softmax'))

model.summary()

代码执行时出现注册GPU失败的错误,但没有使用GPU却执行成功

(venv) PS E:\lolol> & e:/lolol/venv/Scripts/python.exe e:/lolol/testcode2.py
2021-12-01 03:35:40.545756: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-12-01 03:35:40.546020: 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.
2021-12-01 03:35:45.107760: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-12-01 03:35:45.108720: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublas64_11.dll'; dlerror: cublas64_11.dll not found
2021-12-01 03:35:45.109629: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublasLt64_11.dll'; dlerror: cublasLt64_11.dll not found
2021-12-01 03:35:45.110607: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2021-12-01 03:35:45.111655: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2021-12-01 03:35:45.112802: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusolver64_11.dll'; dlerror: cusolver64_11.dll not found
2021-12-01 03:35:45.113631: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusparse64_11.dll'; dlerror: cusparse64_11.dll not found
2021-12-01 03:35:45.114479: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2021-12-01 03:35:45.114979: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] 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...
2021-12-01 03:35:45.116383: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 dense (Dense)               (None, 32, 32, 100)       200

 dense_1 (Dense)             (None, 32, 32, 50)        5050

 dense_2 (Dense)             (None, 32, 32, 5)         255

=================================================================
Total params: 5,505
Trainable params: 5,505
Non-trainable params: 0

我知道运行在虚拟环境中安装tensorflow时要安装CUDA,所以我安装了它,但它仍然没有正常运行

可能CUDA安装在默认安装路径C盘,而venv文件夹安装在E盘,也就是另一个盘,所以好像有问题。我认为 CUDA 应该安装在带有 VENV 的驱动器上,但我不确定该程序的具体安装位置。在 VENV 文件夹中? E 盘中的任何位置?

Tensorflow 在这里提示你需要CUDA 11.0(在“cudartxx_110”和“cublas_110”中,“110”=11.0 是需要的版本)。你有正确的版本吗?使用 ls /usr/local/cuda* 检查它。此外,你需要 CudNN,版本 8.0,否则你的 tensorflow 无法 运行 使用 GPU。 你可以检查这个非常有用的 link 以获得每个版本的 tensorflow 所需的 CUDA/CuDNN 版本:https://www.tensorflow.org/install/source#gpu (这里你似乎有 tf 2.4) 我向您推荐 Python>=3.7 + Cuda 11.2 + CuDNN 8.1(这是我拥有的),然后您可以 运行 在您的环境中使用最新版本的 tf(+ Pytorch 甚至 JAX)