TensorFlow 安装与环境配置

TensorFlow 安装与环境配置 https://tf.wiki/zh/basic/installation.html

如果程序报错,建议包含以下信息以帮助排除问题:

  • TensorFlow 版本(请检查 TensorFlow 版本为 2.0 及以上)
import tensorflow as tf
print (tf.__version__)
  • 完整的最小可运行程序代码(建议将代码在 IDE 中运行而不是交互式环境)
  • 完整的报错信息(Traceback)

请问,
1,如何确定 gpu 是否比 cpu 快还是慢。
譬如 i7 和 gtx 1050 ,有必要安装 gpu 版本吗?
2,是否是要么是 cpu 来计算 要么是 gpu 来计算。要对比的话是否两个版本都要装?

1 Like
  1. 可以分别使用 GPU 和 CPU 运行同一个模型来选择合适的运算设备。GPU 和 CPU 的选择与模型类型也有关系,例如如果是卷积神经网络则 gtx1050 可能胜过 i7,但如果是强化学习则不一定。

  2. 可以简单地认为,是的。不需要两个版本都装,TensorFlow 2.1 默认安装(pip install tensorflow)就已经同时支持 CPU 和 GPU,可以使用

    cpus = tf.config.list_physical_devices (device_type=‘CPU’)
    tf.config.set_visible_devices (devices=cpus)

来限定只使用 CPU 进行运算。

1、tf.config.set_visible_devices (devices=cpus) 设定似乎有问题,搜索了 Tf2 官方,也没看明白这个用法。

2、一种方法,类似于当前程序的全局设定,放在 import tensorflow 之前
import os
os.environ [“CUDA_DEVICE_ORDER”] = “PCI_BUS_ID”
os.environ [“CUDA_VISIBLE_DEVICES”] = “-1”

3,另一种方法,是程序临时指定
with tf.device (’/cpu:0’):
A = tf.constant ([[1, 2], [3, 4]])
B = tf.constant ([[5, 6], [7, 8]])
C = tf.matmul (A, B)
print ©

with tf.device (’/gpu:0’):
A = tf.constant ([[1, 2], [3, 4]])
B = tf.constant ([[5, 6], [7, 8]])
C = tf.matmul (A, B)
print ©

可以参考 tf.config.set_visible_devices  |  TensorFlow v2.14.0 ,相关内容在 https://tf.wiki/zh/basic/tools.html#tf-config-gpu 也有介绍。一个简单的示例程序如下:

import tensorflow as tf
tf.debugging.set_log_device_placement (True)     # 设置输出运算所在的设备

cpus = tf.config.list_physical_devices ('CPU')   # 获取当前设备的 CPU 列表
tf.config.set_visible_devices (cpus)             # 设置 TensorFlow 的可见设备范围为 cpu

A = tf.constant ([[1, 2], [3, 4]])
B = tf.constant ([[5, 6], [7, 8]])
C = tf.matmul (A, B)

print (C)

输出

2020-04-21 11:37:29.007897: I tensorflow/core/common_runtime/eager/execute.cc:573] Executing op MatMul in device /job:localhost/replica:0/task:0/device:CPU:0
tf.Tensor (
[[19 22]
[43 50]], shape=(2, 2), dtype=int32)

当然最保险的办法是新开一个 conda 虚拟环境并且

pip install tensorflow-cpu

,安装仅支持 CPU 的 TensorFlow 版本。

请问安装 cuda 时为什么会报错:(‘Connection broken: OSError ("(10054, ‘WSAECONNRESET’)")’, OSError ("(10054, ‘WSAECONNRESET’)"))

看起来可能是网络问题。如果使用 conda 安装的话建议设置镜像。

When I use pip install tensorflow
I got the feedback:
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow
But I find the command worked:
python -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py3-none-any.whl
If you meet such a question, you can try it.

