我很想在 anaconda Spyder 中执行这段代码
import tensorflow as tf
from tensorflow.python import keras
from keras.models import Model
from keras.models import load_model
from keras.optimizers import Adam
from keras.layers import Input, Conv2D, UpSampling2D, Dropout, LeakyReLU, BatchNormalization, Activation, Lambda
from tensorflow.keras.layers import Concatenate
from keras.applications import VGG16
from keras import backend as K
from keras.utils.multi_gpu_utils import multi_gpu_model --->error
此代码在此行出错
from keras.utils.multi_gpu_utils import multi_gpu_model
错误是
ModuleNotFoundError:没有名为“keras.utils.multi_gpu_utils”的模块
如何解决这个问题?
我试过了
我运行了安装tensorflow-gpu的命令
pip3 install 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\hp\anaconda3\lib\site-packages\setuptools\_vendor\packaging\requirements.py", line 35, in __init__
parsed = parse_requirement(requirement_string)
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_parser.py", line 64, in parse_requirement
return _parse_requirement(Tokenizer(source, rules=DEFAULT_RULES))
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_parser.py", line 82, in _parse_requirement
url, specifier, marker = _parse_requirement_details(tokenizer)
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_parser.py", line 126, in _parse_requirement_details
marker = _parse_requirement_marker(
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_parser.py", line 147, in _parse_requirement_marker
tokenizer.raise_syntax_error(
File "C:\Users\hp\anaconda3\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\hp\AppData\Local\Temp\pip-install-ovwye3d5\tensorflow-gpu_8fc59815d9a64f798e39953c26d47c5b\setup.py", line 40, in <module>
setuptools.setup()
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\__init__.py", line 106, in setup
_install_setup_requires(attrs)
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\__init__.py", line 77, in _install_setup_requires
dist.parse_config_files(ignore_option_errors=True)
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\dist.py", line 910, in parse_config_files
self._finalize_requires()
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\dist.py", line 607, in _finalize_requires
self._move_install_requirements_markers()
File "C:\Users\hp\anaconda3\lib\site-packages\setuptools\dist.py", line 647, in _move_install_requirements_markers
inst_reqs = list(_reqs.parse(spec_inst_reqs))
File "C:\Users\hp\anaconda3\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.
注意:我的电脑没有nvidia GUP但是它radeon GPU
对于第一个错误:
你收到这个错误是因为
multi_gpu_model
被弃用在 Tensorflow 2.x. 中使用
multi_gpu_model api 在 TensorFlow 1.x 版本中使用,并在 TensorFlow 2.x 中替换为
tf.distribute.Strategy
api 以跨多个 GPU、多台机器或 TPU 分布训练。
您可以使用
MirroredStrategy
的分布式训练策略,支持在一台机器上的多个GPU上进行同步分布式训练。它为每个 GPU 设备创建一个副本。
请查看分布式培训链接了解更多详情。
对于第二个错误:
TensorFlow 2.10 是最后一个在 native-Windows 上支持 GPU 的 TensorFlow 版本。因此安装
tensorflow-gpu 2.12
以支持 GPU 在 Winodws 操作系统中将不起作用。