为什么`cuda`可以与`torch`一起使用,但不能与`tensorflow`一起使用

问题描述 投票:0回答:1

我正在尝试将我的 GPU 卡应用到 Jupyter Notebook 中,但我被

TensorFlow
困住了。 但我已经成功了
torch

我有以下设置:


(myen2v) C:\Users\Jan>conda list cudnn
# packages in environment at D:\BitDownlD\Anaconda8\envs\myen2v:
#
# Name                    Version                   Build  Channel
cudnn                     8.9.2.26               cuda11_0    anaconda

(myen2v) C:\Users\Jan>conda list cuda
# packages in environment at D:\BitDownlD\Anaconda8\envs\myen2v:
#
# Name                    Version                   Build  Channel
cudatoolkit               11.8.0               hd77b12b_0

(myen2v) C:\Users\Jan>conda list torch
# packages in environment at D:\BitDownlD\Anaconda8\envs\myen2v:
#
# Name                    Version                   Build  Channel
pytorch                   2.0.1           cpu_py38hb0bdfb8_0
torch                     2.1.0                    pypi_0    pypi

(myen2v) C:\Users\Jan>conda list tensor
# packages in environment at D:\BitDownlD\Anaconda8\envs\myen2v:
#
# Name                    Version                   Build  Channel
tensorboard               2.13.0                   pypi_0    pypi
tensorboard-data-server   0.7.1                    pypi_0    pypi
tensorboard-plugin-wit    1.8.1            py38haa95532_0
tensorflow                2.13.0                   pypi_0    pypi
tensorflow-base           2.3.0           eigen_py38h75a453f_0
tensorflow-estimator      2.13.0                   pypi_0    pypi
tensorflow-gpu            2.3.0                    pypi_0    pypi
tensorflow-gpu-estimator  2.3.0                    pypi_0    pypi
tensorflow-io-gcs-filesystem 0.31.0                   pypi_0    pypi

我可以运行这段代码:


# Create tensors on GPU
a = torch.tensor([1, 2, 3], device="cuda")
b = torch.tensor([4, 5, 6], device="cuda")

# Perform operations on GPU
c = a + b
print(c)
tensor([5, 7, 9], device='cuda:0')

但是我无法运行下面的代码:

import tensorflow as tf

physical_devices = tf.config.list_physical_devices('GPU')
print("Num GPUs:", len(physical_devices))

我收到此错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[12], line 1
----> 1 physical_devices = tf.config.list_physical_devices('GPU')
      2 print("Num GPUs:", len(physical_devices))

AttributeError: module 'tensorflow' has no attribute 'config'

即使这样也行不通

import tensorflow as tf
print("Num of GPUs available: ", len(tf.test.gpu_device_name()))
--------------------------------------------------------------------------- AttributeError                            Traceback (most recent call
> last) Cell In[13], line 2
>       1 import tensorflow as tf
> ----> 2 print("Num of GPUs available: ", len(tf.test.gpu_device_name()))
> 
> AttributeError: module 'tensorflow' has no attribute 'test'
pytorch gpu tensorflow2.0 torch cudnn
1个回答
0
投票

这不太可能与 CUDA 有关,而更可能与 TensorFlow 安装的错误版本有关。首先了解一些基础知识:

import tensorflow as tf
print(tf.__version__)

确保与您所看到的相符

$ pip show tensorflow
© www.soinside.com 2019 - 2024. All rights reserved.