首页 > 解决方案 > 测试 TensorFlow 安装是否在 Ubuntu 20.04 中的 Python 3.8.2 上失败

问题描述

我尝试使用https://www.tensorflow.org/install中的说明在我的 Ubuntu 20.04 LTS 虚拟机中安装 TensorFlow :

sudo apt install python-pip
pip install --upgrade pip
pip install tensorflow

我收到了一些警告,所以我想检查 TensorFlow 是否可以运行:

WARNING: The scripts pyrsa-decrypt, pyrsa-encrypt, pyrsa-keygen, pyrsa-priv2pub, pyrsa-sign and pyrsa-verify are installed in '/home/tlroot/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script markdown_py is installed in '/home/tlroot/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script google-oauthlib-tool is installed in '/home/tlroot/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The scripts f2py, f2py3 and f2py3.8 are installed in '/home/tlroot/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script tensorboard is installed in '/home/tlroot/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The scripts estimator_ckpt_converter, saved_model_cli, tensorboard, tf_upgrade_v2, tflite_convert, toco and toco_from_protos are installed in '/home/tlroot/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

我从这三个链接中找到了用于测试 TensorFlow 是否正常工作的代码:

https://medium.com/createdd-notes/install-tensorflow-with-virtuelenv-and-visual-studio-code-on-mac-6b9f93f0fb08

https://www.oreilly.com/library/view/deep-learning-with/9781786469786/3bdee8f3-ca88-438a-8141-42d2a12db71d.xhtml

https://github.com/tensorflow/tensorflow/issues/34658

这是我运行测试的代码,我在代码的最后一行“print sess.run(hello)”和“print(sess.run(hello))”上得到一个错误,也有一个错误:

tlroot@tlroot-VirtualBox:~$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant("Hello TensorFlow!")
2020-07-15 11:06:33.171630: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-07-15 11:06:33.171702: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2020-07-15 11:06:33.171728: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (tlroot-VirtualBox): /proc/driver/nvidia/version does not exist
2020-07-15 11:06:33.172012: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-07-15 11:06:33.183668: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2591995000 Hz
2020-07-15 11:06:33.183782: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f574c000b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-07-15 11:06:33.183800: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
>>> hello = tf.constant("Hello TensorFlow!")
>>> sess = tf.compat.v1.Session()
>>> print sess.run(hello)
  File "<stdin>", line 1
    print sess.run(hello)
          ^
SyntaxError: invalid syntax
>>> print(sess.run(hello))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tlroot/.local/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 957, in run
    result = self._run(None, fetches, feed_dict, options_ptr,
  File "/home/tlroot/.local/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1106, in _run
    raise RuntimeError('The Session graph is empty.  Add operations to the '
RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().
>>> 

这个问题是我在此处发布的关于安装 TensorFlow 的问题的延续:Problem installation TensorFlow for Visual Studio Code C++ project in Ubuntu 18.04.4 LTS Virtual Machine

更新

我试过“print(sess.run(hello))”,但也没有用。

>>> print(sess.run(hello))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tlroot/.local/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 957, in run
    result = self._run(None, fetches, feed_dict, options_ptr,
  File "/home/tlroot/.local/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1106, in _run
    raise RuntimeError('The Session graph is empty.  Add operations to the '
RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().
>>> 

我试过 python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))" 也没有用。

tlroot@tlroot-VirtualBox:~$ python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
2020-07-15 12:38:47.446234: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-07-15 12:38:47.446361: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2020-07-15 12:38:47.446423: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (tlroot-VirtualBox): /proc/driver/nvidia/version does not exist
2020-07-15 12:38:47.446723: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-07-15 12:38:47.462856: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2591995000 Hz
2020-07-15 12:38:47.463198: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7ff1c0000b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-07-15 12:38:47.463294: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
tf.Tensor(885.7727, shape=(), dtype=float32)
tlroot@tlroot-VirtualBox:~$```

标签: pythontensorflowinstallation

解决方案


正如您在此处看到的,Tensorflow 需要 Python 3.5 到 3.7,我认为不支持 3.8.2。但无论如何我都会在print语句中添加括号,因为这是 Python 3.x 所必需的


推荐阅读