如何安装tensorflow-gpu

3

如何在 Windows 10 上使用 Python 3.10 安装 tensorflow-gpu?

conda 和 pip 都无法正常工作。

有没有人知道如何在 Python 3.10 上安装 tensorflow-gpu?

系统环境:Windows 10,Python 3.10.10。

我已经安装了以下组件: cudnn-windows-x86_64-8.9.0.131_cuda11-archive cuda_12.1.1_531.14_windows

并将以下路径添加到环境变量中: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\libnvvp

conda create --name cuda
conda activate cuda


(cuda) C:\Users\xxx>python -V
Python 3.10.10


(cuda) C:\Users\xxx>conda install -c conda-forge tensorflow-gpu
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.


(cuda) C:\Users\xxx>pip install -U tensorflow-gpu
Collecting tensorflow-gpu
  Using cached tensorflow-gpu-2.12.0.tar.gz (2.6 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [39 lines of output]
      Traceback (most recent call last):
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\_vendor\packaging\requirements.py", line 35, in __init__
          parsed = parse_requirement(requirement_string)
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\_vendor\packaging\_parser.py", line 64, in parse_requirement
          return _parse_requirement(Tokenizer(source, rules=DEFAULT_RULES))
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\_vendor\packaging\_parser.py", line 82, in _parse_requirement
          url, specifier, marker = _parse_requirement_details(tokenizer)
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\_vendor\packaging\_parser.py", line 126, in _parse_requirement_details
          marker = _parse_requirement_marker(
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\_vendor\packaging\_parser.py", line 147, in _parse_requirement_marker
          tokenizer.raise_syntax_error(
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\_vendor\packaging\_tokenizer.py", line 163, in raise_syntax_error
          raise ParserSyntaxError(
      setuptools.extern.packaging._tokenizer.ParserSyntaxError: Expected end or semicolon (after name and no valid version specifier)
          python_version>"3.7"
                        ^

      The above exception was the direct cause of the following exception:

      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "C:\Users\xxx\AppData\Local\Temp\pip-install-t4yr5nvl\tensorflow-gpu_48989e8e399a4c5da19c2d876e93f0d7\setup.py", line 40, in <module>
          setuptools.setup()
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\__init__.py", line 106, in setup
          _install_setup_requires(attrs)
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\__init__.py", line 77, in _install_setup_requires
          dist.parse_config_files(ignore_option_errors=True)
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\dist.py", line 910, in parse_config_files
          self._finalize_requires()
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\dist.py", line 607, in _finalize_requires
          self._move_install_requirements_markers()
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\dist.py", line 647, in _move_install_requirements_markers
          inst_reqs = list(_reqs.parse(spec_inst_reqs))
        File "C:\Users\xxx\anaconda3\envs\cuda\lib\site-packages\setuptools\_vendor\packaging\requirements.py", line 37, in __init__
          raise InvalidRequirement(str(e)) from e
      setuptools.extern.packaging.requirements.InvalidRequirement: Expected end or semicolon (after name and no valid version specifier)
          python_version>"3.7"
                        ^
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

我在Windows中使用PyCharm和Ubuntu找到了一种解决方案,但我不想这样做。


你尝试在新环境中安装了吗?你已经在cuda环境中安装了什么? - FlyingTeller
我觉得令人困惑的是Windows用户为什么不直接创建一个解决这个问题的规范YAML文件。例如,类似于MacOS上的这个:https://dev59.com/nlEG5IYBdhLWcg3wIksH#72970797 - merv
4个回答

1

我认为你应该将 Python 版本降级到 3.9,并使用 conda install tensorflow-gpu


如果我运行这个命令,我会得到"tensorflow-gpu" 包已被移除!请安装 "tensorflow" 替代。 - Szymon Roziewski

0
根据pypi的说明:“tensorflow-gpu已被移除,请安装tensorflow代替。tensorflow包支持通过Nvidia CUDA进行GPU加速操作。”
因此,只需使用以下命令进行安装: pip install tensorflow

0
cuda_11.2.0_460.89_win10 
cudnn-11.2-windows-x64-v8.1.0.77
conda create --name cuda37 python=3.7

pip install tensorflow-gpu==2.6.0
pip install keras==2.6.0

别忘了添加环境变量。

它能够工作,但我没有时间去更新。如果有人知道如何在最新版本上实现它,那对某些人可能会很有用。


执行conda create --name tf python=3.10 pip后,接着执行conda activate tfpip install tensorflow==2.12没有成功吗? - FlyingTeller

0

现在已经不推荐使用了。 请查看发布说明以获取更多信息:https://github.com/tensorflow/tensorflow/releases

或者访问pypi页面: https://pypi.org/project/tensorflow-gpu

如果你正在寻找教程,这个对我来说很有效: https://www.youtube.com/watch?v=OEFKlRSd8Ic

我只需要改变教程的最后一步。截至2023年8月,你需要按照以下方式进行安装:

pip install tensorflow==2.10.0

然后检查一下它是否像这样工作:
>>> import tensorflow as tf
>>> tf.test.is_built_with_cuda()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接