你这个 TensorFlow 的版本也太老啦。本教程面向 TensorFlow 2.1,如果 pip install tensorflow 出现错误(这种情况一般很少见),请检查 Python 环境设置,重新建立一个新的 conda 环境再安装,或者在搜索引擎里搜索一下报错内容。

好吧我找到了原因了
安装一直出错是因为我之前下了 Python3.7.4-32bit 版本,换成了 64bit 就没有问题了

关于 IDE 设置 的,我的路径是 /opt/anaconda3/envs/tf2/bin/python ,不知道会不会有问题……。我系统是 macOS 10.15.3 (19D76)

没有问题,我与你的一致

你好,按照这种方式在 Anaconda 环境中使用 TF2.1 会遇到在使用 Tensorboard 的 Profile 时报错的问题,报错如下:

2020-05-08 11:09:24.374761: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcupti.so.10.1'; dlerror: libcupti.so.10.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /opt/gridview//pbs/dispatcher/lib::/usr/local/lib64:/usr/local/lib
2020-05-08 11:09:24.374801: E tensorflow/core/profiler/internal/gpu/cupti_tracer.cc:1307] function cupti_interface_->Subscribe ( &subscriber_, (CUpti_CallbackFunc) ApiCallback, this) failed with error CUPTI could not be loaded or symbol could not be found.
2020-05-08 11:09:24.374816: E tensorflow/core/profiler/internal/gpu/cupti_tracer.cc:1346] function cupti_interface_->ActivityRegisterCallbacks ( AllocCuptiActivityBuffer, FreeCuptiActivityBuffer) failed with error CUPTI could not be loaded or symbol could not be found.

另附 nvidia-smi 和 conda list 的结果:

