簡體   English   中英

在 Python 中導入 TensorFlow 2 時出現問題(在 Windows 中的 WSL 上運行)

[英]Problem importing TensorFlow 2 in Python (running on WSL in Windows)

問題:我按照 Microsoft 的說明使用 DirectML 在 WSL 中正確安裝和運行 TensorFlow 2 並使用 GPU 加速( 此處是文檔)。

安裝后,當我嘗試在 Python 中導入 tensorflow 時,我得到以下 output:

>>> import tensorflow 
                                                                                               
2022-11-22 15:52:33.090032: 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:  AVX2 FMA                                                                                                               

To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):                                                                                       
File "<stdin>", line 1, in <module>                                                                                    
File "/home/pietro/miniconda3/envs/testing/lib/python3.9/site-package
/tensorflow/__init__.py", line 440, in <module>   
_ll.load_library(_plugin_dir)                                                                                        
File "/home/pietro/miniconda3/envs/testing/lib/python3.9/site-package
/tensorflow/python/framework/load_library.py", line 151, in load_library                                                                                                   
py_tf.TF_LoadLibrary(lib)                                                                                          
tensorflow.python.framework.errors_impl.NotFoundError: /home/pietro
/miniconda3/envs/testing/lib/python3.9/site-packages/tensorflow-plugin
/libtfdml_plugin.so: undefined symbol:_ZN10tensorflow8internal15LogMessageFatalD1Ev, version tensorflow  

我嘗試按照 TensorFlow 1 和 PyTorch 的說明進行操作(以防萬一我的機器出現問題)並且它們都可以正常工作,所以我假設這個問題只涉及 TensorFlow 2 不知何故。

有人遇到過同樣的問題嗎? 提前感謝大家:) Pietro

有同樣的問題,從 2.11 降級 TensorFlow 修復了它。 首先刪除現有版本:

pip uninstall tensorflow-cpu

然后重新安裝,這次是 2.10.0:

pip install tensorflow-cpu==2.10.0

之后,嘗試在 Python 中導入它。您應該會看到類似以下內容(對於混亂的輸出表示歉意):

>>> import tensorflow as tf
2022-11-28 22:41:21.693757: 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:  AVX2 AVX512F AVX512_VNNI FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-11-28 22:41:21.806150: I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2022-11-28 22:41:22.982148: I tensorflow/c/logging.cc:34] Successfully opened dynamic library libdirectml.d6f03b303ac3c4f2eeb8ca631688c9757b361310.so
2022-11-28 22:41:22.982289: I tensorflow/c/logging.cc:34] Successfully opened dynamic library libdxcore.so
2022-11-28 22:41:22.996385: I tensorflow/c/logging.cc:34] Successfully opened dynamic library libd3d12.so
2022-11-28 22:41:27.615851: I tensorflow/c/logging.cc:34] DirectML device enumeration: found 1 compatible adapters.

您可以通過添加兩個張量來測試它是否有效。 運行如下命令:

print(tf.add([1.0, 2.0], [3.0, 4.0]))

在 output 的某處,您應該能夠驗證 DirectML 是否找到了您的 GPU:

2022-11-28 22:43:42.632447: I tensorflow/c/logging.cc:34] DirectML: creating device on adapter 0 (NVIDIA GeForce RTX 3080)

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM