MAC M1芯片tensorflow下的fit报错Graph execution error:

Mac M1Pro芯片,在找TB30元解决了下载安装问题之后,import tensorflow可以正常运行,但是在keras库下,

出现错误代码如下:


history = ann.fit(X_train, y_train, # 指定训练集
                  epochs=30,        # 指定训练的轮次
                  batch_size=64,    # 指定数据批量
                  validation_data=(X_test, y_test)) #指定验证集,这里为了简化模型,直接用测试集数据进行验证

报错 信息:


Epoch 1/30
2023-01-09 21:09:03.019104: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.
2023-01-09 21:09:04.182418: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x153714350
2023-01-09 21:09:04.182452: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x153714350
2023-01-09 21:09:04.529939: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x153714350
2023-01-09 21:09:04.529967: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x153714350
2023-01-09 21:09:04.617016: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x153714350
2023-01-09 21:09:04.617046: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x153714350
---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
Cell In[17], line 1
----> 1 history = ann.fit(X_train, y_train, # 指定训练集
      2                   epochs=30,        # 指定训练的轮次
      3                   batch_size=64,    # 指定数据批量
      4                   validation_data=(X_test, y_test))

File ~/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File ~/opt/anaconda3/envs/tf/lib/python3.9/site-packages/tensorflow/python/eager/execute.py:52, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     50 try:
     51   ctx.ensure_initialized()
---> 52   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     53                                       inputs, attrs, num_outputs)
     54 except core._NotOkStatusException as e:
     55   if name is not None:

NotFoundError: Graph execution error:

Detected at node 'StatefulPartitionedCall_4' defined at (most recent call last):
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/runpy.py", line 197, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/ipykernel_launcher.py", line 17, in 
      app.launch_new_instance()
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/traitlets/config/application.py", line 992, in launch_instance
      app.start()
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/ipykernel/kernelapp.py", line 711, in start
      self.io_loop.start()
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 215, in start
      self.asyncio_loop.run_forever()
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/asyncio/base_events.py", line 601, in run_forever
      self._run_once()
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/asyncio/base_events.py", line 1905, in _run_once
      handle._run()
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/asyncio/events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 510, in dispatch_queue
      await self.process_one()
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 499, in process_one
      await dispatch(*args)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 406, in dispatch_shell
      await result
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 729, in execute_request
      reply_content = await reply_content
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/ipykernel/ipkernel.py", line 411, in do_execute
      res = shell.run_cell(
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/ipykernel/zmqshell.py", line 531, in run_cell
      return super().run_cell(*args, **kwargs)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2940, in run_cell
      result = self._run_cell(
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2995, in _run_cell
      return runner(coro)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
      coro.send(None)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3194, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3373, in run_ast_nodes
      if await self.run_code(code, result, async_=asy):
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3433, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "/var/folders/9s/w9b7v1sj66d3j12lzwmm59xr0000gn/T/ipykernel_64050/3685881096.py", line 1, in 
      history = ann.fit(X_train, y_train, # 指定训练集
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/engine/training.py", line 1650, in fit
      tmp_logs = self.train_function(iterator)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/engine/training.py", line 1249, in train_function
      return step_function(self, iterator)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/engine/training.py", line 1233, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/engine/training.py", line 1222, in run_step
      outputs = model.train_step(data)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/engine/training.py", line 1027, in train_step
      self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 527, in minimize
      self.apply_gradients(grads_and_vars)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1140, in apply_gradients
      return super().apply_gradients(grads_and_vars, name=name)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 634, in apply_gradients
      iteration = self._internal_apply_gradients(grads_and_vars)
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1166, in _internal_apply_gradients
      return tf.__internal__.distribute.interim.maybe_merge_call(
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1216, in _distributed_apply_gradients_fn
      distribution.extended.update(
    File "/Users/atongmu/opt/anaconda3/envs/tf/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1211, in apply_grad_to_update_var
      return self._update_step_xla(grad, var, id(self._var_key(var)))
Node: 'StatefulPartitionedCall_4'
could not find registered platform with id: 0x153714350
     [[{{node StatefulPartitionedCall_4}}]] [Op:__inference_train_function_1200]

望有人能帮忙解决!不胜感激!

版本不匹配要降级
pip install tensorflow-metal==0.5.0
pip install tensorflow-macos==2.9.0

报错主要意思:
NotFoundError: Graph execution error:
NotFoundError:图形执行错误:
Node: 'StatefulPartitionedCall_4'
could not find registered platform with id: 0x153714350
节点:'StatefulPartitionedCall_4'
找不到id为0x153714350的注册平台

看到这个错误,我们可以分析一下:OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x153714350。这个错误似乎和TensorFlow的XLA (Accelerated Linear Algebra)有关,XLA是TensorFlow中的一个优化器,它可以使用高性能线性代数库来优化TensorFlow代码。

那么,为什么会出现这个错误呢?这个错误的原因可能是在Mac上安装TensorFlow时使用了错误的版本,或者你使用的设备没有安装对应的线性代数库。

建议你可以尝试以下方法:

安装支持Mac M1Pro芯片的TensorFlow版本,可以参考官方文档,试着重新安装TensorFlow
如果你使用的是CPU版本的TensorFlow,可以尝试使用GPU版本。可以参考官方文档,安装支持M1芯片的GPU版本。

这个错误是由于TensorFlow找不到一个注册的平台造成的。

这个错误的原因可能是系统环境中缺少了必要的库或者依赖项,或者是使用了不支持的硬件。

可以试试以下步骤来解决这个问题:

1、确保已经正确地安装了TensorFlow和Keras,并且系统中没有其他版本的TensorFlow或Keras。

2、确保系统中安装了所有必要的库和依赖项。TensorFlow和Keras可能需要其他库来正常工作,比如NumPy和SciPy。

3、确保使用的是支持TensorFlow的硬件。TensorFlow可能不支持某些硬件,所以应该检查一下TensorFlow的文档,确保硬件是受支持的。

4、还可以试试在代码中禁用XLA优化。在代码中加入以下行可以禁用XLA优化:

import os
os.environ['TF_XLA_FLAGS'] = '--tf_xla_cpu_global_jit'

5、如果以上步骤都不能解决问题,可以试试在TensorFlow的GitHub issue跟踪器中搜索这个错误,看看有没有人遇到过类似的问题。
仅供参考,望采纳,谢谢。

望采纳!!!点击回答右侧采纳即可!!

这个错误是由于在 TensorFlow 中无法找到合法的平台,导致无法执行操作。

可能的原因有:

TensorFlow 不支持 M1 芯片,导致无法使用 GPU 加速。
在初始化 TensorFlow 时没有设置正确的 GPU 参数,导致无法找到合法的平台。
在运行 Keras 模型时出现了其他错误,导致无法执行操作。
为了解决这个问题,建议您尝试以下操作:

检查 TensorFlow 是否支持 M1 芯片。如果 TensorFlow 不支持 M1 芯片,可能需要使用 CPU 运行模型。
检查是否在初始化 TensorFlow 时设置了正确的 GPU 参数。例如,在使用 GPU 运行模型时,需要设