Fri May  8 11:25:59 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.50       Driver Version: 430.50       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla V100-PCIE...  Off  | 00000000:89:00.0 Off |                    0 |
| N/A   40C    P0    37W / 250W |   1354MiB / 32510MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla V100-PCIE...  Off  | 00000000:8A:00.0 Off |                    0 |
| N/A   39C    P0    38W / 250W |    320MiB / 32510MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  Tesla V100-PCIE...  Off  | 00000000:8B:00.0 Off |                    0 |
| N/A   39C    P0    36W / 250W |    320MiB / 32510MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   3  Tesla V100-PCIE...  Off  | 00000000:8C:00.0 Off |                    0 |
| N/A   43C    P0    49W / 250W |  18828MiB / 32510MiB |     65%      Default |
+-------------------------------+----------------------+----------------------+
|   4  Tesla V100-PCIE...  Off  | 00000000:DA:00.0 Off |                    0 |
| N/A   40C    P0    46W / 250W |  18828MiB / 32510MiB |     73%      Default |
+-------------------------------+----------------------+----------------------+
|   5  Tesla V100-PCIE...  Off  | 00000000:DB:00.0 Off |                    0 |
| N/A   38C    P0    39W / 250W |    320MiB / 32510MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   6  Tesla V100-PCIE...  Off  | 00000000:DC:00.0 Off |                    0 |
| N/A   64C    P0   155W / 250W |   5789MiB / 32510MiB |     86%      Default |
+-------------------------------+----------------------+----------------------+
|   7  Tesla V100-PCIE...  Off  | 00000000:DD:00.0 Off |                    0 |
| N/A   40C    P0    40W / 250W |    320MiB / 32510MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      2234      G   /usr/bin/X                                    22MiB |
|    0     83392      C   /usr/lhy/anaconda3/envs/tf2/bin/python       363MiB |
|    0    135660      C   /usr/lhy/anaconda3/envs/tf2/bin/python       955MiB |
|    1    135660      C   /usr/lhy/anaconda3/envs/tf2/bin/python       307MiB |
|    2    135660      C   /usr/lhy/anaconda3/envs/tf2/bin/python       307MiB |
|    3    135660      C   /usr/lhy/anaconda3/envs/tf2/bin/python       307MiB |
|    3    139517      C   python                                      4627MiB |
|    3    140633      C   python                                      4627MiB |
|    3    142274      C   python                                      4627MiB |
|    3    143266      C   python                                      4627MiB |
|    4    133393      C   python                                      4627MiB |
|    4    133906      C   python                                      4627MiB |
|    4    134679      C   python                                      4627MiB |
|    4    135379      C   python                                      4627MiB |
|    4    135660      C   /usr/lhy/anaconda3/envs/tf2/bin/python       307MiB |
|    5    135660      C   /usr/lhy/anaconda3/envs/tf2/bin/python       307MiB |
|    6     15156      C   python                                      5469MiB |
|    6    135660      C   /usr/lhy/anaconda3/envs/tf2/bin/python       307MiB |
|    7    135660      C   /usr/lhy/anaconda3/envs/tf2/bin/python       307MiB |
+-----------------------------------------------------------------------------+
# packages in environment at /usr/lhy/anaconda3/envs/tf2:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main    defaults
absl-py                   0.9.0                    pypi_0    pypi
astor                     0.8.1                    pypi_0    pypi
attrs                     19.3.0                   pypi_0    pypi
backcall                  0.1.0                    py37_0    defaults
bleach                    3.1.4                    pypi_0    pypi
ca-certificates           2020.1.1                      0    defaults
cachetools                4.1.0                    pypi_0    pypi
certifi                   2020.4.5.1               py37_0    defaults
chardet                   3.0.4                    pypi_0    pypi
cloudpickle               1.3.0                    pypi_0    pypi
cudatoolkit               10.1.243             h6bb024c_0    defaults
cudnn                     7.6.5                cuda10.1_0    defaults
cycler                    0.10.0                   pypi_0    pypi
decorator                 4.4.2                      py_0    defaults
defusedxml                0.6.0                    pypi_0    pypi
entrypoints               0.3                      pypi_0    pypi
gast                      0.2.2                    pypi_0    pypi
gin-config                0.1.3                    pypi_0    pypi
google-auth               1.14.0                   pypi_0    pypi
google-auth-oauthlib      0.4.1                    pypi_0    pypi
google-pasta              0.2.0                    pypi_0    pypi
grpcio                    1.28.1                   pypi_0    pypi
gym                       0.10.11                  pypi_0    pypi
h5py                      2.10.0                   pypi_0    pypi
idna                      2.9                      pypi_0    pypi
importlib-metadata        1.6.0                    pypi_0    pypi
ipykernel                 5.2.1                    pypi_0    pypi
ipython                   7.13.0           py37h5ca1d4c_0    defaults
ipython-genutils          0.2.0                    pypi_0    pypi
ipython_genutils          0.2.0                    py37_0    defaults
jedi                      0.17.0                   pypi_0    pypi
jinja2                    2.11.2                   pypi_0    pypi
joblib                    0.14.1                   pypi_0    pypi
json5                     0.9.4                    pypi_0    pypi
jsonschema                3.2.0                    pypi_0    pypi
jupyter-client            6.1.3                    pypi_0    pypi
jupyter_client            6.1.2                      py_0    defaults
jupyter_core              4.6.3                    py37_0    defaults
jupyterlab                2.1.0                    pypi_0    pypi
jupyterlab-server         1.1.1                    pypi_0    pypi
keras-applications        1.0.8                    pypi_0    pypi
keras-preprocessing       1.1.0                    pypi_0    pypi
kiwisolver                1.2.0                    pypi_0    pypi
ld_impl_linux-64          2.33.1               h53a641e_7    defaults
libedit                   3.1.20181209         hc058e9b_0    defaults
libffi                    3.2.1                hd88cf55_4    defaults
libgcc-ng                 9.1.0                hdf63c60_0    defaults
libsodium                 1.0.16               h1bed415_0    defaults
libstdcxx-ng              9.1.0                hdf63c60_0    defaults
lightgbm                  2.3.1                    pypi_0    pypi
markdown                  3.2.1                    pypi_0    pypi
markupsafe                1.1.1                    pypi_0    pypi
matplotlib                3.2.1                    pypi_0    pypi
minepy                    1.2.4                    pypi_0    pypi
mistune                   0.8.4                    pypi_0    pypi
nbconvert                 5.6.1                    pypi_0    pypi
nbformat                  5.0.6                    pypi_0    pypi
ncurses                   6.2                  he6710b0_0    defaults
notebook                  6.0.3                    pypi_0    pypi
numpy                     1.18.3                   pypi_0    pypi
oauthlib                  3.1.0                    pypi_0    pypi
openssl                   1.1.1g               h7b6447c_0    defaults
opt-einsum                3.2.1                    pypi_0    pypi
pandas                    1.0.3                    pypi_0    pypi
pandocfilters             1.4.2                    pypi_0    pypi
parso                     0.7.0                    pypi_0    pypi
pexpect                   4.8.0                    py37_0    defaults
pickleshare               0.7.5                    pypi_0    pypi
pip                       20.0.2                   py37_1    defaults
prometheus-client         0.7.1                    pypi_0    pypi
prompt-toolkit            3.0.5                    pypi_0    pypi
prompt_toolkit            3.0.4                         0    defaults
protobuf                  3.11.3                   pypi_0    pypi
ptyprocess                0.6.0                    pypi_0    pypi
pyaml                     20.4.0                   pypi_0    pypi
pyasn1                    0.4.8                    pypi_0    pypi
pyasn1-modules            0.2.8                    pypi_0    pypi
pydot                     1.4.1                    pypi_0    pypi
pyglet                    1.5.4                    pypi_0    pypi
pygments                  2.6.1                      py_0    defaults
pyparsing                 2.4.7                    pypi_0    pypi
pyrsistent                0.16.0                   pypi_0    pypi
python                    3.7.7           hcf32534_0_cpython    defaults
python-dateutil           2.8.1                      py_0    defaults
python-graphviz           0.14                     pypi_0    pypi
pytz                      2019.3                   pypi_0    pypi
pyyaml                    5.3.1                    pypi_0    pypi
pyzmq                     19.0.0                   pypi_0    pypi
readline                  8.0                  h7b6447c_0    defaults
requests                  2.23.0                   pypi_0    pypi
requests-oauthlib         1.3.0                    pypi_0    pypi
rsa                       4.0                      pypi_0    pypi
scikit-learn              0.22.2.post1             pypi_0    pypi
scipy                     1.4.1                    pypi_0    pypi
send2trash                1.5.0                    pypi_0    pypi
setuptools                46.1.3                   py37_0    defaults
six                       1.14.0                   py37_0    defaults
SQLite                    3.31.1               h7b6447c_0    defaults
tensorboard               2.1.1                    pypi_0    pypi
tensorflow                2.1.0                    pypi_0    pypi
tensorflow-addons         0.9.1                    pypi_0    pypi
tensorflow-estimator      2.1.0                    pypi_0    pypi
tensorflow-probability    0.9.0                    pypi_0    pypi
termcolor                 1.1.0                    pypi_0    pypi
terminado                 0.8.3                    pypi_0    pypi
testpath                  0.4.4                    pypi_0    pypi
tf-agents                 0.4.0                    pypi_0    pypi
tk                        8.6.8                hbc83047_0    defaults
tornado                   6.0.4            py37h7b6447c_1    defaults
tqdm                      4.45.0                   pypi_0    pypi
traitlets                 4.3.3                    py37_0    defaults
typeguard                 2.7.1                    pypi_0    pypi
urllib3                   1.25.9                   pypi_0    pypi
wcwidth                   0.1.9                      py_0    defaults
webencodings              0.5.1                    pypi_0    pypi
werkzeug                  1.0.1                    pypi_0    pypi
wheel                     0.34.2                   py37_0    defaults
wrapt                     1.12.1                   pypi_0    pypi
xgboost                   0.80                     pypi_0    pypi
xz                        5.2.5                h7b6447c_0    defaults
zeromq                    4.3.1                he6710b0_3    defaults
zipp                      3.1.0                    pypi_0    pypi
zlib                      1.2.11               h7b6447c_3    defaults

参考一下 python - Tensorflow CUDA - CUPTI error: CUPTI could not be loaded or symbol could not be found - Stack Overflow

ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow

按你说的装不了 tensorflow

请检查你的 Python 版本是否为 64 位,参考 https://stackoverflow.com/questions/48720833/could-not-find-a-version-that-satisfies-the-requirement-tensorflow

您好!我安装的 tf 2.0,每次启动时,cuda 的加载信息如下,有的信息重复显示了两次,但是计算上没有问题,请问您知道是什么原因吗?
2020-05-22 09:09:35.094679: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-05-22 09:09:35.146396: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: TITAN X (Pascal) major: 6 minor: 1 memoryClockRate (GHz): 1.531
pciBusID: 0000:02:00.0
2020-05-22 09:09:35.147109: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 1 with properties:
name: Quadro P2000 major: 6 minor: 1 memoryClockRate (GHz): 1.4805
pciBusID: 0000:01:00.0
2020-05-22 09:09:35.147377: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2020-05-22 09:09:35.149235: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
2020-05-22 09:09:35.150833: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10.0
2020-05-22 09:09:35.151172: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10.0
2020-05-22 09:09:35.153255: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10.0
2020-05-22 09:09:35.154837: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10.0
2020-05-22 09:09:35.159338: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-05-22 09:09:35.162403: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0, 1
2020-05-22 09:09:35.198717: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2194750000 Hz
2020-05-22 09:09:35.201895: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x56304c7438d0 executing computations on platform Host. Devices:
2020-05-22 09:09:35.201939: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version
2020-05-22 09:09:35.545476: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x56304ca88560 executing computations on platform CUDA. Devices:
2020-05-22 09:09:35.545540: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): TITAN X (Pascal), Compute Capability 6.1
2020-05-22 09:09:35.545550: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (1): Quadro P2000, Compute Capability 6.1
2020-05-22 09:09:35.547244: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: TITAN X (Pascal) major: 6 minor: 1 memoryClockRate (GHz): 1.531
pciBusID: 0000:02:00.0
2020-05-22 09:09:35.548178: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 1 with properties:
name: Quadro P2000 major: 6 minor: 1 memoryClockRate (GHz): 1.4805
pciBusID: 0000:01:00.0
2020-05-22 09:09:35.548249: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2020-05-22 09:09:35.548280: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
2020-05-22 09:09:35.548307: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10.0
2020-05-22 09:09:35.548333: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10.0
2020-05-22 09:09:35.548359: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10.0
2020-05-22 09:09:35.548384: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10.0
2020-05-22 09:09:35.548411: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-05-22 09:09:35.552360: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0, 1
2020-05-22 09:09:35.552423: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2020-05-22 09:09:35.555863: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-05-22 09:09:35.555894: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0 1
2020-05-22 09:09:35.555908: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N N
2020-05-22 09:09:35.555919: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 1: N N
2020-05-22 09:09:35.559429: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 11435 MB memory) -> physical GPU (device: 0, name: TITAN X (Pascal), pci bus id: 0000:02:00.0, compute capability: 6.1)
2020-05-22 09:09:35.560428: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 4518 MB memory) -> physical GPU (device: 1, name: Quadro P2000, pci bus id: 0000:01:00.0, compute capability: 6.1)
<tf.Tensor: id=0, shape=(), dtype=int32, numpy=1>

1 Like

同问,安装完 tensorflow2.2,也是一样的,但是输出过程正常

@MingCheung @hifeng2017 TensorFlow 在运行过程中会在终端打一些 log,这个是正常的,具体使用上只要不报错就行(有 warning 的话大多是使用了过时的 api,可以根据提示做一些调整)

如果不喜欢这些 log,可以参考 https://stackoverflow.com/questions/35911252/disable-tensorflow-debugging-information 把 log 关掉。